topical media & game development

talk show tell print

mashup-flickr-11-Complete-lib-NewsVisualizer.php / php



  <?php
  
  class NewsVisualizer
  {
    var flickr;
    var feedUrl;
    var rss;
    var maxPhotos = 6;
    var maxStories = 5;
  
    // Constructor        
    function NewsVisualizer(feedUrl)
    {
      global flickrApiKey;
      global flickrApiSecret;
  
      this->flickr = new phpFlickr(flickrApiKey, flickrApiSecret, false);
      this->feedUrl = feedUrl;
      this->rss = fetch_rss(this->feedUrl);
    }
  
    function getTitle()
    {
      return this->rss->channel['title'];
    }
  
    function getDescription()
    {
      return this->rss->channel['description'];
    }
  
    function getLink()
    {
      return this->rss->channel['link'];
    }
  
    function getItems()
    {
      items = array();
  
      foreach (this->rss->items as item)
      {
        newsItem = new NewsItem(item);
        photos = this->findImages(newsItem->getKeywords());
  
        photoCount = 0;
        foreach (photos as photo)
        {
          squarePhoto = 'http://static.flickr.com/' . photo['server'] 
            . '/' . photo['id'] . '_' .photo['secret'] . '_s.jpg';
          photoPage = 'http://www.flickr.com/photos/' . photo['owner'] 
            . '/' . photo['id'] . '/';
          newsItem->addPhoto(squarePhoto, photoPage);
          if (++photoCount >= this->maxPhotos)
          {
            break;
          }
        }
  
        array_push(items, newsItem);
  
        if (count(items) >= this->maxStories)
        {
          break;
        }
      }
     return items;
    }
  
    function findImages(keywords)
   {
     args = array(
        'tags' => join(keywords, ','),
        'tag_mode' => 'any',
        'sort' => 'relevance', 
        'page' => 1, 
        'per_page' => this->maxPhotos,
       );
  
      photos = this->flickr->photos_search(args);
  
      if (this->flickr->getErrorCode())
      {
        // Display error message and return an empty set of photos
        echo this->flickr->getErrorMsg() . " whilst searching for photos";
        return array();
      }
  
      return photos['photo'];
    }
  }
  ?>
  


(C) Æliens 20/2/2008

You may not copy or print any of this material without explicit permission of the author or the publisher. In case of other copyright issues, contact the author.