Guest User

pregen_flickr

a guest
Sep 15th, 2011
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.04 KB | None | 0 0
  1. <?php /* vim: set noet ts=4 sw=4 ft=php: : */
  2.  
  3. define('FLICKR_GROUP_ID',    '610963@N20');
  4. define('FLICKR_REST_URL',    'http://api.flickr.com/services/rest/');
  5. define('FLICKR_REST_METHOD', 'flickr.groups.pools.getPhotos');
  6. define('FLICKR_REST_FORMAT', 'json');
  7.  
  8. function pregen_flickr($apiKey, $outputPath, $limit = 100)
  9. {
  10.     // ensure caller specified a place to write to.
  11.     $outputPath = rtrim($outputPath, '/');
  12.     if (!$outputPath) {
  13.         trigger_error("No flickr output directory specified.", E_USER_WARNING);
  14.         return;
  15.     }
  16.  
  17.     // ensure path exists and is writable.
  18.     if (!is_dir($outputPath)) {
  19.         if (!@mkdir($outputPath, 0755)) {
  20.             trigger_error("Can't create flickr output directory: $outputPath", E_USER_WARNING);
  21.             return;
  22.         }
  23.     }
  24.     if (!is_writable($outputPath)) {
  25.         if (!@chmod($outputPath, 0755)) {
  26.             trigger_error("Can't make flickr output directory writable: $outputPath", E_USER_WARNING);
  27.             return;
  28.         }
  29.     }
  30.  
  31.     // construct flickr api call
  32.     // extras is set to 'url_sq' to get the href of the square thumbnail.
  33.     // see http://www.flickr.com/services/api/flickr.groups.pools.getPhotos.html
  34.     $url = FLICKR_REST_URL . '?' . http_build_query(array(
  35.         'api_key'        => $apiKey,
  36.         'per_page'       => $limit,
  37.         'extras'         => 'url_sq',
  38.         'group_id'       => FLICKR_GROUP_ID,
  39.         'method'         => FLICKR_REST_METHOD,
  40.         'format'         => FLICKR_REST_FORMAT,
  41.         'nojsoncallback' => 1
  42.     ));
  43.  
  44.     // fetch photo info
  45.     $response = file_get_contents($url);
  46.     $decoded  = json_decode($response, true);
  47.     $photos   = $decoded['photos']['photo'];
  48.  
  49.     // write out the photo info.
  50.     file_put_contents($outputPath . '/photos.json', $response);
  51.  
  52.     // fetch all of the photos
  53.     foreach ($photos as $photo) {
  54.         file_put_contents(
  55.             $outputPath . '/' . basename($photo['url_sq']),
  56.             file_get_contents($photo['url_sq'])
  57.         );
  58.     }
  59. }
Advertisement
Add Comment
Please, Sign In to add comment