Class: Ayadn::Workers

Inherits:
Object
  • Object
show all
Defined in:
lib/ayadn/workers.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeWorkers

Returns a new instance of Workers.



7
8
9
10
# File 'lib/ayadn/workers.rb', line 7

def initialize
  @thor = Thor::Shell::Color.new
  @status = Status.new
end

Instance Attribute Details

#thorObject (readonly)

Returns the value of attribute thor.



5
6
7
# File 'lib/ayadn/workers.rb', line 5

def thor
  @thor
end

Class Method Details

.epoch_to_date(epoch) ⇒ Object



619
620
621
# File 'lib/ayadn/workers.rb', line 619

def self.epoch_to_date(epoch)
  Time.at(epoch).to_time
end

Instance Method Details

#add_arobase(username) ⇒ Object



476
477
478
# File 'lib/ayadn/workers.rb', line 476

def add_arobase username
  add_arobase_if_missing(username)
end

#add_arobase_if_missing(username) ⇒ Object

expects an array of username(s), works on the first one and outputs a string



480
481
482
483
484
485
486
487
488
# File 'lib/ayadn/workers.rb', line 480

def add_arobase_if_missing(username) # expects an array of username(s), works on the first one and outputs a string
  unless username.first == "me"
    username = username.first.chars
    username.unshift("@") unless username.first == "@"
  else
    username = "me".chars
  end
  username.join
end

#add_arobases_to_usernames(args) ⇒ Object



499
500
501
502
503
504
505
506
507
508
509
# File 'lib/ayadn/workers.rb', line 499

def add_arobases_to_usernames args
  args.map do |username|
    if username == 'me'
      who_am_i
    else
      temp = username.chars
      temp.unshift("@") unless temp.first == "@"
      temp.join
    end
  end
end

#all_but_me(usernames) ⇒ Object



614
615
616
617
# File 'lib/ayadn/workers.rb', line 614

def all_but_me usernames
  arr = usernames.select {|user| user != 'me'}
  at(arr)
end

#at(usernames) ⇒ Object



511
512
513
514
515
516
517
518
519
520
521
# File 'lib/ayadn/workers.rb', line 511

def at usernames
  usernames.map do |user|
    if user == 'me'
      'me'
    elsif user[0] == '@'
      user
    else
      "@#{user}"
    end
  end
end

#build_aliases_list(list) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/ayadn/workers.rb', line 20

def build_aliases_list(list)
  table = init_table
  table.title = "List of your channel aliases".color(:cyan) + "".color(:white)
  table.style = table_borders()
  table.headings = [ 'Channel', 'Alias' ]
  list.each_with_index do |obj, index|
    table << [obj[0].to_s.color(:green), obj[1].color(:red)]
    if index + 1 != list.length && Settings.options[:timeline][:compact] == false
      table << :separator
    end
  end
  table
end

#build_blacklist_list(list) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/ayadn/workers.rb', line 34

def build_blacklist_list(list)
  table = init_table
  table.title = "Your blacklist".color(:cyan) + "".color(:white)
  table.style = table_borders()
  table.headings = [ 'Name', 'Type' ]
  list.sort!
  list.each_with_index do |obj, index|
    table << ["#{obj[1].capitalize}".to_s.force_encoding("UTF-8").color(:green), "#{obj[0]}".to_s.force_encoding("UTF-8").color(:red)]
    if index + 1 != list.length && Settings.options[:timeline][:compact] == false
      table << :separator
    end
  end
  table
end

#build_blocked_list(list, options = {}) ⇒ Object



103
104
105
106
107
108
# File 'lib/ayadn/workers.rb', line 103

def build_blocked_list(list, options = {})
  table = init_table
  table.title = "List of users you blocked".color(:cyan) + "".color(:white)
  table.style = table_borders()
  build_users_list(build_users_array(list), table, options)
end

