-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.rb
401 lines (302 loc) · 10.2 KB
/
script.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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
#!/usr/bin/env ruby
require 'koala'
require 'active_support'
require 'active_support/core_ext'
require 'json'
require 'uri'
require 'fastimage'
Dir.glob("lib/*.rb").each{|f| require_relative(f)}
$icons={:photo=>'assets/noun_778937_cc.svg',:video=>'assets/noun_1018049_cc.svg',:link=>'assets/noun_1038899_cc.svg',:status=>'assets/noun_13795_cc.svg'}
def initializeFBApi
@oauth = Koala::Facebook::OAuth.new
token = @oauth.get_app_access_token
@graph = Koala::Facebook::API.new(token)
end
def test
initializeFBApi
get_data(ARTISTS,false)
read_updates_json
end
def test_msg
initializeFBApi
p @graph
responses = @graph.get_connections("me", "feed")
p responses.inspect
end
def update_html
initializeFBApi
get_data(ARTISTS,true)
save_last
all_data = render_page
end
def load_last
last = {}
data = JSON.parse(File.readlines("json/last.json")[0])
data.each{|e| last[e.keys.first]=e.values.first}
last
end
def save_last
results = []
ARTISTS.each do |artist|
data = JSON.parse(File.readlines("json/#{artist['file']}")[0])
results << {artist['name']=>data.first['id']}
end
save_file_as(results.to_json, 'json/last.json')
end
def how_many_new_stories?(newdata,name)
last_data = load_last
last_id_for_artist = last_data[name]
ids = newdata.collect{|d|d['id']}
index = ids.index(last_id_for_artist)
index
end
def get_data(object,saving)
updates = []
case object
when ARTISTS
object.each do |artist|
p "Updating page - #{artist['name']}"
responses = @graph.get_connection("#{artist['page_id']}", 'feed', {
limit: '50',
fields: ['message', 'actions', 'child_attachments', 'id', 'from', 'type', 'picture', 'link', 'created_time', 'updated_time']
})
shares = responses # .reject{|m| m['picture'].nil? && m['link'].nil? }
newstories = how_many_new_stories?(shares,artist['name'])
updates << {artist['name'] => newstories}
save_file_as(shares.to_json, "json/#{artist['file']}") if saving
end
save_file_as(updates.to_json, "json/updates.json") if saving
when FRIENDS
responses = []
users = FRIENDS['people'].collect{|o|o['page_id']}.map(&:to_i)
p users
@graph.batch do |batch_api|
users.each do |user|
p batch_api.get_connections(user,'permissions')
responses << batch_api.get_connections(user, 'posts', {
limit: '20',
fields: ['message', 'actions', 'child_attachments', 'id', 'from', 'type', 'picture', 'link', 'created_time', 'updated_time']
})
end
end
shares = responses.flatten.sort{|x,y| Time.parse(y['updated_time']) <=> Time.parse(x['updated_time'])}
save_file_as(shares.to_json, "json/#{FRIENDS['file']}") if saving
end
end
def resolve_image(item)
pic = item["picture"]
if pic.nil?
pic = "assets/FFFFFF-0.8.png"
elsif URI.unescape(pic).include?("ytimg") || URI.unescape(pic).include?("youtube")
pic = URI.unescape(pic)
starting = pic[(pic.index("url=")+4)...-1]
pic = (starting.include?("?") ? starting[0...starting.index("?")] : starting[0...starting.index("&")])
elsif URI.unescape(pic).include?("http://www")
pic = URI.unescape(pic)
starting = pic[pic.index("http://www")...-1]
if starting.include?("jpg")
pic = starting[0...(starting.index("jpg")+3)]
elsif starting.include?("png")
pic = starting[0...(starting.index("png")+3)]
end
end
pic
end
def read_updates_json
updates = JSON.parse(File.readlines("json/updates.json")[0])
p updates
end
def render_page
@opening = %Q(
<!DOCTYPE html>
<html>
<head>
<link rel="shortcut icon" href="favicon.ico" type="image/x-icon">
<title>Foosbake</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="assets/w3.css">
<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Montserrat">
<link rel="stylesheet" href="assets/font-awesome-4.7.0/css/font-awesome.min.css">
</head>
<script>
function isScrolledIntoView(el) {
var elemTop = el.getBoundingClientRect().top;
var elemBottom = el.getBoundingClientRect().bottom;
var isVisible = (elemTop >= 0) && (elemBottom <= window.innerHeight);
return isVisible;
}
</script>
<style>
body,h1 {font-family: "Montserrat", sans-serif}
img {margin-bottom: -7px}
.w3-col,.w3-half,.w3-third,.w3-twothird,.w3-threequarter,.w3-quarter{float:left;width:100%}
.w3-row-padding img {margin-bottom: 12px}
.w3-container:after,.w3-container:before,.w3-panel:after,.w3-panel:before,.w3-row:after,.w3-row:before,.w3-row-padding:after,.w3-row-padding:before,
.w3-row-padding,.w3-row-padding>.w3-half,.w3-row-padding>.w3-third,.w3-row-padding>.w3-twothird,.w3-row-padding>.w3-threequarter,.w3-row-padding>.w3-quarter,.w3-row-padding>.w3-col{padding:0 8px}
@media (min-width:601px){.w3-col.m1{width:8.33333%}.w3-col.m2{width:16.66666%}.w3-col.m3,.w3-quarter{width:24.99999%}.w3-col.m4,.w3-third{width:33.33333%}
.w3-content{max-width:980px;margin:auto}.w3-rest{overflow:hidden}
.w3-content{
display: flex;
flex-wrap: nowrap;
overflow-x: auto;
max-width:10000px;
-webkit-overflow-scrolling: touch;
-ms-overflow-style: -ms-autohiding-scrollbar;
}
.w3-third{
flex: 0 0 auto;
margin-top: 5vh;
}
.navbar{font-size: 22px;
padding: 5px 10px;
width: 100vw;
position:fixed;
}
.navitem{
width:85%;
text-align: center;
font-size: 1.5rem;
border-bottom: 3px solid rgba(0,0,0,0.1);
font-size: 1.5rem;
margin-left: 5%;
padding-bottom: 5px;
}
.scrolltext {
float:right;
margin-right: 5px;
font-size: 12px;
}
.np-icon {margin-right:5px;min-width:20px;}
.mybutton {
color: black;
display: inline-block; /* Inline elements with width and height. TL;DR they make the icon buttons stack from left-to-right instead of top-to-bottom */
position: relative; /* All 'absolute'ly positioned elements are relative to this one */
padding: 2px 5px; /* Add some padding so it looks nice */
}
.button__badge {
background-color: #fa3e3e;
border-radius: 2px;
color: white;
padding: 1px 3px;
font-size: 10px;
position: absolute; /* Position the badge within the relatively positioned button */
top: 0;
right: 0;
}
.new_stories {margin-right:20px;min-width:50px;font-size:24px;background-color:red;color:white;border-radius:25px;}
.story{
overflow-x:hidden;
border-bottom: 2px dotted gray;
padding:0 5px 0 5px;
margin:0 1em 0 1em;
}
</style>
<body>
<div class="navbar"><span class='scrolltext'>Scroll right -></span></div>
<div class="w3-content">
<div class='w3-third' style='overflow: auto; max-height: 100vh;'>
)
@closing = %Q(
</div>
</div>
</body>
</html>
)
ARTISTS.each do |artist|
new_story_count_artist = read_updates_json.select{|o|o.keys.first==artist['name']}.first
new_story_count = new_story_count_artist.values.first
notification_span = (new_story_count.to_i==0 ? \
"" : \
"<span class='button__badge'>#{new_story_count}</span>")
@opening += "<div class='navitem' >
#{artist['name']}
<div class='mybutton'>
<i class='fa fa-comments'></i>
#{notification_span}
</div>
</div>"
data = File.readlines("json/#{artist['file']}")
size = JSON.parse(data[0]).length
puts "#{artist['name']}: #{size} items"
puts ("*" * size)
data.each do |d|
obj = JSON.parse(d)
obj.each_with_index do |item,index|
print("*")
extrastyle = (item==obj.first ? 'margin-top:2em;' : '')
date = Date.parse(item['updated_time']).strftime("%A, %d %b %Y")
from = "#{item['from']['name']}"
header = item['type'] #only 'event' does not have icon associated
icon = $icons[header.to_sym]
if header=='status'
# to do - extract image from here
# p item
end
message = item['message']
pic = resolve_image(item)
if pic.include?('default')
picwidth = 1280
else
dimensions = FastImage.size(pic)
if dimensions.nil?
picwidth = 0
else
picwidth = dimensions[0]
end
end
picstyle = (picwidth < 300 ? "auto" : "90%")
photo = "<img src='#{pic}' style='width:#{picstyle};text-align:center;' />"
@opening += %Q(
<div class='story' style='#{extrastyle}'>
<a href='#{item["link"]}' style='font-decoration:none;'>
<h5><span>#{from}</span></h5>
<h6 style='width:100%;'><span>#{date}</span><span class='np-icon' style='float:right;'><img src='#{icon}' alt='#{header} icon' title='#{header}'/></span></h6>
#{photo}
</a>
<p style='font-size:0.9em;overflow:hidden;'>#{message}</p>
</div>
)
end
end
unless artist==ARTISTS.last
@opening +=%Q(</div><div class='w3-third' style='overflow: auto; max-height: 100vh;'>)
end
print "\n"
end
all_data = @opening + @closing
save_file_as(all_data, 'index.html')
end
def save_file_as(d,target)
File.open(target, "w") {|file|file.puts(d)}
end
# ----------- unused functions -----------
def send_notifications_if_new(shares)
shares.each do |link|
if less_than_x_hours_old?(24,link)
p ".....";link.each_pair{|k,v| p "*** #{k}: #{v}"};p "....."
apple_notification(link)
end
end
end
def apple_notification(link)
# `osascript -e 'display notification "#{usage}" with title "Prince Rama Varma"'`
# `osascript -e 'tell application (path to frontmost application as text) to display dialog "#{usage}"'`
url = link['link']
# yes = (link['type']=='photo' ? "Photo" : "Video")
`terminal-notifier -message "#{link['message']}" -title "Prince Rama Varma" -open #{url}`
end
def less_than_x_hours_old?(x,link)
result = false
linktime = Time.parse(link['updated_time'])
if linktime > x.hours.ago
# puts "It is less than #{x} hours after #{linktime}"
result = true
else
# puts "It is not less than #{x} hours after #{linktime}"
end
result
end
def get_detailed_info(link)
@graph.get_object(link[:id])
end