-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.rb
235 lines (208 loc) · 6.92 KB
/
app.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
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
require 'sinatra'
require 'twitter'
require 'ruby_rhymes'
require 'twitter-text'
require 'pony'
require 'redis'
require 'json'
$client = Twitter::REST::Client.new do |config|
config.consumer_key = ENV['TWITTER_CONSUMER_KEY']
config.consumer_secret = ENV['TWITTER_CONSUMER_SECRET']
config.access_token = ENV['TWITTER_ACCESS_TOKEN']
config.access_token_secret = ENV['TWITTER_ACCESS_TOKEN_SECRET']
end
# GLOBALS
include Twitter::TwitterText::Autolink
Encoding.default_external = "utf-8"
# CONFIGURE
configure do
set :public_folder, Proc.new { File.join(root, "static") }
enable :sessions
end
configure do
redis_uri = (ENV["REDISTOGO_URL"]) || 'redis://localhost:6379/'
uri = URI.parse(redis_uri)
REDIS = Redis.new(:host => uri.host, :port => uri.port || '1234', :password => uri.password)
end
$words = ["iphone", "Panic", "love", "quote", "guilt", "pleasure",
"angry", "happy", "sad", "bored", "hate", "insult", "sex", "valentine", "kim", "justin", "beiber","
having","then","now","what","nowplaying","coffee","love","hello","world"]
$top10 = []
def cleanTweet(tweet)
#remove hashtags,RT,@username, and urls
tweet = tweet.gsub("RT ","").gsub(": ","").gsub("?","").gsub("\n","").gsub('"',"").gsub("'","").strip.chomp
tweet = auto_link(tweet).gsub(/.*<a.*<\/a>/,"").strip.chomp.gsub("#","")
special = [">","<","!","@","{","}","<",">","-","_",":",";","/","\\"]
clean = true
special.each { |i| clean = false if tweet.include? i}
if(tweet.length < 40 or clean == false)
nil
else
tweet
end
end
def findTweet(word)
begin
# $client.search(word, {:lang => 'en', :rpp => 100}).collect
$client.search(word, {:lang => 'en'}).take(100).collect
rescue Exception => e
puts e
[]
end
end
def poemize(word1,word2)
word1 = $words.sample.downcase if(word1 == "" or word1 == nil)
word2 = $words.sample.downcase if(word2 == "" or word2 == nil)
@first_pair = []
findTweet(word1).each do |tweet|
first = cleanTweet(tweet.text)
if first and !first.start_with? word1 and !first.end_with? word1
puts "FIRST1 " + first
rhymes = first.to_phrase.flat_rhymes.delete_if{|x| x.include? "'"}
if rhymes!=[]
rhyme = rhymes.sample
findTweet(rhyme).each do |nexttweet|
second = cleanTweet(nexttweet.text)
if second
#We are done
puts "SECOND1 " + second
@first_pair.push({1 => first, 2=>second, "t1" => tweet.user.screen_name, "t2" => nexttweet.user.screen_name, "s1" => tweet.id, "s2" => nexttweet.id})
break
end
end
end
end
end
#puts @first_pair.inspect
@second_pair = []
findTweet(word2).each do |tweet|
first = cleanTweet(tweet.text)
if first and !first.start_with? word2 and !first.end_with? word2
puts "FIRST2 " + first
rhymes = first.to_phrase.flat_rhymes.delete_if{|x| x.include? "'"}
if rhymes!=[]
rhyme = rhymes.sample
findTweet(rhyme).each do |nexttweet|
second = cleanTweet(nexttweet.text)
if second
#We are done
puts "SECOND2 " + second
@second_pair.push({3 => first, 4=>second, "t3" => tweet.user.screen_name, "t4" => nexttweet.user.screen_name, "s3" => tweet.id, "s4" => nexttweet.id})
break
end
end
end
end
end
#puts @second_pair.inspect
puts @poem
@poem = @first_pair.sample.merge(@second_pair.sample) rescue nil
end
get '/' do
@poems = []
$top10.each do |key|
@poems.push(eval(REDIS.get(key)))
end
erb :index
end
get '/about' do
erb :about
end
get '/create' do
session[:create] = true
word1 = params[:word1]
word2 = params[:word2]
if(word1 == nil and word2 == nil)
@poems = ""
else
puts word1
puts word2
@poems = poemize(word1, word2)
key = (0...7).map{ ('a'..'z').to_a[rand(26)] }.join
if(@poems)
puts "HERE_WE_GO"
puts @poems
@poems["key"] = key
url = "http://www.140verses.com/share/" + key
REDIS.set(key,@poems)
$client.update("@"+ @poems["t1"] + " ,We just composed a poem using your tweet at #140verses, take a look at #{url}") rescue nil
$client.update("@"+ @poems["t2"] + " ,We just composed a poem using your tweet at #140verses, take a look at #{url}") rescue nil
$client.update("@"+ @poems["t3"] + " ,We just composed a poem using your tweet at #140verses, take a look at #{url}") rescue nil
$client.update("@"+ @poems["t4"] + " ,We just composed a poem using your tweet at #140verses, take a look at #{url}") rescue nil
end
end
erb :create
#@poems = {1 => "Hello", 2 => "World", 3=>"somtimes I think this should be longer that what it should be and then I worry about what is next, aaaaaaaasdfasdfasdfasdfasfdfasdfasdfasdfsadf", 4=>"a", "t1"=>"cggaurav", "t2" => "nikhilv", "t3" => "140verses", "t4" => "__asf"}
end
get '/discover' do
@poems = []
(0..4).each do |i|
key = REDIS.randomkey()
poem = eval(REDIS.get(key))
poem["key"] = key
@poems.push(poem) if poem
end
erb :discover
end
get '/tweet' do
$client.update("Welcome to #140verses")
"Done"
end
get '/flushall' do
REDIS.flushall()
end
post '/contact' do
puts params.inspect
begin
Pony.mail(
:from => params[:email],
:to => 'nikhil@140verses.com',
:subject => params[:name] + " has contacted you",
:body => params[:feedback],
:port => '587',
:via => :smtp,
:via_options => {
:address => 'smtp.sendgrid.net',
:port => '587',
:enable_starttls_auto => true,
:user_name => ENV['SENDGRID_USERNAME'],
:password => ENV['SENDGRID_PASSWORD'],
:authentication => :plain,
:domain => ENV['SENDGRID_DOMAIN']
})
redirect '/contact?status=true'
rescue Exception => e
redirect '/contact?status=false'
end
end
get '/contact' do
@status = params[:status]
erb :contact
end
get '/top10/:id' do
key = params[:id]
$top10.unshift(key) if REDIS.exists(key)
end
get '/share/:id' do
key = params[:id].to_s
begin
@poem = eval(REDIS.get(key))
url = "http://www.140verses.com/share/" + key
if(session[:create] == true)
$client.update("@"+ @poem["t1"] + " ,We just composed a poem using your tweet at #140verses, take a look at #{url}") rescue nil
$client.update("@"+ @poem["t2"] + " ,We just composed a poem using your tweet at #140verses, take a look at #{url}") rescue nil
$client.update("@"+ @poem["t3"] + " ,We just composed a poem using your tweet at #140verses, take a look at #{url}") rescue nil
$client.update("@"+ @poem["t4"] + " ,We just composed a poem using your tweet at #140verses, take a look at #{url}") rescue nil
end
erb :share
rescue Exception => e
erb :notfound
end
end
not_found do
erb :notfound
end
# topics = ["coffee", "tea"]
# client.filter(:track => topics.join(",")) do |tweet|
# puts tweet.text
# end