ayadn 0.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/lib/ayadn/main.rb ADDED
@@ -0,0 +1,536 @@
1
+ #!/usr/bin/env ruby
2
+ require 'pstore'
3
+
4
+ # encoding: utf-8
5
+ class AyaDN
6
+ def initialize(token)
7
+ $PROGRAM_NAME = "AyaDN"
8
+ @token = token
9
+ @api = AyaDN::API.new(@token)
10
+ @view = AyaDN::View
11
+ @last_page_id_path = $tools.ayadn_configuration[:last_page_id_path]
12
+ @progress_indicator = $tools.ayadn_configuration[:progress_indicator]
13
+ end
14
+ def stream
15
+ $files.makedir(@last_page_id_path)
16
+ puts @view.new(@hash).showStream
17
+ end
18
+ def completeStream
19
+ $files.makedir(@last_page_id_path)
20
+ stream, pagination_array = @view.new(@hash).showCompleteStream
21
+ last_page_id = pagination_array.last
22
+ puts "\n" if last_page_id != nil
23
+ return stream, last_page_id
24
+ end
25
+ def displayStream(stream)
26
+ !stream.empty? ? (puts stream) : (puts $status.noNewPosts)
27
+ end
28
+ def displayScrollStream(stream)
29
+ !stream.empty? ? (puts stream) : (print "\r")
30
+ end
31
+ def ayadnScroll(value, target)
32
+ @progress_indicator = true
33
+ value = "unified" if value == nil
34
+ if target == nil
35
+ fileURL = @last_page_id_path + "/last_page_id-#{value}"
36
+ else
37
+ fileURL = @last_page_id_path + "/last_page_id-#{value}-#{target}"
38
+ end
39
+ loop do
40
+ begin
41
+ print "\r \r"
42
+ #puts "\n"
43
+ last_page_id = $files.get_last_page_id(fileURL)
44
+ case value
45
+ when "unified"
46
+ @hash = @api.getUnified(last_page_id)
47
+ when "global"
48
+ @hash = @api.getGlobal(last_page_id)
49
+ when "checkins", "photos", "conversations", "trending"
50
+ @hash = @api.getExplore(value, last_page_id)
51
+ when "mentions"
52
+ @hash = @api.getUserMentions(target, last_page_id)
53
+ when "posts"
54
+ @hash = @api.getUserPosts(target, last_page_id)
55
+ end
56
+ stream, last_page_id = completeStream
57
+ displayScrollStream(stream)
58
+ @progress_indicator = false
59
+ if last_page_id != nil
60
+ $files.write_last_page_id(fileURL, last_page_id)
61
+ print "\r "
62
+ puts "\n"
63
+ $tools.countdown($tools.config['timeline']['countdown_1'])
64
+ else
65
+ print "\rNo new posts ".red
66
+ sleep 2
67
+ $tools.countdown($tools.config['timeline']['countdown_2'])
68
+ end
69
+ rescue Exception
70
+ abort($status.stopped)
71
+ end
72
+ end
73
+ end
74
+ def ayadnInteractions
75
+ puts $status.getInteractions
76
+ puts @view.new(@api.getInteractions).showInteractions + "\n\n"
77
+ end
78
+ def check_count(count, target)
79
+ @counted = false
80
+ if count != nil && count.is_integer?
81
+ case target
82
+ when "conversations", "photos", "trending"
83
+ $tools.config['counts']['explore'] = count
84
+ else
85
+ $tools.config['counts'][target] = count
86
+ end
87
+ @counted = true
88
+ end
89
+ end
90
+ def ayadnGlobal(count=nil)
91
+ puts $status.getGlobal
92
+ fileURL = @last_page_id_path + "/last_page_id-global"
93
+ check_count(count, "global")
94
+ @hash = @api.getGlobal($files.get_last_page_id(fileURL))
95
+ stream, last_page_id = completeStream
96
+ if @counted == false
97
+ $files.write_last_page_id(fileURL, last_page_id) unless last_page_id == nil
98
+ end
99
+ displayStream(stream)
100
+ puts "\n"
101
+ end
102
+ def ayadnUnified(count=nil)
103
+ fileURL = @last_page_id_path + "/last_page_id-unified"
104
+ puts $status.getUnified
105
+ check_count(count, "unified")
106
+ @hash = @api.getUnified($files.get_last_page_id(fileURL))
107
+ stream, last_page_id = completeStream
108
+ if @counted == false
109
+ $files.write_last_page_id(fileURL, last_page_id) unless last_page_id == nil
110
+ end
111
+ displayStream(stream)
112
+ puts "\n"
113
+ end
114
+ def ayadnHashtags(tag)
115
+ puts $status.getHashtags(tag)
116
+ @hash = @api.getHashtags(tag)
117
+ displayStream(completeStream[0])
118
+ puts "\n"
119
+ end
120
+ def ayadnExplore(explore, count=nil)
121
+ fileURL = @last_page_id_path + "/last_page_id-#{explore}"
122
+ puts $status.getExplore(explore)
123
+ check_count(count, explore)
124
+ @hash = @api.getExplore(explore, $files.get_last_page_id(fileURL))
125
+ stream, last_page_id = completeStream
126
+ if @counted == false
127
+ $files.write_last_page_id(fileURL, last_page_id) unless last_page_id == nil
128
+ end
129
+ displayStream(stream)
130
+ puts "\n"
131
+ end
132
+ def ayadnUserMentions(name, count=nil)
133
+ fileURL = @last_page_id_path + "/last_page_id-mentions-#{name}"
134
+ puts $status.mentionsUser(name)
135
+ check_count(count, "mentions")
136
+ @hash = @api.getUserMentions(name, $files.get_last_page_id(fileURL))
137
+ stream, last_page_id = completeStream
138
+ if @counted == false
139
+ $files.write_last_page_id(fileURL, last_page_id) unless last_page_id == nil
140
+ end
141
+ displayStream(stream)
142
+ puts "\n"
143
+ end
144
+ def ayadnUserPosts(name, count=nil)
145
+ fileURL = @last_page_id_path + "/last_page_id-posts-#{name}"
146
+ puts $status.postsUser(name)
147
+ check_count(count, "posts")
148
+ @hash = @api.getUserPosts(name, $files.get_last_page_id(fileURL))
149
+ stream, last_page_id = completeStream
150
+ if @counted == false
151
+ $files.write_last_page_id(fileURL, last_page_id) unless last_page_id == nil
152
+ end
153
+ displayStream(stream)
154
+ end
155
+ def ayadnUserInfos(name)
156
+ puts $status.infosUser(name)
157
+ puts @view.new(@api.getUserInfos(name)).showUsersInfos(name)
158
+ end
159
+ def get_loaded_channels
160
+ loaded_channels = $files.load_channels
161
+ channels_with_messages = $files.load_channels_with_messages
162
+ if loaded_channels != nil
163
+ puts "Backed-up list of your active channels:\n".green
164
+ loaded_channels.each do |k,v|
165
+ puts "Channel: ".cyan + k.brown
166
+ puts "Title: ".cyan + v.magenta
167
+ if channels_with_messages != nil && channels_with_messages[k] != nil
168
+ puts "Last message by @#{channels_with_messages[k]['username']} (#{channels_with_messages[k]['message_date']}): \n".cyan + channels_with_messages[k]['text']
169
+ end
170
+ puts "\n"
171
+ end
172
+ puts "Do you want to refresh the list? (y/N)".green
173
+ abort("\nCanceled.\n\n".red) unless STDIN.getch == ("y" || "Y")
174
+ puts "\n"
175
+ end
176
+ end
177
+ def ayadn_get_channels
178
+ @hash = @api.get_pm_channels
179
+ the_channels, channels_list = @view.new(@hash).show_pm_channels
180
+ puts the_channels
181
+ @hash = @api.get_channels
182
+ the_channels, channels_list = @view.new(@hash).show_channels
183
+ puts the_channels
184
+ end
185
+ def ayadnGetMessages(target, action = nil)
186
+ $files.makedir($tools.ayadn_configuration[:messages_path])
187
+ @progress_indicator = false
188
+ if target != nil
189
+ if !target.is_integer?
190
+ target = $files.load_channel_id(target)
191
+ end
192
+ fileURL = @last_page_id_path + "/last_page_id-channels-#{target}"
193
+ last_page_id = $files.get_last_page_id(fileURL) unless action == "all"
194
+ messages_string, last_page_id = @view.new(@api.getMessages(target, last_page_id)).showMessagesFromChannel
195
+ $files.write_last_page_id(fileURL, last_page_id) unless last_page_id == nil
196
+ displayStream(messages_string)
197
+ puts $status.noNewPosts if messages_string == "\n"
198
+ else
199
+ puts $status.errorSyntax
200
+ end
201
+ end
202
+
203
+ def ayadnDeletePost(postID)
204
+ puts $status.deletePost(postID)
205
+ if @api.goDelete(postID) == nil
206
+ abort($status.errorAlreadyDeleted)
207
+ else
208
+ $tools.checkHTTPResp(@api.http_delete)
209
+ puts $status.postDeleted
210
+ exit
211
+ end
212
+ end
213
+ def ayadn_delete_message(channel_id, message_id)
214
+ puts "\nDeleting message #{message_id} in channel #{channel_id}".green
215
+ @url = @api.unique_message(channel_id, message_id)
216
+ $tools.checkHTTPResp(@api.http_delete)
217
+ puts "\nDone!\n".green
218
+ end
219
+
220
+ def ayadnWhoReposted(postID)
221
+ puts $status.whoReposted(postID)
222
+ @hash = @api.getWhoReposted(postID)
223
+ abort($status.errorNobodyReposted) if @hash['data'].empty?
224
+ puts @view.new(@hash).showUsersList
225
+ end
226
+ def ayadnWhoStarred(postID)
227
+ puts $status.whoStarred(postID)
228
+ @hash = @api.getWhoStarred(postID)
229
+ abort($status.errorNobodyStarred) if @hash['data'].empty?
230
+ puts @view.new(@hash).showUsersList
231
+ end
232
+ def ayadnStarredPosts(name, count=nil)
233
+ puts $status.starsUser(name)
234
+ check_count(count, "starred")
235
+ @hash = @api.getStarredPosts(name)
236
+ displayStream(completeStream[0])
237
+ end
238
+ def ayadnConversation(postID)
239
+ puts $status.getPostReplies(postID)
240
+ @hash = @api.getPostReplies(postID)
241
+ displayStream(completeStream[0])
242
+ end
243
+ def ayadnPostInfos(postID)
244
+ puts $status.infosPost(postID)
245
+ puts @view.new(@api.getPostInfos(postID)).showPostInfos(postID, false)
246
+ end
247
+ def ayadnLoadPost(postID)
248
+ puts $status.infosPost(postID)
249
+ puts @view.new(nil).buildPostInfo(load_post(postID), true)
250
+ end
251
+ def load_post(post_id)
252
+ fileContent = {}
253
+ File.open("#{$tools.ayadn_configuration[:posts_path]}/#{post_id}.post", "r") do |f|
254
+ fileContent = f.gets
255
+ end
256
+ eval(fileContent)
257
+ end
258
+ def ayadnSavePost(postID)
259
+ @progress_indicator = false
260
+ name = postID.to_s
261
+ posts_path = $tools.ayadn_configuration[:posts_path]
262
+ $files.makedir(posts_path)
263
+ file = "/#{name}.post"
264
+ fileURL = posts_path + file
265
+ abort("\nYou already saved this post.\n\n".red) if File.exists?(fileURL)
266
+ puts "\nLoading post from App.net...".green + name.brown
267
+ puts $status.savingFile(name, posts_path, file)
268
+ f = File.new(fileURL, "w")
269
+ resp = @api.getSinglePost(postID)
270
+ f.puts(resp['data'])
271
+ f.close
272
+ puts "\nSuccessfully saved the post.\n\n".green
273
+ exit
274
+ end
275
+
276
+ def ayadnSearch(value)
277
+ @hash = @api.getSearch(value)
278
+ displayStream(completeStream[0])
279
+ end
280
+
281
+ def ayadnFollowing(action, name)
282
+ @progress_indicator = false
283
+ following = @api.getUserFollowInfo(name)
284
+ if action == "follow"
285
+ if following[:you_follow]
286
+ abort("You're already following this user.\n\n".red)
287
+ else
288
+ @api.followUser(name)
289
+ puts "\nYou just followed user ".green + "#{name}".brown + "\n\n"
290
+ end
291
+ elsif action == "unfollow"
292
+ if following[:you_follow]
293
+ @api.unfollowUser(name)
294
+ puts "\nYou just unfollowed user ".green + "#{name}".brown + "\n\n"
295
+ else
296
+ abort("You're already not following this user.\n\n".red)
297
+ end
298
+ else
299
+ abort($status.errorSyntax)
300
+ end
301
+ end
302
+
303
+ def ayadnMuting(action, name)
304
+ @progress_indicator = false
305
+ you_muted = @api.getUserMuteInfo(name)
306
+ if action == "mute"
307
+ if you_muted
308
+ abort("You've already muted this user.\n\n".red)
309
+ else
310
+ @api.muteUser(name)
311
+ puts "\nYou just muted user ".green + "#{name}".brown + "\n\n"
312
+ end
313
+ elsif action == "unmute"
314
+ if you_muted
315
+ @api.unmuteUser(name)
316
+ puts "\nYou just unmuted user ".green + "#{name}".brown + "\n\n"
317
+ else
318
+ abort("This user is not muted.\n\n".red)
319
+ end
320
+ else
321
+ abort($status.errorSyntax)
322
+ end
323
+ end
324
+
325
+ def ayadnBlocking(action, name)
326
+ @progress_indicator = false
327
+ you_blocked = @api.getUserBlockInfo(name)
328
+ if action == "block"
329
+ if you_blocked
330
+ abort("\nYou've already blocked this user.\n\n".red)
331
+ else
332
+ puts "\nAre you sure you want to block ".red + "#{name} ".brown + "?\n\nIt will mute him/her, then both of you will automatically unfollow each other (if applicable).".red + "\n\n(y/N)?\n\n".brown
333
+ case STDIN.getch
334
+ when "y", "Y"
335
+ @api.blockUser(name)
336
+ puts "debug"
337
+ puts "\nYou just blocked user ".green + "#{name}".brown + "\n\n"
338
+ exit
339
+ end
340
+ puts $status.canceled
341
+ end
342
+ elsif action == "unblock"
343
+ if you_blocked
344
+ @api.unblockUser(name)
345
+ puts "\nYou just unblocked user ".green + "#{name}".brown + "\n\n"
346
+ else
347
+ abort("\nThis user is not blocked.\n\n".red)
348
+ end
349
+ else
350
+ abort($status.errorSyntax)
351
+ end
352
+ end
353
+
354
+ def ayadnStarringPost(action, postID)
355
+ @progress_indicator = false
356
+ @hash = @api.getSinglePost(postID)
357
+ post_data = @hash['data']
358
+ you_starred = post_data['you_starred']
359
+ is_repost = post_data['repost_of']
360
+ if is_repost != nil
361
+ puts $status.errorIsRepost(postID)
362
+ puts "Redirecting to the original post.\n".cyan
363
+ postID = is_repost['id']
364
+ you_starred = is_repost['you_starred']
365
+ end
366
+ if action == "star"
367
+ if you_starred == false
368
+ puts "\nStarring post ".green + "#{postID}\n".brown
369
+ @api.starPost(postID)
370
+ puts "\nSuccessfully starred the post.\n\n".green
371
+ else
372
+ abort("Canceled: the post is already starred.\n\n".red)
373
+ end
374
+ elsif action == "unstar"
375
+ if you_starred == false
376
+ abort("Canceled: the post wasn't already starred.\n\n".red)
377
+ else
378
+ puts "\nUnstarring post ".green + "#{postID}\n".brown
379
+ @api.unstarPost(postID)
380
+ puts "\nSuccessfully unstarred the post.\n\n".green
381
+ end
382
+ else
383
+ abort("\nsyntax error\n".red)
384
+ end
385
+ end
386
+ def ayadnReposting(action, postID)
387
+ @progress_indicator = false
388
+ @hash = @api.getSinglePost(postID)
389
+ post_data = @hash['data']
390
+ is_repost = post_data['repost_of']
391
+ you_reposted = post_data['you_reposted']
392
+ if is_repost != nil && you_reposted == false
393
+ puts $status.errorIsRepost(postID)
394
+ puts "Redirecting to the original post.\n".cyan
395
+ postID = is_repost['id']
396
+ you_reposted = is_repost['you_reposted']
397
+ end
398
+ if action == "repost"
399
+ if you_reposted
400
+ abort("Canceled: you already reposted this post.\n\n".red)
401
+ else
402
+ puts "\nReposting post ".green + "#{postID}\n".brown
403
+ @api.repostPost(postID)
404
+ puts "\nSuccessfully reposted the post.\n\n".green
405
+ end
406
+ elsif action == "unrepost"
407
+ if you_reposted
408
+ puts "\nUnreposting post ".green + "#{postID}\n".brown
409
+ @api.unrepostPost(postID)
410
+ puts "\nSuccessfully unreposted the post.\n\n".green
411
+ else
412
+ abort("Canceled: this post wasn't reposted by you.\n\n".red)
413
+ end
414
+ else
415
+ abort($status.errorSyntax)
416
+ end
417
+ end
418
+ def ayadnReset(content, option)
419
+ $files.reset_pagination(content, option)
420
+ end
421
+ def ayadn_list_aliases
422
+ db = PStore.new($tools.ayadn_configuration[:db_path] + "/channels_alias.db")
423
+ db.transaction do
424
+ db.roots.each do |root|
425
+ puts "#{db[root]} ".brown + "=> " + "#{root} ".green
426
+ end
427
+ end
428
+ puts "\n"
429
+ end
430
+ def ayadn_alias_channel(channel_id, channel_alias)
431
+ puts "\nAdding new alias: ".cyan + "#{channel_id} ".brown + "=> " + "#{channel_alias}\n".green
432
+ if channel_id.is_integer? && channel_alias != nil
433
+ $files.save_channel_alias(channel_id, channel_alias)
434
+ else
435
+ puts $status.errorSyntax
436
+ exit
437
+ end
438
+ puts "List of saved aliases: \n".cyan
439
+ puts ayadn_list_aliases
440
+ puts "Done!\n\n".green
441
+ end
442
+ def ayadn_nowplaying
443
+ case $tools.ayadn_configuration[:platform]
444
+ when $tools.winplatforms
445
+ puts "\nThis feature only works with Mac OS X and iTunes. Sorry.\n\n".red
446
+ exit
447
+ end
448
+ begin
449
+ track = `osascript -e 'tell application "iTunes"' -e 'set trackName to name of current track' -e 'return trackName' -e 'end tell'`
450
+ artist = `osascript -e 'tell application "iTunes"' -e 'set trackArtist to artist of current track' -e 'return trackArtist' -e 'end tell'`
451
+ rescue => e
452
+ puts "\n\nError: #{e}\n\n".red
453
+ exit
454
+ end
455
+ track.chomp!
456
+ artist.chomp!
457
+ if track.length == 0 || artist.length == 0
458
+ abort("\nCanceled: couldn't get enough information (empty field).\n\n".red)
459
+ end
460
+ text_to_post = "#nowplaying '#{track}' by #{artist}"
461
+ puts "\nAyaDN will post this to your timeline:\n\n".cyan
462
+ puts text_to_post + "\n\n"
463
+ puts "Do you confirm? (y/N) ".brown
464
+ abort("\nCanceled.\n\n".red) unless STDIN.getch == ("y" || "Y")
465
+ puts "\n"
466
+ ayadnSendPost(text_to_post, nil)
467
+ end
468
+ def ayadn_does(params)
469
+ target_name, source_name = params[3].dup, params[1].dup
470
+ case params[2]
471
+ when "follow", "follows", "following", "followed"
472
+ fw = []
473
+ target_name[0,0] = '@' if target_name[0] != "@"
474
+ source_name[0,0] = '@' if source_name[0] != "@"
475
+ real_target_name = target_name.dup
476
+ real_target_name[0] = ''
477
+ source_list = @api.getFollowings(source_name, nil)
478
+ source_list['data'].each {|user| fw << user['username']}
479
+ min_id, more = source_list['meta']['min_id'], source_list['meta']['more']
480
+ if more
481
+ loop do
482
+ source_list = @api.getFollowings(source_name, min_id)
483
+ break if source_list['meta']['more'] == false
484
+ source_list['data'].each {|user| fw << user['username']}
485
+ min_id = source_list['meta']['min_id']
486
+ end
487
+ end
488
+ @f = false
489
+ fw.each do |name|
490
+ if name == real_target_name
491
+ @f = true
492
+ break
493
+ end
494
+ end
495
+ if @f
496
+ puts "\nYes, " + "#{source_name} ".green + "follows " + "#{target_name}\n\n".green
497
+ else
498
+ puts "\nNo, " + "#{source_name} ".magenta + "doesn't follow " + "#{target_name}\n\n".magenta
499
+ end
500
+ else
501
+ puts $status.errorSyntax
502
+ end
503
+ end
504
+ def ayadn_show_options
505
+ puts "\nCurrent options in ".cyan + "config.yml\n".magenta
506
+ $tools.config.each do |k,v|
507
+ puts "#{k.capitalize}:".cyan
508
+ v.each do |x,y|
509
+ puts "\t#{x.green} => #{y.to_s.brown}"
510
+ end
511
+ end
512
+ puts "\n"
513
+ puts "AyaDN local data: \n".magenta
514
+ puts "Posts path:".cyan
515
+ puts "\t" + $tools.ayadn_configuration[:posts_path].brown + "/".brown
516
+ puts "Messages path:".cyan
517
+ puts "\t" + $tools.ayadn_configuration[:messages_path].brown + "/".brown
518
+ puts "Lists path:".cyan
519
+ puts "\t" + $tools.ayadn_configuration[:lists_path].brown + "/".brown
520
+ puts "Files path:".cyan
521
+ puts "\t" + $tools.ayadn_configuration[:files_path].brown + "/".brown
522
+ puts "Database path:".cyan
523
+ puts "\t" + $tools.ayadn_configuration[:db_path].brown + "/".brown
524
+ puts "\n"
525
+ puts "AyaDN system configuration: \n".magenta
526
+ puts "Authorization token path:".cyan
527
+ puts "\t" + $tools.ayadn_configuration[:authorization_path].brown + "/".brown
528
+ puts "API configuration path:".cyan
529
+ puts "\t" + $tools.ayadn_configuration[:api_config_path].brown + "/".brown
530
+ puts "Pagination data path:".cyan
531
+ puts "\t" + $tools.ayadn_configuration[:last_page_id_path].brown + "/".brown
532
+ puts "Detected platform:".cyan
533
+ puts "\t" + $tools.ayadn_configuration[:platform].brown
534
+ puts "\n"
535
+ end
536
+ end
@@ -0,0 +1,37 @@
1
+ #!/usr/bin/env ruby
2
+ require 'base64'
3
+ require 'open-uri'
4
+
5
+ # encoding: utf-8
6
+ class AyaDN
7
+ def ayadnBookmark(*args)
8
+ post_id = args[0][1]
9
+ tags = args[0][2]
10
+ hash = @api.getSinglePost(post_id)
11
+ data = hash['data']
12
+ post_text = data['text']
13
+ user_name = data['user']['username']
14
+ link = data['entities']['links'][0]['url']
15
+ if $tools.config['pinboard']['username'] != nil
16
+ puts "\nSaving post ".green + post_id.brown + " to Pinboard...\n".green
17
+ $tools.saveToPinboard(post_id, $tools.config['pinboard']['username'], URI.unescape(Base64::decode64($tools.config['pinboard']['password'])), link, tags, post_text, user_name)
18
+ puts "Done!\n\n".green
19
+ else
20
+ puts "\nConfiguration does not include your Pinbard credentials.\n".red
21
+ begin
22
+ puts "Please enter your Pinboard username (CTRL+C to cancel): ".green
23
+ pin_username = STDIN.gets.chomp()
24
+ puts "\nPlease enter your Pinboard password (invisible, CTRL+C to cancel): ".green
25
+ pin_password = STDIN.noecho(&:gets).chomp()
26
+ rescue Exception
27
+ abort($status.stopped)
28
+ end
29
+ $tools.config['pinboard']['username'] = pin_username
30
+ $tools.config['pinboard']['password'] = URI.escape(Base64::encode64(pin_password))
31
+ $tools.saveConfig
32
+ puts "Saving post ".green + post_id.brown + " to Pinboard...\n".green
33
+ $tools.saveToPinboard(post_id, pin_username, pin_password, link, tags, post_text, user_name)
34
+ puts "Done!\n\n".green
35
+ end
36
+ end
37
+ end