Beispiel #1
0
 /**
  * Upload a batch of files to Flickr.
  *
  * @param   Phlickr_Uploader $uploader
  * @param   Phlickr_Framework_IUploadBatch $batch Provides a list of files
  *          and information on them.
  * @param   Phlickr_Framework_IUploadListener $listener Listens to event
  *          notifications on the status of the upload process.
  * @return  array An array of Phlickr_AuthedPhoto objects with the ids as
  *          the keys.
  * @uses    upload() to do the actual file uploads.
  * @uses    Phlickr_AuthedPhotosetList::create() to create the photoset
  *          if it's requested.
  * @uses    Phlickr_AuthedPhotoset::editPhotos() to put the photos into the
  *          set and select the primary photo.
  * @uses    Phlickr_AuthedPhoto::setTaken() to set the taken date if it's
  *          provided.
  * @since   0.2.5
  */
 function uploadBatch(Phlickr_Framework_IUploadBatch $batch, Phlickr_Framework_IUploadListener $listener)
 {
     // array of uploaded photo objects keyed by id
     $photos = array();
     // array of photo ids keyed by original filename
     $photoIds = array();
     // notify that the upload is starting
     $listener->beforeUpload();
     foreach ($batch->getFiles() as $file) {
         // notify that a file will be uploaded
         $listener->beforeFileUpload($file);
         // fetch the info for the photo
         $title = $batch->getTitleForFile($file);
         $desc = $batch->getDescriptionForFile($file);
         $tags = $batch->getTagsForFile($file);
         // upload it
         try {
             $photoId = $this->upload($file, $title, $desc, $tags);
             // some times it takes a second for the photo to show up.
             try {
                 $photo = new Phlickr_AuthedPhoto($this->_api, $photoId);
             } catch (Phlickr_MethodFailureException $ex) {
                 // give it 10 seconds and try again.
                 sleep(10);
                 $photo = new Phlickr_AuthedPhoto($this->_api, $photoId);
             }
             // keep a filename:id mapping...
             $photoIds[$file] = $photoId;
             // ... and a id:photo mapping
             $photos[$photoId] = $photo;
             // notify of success
             $listener->afterFileUpload($file, $photo);
         } catch (Phlickr_Exception $ex) {
             // notify of failure
             $listener->failedFileUpload($file, $ex);
         }
         // assign the taken date if one is provided
         try {
             $taken = $batch->getTakenDateForFile($file);
             if ($taken) {
                 $photo->setTaken($taken);
             }
         } catch (Phlickr_Exception $ex) {
             // don't worry about it.
         }
     }
     // create a photo set? only if we've got photos and a name.
     if ($photoIds && $batch->isSetWanted()) {
         // figure out the primary photo (if none was specified use the
         // first image)
         if (array_key_exists($batch->getSetPrimary(), $photoIds)) {
             $primaryId = $photoIds[$batch->getSetPrimary()];
         } else {
             // reset $photoIds so we can use current() to find the first value
             reset($photoIds);
             $primaryId = current($photoIds);
         }
         // create the photoset
         $list = new Phlickr_AuthedPhotosetList($this->_api);
         $set = $list->create($batch->getSetTitle(), $batch->getSetDescription(), $primaryId);
         // add the photos to the set
         $set->editPhotos($primaryId, $photoIds);
         // notify of the photoset creation
         $listener->afterCreatePhotoset($set);
     }
     // notify that the upload is complete
     $listener->afterUpload($photos);
     return $photos;
 }
    die("invalid flickr logon");
}
$api->setCacheFilename(CACHE_FILE);
// get a list of tags
printf('Comma separated list of tags: ');
$tags = trim(fgets(STDIN));
// create a request to search for photos tagged with person and happy
// from all users.
$request = $api->createRequest('flickr.photos.search', array('tags' => $tags, 'tag_mode' => 'all', 'user_id' => $api->getUserId()));
// use the photo list and photo list iterator to display the titles and urls
// of each of the photos.
printf("Searching for matching photos tagged with '%s'...\n", $tags);
$pl = new Phlickr_PhotoList($request, Phlickr_PhotoList::PER_PAGE_MAX);
// create a sorter that will sort by color
$sorter = new Phlickr_PhotoSorter(new Phlickr_PhotoSortStrategy_ById());
// use a photolist iterator so that all the pages are sorted.
$photos = $sorter->sort(new Phlickr_PhotoListIterator($pl));
$photo_ids = Phlickr_PhotoSorter::idsFromPhotos($photos);
if (count($photo_ids) == 0) {
    printf("No photos were found.\n", $tags);
} else {
    printf("Found %d photos...\n", count($photo_ids));
    $apsl = new Phlickr_AuthedPhotosetList($api);
    $aps = $apsl->create($tags, 'This photoset was created from the tag(s) ' . $tags, $photo_ids[0]);
    $aps->editPhotos($photo_ids[0], $photo_ids);
    printf("Created a photoset named '%s'. You can view it at:\n%s\n", $tags, $aps->buildUrl());
}
exit(0);
?>