Add Flickr Collector

This commit is contained in:
Clément 2021-02-22 17:44:11 +01:00
parent 48cc6e9f28
commit 8fa8302792
3 changed files with 241 additions and 3 deletions

View File

@ -10,7 +10,8 @@
], ],
"require": { "require": {
"fabpot/goutte": "^4.0", "fabpot/goutte": "^4.0",
"ext-imagick": "*" "ext-imagick": "*",
"samwilson/phpflickr": "^4.14"
}, },
"autoload": { "autoload": {
"psr-4": { "psr-4": {

173
composer.lock generated
View File

@ -4,7 +4,7 @@
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
"This file is @generated automatically" "This file is @generated automatically"
], ],
"content-hash": "d22eac6a3ef39dd7fd1941592118e42a", "content-hash": "ace979567916bbdd1a9fd3e2222dcf3d",
"packages": [ "packages": [
{ {
"name": "fabpot/goutte", "name": "fabpot/goutte",
@ -61,6 +61,126 @@
}, },
"time": "2020-10-14T06:49:09+00:00" "time": "2020-10-14T06:49:09+00:00"
}, },
{
"name": "lusitanian/oauth",
"version": "v0.8.11",
"source": {
"type": "git",
"url": "https://github.com/Lusitanian/PHPoAuthLib.git",
"reference": "fc11a53db4b66da555a6a11fce294f574a8374f9"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/Lusitanian/PHPoAuthLib/zipball/fc11a53db4b66da555a6a11fce294f574a8374f9",
"reference": "fc11a53db4b66da555a6a11fce294f574a8374f9",
"shasum": ""
},
"require": {
"php": ">=5.3.0"
},
"require-dev": {
"phpunit/phpunit": "3.7.*",
"predis/predis": "0.8.*@dev",
"squizlabs/php_codesniffer": "2.*",
"symfony/http-foundation": "~2.1"
},
"suggest": {
"ext-openssl": "Allows for usage of secure connections with the stream-based HTTP client.",
"predis/predis": "Allows using the Redis storage backend.",
"symfony/http-foundation": "Allows using the Symfony Session storage backend."
},
"type": "library",
"extra": {
"branch-alias": {
"dev-master": "0.1-dev"
}
},
"autoload": {
"psr-0": {
"OAuth": "src",
"OAuth\\Unit": "tests"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "David Desberg",
"email": "david@daviddesberg.com"
},
{
"name": "Elliot Chance",
"email": "elliotchance@gmail.com"
},
{
"name": "Pieter Hordijk",
"email": "info@pieterhordijk.com"
}
],
"description": "PHP 5.3+ oAuth 1/2 Library",
"keywords": [
"Authentication",
"authorization",
"oauth",
"security"
],
"support": {
"issues": "https://github.com/Lusitanian/PHPoAuthLib/issues",
"source": "https://github.com/Lusitanian/PHPoAuthLib/tree/master"
},
"time": "2018-02-14T22:37:14+00:00"
},
{
"name": "psr/cache",
"version": "1.0.1",
"source": {
"type": "git",
"url": "https://github.com/php-fig/cache.git",
"reference": "d11b50ad223250cf17b86e38383413f5a6764bf8"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/php-fig/cache/zipball/d11b50ad223250cf17b86e38383413f5a6764bf8",
"reference": "d11b50ad223250cf17b86e38383413f5a6764bf8",
"shasum": ""
},
"require": {
"php": ">=5.3.0"
},
"type": "library",
"extra": {
"branch-alias": {
"dev-master": "1.0.x-dev"
}
},
"autoload": {
"psr-4": {
"Psr\\Cache\\": "src/"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "PHP-FIG",
"homepage": "http://www.php-fig.org/"
}
],
"description": "Common interface for caching libraries",
"keywords": [
"cache",
"psr",
"psr-6"
],
"support": {
"source": "https://github.com/php-fig/cache/tree/master"
},
"time": "2016-08-06T20:24:11+00:00"
},
{ {
"name": "psr/container", "name": "psr/container",
"version": "1.0.0", "version": "1.0.0",
@ -164,6 +284,53 @@
}, },
"time": "2020-03-23T09:12:05+00:00" "time": "2020-03-23T09:12:05+00:00"
}, },
{
"name": "samwilson/phpflickr",
"version": "4.14.1",
"source": {
"type": "git",
"url": "https://github.com/samwilson/phpflickr.git",
"reference": "30026afb87e5acb4a3bd75b666fdc39a3a45b161"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/samwilson/phpflickr/zipball/30026afb87e5acb4a3bd75b666fdc39a3a45b161",
"reference": "30026afb87e5acb4a3bd75b666fdc39a3a45b161",
"shasum": ""
},
"require": {
"ext-curl": "*",
"ext-json": "*",
"ext-libxml": "*",
"ext-simplexml": "*",
"lusitanian/oauth": "^0.8",
"php": ">=5.6",
"psr/cache": "^1.0"
},
"require-dev": {
"jakub-onderka/php-parallel-lint": "1.0.0",
"mediawiki/minus-x": "^0.3",
"phpunit/phpunit": "^5.0",
"squizlabs/php_codesniffer": "^3.0",
"tedivm/stash": "^0.14"
},
"type": "library",
"autoload": {
"psr-4": {
"Samwilson\\PhpFlickr\\": "src/"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"GPL-3.0-or-later"
],
"description": "A PHP wrapper for the Flickr API, including Oauth.",
"support": {
"issues": "https://github.com/samwilson/phpflickr/issues",
"source": "https://github.com/samwilson/phpflickr/tree/master"
},
"time": "2020-08-12T08:41:42+00:00"
},
{ {
"name": "symfony/browser-kit", "name": "symfony/browser-kit",
"version": "v5.2.3", "version": "v5.2.3",
@ -1342,7 +1509,9 @@
"stability-flags": [], "stability-flags": [],
"prefer-stable": false, "prefer-stable": false,
"prefer-lowest": false, "prefer-lowest": false,
"platform": [], "platform": {
"ext-imagick": "*"
},
"platform-dev": [], "platform-dev": [],
"plugin-api-version": "2.0.0" "plugin-api-version": "2.0.0"
} }

View File

@ -0,0 +1,68 @@
<?php
namespace Shikiryu\WebGobbler\Collector;
use Samwilson\PhpFlickr\PhpFlickr;
use Shikiryu\WebGobbler\Collector;
class FlickrCollector extends Collector
{
/**
* @return string
*/
public function getName()
{
return 'collector_flickr';
}
/**
* @return int
*/
public function getRandomImage()
{
if (!$this->getConfig()->has('collector.flickr.key')) {
throw new \Exception('Flickr API key not found.');
}
$flickr_client = new PhpFlickr(
$this->getConfig()->get('collector.flickr.key'),
$this->getConfig()->get('collector.flickr.secret')
);
$word_to_search = $this->getConfig()->get('collector.keywords.keywords', $this->generateRandomWord());
$photos = $flickr_client->photos()->search($word_to_search);
var_dump($photos);
}
/**
* @param int $number
*
* @return int
*/
public function getRandomImages(int $number)
{
if (!$this->getConfig()->has('collector.flickr.key')) {
throw new \Exception('Flickr API key not found.');
}
$flickr_client = new PhpFlickr(
$this->getConfig()->get('collector.flickr.key'),
$this->getConfig()->get('collector.flickr.secret')
);
$word_to_search = $this->getConfig()->get('collector.keywords.keywords', $this->generateRandomWord());
$photos = $flickr_client->photos()->search(['text' => $word_to_search]);
$photos = $photos['photo'];
$index_to_download = array_rand($photos, $number);
if (!is_array($index_to_download)) {
$index_to_download = [$index_to_download];
}
foreach ($index_to_download as $photo_index) {
$photo = $photos[$photo_index];
$img_url = sprintf('https://live.staticflickr.com/%s/%s_%s_b.jpg', $photo['server'], $photo['id'], $photo['secret']);
file_put_contents($this->getPoolDirectory() . '/' . basename(parse_url($img_url, PHP_URL_PATH)), file_get_contents($img_url));
}
var_dump($photos);
}
}