Skip to content

Instantly share code, notes, and snippets.

@sanmai
Last active October 4, 2015 05:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sanmai/2584830 to your computer and use it in GitHub Desktop.
Save sanmai/2584830 to your computer and use it in GitHub Desktop.
My Flickr to HTML convertor
<?php
define('API_KEY', 'set your Flickr API key here');
define('MY_USER_ID', '42620619@N05');
set_time_limit(600);
class flickrCall {
const URL_ENDPOINT = 'http://api.flickr.com/services/rest/?';
protected $data = array();
protected static $ch;
public function __construct($method)
{
$this->data = array(
'method' => $method,
'api_key' => API_KEY,
'format' => 'json',
'nojsoncallback' => 1,
);
self::$ch = curl_init();
curl_setopt_array(self::$ch, array(
CURLOPT_FAILONERROR => 1,
CURLOPT_RETURNTRANSFER => 1,
));
}
public function __set($key, $value)
{
$this->data[$key] = $value;
}
public $requestTime = 0;
public $errorCode = null;
public $httpStatus = null;
public function execute()
{
curl_setopt(self::$ch, CURLOPT_URL, self::URL_ENDPOINT.http_build_query($this->data));
$this->requestTime = microtime(true);
$result = curl_exec(self::$ch);
$this->requestTime = microtime(true) - $this->requestTime;
$this->errorCode = curl_errno(self::$ch);
$this->httpStatus = curl_getinfo(self::$ch, CURLINFO_HTTP_CODE);
return json_decode($result, true);
}
}
class flickrCallWithCache extends flickrCall {
const CACHE = 'flickrcache/';
public function __construct($method)
{
parent::__construct($method);
is_dir(self::CACHE) || mkdir(self::CACHE);
}
public function execute()
{
$key = $this->data;
unset($key['api_key']);
$key = self::CACHE.implode('-', $key);
if (is_file($key) && time() - filemtime($key) < 7200) {
return unserialize(file_get_contents($key));
}
$result = parent::execute();
if ($this->errorCode == 0) {
file_put_contents($key, serialize($result));
}
return $result;
}
}
?><!doctype html>
<html lang="ru">
<head>
<meta charset="utf-8" content="">
<title>Код для фликра</title>
<style type="text/css">
.cnt {
color: gray;
font-size: 80%;
text-decoration: none;
}
.flfoto {
border: 1px solid black;
margin-top: 5px;
margin-bottom: 10px;
}
</style>
</head>
<body>
<?php if (empty($_REQUEST['photoset'])):
$call = new flickrCall('flickr.photosets.getList');
$call->per_page = 20;
$call->page = 1;
$call->user_id = MY_USER_ID;
$result = $call->execute();
?>
<ul>
<?php foreach ($result['photosets']['photoset'] as $photoset):?>
<li><a href="<?php echo $_SERVER['PHP_SELF']?>?photoset=<?php echo $photoset['id']?>"><?php echo $photoset['title']['_content']?></a> - <?php echo $photoset['photos']?></li>
<?php endforeach; ?>
</ul>
<?php else:
$call = new flickrCall('flickr.photosets.getPhotos');
$call->photoset_id = trim($_REQUEST['photoset']);
$result = $call->execute();
ob_start();
$exifKeys = array('ExposureTime', 'FNumber', 'FocalLength', 'Lens');
$count = 1;
foreach ($result['photoset']['photo'] as $photo) {
$call = new flickrCallWithCache('flickr.photos.getInfo');
$call->photo_id = $photo['id'];
$call->secret = $photo['secret'];
$result = $call->execute();
$result = $result['photo'];
$url = $result['urls']['url'][0]['_content'];
$description = $result['description']['_content'];
//$title = array($result['title']['_content']);
$title = array();
if (!empty($result['location'])) array_walk_recursive ($result['location'], function($value, $key) use (&$title) {
if ($key == '_content') {
$title[] = $value;
}
});
$call = new flickrCallWithCache('flickr.photos.getExif');
$call->photo_id = $photo['id'];
$call->secret = $photo['secret'];
$result = $call->execute();
isset($result['photo']['exif']) && array_walk($result['photo']['exif'], function ($row) use (&$title, $exifKeys) {
if (!in_array($row['tag'], $exifKeys)) return;
if (!empty($row['clean']['_content'])) {
$title[] = $row['clean']['_content'];
} else {
$title[] = $row['raw']['_content'];
}
});
$title = implode(', ', $title);
$call = new flickrCallWithCache('flickr.photos.getSizes');
$call->photo_id = $photo['id'];
$call->secret = $photo['secret'];
$result = $call->execute();
$img = array_pop(array_filter($result['sizes']['size'], function($row) {
//return $row['width'] == 1024 || $row['height'] = '1024';
return $row['label'] == 'Large';
}));
if (empty($img['source'])) {
ob_end_clean();
var_dump($result['sizes']['size']);
break;
}
?>
<a name="photo<?php echo $count?>" href="#photo<?php echo $count?>" class="cnt"><?php echo $count?></a> <?php echo $description?>
<a href="<?php echo $url?>"><img src="<?php echo $img['source']?>" alt="<?php echo htmlspecialchars($title)?>" title="<?php echo htmlspecialchars($title)?>" class="flfoto"></a>
<?php
//var_dump($url, $title, $description);
//var_dump($img['source']);
$count += 1;
}
$result = ob_get_clean();
?><textarea>
<?php echo htmlspecialchars($result);?>
</textarea>
<hr><?php
echo nl2br($result);
//echo htmlspecialchars(nl2br($result));
endif; ?>
</body></html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment