xref: /web-master/scripts/pregen_flickr (revision 0e511803)
1<?php /* vim: set noet ts=4 sw=4 ft=php: : */
2
3define('FLICKR_GROUP_ID',    '610963@N20');
4define('FLICKR_REST_URL',    'https://api.flickr.com/services/rest/');
5define('FLICKR_REST_METHOD', 'flickr.groups.pools.getPhotos');
6define('FLICKR_REST_FORMAT', 'json');
7define('FLICKR_FILE_PREFIX', 'flickr-');
8
9function pregen_flickr($apiKey, $outputPath, $limit = 100)
10{
11    // ensure caller specified a place to write to.
12    $outputPath = rtrim($outputPath, '/');
13    if (!$outputPath) {
14        trigger_error("No flickr output directory specified.", E_USER_WARNING);
15        return;
16    }
17
18    // ensure path exists and is writable.
19    if (!is_dir($outputPath)) {
20        if (!@mkdir($outputPath, 0755)) {
21            trigger_error("Can't create flickr output directory: $outputPath", E_USER_WARNING);
22            return;
23        }
24    }
25    if (!is_writable($outputPath)) {
26        if (!@chmod($outputPath, 0755)) {
27            trigger_error("Can't make flickr output directory writable: $outputPath", E_USER_WARNING);
28            return;
29        }
30    }
31
32    // construct flickr api call
33    // extras is set to 'url_sq' to get the href of the square thumbnail.
34    // see http://www.flickr.com/services/api/flickr.groups.pools.getPhotos.html
35    $url = FLICKR_REST_URL . '?' . http_build_query([
36        'api_key'        => $apiKey,
37        'per_page'       => $limit,
38        'extras'         => 'url_sq',
39        'group_id'       => FLICKR_GROUP_ID,
40        'method'         => FLICKR_REST_METHOD,
41        'format'         => FLICKR_REST_FORMAT,
42        'nojsoncallback' => 1
43    ]);
44
45    // fetch photo info
46    $response = file_get_contents($url);
47    $decoded  = json_decode($response, true);
48
49    // ensure photo info decoded successfully.
50    if (!isset($decoded['photos']['photo'])) {
51        trigger_error("Failed to decode flickr service response.", E_USER_WARNING);
52        return;
53    }
54
55    // fetch all of the photos
56    $fresh  = [];
57    $photos = $decoded['photos']['photo'];
58    foreach ($photos as $key => &$photo) {
59
60        // sanitize url_sq for use as the image filename.
61        $imageFile          = $outputPath . '/' . FLICKR_FILE_PREFIX
62                            . preg_replace('/[^a-z0-9_\.]/i', '', basename($photo['url_sq']));
63        $photo['filename']  = basename($imageFile);
64        $fresh[]            = $photo['filename'];
65
66        // skip photo if we've already got it.
67        if (file_exists($imageFile)) {
68            continue;
69        }
70
71        // attempt to fetch the file from flickr.
72        $imageData = file_get_contents($photo['url_sq']);
73
74        // if image downloaded successfully, write it out;
75        // otherwise, remove it from the list of photos.
76        if ($imageData) {
77            file_put_contents($imageFile, $imageData);
78        } else {
79            unset($photos[$key]);
80        }
81    }
82
83    // if we failed to download any images, don't commit photo info.
84    if (!count($photos)) {
85        trigger_error("Failed to download any flickr photos.", E_USER_WARNING);
86        return;
87    }
88
89    // write out the photo info.
90    file_put_contents($outputPath . '/flickr.json', json_encode($photos));
91
92    // remove stale flickr images.
93    $files = scandir($outputPath);
94    foreach ($files as $file) {
95        if (strpos($file, FLICKR_FILE_PREFIX) === 0 && !in_array($file, $fresh)) {
96            unlink($outputPath . '/' . $file);
97        }
98    }
99}