#build_channels(data, options = {}) ⇒ Object



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
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
# File 'lib/ayadn/workers.rb', line 368

def build_channels(data, options = {})
  bucket = []
  data = [data] unless data.is_a?(Array)
  if options[:channels]
    @thor.say_status :downloading, "list of channels and their users credentials", :yellow
    @thor.say_status :info, "it could take a while if there are many results and users", :cyan
  else
    @thor.say_status :downloading, "the channels and their users attributes (owners, writers, editors and readers)", :yellow
    @thor.say_status :info, "users are recorded in a database for later filtering and analyzing", :cyan
    @thor.say_status :info, "it could take a while if there are many results", :cyan
  end
  chan = Struct.new(:id, :num_messages, :subscribers, :type, :owner, :annotations, :readers, :editors, :writers, :you_subscribed, :unread, :recent_message_id, :recent_message)
  no_user = {}
  @api = API.new
  data.each do |ch|
    unless ch['writers']['user_ids'].empty?
      @thor.say_status :parsing, "channel #{ch['id']}", :cyan
      usernames = []
      ch['writers']['user_ids'].each do |id|
        next if no_user[id]
        db = Databases.find_user_by_id(id)
        if db.nil?
          @thor.say_status :downloading, "user #{id}", :yellow
          resp = @api.get_user(id)

          if resp['meta']['code'] != 200
            @thor.say_status :error, "can't get user #{id}'s data, skipping", :red
            no_user[id] = true
            next
          end

          the_username = resp['data']['username']
          @thor.say_status :recording, "@#{the_username}", :yellow

          usernames << "@" + the_username
          Databases.add_to_users_db(id, the_username, resp['data']['name'])
        else
          the_username = "@#{db}"
          @thor.say_status :match, "#{the_username} is already in the database", :blue

          usernames << the_username
        end
      end
      usernames << Settings.config[:identity][:handle] unless usernames.length == 1 && usernames.first == Settings.config[:identity][:handle]
      writers = usernames.join(", ")
    else
      writers = Settings.config[:identity][:handle]
    end
    if ch['has_unread']
      unread = "This channel has unread message(s)"
    else
      unread = "No unread messages"
    end
    bucket << chan.new(ch['id'], ch['counts']['messages'], ch['counts']['subscribers'], ch['type'], ch['owner'], ch['annotations'], ch['readers'], ch['editors'], writers, ch['you_subscribed'], unread, ch['recent_message_id'], ch['recent_message'])
  end
  puts "\e[H\e[2J"
  bucket
end

#build_followers_list(list, target, options = {}) ⇒ Object



85
86
87
88
89
90
91
92
93
94
# File 'lib/ayadn/workers.rb', line 85

def build_followers_list(list, target, options = {})
  table = init_table
  table.title = if target == "me"
    "List of your followers".color(:cyan) + "".color(:white)
  else
    "List of users following ".color(:cyan) + "#{target}".color(:red) + "".color(:white)
  end
  table.style = table_borders()
  build_users_list(build_users_array(list), table, options)
end

#build_followings_list(list, target, options = {}) ⇒ Object

takes a hash of users with ayadn format



73
74
75
76
77
78
79
80
81
82
83
# File 'lib/ayadn/workers.rb', line 73

def build_followings_list(list, target, options = {}) #takes a hash of users with ayadn format
  table = init_table
  table.title = if target == "me"
    "List of users you're following".color(:cyan) + "".color(:white)
  else
    "List of users ".color(:cyan) + "#{target}".color(:red) + " is following ".color(:cyan) + "".color(:white)
  end
  table.style = table_borders()
  users_list = build_users_array(list)
  build_users_list(users_list, table, options)
end

#build_muted_list(list, options = {}) ⇒ Object



96
97
98
99
100
101
# File 'lib/ayadn/workers.rb', line 96

def build_muted_list(list, options = {})
  table = init_table
  table.title = "List of users you muted".color(:cyan) + "".color(:white)
  table.style = table_borders()
  build_users_list(build_users_array(list), table, options)
