Skip to content

Instantly share code, notes, and snippets.

@johnholdun
Created August 4, 2011 06:32
Show Gist options
  • Save johnholdun/1124603 to your computer and use it in GitHub Desktop.
Save johnholdun/1124603 to your computer and use it in GitHub Desktop.
Retrieve post notes for any tumblr tumblelog
<!--
a styled php version you can throw up on your apache
because i'm some kind of machinist
-->
<style>
/* oooh */
html, body, ul, li { font-family: Verdana; font-size: 14px; line-height: 20px; }
body { width: 400px; margin: 20px auto; background: #333; }
ul { list-style: none; }
li { margin-bottom: 10px; color: #AAA; overflow: hidden; height: 20px; white-space: nowrap; text-overflow: ellipsis; }
a { color: #CCC; text-decoration: none; }
.user { color: #FFF; }
</style>
<?php
# configure thusly:
$blog_name = 'johnholdun';
$api_key = 'oops'; # get this from http://www.tumblr.com/oauth/apps, oy
# then stop configuring
$url = "http://api.tumblr.com/v2/blog/$blog_name.tumblr.com/posts/?notes_info=true&api_key=$api_key";
$response = json_decode(file_get_contents($url));
$response = $response->response;
$summary_max_length = 60;
$summaries = array();
foreach($response->posts as $p) {
$summary = (isset($p->title) ? $p->title :
(isset($p->caption) ? $p->caption :
ucfirst($p->type)));
$summary = preg_replace('/<[^>]+>/', '', $summary);
$summary = substr($summary, 0, $summary_max_length) . (strlen($summary) > $summary_max_length ? '...' : '');
$summaries[$p->id] = $summary;
}
$notes = array();
foreach($response->posts as $p) {
if (!isset($p->notes)) continue;
foreach ($p->notes as $n) {
if ($n->timestamp > $p->timestamp && $n->blog_name != $blog_name) {
$n->post = $p->id;
$notes[] = $n;
}
}
}
usort($notes, function($a, $b) { return $a->timestamp > $b->timestamp ? -1 : 1; });
# can't actually get replies, i guess. super wow.
$past_tense_note_types = array('like' => 'liked', 'reblog' => 'reblogged', 'note_model' => 'replied to');
$note_stories = array();
foreach ($notes as $n) {
$note_stories[] = <<<NOTE
<li>
<a class="user" href="{$n->blog_url}">
{$n->blog_name}
</a>
{$past_tense_note_types[$n->type]}
<a class="post" href="{$response->blog->url}post/{$n->post}">
<em>{$summaries[$n->post]}</em>
</a>
</li>
NOTE;
}
# lol check me out
echo '<ul>' . implode('', $note_stories) . '</ul>';
# configure thusly:
blog_name = 'johnholdun'
api_key = 'oops' # get this from http://www.tumblr.com/oauth/apps, oy
# then stop configuring
url = "http://api.tumblr.com/v2/blog/#{blog_name}.tumblr.com/posts/?notes_info=true&api_key=#{api_key}"
response = JSON.parse(open(url).read)['response']
summary_max_length = 60
summaries = Hash[* response['posts'].map { |p|
summary = p['title'] || p['caption'] || p['type'].capitalize
summary.gsub! /<[^>]+>/, ''
summary = summary[0, summary_max_length] + (summary.length > summary_max_length ? '...' : '')
[p['id'], summary]
}.flatten ]
notes = response['posts'].map { |p|
next unless p['notes'].is_a? Array
p['notes'].map { |n|
next unless n['timestamp'].to_i > p['timestamp'].to_i and n['blog_name'] != blog_name
n.merge 'post' => p['id']
}.compact
}.flatten.compact.sort_by{ |n| n['timestamp'].to_i }.reverse
# can't actually get replies, i guess. super wow.
past_tense_note_types = { 'like' => 'liked', 'reblog' => 'reblogged', 'note_model' => 'replied to' }
note_stories = notes.map do |n|
<<-NOTE
<li>
<a href="#{n['blog_url']}">
#{n['blog_name']}
</a>
#{past_tense_note_types[n['type']]}
<a href="#{response['blog']['url']}post/#{n['post']}">
<em>#{summaries[n['post']]}</em>
</a>
</li>
NOTE
end
# lol check me out
puts "<ul>#{note_stories.join}</ul>"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment