-
Notifications
You must be signed in to change notification settings - Fork 0
/
web.rb.save
136 lines (125 loc) · 4.75 KB
/
web.rb.save
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
require 'rubygems'
require 'sinatra'
require 'yaml'
require 'news'
class ManufactureNewspaperJob < Struct.new(:username)
def perform
manufacture_newspaper_for(username)
end
end
def manufacture_newspaper_for(username)
puts "Calculating strongies"
strong_ties = get_strong_ties_for(username)
puts "Calculating Centralities"
centralities = get_centralities_for(username)
puts "Getting tweets"
result = get_tweets_for(username)
puts "Filtering only to tweets with links"
result = get_links_for(result)
puts "Getting retweets for tweets"
result = get_retweets_for(result)
puts "Getting news value for tweets"
result = get_news_value_for(result)
puts "Calculating newspaper"
result = calculate_newspaper(result,strong_ties,centralities)
puts "Getting content for best 20 items"
result = result.sort!{|a,b| a[:score] <=> b[:score]}.reverse[0..20]
result = get_content_for(result)
puts "DONE"
File.open("/data/" + username + "_results","w") {|file| file.puts(result.to_yaml)}
return result
end
get '/' do
@username = "Twitter"
haml :index
end
post '/' do
@username = params[:username]
@email = params[:email]
end
get '/wait' do
haml :wait
end
get '/new/:username' do |username|
Delayed::Job.enqueue(ManufactureNewspaperJob.new(username))
redirect '/wait'
end
get '/show/:username' do |username|
@username = username
result = check("/data/" + username + "_results")
if result == nil
Delayed::Job.enqueue(ManufactureNewspaperJob.new(username))
redirect '/wait'
else
puts "Serving precomputed newspaper"
@news = result
haml :show
end
end
__END__
@@layout
%html
<?xml version="1.0" encoding="UTF-8"?>
%header
%link{ :rel => "stylesheet", :type => "text/css", :href => "/reset.css"}
%link{ :rel => "stylesheet", :type => "text/css", :href => "/style.css"}
%link{ :rel => "stylesheet", :type => "text/css", :href => "/typography.css"}
%link{ :rel => "stylesheet", :type => "text/css", :href => "/uni-form.css"}
%link{ :rel => "stylesheet", :type => "text/css", :href => "/default.uni-form.css"}
%script{ :type =>"text/javascript", :src => "http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js"}
%script{ :type =>"text/javascript", :src => "/uni-form-validation.jquery.min.js"}
%meta{ "http-equiv" => "Content-Type", :content => "text/html; charset=utf-8"}
%title="The #{@username} Zeit"
%body
:javascript
$(function(){ $('form.uniForm').uniform(); });
#header
%h1{:id => "title"}
="The #{@username} Zeit"
.main
=yield
@@index
%h3 The Twitter Zeit is a personalized social newspaper that gives you the news that your friends talk about.
%p
%img{ :src => "/newspaper.png", :alt => "Reading is fun again"}
%form{ :action => "", :method => "post", :class => "uniForm"}
%fieldset.inlineLabels
.ctrlHolder
%label{:for => "name"} Your Twitter Username:
%input{:type => "text", :name => "username", :class => "textInput required"}
%p.formHint Enter your Twitter Username
.ctrlHolder
%label{:for => "mail"} Your Email:
%input{:type => "text", :name => "email", :class => "textInput required"}
%p.formHint Enter your Email to get a notification once the Newspaper has been manufactured.
.buttonHolder
%button{:type => "submit", :class => "primaryAction"} Create my Twitter Zeit
@@wait
%h3 The smurfs are working. You have to wait a little now. We will send you an email once we are done manufacturing your newspaper.
%h3 While waiting you might want to surf reddit :)
@@show
- @news.each do |item|
#separator
%p
%h2
-if item[:icon].to_s.lstrip.rstrip != ""
%img{:src => item[:icon], :width => 50, :height => 50}
%a{:href => item[:uri]}
=item[:title]
%blockquote
="Tweet: #{item[:text]} (#{item[:created_at]}). "
%h3
Tweeted by:
%a{:href => "http://twitter.com/#{item[:user][:screen_name]}/status/#{item[:id_str]}"}
=item[:user][:screen_name]
%h4="Retweeted by #{item[:retweet_ids].count}:"
%h6="#{item[:retweet_ids].collect{|r| r[:person]}.join(' ')}"
- total_score_string = "http://chart.apis.google.com/chart?chxr=0,0,4&chxt=y&chs=300x150&cht=gm&chd=t:#{item[:score]}&chds=0,4&chtt=Total+Value"
%img{ :src => total_score_string, :width => "300", :height => "150", :alt => "Tweet Value"}
- pre = "http://chart.apis.google.com/chart?chbh=a,6,20&chs=300x150&cht=bhg&chco=FF0000,00FF00,FF9900,0000FF&chds=0,1,0,1,0,1,0,1&chd="
- val = "t:#{item[:nw_score]}|#{item[:rt_score]}|#{item[:ct_score]}|#{item[:st_score]}"
- suff = "&chm=tRecency,000000,0,0,13|tRetweets,000000,1,1,13,-1|tAuthority,000000,2,0,13|tFriend+Value,000000,3,1,13"
%img{ :src => "#{pre+val+suff}", :width => "300", :height => "150", :alt => "" }
%p
=item[:content]
%hr