end

#build_posts(data, niceranks = {}) ⇒ Object

builds a hash of hashes, each hash is a normalized post with post id as a key



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
# File 'lib/ayadn/workers.rb', line 168

def build_posts(data, niceranks = {})
  # skip objects in blacklist unless force
  posts = {}
  data.each.with_index(1) do |post, index|
    unless Settings.global[:force] == true
      if Settings.options[:blacklist][:active] == true
        if Databases.is_in_blacklist?('client', post['source']['name'].downcase)
          Debug.skipped({source: post['source']['name']})
          next
        end
      end
    end
    unless Settings.global[:force] == true
      if Settings.options[:blacklist][:active] == true
        if Databases.is_in_blacklist?('user', post['user']['username'].downcase)
          Debug.skipped({user: post['user']['username']})
          next
        end
      end
    end
    hashtags = extract_hashtags(post)
    @skip = false
    unless Settings.global[:force] == true
      if Settings.options[:blacklist][:active] == true
        hashtags.each do |h|
          if Databases.is_in_blacklist?('hashtag', h.downcase)
            @skip = true
            Debug.skipped({hashtag: h})
            break
          end
        end
      end
    end
    next if @skip
    mentions= []
    post['entities']['mentions'].each { |m| mentions << m['name'] }
    unless Settings.global[:force] == true
      if Settings.options[:blacklist][:active] == true
        mentions.each do |m|
          if Databases.is_in_blacklist?('mention', m.downcase)
            @skip = true
            Debug.skipped({mention: m})
            break
          end
        end
      end
    end
    next if @skip
    unless Settings.global[:force] == true
      if Settings.options[:blacklist][:active] == true
        post['text'].split(" ").each do |word|
          target_word = word.gsub(/[~:-;,?!\'&`^=+<>*%()\/"“”’°£$€.…]/, "")
          if Databases.is_in_blacklist?('word', target_word.downcase)
            Debug.skipped({word: target_word})
            @skip = true
            break
          end
        end
      end
    end
    next if @skip
    
    # create custom objects from ADN response
    if niceranks[post['user']['id'].to_i]
      rank = niceranks[post['user']['id'].to_i][:rank]
      is_human = niceranks[post['user']['id'].to_i][:is_human]
      real_person = niceranks[post['user']['id'].to_i][:real_person]
    else
      rank = false
      is_human = 'unknown'
      real_person = 'unknown'
    end

    if post['user'].has_key?('name')
      name = post['user']['name'].to_s.force_encoding("UTF-8")
    else
      name = "(no name)"
    end

    source = post['source']['name'].to_s.force_encoding("UTF-8")

    values = {
      count: index,
      id: post['id'].to_i,
      name: name,
      thread_id: post['thread_id'],
      username: post['user']['username'],
      user_id: post['user']['id'].to_i,
      nicerank: rank,
      is_human: is_human,
      real_person: real_person,
      handle: "@#{post['user']['username']}",
      type: post['user']['type'],
      date: parsed_time(post['created_at']),
      date_short: parsed_time_short(post['created_at']),
      you_starred: post['you_starred'],
      source_name: source,
      source_link: post['source']['link'],
      canonical_url: post['canonical_url'],
      tags: hashtags,
      links: extract_links(post),
      mentions: mentions,
      directed_to: mentions.first || false
    }

    values[:checkins], values[:has_checkins] = extract_checkins(post)

    if post['repost_of']
      values[:is_repost] = true
      values[:repost_of] = post['repost_of']['id']
      values[:original_poster] = post['repost_of']['user']['username']
    else
      values[:is_repost] = false
      values[:repost_of] = nil
      values[:original_poster] = post['user']['username']
    end

    unless post['text'].nil?
      values[:raw_text] = post['text']
      values[:text] = colorize_text(post['text'], mentions, hashtags)
    else
      values[:raw_text] = ""
      values[:text] = "(no text)"
    end

    unless post['num_stars'].nil? || post['num_stars'] == 0
      values[:is_starred] = true
      values[:num_stars] = post['num_stars']
    else
      values[:is_starred] = false
      values[:num_stars] = 0
    end

    if post['num_replies']
      values[:num_replies] = post['num_replies']
    else
      values[:num_replies] = 0
    end

    if post['reply_to']
      values[:is_reply] = true
      values[:reply_to] = post['reply_to']
    else
      values[:is_reply] = false
      values[:reply_to] = nil
    end
    if post['num_reposts']
      values[:num_reposts] = post['num_reposts']
    else
      values[:num_reposts] = 0
    end

    posts[post['id'].to_i] = values

  end

  posts
end

#build_reposted_list(list, target) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
# File 'lib/ayadn/workers.rb', line 49

def build_reposted_list(list, target)
  table = init_table
  table.title = "List of users who reposted post ".color(:cyan) + "#{target}".color(:red) + "".color(:white)
  users_list = []
  list.each do |obj|
    obj['name'].nil? ? name = "" : name = obj['name'].to_s.force_encoding("UTF-8")
    users_list << {:username => obj['username'], :name => name, :you_follow => obj['you_follow'], :follows_you => obj['follows_you'], :id => obj['id'], :posts => obj['counts']['posts']}
  end
  table.style = table_borders()
  return users_list, table
end

#build_starred_list(list, target) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
# File 'lib/ayadn/workers.rb', line 61

def build_starred_list(list, target)
  table = init_table
  table.title = "List of users who starred post ".color(:cyan) + "#{target}".color(:red) + "".color(:white)
  users_list = []
  list.each do |obj|
    obj['name'].nil? ? name = "" : name = obj['name'].to_s.force_encoding("UTF-8")
    users_list << {:username => obj['username'], :name => name, :you_follow => obj['you_follow'], :follows_you => obj['follows_you'], :id => obj['id'], :posts => obj['counts']['posts']}
  end
  table.style = table_borders()
  return users_list, table
end

#build_users_list(list, table, options = {}) ⇒ Object



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
# File 'lib/ayadn/workers.rb', line 110

def build_users_list(list, table, options = {})
  users = at(list.map {|obj| obj[:username]})
  ids = list.map {|obj| obj[:id].to_i}
  ranks = NiceRank.new.from_ids(ids)
  indexed_ranks = {}
  ranks.each do |r|
    if r.empty?
      indexed_ranks = false
      break
    else
      indexed_ranks[r['user_id']] = r
    end
  end
  table << ['USERNAME'.color(:red), 'NAME'.color(:red), 'POSTS'.color(:red), 'POSTS/DAY'.color(:red)]
  table << :separator
  arr = []
  if options[:username]
    list.sort_by! { |obj| obj[:username] }
  elsif options[:name]
    list.sort_by! { |obj| obj[:name].downcase }
  elsif options[:posts]
    list.sort_by! { |obj| [obj[:posts], obj[:username]] }
  end
  list.each do |obj|
    obj[:name] = "" if obj[:name].nil?
    unless indexed_ranks == false
      details = indexed_ranks[obj[:id].to_i]
      if details['user']['posts_day'] == -1
        posts_day = 'ignored'
      else
        posts_day = details['user']['posts_day'].round(2).to_s
      end
    else
      posts_day = 'unknown'
    end
    obj[:username].length > 23 ? username = "#{obj[:username][0..20]}..." : username = obj[:username]
    obj[:name].length > 23 ? name = "#{obj[:name][0..20]}..." : name = obj[:name]
    arr << [ "@#{username} ".color(Settings.options[:colors][:username]), "#{name.to_s.force_encoding('UTF-8')}", obj[:posts], posts_day ]
  end
  if options[:posts_day]
    arr.sort_by! { |obj| obj[3].to_f }
  end
  if options[:reverse]
    arr.reverse!
  end
  if Settings.options[:formats][:list][:reverse] == true
    arr.reverse!
  end
  arr.each_with_index do |obj, index|
    table << arr[index]
    if index + 1 != arr.length && Settings.options[:timeline][:compact] == false
      table << :separator
    end
  end
  table
end

#colorize_text(text, mentions, hashtags) ⇒ Object



535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
# File 'lib/ayadn/workers.rb', line 535

def colorize_text(text, mentions, hashtags)
  reg_split = '[~:-;,?!\'&`^=+<>*%()\/"“”’°£$€.…]'
  reg_tag = '#([[:alpha:]0-9_]{1,255})(?![\w+])'
  reg_mention = '@([A-Za-z0-9_]{1,20})(?![\w+])'
  reg_sentence = '^.+[\r\n]*'
  handles, words, sentences = [], [], []
  mentions.each {|username| handles << "@#{username}"}
  hashtag_color = Settings.options[:colors][:hashtags]
  mention_color = Settings.options[:colors][:mentions]
  text.scan(/#{reg_sentence}/) do |sentence|
    sentence.split(' ').each do |word|

      word_chars = word.chars
      sanitized, word = [], []
      word_chars.each do |ch|
        if UnicodeUtils.general_category(ch) == :Other_Symbol
          sanitized << "#{ch} "
        elsif UnicodeUtils.char_name(ch).nil?
          next
        else
          sanitized << ch
        end
      end
      word = sanitized.join

      if word =~ /#\w+/
        slices = word.split('#')
        has_h = false
        slices.each do |tag|
          has_h = true if hashtags.include?(tag.downcase.scan(/[[:alpha:]0-9_]/).join(''))
        end
        if has_h == true
          if slices.length > 1
            words << slices.join('#').gsub(/#{reg_tag}/, '#\1'.color(hashtag_color))
          else
            words << word.gsub(/#{reg_tag}/, '#\1'.color(hashtag_color))
          end
        else
          words << word
        end
      elsif word =~ /@\w+/
        enc = []
        warr = word.split(' ')
        warr.each do |w|
          @str = def_str(w, reg_split)
          if handles.include?(@str.downcase)
            if warr.length == 1
              enc << w.gsub(/#{reg_mention}/, '@\1'.color(mention_color))
            else
              enc << " #{w.gsub(/#{reg_mention}/, '@\1'.color(mention_color))}"
            end
          else
            enc << w
          end
        end
        words << enc.join
      else
        words << word
      end
    end
    sentences << words.join(' ')
    words = Array.new
  end
  if Settings.options[:timeline][:compact] == true
    without_linebreaks = sentences.keep_if { |s| s != "" }
    without_linebreaks.join("\n")
  else
    sentences.join("\n")
  end
end

#extract_hashtags(post) ⇒ Object



360
361
362
# File 'lib/ayadn/workers.rb', line 360

def extract_hashtags(post)
  post['entities']['hashtags'].map { |h| h['name'] }
end


327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
# File 'lib/ayadn/workers.rb', line 327

def extract_links(post)
  links = post['entities']['links'].map { |l| l['url'] }
  unless post['annotations'].nil? || post['annotations'].empty?
    post['annotations'].each do |ann|
      if ann['type'] == "net.app.core.oembed"
        if ann['value']['embeddable_url']
          links << ann['value']['embeddable_url']
        elsif ann['value']['url'] && Settings.options[:channels][:links] == true
          links << ann['value']['url']
        end
      end
    end
  end
  links.uniq
end

#extract_mentions(post) ⇒ Object



364
365
366
# File 'lib/ayadn/workers.rb', line 364

def extract_mentions(post)
  post['entities']['mentions'].map { |m| m['name'] }
end

#extract_users(resp) ⇒ Object



527
528
529
530
531
532
533
# File 'lib/ayadn/workers.rb', line 527

def extract_users(resp)
  users_hash = {}
  resp['data'].each do |item|
    users_hash[item['id']] = [item['username'], item['name'], item['you_follow'], item['follows_you'], item['counts']['posts']]
  end
  users_hash
end

#get_channel_id_from_alias(channel_id) ⇒ Object



446
447
448
449
450
451
452
453
454
455
456
457
# File 'lib/ayadn/workers.rb', line 446

def get_channel_id_from_alias(channel_id)
  unless channel_id.is_integer?
    orig = channel_id
    channel_id = Databases.get_channel_id(orig)
    if channel_id.nil?
      Errors.warn("Alias '#{orig}' doesn't exist.")
      @status.no_alias
      exit
    end
  end
  channel_id
end

#get_original_id(post_id, resp) ⇒ Object



435
436
437
438
439
440
441
442
443
444
# File 'lib/ayadn/workers.rb', line 435

def get_original_id(post_id, resp)
  if resp['data']['repost_of']
    @status.redirecting
    id = resp['data']['repost_of']['id']
    Errors.repost(post_id, id)
    return id
  else
    return post_id
  end
end

#get_post_from_index(id) ⇒ Object



463
464
465
# File 'lib/ayadn/workers.rb', line 463

def get_post_from_index id
  Databases.get_post_from_index id
end

#get_real_post_id(post_id) ⇒ Object



467
468
469
470
471
472
473
474
# File 'lib/ayadn/workers.rb', line 467

def get_real_post_id post_id
  id = post_id.to_i
  if id <= 200
    resp = get_post_from_index(id)
    post_id = resp['id']
  end
  post_id
end

#length_of_indexObject



459
460
461
# File 'lib/ayadn/workers.rb', line 459

def length_of_index
  Databases.get_index_length
end


606
607
608
609
610
611
612
# File 'lib/ayadn/workers.rb', line 606

def links_from_posts(stream)
  links = []
  stream['data'].each do |post|
    extract_links(post).each {|l| links << l}
  end
  links.uniq
end

#parsed_time(string) ⇒ Object



427
428
429
# File 'lib/ayadn/workers.rb', line 427

def parsed_time(string)
  "#{string[0...10]} #{string[11...19]}"
end

#parsed_time_short(string) ⇒ Object



431
432
433
# File 'lib/ayadn/workers.rb', line 431

def parsed_time_short(string)
  "#{string[11...19]}"
end

#remove_arobase_if_present(args) ⇒ Object



490
491
492
493
494
495
496
497
# File 'lib/ayadn/workers.rb', line 490

def remove_arobase_if_present args
  args.map! do |username|
    temp = username.chars
    temp.shift if temp.first == "@"
    temp.join
  end
  args
end


343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
# File 'lib/ayadn/workers.rb', line 343

def save_links(links, origin, args = "")
  links.sort!
  obj = {
    'meta' => {
      'type' => 'links',
      'origin' => origin,
      'args' => args,
      'created_at' => Time.now,
      'username' => Settings.config[:identity][:handle]
    },
    'data' => links
  }
  filename = "#{Settings.config[:identity][:handle]}_#{origin}_links.json"
  FileOps.save_links(obj, filename)
  @status.links_saved(filename)
end

#table_bordersObject



12
13
14
15
16
17
18
# File 'lib/ayadn/workers.rb', line 12

def table_borders
  if Settings.options[:formats][:table][:borders] == true
    { :width => Settings.options[:formats][:table][:width], border_x: '', border_i: '+', border_y: '|' }
  else
    { :width => Settings.options[:formats][:table][:width], border_x: ' ', border_i: ' ', border_y: ' ' }
  end
end

#who_am_iObject



523
524
525
# File 'lib/ayadn/workers.rb', line 523

def who_am_i
  Databases.(Amalgalite::Database.new(Dir.home + "/ayadn/accounts.sqlite"))[2]
end