peterc (owner)

Revisions

gist: 92847 Download_button fork
public
Public Clone URL: git://gist.github.com/92847.git
Embed All Files: show embed
tumblr_sidebar.rb #
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#!/usr/bin/env ruby
 
require 'erb'
require 'rubygems'
require 'hpricot'
require 'open-uri'
 
base_dir = '/home/avillenw/nwprogressive.org/portal/scripts/'
input_url = 'http://nwprogressive.tumblr.com/api/read'
output_file = File.open(base_dir + '/tumblr_sidebar.html', 'w')
#output_file = STDOUT
 
 
# Read in the feed
doc = Hpricot(open(input_url))
 
# Go through each post (change 100 to limit number)
(doc / "post").first(100).each do |post|
  # Get basic post information
  post_time = Time.at(post[:"unix-timestamp"].to_i) rescue Time.now
  post_type = post[:type] rescue "regular"
  post_timestring = post_time.strftime("%A, %B %e, %Y")
  post_url = post[:url] rescue ""
  
  # Get post attributes
  regular_title = (post / "regular-title").first.inner_text rescue ""
  regular_body = (post / "regular-body").first.inner_text rescue ""
  photo_url = (post / "photo-url").first.inner_text rescue ""
  photo_caption = (post / "photo-caption").first.inner_text rescue ""
  audio_player = (post / "audio-player").first.inner_text rescue ""
  audio_caption = (post / "audio-caption").first.inner_text rescue ""
  quote_text = (post / "quote-text").first.inner_text rescue ""
  quote_source = (post / "quote-source").first.inner_text rescue ""
  conversation_title = (post / "conversation-title").first.inner_text rescue ""
  conversation = (post / "conversation").first rescue []
  link_url = (post / "link-url").first.inner_text rescue ""
  link_text = (post / "link-text").first.inner_text rescue ""
  link_description = (post / "link-description").first.inner_text rescue ""
  video_player = (post / "video-player").first.inner_text rescue ""
  video_caption = (post / "video-caption").first.inner_text rescue ""
  
  # For each post type, use a different HTML template
 
  template = if post_type == 'regular'
    ERB.new %{
<div><p><img src="/weblog/wp-content/themes/kokanee/images/Aside.gif" alt="Aside" /></p>
<p><strong><%= regular_title %></strong></p>
<%= regular_body %>
<p><a class="microlink" href="<%= post_url %>">#</a> <em>(<%= post_timestring %>)</em></p></div>
}
  elsif post_type == 'photo'
    ERB.new %{
<div><p><img src="/weblog/wp-content/themes/kokanee/images/Photo.gif" alt="Photo" /></p>
<p><img src="<%= photo_url %>" width="100" /></p>
<p><%= photo_caption %></p>
<p><a class="microlink" href="<%= post_url %>">#</a> <em>(<%= post_timestring %>)</em></p></div>
}
  elsif post_type == 'audio'
    ERB.new %{
<div><p><img src="/weblog/wp-content/themes/kokanee/images/Audio.gif" alt="Audio" /></p>
<p><%= audio_player.sub('207', '190') %></p>
<p><%= audio_caption %></p>
<p><a class="microlink" href="<%= post_url %>">#</a> <em>(<%= post_timestring %>)</em></p></div>
}
  elsif post_type == 'quote'
    ERB.new %{
<div><p><img src="/weblog/wp-content/themes/kokanee/images/Quote.gif" alt="Quote" /></p>
<blockquote><p><%= quote_text %></p></blockquote>
<p>- <%= quote_source %></p>
<p><a class="microlink" href="<%= post_url %>">#</a> <em>(<%= post_timestring %>)</em></p></div>
}
  elsif post_type == 'conversation'
    ERB.new %{
<div class="chat"><p><img src="/weblog/wp-content/themes/kokanee/images/Chat.gif" alt="Chat" /></p>
<p><strong><%= conversation_title %></strong></p>
<ul>
<% (conversation / "line").each do |line| %>
<li><span class="label"><%= line[:label] %></span> <%= line.inner_text.chomp %></li>
<% end %>
</ul>
<p><a class="microlink" href="<%= post_url %>">#</a> <em>(<%= post_timestring %>)</em></p></div>
}
  elsif post_type == 'link'
    ERB.new %{
<div><p><img src="/weblog/wp-content/themes/kokanee/images/RecLink.gif" alt="Recommended Link" /></p>
<p><strong><a href="<%= link_url %>"><%= link_text %></a>:</strong> <%= link_description %></p>
<p><a class="microlink" href="<%= post_url %>">#</a> <em>(<%= post_timestring %>)</em></p></div>
}
  elsif post_type == 'video'
    ERB.new %{
<div><p><img src="/weblog/wp-content/themes/kokanee/images/Video.gif" alt="Video" /></p>
<p><%= video_player.sub('400', '190').sub('225', '142') %></p>
<p><%= video_caption %></p>
<p><a class="microlink" href="<%= post_url %>">#</a> <em>(<%= post_timestring %>)</em></p></div>
}
  end
  
  output_file.puts(template.result(binding)) rescue nil
end
 
output_file.close