-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtweet_funds.rb
291 lines (241 loc) · 9.34 KB
/
tweet_funds.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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
require 'pg'
require 'open-uri'
require 'nokogiri'
require 'date'
require 'time'
require 'blitline'
require 'twitter'
require_relative 'lib/deep_dup'
require_relative 'lib/ext_classes_methods'
#ACTION: Get data from the database.
connection = PGconn.connect(
:host => ENV['DB_HOST'],
:port => ENV['DB_PORT'],
:dbname => ENV['DB_NAME'],
:user => ENV['DB_USER'],
:password => ENV['DB_PASSWORD']
)
table_name = ENV['DB_TABLE_NAME']
data_from = {:database => {}, :website => {} }
data_from_database = data_from[:database]
data_from_website = data_from[:website]
rows = connection.exec("SELECT * FROM #{table_name} ORDER BY date_and_time_as_int_id DESC LIMIT 1")
fields = rows.fields
rows.each {|row|
fields.each {|field|
data_from_database[field.to_sym] = row[field]
}
}
#ACTION: Database's data's elements formation.
elements_symbols = [:date_and_time_as_int_id, :money_amount, :backers_number]
elements_symbols.each {|element_symbol|
data_from_database[element_symbol] = data_from_database[element_symbol].to_i
}
#ACTION: Get data from the website.
shenmue3_url = 'https://shenmue.link/order'
html_file = open(shenmue3_url)
html_doc = Nokogiri::HTML(html_file)
key_by_index = {
0 => :money_amount,
1 => :backers_number,
2 => :date_phrase
}
html_doc.css('dd, .date').each_with_index {|element, index|
data_from_website[key_by_index[index]] = element.text
}
#ACTION: Website's data's elements formation.
non_digit_regex = /\D/
empty_string = ''
elements_symbols.shift; stats_symbols = elements_symbols
stats_symbols.each {|stat_symbol|
data_from_website[stat_symbol] = data_from_website[stat_symbol].gsub!(non_digit_regex, empty_string).to_i
}
date_phrase = data_from_website[:date_phrase]
parsed_date = Date.parse(date_phrase)
parsed_time = Time.parse(date_phrase)
form_string = '%02d'
year = parsed_date.year
month = parsed_date.month; formed_month = form_string % month
day = parsed_date.day; formed_day = form_string % day
hour = parsed_time.hour; formed_hour = form_string % hour
min = parsed_time.min; formed_min = form_string % min
data_from_website[:date_and_time_as_int_id] =
"#{year}#{formed_month}#{formed_day}#{formed_hour}#{formed_min}".to_i
data_from_website[:date] =
"#{year}/#{formed_month}/#{day}"
#ACTION: Check if website's data has been updated. If not, then exit (else continue).
exit if data_from_website[:date_and_time_as_int_id] == data_from_database[:date_and_time_as_int_id]
#ACTION: Insert website's data in the database.
connection.prepare(
'statement',
"INSERT INTO #{table_name} (date_and_time_as_int_id, money_amount, backers_number, date) VALUES ($1, $2, $3, $4)"
)
connection.exec_prepared(
'statement',
[
data_from_website[:date_and_time_as_int_id],
data_from_website[:money_amount],
data_from_website[:backers_number],
data_from_website[:date]
]
)
#ACTION: Calculate the differences between stats of the database and of the website.
# Also, the difference between dates, that is, how many days have passed.
difference_on = {}
sign_char_of_difference_on = {}
stats_symbols.each {|stat_symbol|
difference_on[stat_symbol] = data_from_website[stat_symbol] - data_from_database[stat_symbol]
sign_char_of_difference_on[stat_symbol] = difference_on[stat_symbol].sign_char
difference_on[stat_symbol] = difference_on[stat_symbol].abs
}
difference_on[:date] = Date.parse(data_from_website[:date]).mjd - Date.parse(data_from_database[:date]).mjd
#ACTION: Create delimited strings (with commas every 3 digits) of the respective numbers.
elements_symbols.push(:date)
delimited_stat_from = {}
[:database, :website].each {|source_symbol|
delimited_stat_from[source_symbol] = {}
elements_symbols.each {|element_symbol|
delimited_stat_from[source_symbol][element_symbol] = delimited_by_number( data_from[source_symbol][element_symbol])
}
}
delimited_stat_from_database = delimited_stat_from[:database]
delimited_stat_from_website = delimited_stat_from[:website]
delimited_difference_on = {}
elements_symbols.each {|element_symbol|
delimited_difference_on[element_symbol] = delimited_by_number( difference_on[element_symbol])
}
#ACTION: Create an image, based on an image prototype and the previously calculated values.
arrow1_char = '►'
fast_forward_char = '»'
date_text = "#{data_from_database[:date]} #{arrow1_char} #{data_from_website[:date]} (#{fast_forward_char}#{delimited_difference_on[:date]})"
money_amount_text =
"$#{delimited_stat_from_database[:money_amount]} #{arrow1_char} $#{delimited_stat_from_website[:money_amount]} (#{sign_char_of_difference_on[:money_amount]}$#{delimited_difference_on[:money_amount]})"
money_per_backer = difference_on[:money_amount]/difference_on[:backers_number]
sign_char_of_money_per_backer = money_per_backer.sign_char
money_per_backer = money_per_backer.abs
delimited_money_per_backer = delimited_by_number(money_per_backer)
backers_number_text =
"#{delimited_stat_from_database[:backers_number]} #{arrow1_char} #{delimited_stat_from_website[:backers_number]} (#{sign_char_of_difference_on[:backers_number]}#{delimited_difference_on[:backers_number]}) backers (#{sign_char_of_money_per_backer}$#{delimited_money_per_backer}/backer)"
functions_word = 'functions'
name_word = 'name'
annotate_word = 'annotate'
params_word = 'params'
text_word = 'text'
color_word = 'color'
point_size_phrase = 'point_size'
x_word = 'x'
y_word = 'y'
gravity_word = 'gravity'
dropshadow_color_phrase = 'dropshadow_color'
dropshadow_offset_phrase = 'dropshadow_offset'
font_family_phrase = 'font_family'
text_colour = '#434343'
text_gravity = 'NorthGravity'
text_font = 'Work Sans'
dropshadow_colour = '#e5e5e5'
functions_data = [{
name_word => annotate_word,
params_word => {
text_word => nil,
color_word => text_colour,
point_size_phrase => nil,
x_word => 0,
y_word => nil,
gravity_word => text_gravity,
dropshadow_color_phrase => dropshadow_colour,
dropshadow_offset_phrase => 2,
font_family_phrase => text_font
}
}]
functions_data_for_date = functions_data
functions_hash_for_date = functions_data_for_date[0]
params = functions_hash_for_date[params_word]
params[text_word] = date_text
params[point_size_phrase] = '16'
params[y_word] = 71
functions_data_for_money_amount = functions_data_for_date.deep_dup
functions_hash_for_money_amount = functions_data_for_money_amount[0]
params = functions_hash_for_money_amount[params_word]
params[text_word] = money_amount_text
params[point_size_phrase] = '20'
params[y_word] = 112
functions_hash_for_date[functions_word] = functions_data_for_money_amount
functions_data_for_backers_number = functions_data_for_money_amount.deep_dup
functions_hash_for_backers_number = functions_data_for_backers_number[0]
params = functions_hash_for_backers_number[params_word]
params[text_word] = backers_number_text
params[point_size_phrase] = '14'
params[y_word] = 160
functions_hash_for_backers_number['save'] = {
'image_identifier' => 'shenmue3_img_data',
'quality' => 100,
'extension' => '.png'
}
functions_hash_for_money_amount[functions_word] = functions_data_for_backers_number
job_hash = {
'application_id' => ENV['BLITLINE_APP_ID'],
'src' => ENV['IMG_SRC'],
functions_word => functions_data
}
img_url = nil
begin
blitline_service = Blitline.new
blitline_service.add_job_via_hash(job_hash)
json_hash = blitline_service.post_job_and_wait_for_poll
img_url = json_hash['images'][0]['s3_url'] unless json_hash.key?('error')
puts json_hash['error']
rescue
puts 'Blitline service fails.'
end
img_file = img_url.nil? ? img_url : open(img_url)
#ACTION: Create a text to be sent as a tweet.
client = Twitter::REST::Client.new do |config|
config.consumer_key = ENV['CONSUMER_KEY']
config.consumer_secret = ENV['CONSUMER_SECRET']
config.access_token = ENV['ACCESS_TOKEN']
config.access_token_secret = ENV['ACCESS_TOKEN_SECRET']
end
arrow2_char = '➡'
calendar_icon_char_code = '🗓'
pouch_icon_char_code = '💰'
duck_icon_char_code = '🦆'
hashtag = '#Shenmue3'
[date_text, money_amount_text, backers_number_text].each {|text|
text.gsub!(arrow1_char, arrow2_char)
}
backers_number_text.gsub!(/\s{1}backers/, empty_string).gsub!(/backer/, duck_icon_char_code)
tweet_prim_text =
"#{calendar_icon_char_code} #{date_text}
#{pouch_icon_char_code} #{money_amount_text}
#{duck_icon_char_code} #{backers_number_text}
#{hashtag}"
tweet_prim_text_size = tweet_prim_text.size
shenmue3_url.gsub!(/https:\/\//, empty_string)
tweet_scnd_text =
"#{calendar_icon_char_code} #{data_from_website[:date]} (#{fast_forward_char}#{delimited_difference_on[:date]})
#{pouch_icon_char_code} $#{delimited_stat_from_website[:money_amount]} (#{sign_char_of_difference_on[:money_amount]}$#{delimited_difference_on[:money_amount]})
#{duck_icon_char_code} $#{delimited_stat_from_website[:backers_number]} (#{sign_char_of_difference_on[:backers_number]}#{delimited_difference_on[:backers_number]})
#{hashtag} #{shenmue3_url}"
tweet_scnd_text_size = tweet_scnd_text.size
tweet_text, img_file =
if tweet_prim_text_size > 140
if tweet_scnd_text_size > 117 then ["#{hashtag} #{shenmue3_url}", img_file]
elsif tweet_scnd_text_size > 93 then [tweet_scnd_text, nil]
else
[tweet_scnd_text, img_file]
end
elsif tweet_prim_text_size > 116 then [tweet_prim_text, nil]
else
[tweet_prim_text, img_file]
end
# max text size:
# 140 : (w/o an img &/or a url)
# 117 : w/ a url (& w/o an img)
# 93 : w/ a url & an img
# 116 : w/ an img (& w/o a url)
case img_file
when nil
client.update(tweet_text)
else
client.update_with_media(tweet_text, img_file)
end