Skip to content

Commit

Permalink
Merge pull request #57 from H2-invent/feature/pexelsRelaodTime
Browse files Browse the repository at this point in the history
Add the reloadtime for pexels Image to use the limited API without co…
  • Loading branch information
holema authored Feb 6, 2021
2 parents 72450f7 + fcc91b3 commit e61e450
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 10 deletions.
2 changes: 2 additions & 0 deletions .env
Original file line number Diff line number Diff line change
Expand Up @@ -80,11 +80,13 @@ matomo_id=1
matomo_url=analytics.local.com
###< matomo ###


###> LaF ###
laF_startpage=false
laF_onlyRegisteredParticipents=0
laF_version=2.0.0-dev
laF_pexel_api_key=#EnterAPIKeyHere
laF_pexel_refresh_time=1200
###< LaF ###

###> Default Jitsi Server ###
Expand Down
1 change: 1 addition & 0 deletions .env.sample
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ laF_startpage=false
laF_onlyRegisteredParticipents=0
laF_version=2.0.0-dev
laF_pexel_api_key=#EnterAPIKeyHere
laF_pexel_refresh_time=600
###< LaF ###

###> Default Jitsi Server ###
Expand Down
2 changes: 1 addition & 1 deletion config/packages/twig.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@ twig:
laf_version: '%env(laF_version)%'
matomo_enable: '%env(matomo_enable)%'
matomo_id: '%env(matomo_id)%'
matomo_url: '%env(matomo_url)%'
matomo_url: '%env(matomo_url)%'
1 change: 1 addition & 0 deletions config/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ parameters:
laF_onlyRegisteredParticipents: '%env(laF_onlyRegisteredParticipents)%'
delivery_addresses: ['%env(DEV_EMAIL)%']
laF_pexel_api_key: '%env(laF_pexel_api_key)%'
laF_pexel_refresh_time: '%env(laF_pexel_refresh_time)%'

services:
# default configuration for services in *this* file
Expand Down
21 changes: 12 additions & 9 deletions src/Service/PexelService.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,21 +24,24 @@ public function getImageFromPexels()
$image = null;
try {
$cache = new FilesystemAdapter();
$cache->delete('pexels_image');
$value = $cache->get('pexels_image', function (ItemInterface $item) {
$item->expiresAfter(600);
$item->expiresAfter(intval($this->parameterBag->get('laF_pexel_refresh_time')));

$s = array();
$hour = (new \DateTime())->format('H');
if ($hour < 7) {
$s = ['night', 'moon'];
$s = [ 'night', 'northern lights'];
} elseif ($hour < 9) {
$s = ['sunrise', 'germany', 'bavaria'];
} elseif ($hour < 11) {
$s = ['city', 'valley', 'animal', 'dessert', 'cat', 'forest'];
} elseif ($hour < 17) {
$s = ['lake', 'ocean', 'weather', 'mountain'];
} elseif ($hour < 23) {
$s = ['sunset', 'night', 'moon'];
$s = ['city', 'valley', 'animal', 'dessert', 'cat', 'forest','mountain'];
} elseif ($hour < 18) {
$s = ['lake', 'ocean', 'underwater', 'reef'];
}elseif ($hour < 21) {
$s = ['sunset','clouds'];
} else {
$s = [ 'night', 'northern lights'];
}

$response = $this->client->request('GET', 'https://api.pexels.com/v1/search?query=' . $s[rand(0, sizeof($s) - 1)] . '&per_page=80', [
Expand All @@ -49,8 +52,8 @@ public function getImageFromPexels()
);
return $response->getContent();
});
$image = json_decode($value, true)['photos'][rand(0, 79)];

$imageArr = json_decode($value, true)['photos'];
$image = $imageArr[rand(0,sizeof($imageArr)-1)];
}catch (\Exception $e){

}
Expand Down

0 comments on commit e61e450

Please sign in to comment.