Flickr Picture Scraper Class
This class was created for a clients project of mine. All I required was the thumbnails of the last 6 pictures which linked to the real pictures. I purposely decided to go old school and just scrape the flickr site for the photos rather than implement the API.
<?php
/*
*
* This is a flickr script that will grab thumbnails from a
* flickr account by scraping the site. I created this with
* intentionally without usage of the API. It also contains
* a caching feature.
*
* Example usage:
* $flickr = new flickr($flickr_username);
* $thumbs_arr = $flickr->thumbs();
* $pics_arr = $flickr->pics();
*/
class flickr {
private $username, $rss, $id, $limit, $thumbs, $pics;
// takes the username as a required field and can optionally limit amount of pics
public function __construct($username, $limit = 6){
$this->username = $username;
$this->limit = $limit;
// if its not in the cache grab from flickr
if(!$this->check_cache()){
$this->get_id();
$this->get_rss();
$this->get_pics();
$this->write_cache();
}
}
// check the cache in the tmp dir, cache lasts 1 hour
private function check_cache(){
$timestamp = mktime(date('G'), 0, 0); // the hour
if(file_exists('/tmp/flickr_'.$timestamp.'.txt')){
$json = file_get_contents('/tmp/flickr_'.mktime(date('G'), 0, 0).'.txt');
$json_obj = json_decode($json);
$this->thumbs = $json_obj->thumbs;
$this->pics = $json_obj->pics;
return true;
}
return false;
}
// write the arrays of thumbs and pics to the cache
private function write_cache(){
$timestamp = mktime(date('G'), 0, 0); // the hour
file_put_contents('/tmp/flickr_'.$timestamp.'.txt', json_encode(array('pics' => $this->pics, 'thumbs' =>
$this->thumbs)));
}
// delete the cache if for any reason we want fresh results
public function delete_cache(){
$timestamp = mktime(date('G'), 0, 0); // the hour
unlink('/tmp/flickr_'.$timestamp.'.txt');
}
// get the flickr id from the username
private function get_id(){
$flickr_page = file_get_contents('http://flickr.com/photos/'.$this->username);
preg_match('|id=(.+)@|i', $flickr_page, $matches);
$this->id = $matches[1];
}
// get the rss feed for the photostream
private function get_rss(){
$feed_url = 'http://api.flickr.com/services/feeds/photos_public.gne?id='.$this->id.'@N00&lang=en-us&format=rss_200';
$this->rss = file_get_contents($feed_url);
}
// get hte pics and thumbs from the photostream
private function get_pics(){
preg_match_all('/media:thumbnail url="(.+)" height="75" width="75"/i', $this->rss, $matches);
preg_match_all('/media:content url="(.+)"/i', $this->rss, $matches2);
$i = 0;
foreach($matches[1] as $thumb){
$this->thumbs[] = $thumb;
$this->pics[] = $matches2[1][$i];
$i++;
}
}
// return the thumbs, using limit
public function thumbs(){
return array_slice($this->thumbs, 0, $this->limit, true);
}
// return the pics, using limit
public function pics(){
return array_slice($this->pics, 0, $this->limit, true);
}
}
?>