Skip to content

Instantly share code, notes, and snippets.

@cherenkov
Created May 12, 2012 23:01
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 cherenkov/2669611 to your computer and use it in GitHub Desktop.
Save cherenkov/2669611 to your computer and use it in GitHub Desktop.
JSONでデータを読み込んで更新された分だけ追加していきたい Goo.. - 人力検索はてな http://q.hatena.ne.jp/1336822696
<?php
//JSONでデータを読み込んで更新された分だけ追加していきたい Goo.. - 人力検索はてな
//http://q.hatena.ne.jp/1336822696
//APIからつぶやき部分だけを抜き出してファイルにJSONで保存する。
//reference
//https://developers.google.com/+/api/
//https://developers.google.com/+/api/latest/activities
//https://developers.google.com/+/api/latest/activities/list
header("Content-Type: text/html; charset=UTF-8");
echo <<<EOT
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<style>
.entry {
border: 1px solid black;
}
.attachment .left {
float: left;
}
</style>
</head>
<body>
EOT;
function pr($var) {
echo '<pre>'; print_r($var); echo '</pre>';
}
//dump用
function displayContents($items) {
foreach ($items as $item) {
$date = date("Y/m/d G:i:s", strtotime($item['updated']));
$detailLink = "<a href=\"{$item['url']}\">{$date}</a>";
$hostIcon = $item['actor']['image']['url'];
switch ($item['verb']) {
case 'post':
$attachments = getAttachments($item);
$str = <<<EOT
<div class="entry">
<img src="{$hostIcon}"><span>{$detailLink}</span>
<div>{$item['object']['content']}</div>
<div class="attachment">{$attachments}</div>
<i>{$item['verb']}</i>
</div>
EOT;
echo $str;
break;
case 'share':
$attachments = '<hr>' . getAttachments($item);
$srcUserName = $item['object']['actor']['displayName'];
$srcIcon = $item['object']['actor']['image']['url'];
$srcUserLink = $item['object']['actor']['url'];
$srcLink = $item['object']['url'];
$str = <<<EOT
<div class="entry">
<img src="{$hostIcon}"><span>{$detailLink}</span>
<div>{$item['annotation']}</div>
<hr>
<div><img src="{$srcIcon}">最初に <a href="$srcUserLink">{$srcUserName}</a> さんが共有した<a href="$srcLink">投稿</a></div>
<div class="share_content">{$item['object']['content']}</div>
<div class="attachment">{$attachments}</div>
<i>{$item['verb']}</i>
</div>
EOT;
echo $str;
break;
case 'checkin':
$str = <<<EOT
<div class="entry">
<img src="{$hostIcon}"><span>{$detailLink}</span>
<div>{$item['object']['content']}</div>
<div>{$item['placeName']} {$item['address']}</div>
<i>{$item['verb']}</i>
</div>
EOT;
echo $str;
break;
}
echo '<hr>';
}
}
function getAttachments($item) {
$attachments = '';
$article = '';
$photo = '';
if (isset($item['object']['attachments'])) {
foreach ($item['object']['attachments'] as $att) {
if ($att['objectType'] == 'article') {
$content = isset($att['content']) ? $att['content'] : '';
$article = "<div><a href=\"{$att['url']}\">{$att['displayName']}</a><div>{$content}</div></div>";
} elseif ($att['objectType'] == 'photo') {
$photo = "<div class=\"left\"><img src=\"{$att['image']['url']}\"></div>";
}
}
$attachments = $photo . $article;
}
return $attachments;
}
function jwrite($filename, $data) {
$handle = fopen($filename, 'w');
fwrite($handle, json_encode($data));
fclose($handle);
}
$apikey = '自分のAPIキー';
$targetId = '103748274114027132441';
$url = "https://www.googleapis.com/plus/v1/people/{$targetId}/activities/public?key={$apikey}&maxResults=100&prettyPrint=true";
$json = file_get_contents($url);
$data = json_decode($json, true); //第2引数にtrueで連想配列になる。
//pr($data);
//publicなつぶやきが無かったら終了。
if (!count($data['items']))
exit();
//つぶやき部分だけ残す。
$new_items = $data['items'];
$filename = "data{$targetId}.json";
if (file_exists($filename)) {
$old_items = file_get_contents($filename);
$old_items = json_decode($old_items, true);
//一時的に新着分を格納。
$tmp = array();
foreach ($new_items as $item) {
if (strtotime($old_items[0]['updated']) < strtotime($item['updated'])) {
$tmp[] = $item;
}
}
//新着分を先頭に追加
$items = array_merge($tmp, $old_items);
$output = $items;
if (count($tmp)) {
jwrite($filename, $output);
}
} else {
$output = $new_items;
jwrite($filename, $output);
}
//表示確認
displayContents($output);
echo <<<EOT
</body>
</html>
EOT;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment