lib/ayadn/workers.rb in ayadn-1.7.5 vs lib/ayadn/workers.rb in ayadn-1.7.6
- old
+ new
@@ -1,9 +1,13 @@
# encoding: utf-8
module Ayadn
class Workers
+ def initialize
+ @shell = Thor::Shell::Color.new
+ end
+
def build_aliases_list(list)
table = init_table
table.title = "List of your channel aliases".color(:cyan) + "".color(:white)
table.style = {border_x: '~', border_i: '+', border_y: ':'}
list.each {|k,v| table << ["#{k}".color(:green), "#{v}".color(:red)]}
@@ -273,23 +277,40 @@
bucket = []
data = [data] unless data.is_a?(Array)
if options[:channels]
puts "Downloading list of channels and their users credentials.\n\nPlease wait, it could take a while if there are many results and users...".color(:cyan)
else
- puts "Downloading new channels and unknown users ids.\nThis is a one time operation, ids are being recorded in a database.\n\nPlease wait, it could take a while if you have many channels...".color(:cyan)
+ puts "Downloading the channels and recording their users attributes (owners, writers, editors and readers).\n\nThe users are recorded in a database for later filtering and analyzing.\n\nPlease wait, it could take a while the first time if you have many channels.\n\n".color(: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 = {}
data.each do |ch|
unless ch['writers']['user_ids'].empty?
+ @shell.say_status :parsing, "channel #{ch['id']}", :cyan
usernames = []
ch['writers']['user_ids'].each do |id|
+ next if no_user[id]
db = Databases.users[id]
- unless db.nil?
- usernames << "@" + db.keys.first
- else
+ if db.nil?
+ @shell.say_status :downloading, "user #{id}"
resp = API.new.get_user(id)
- usernames << "@" + resp['data']['username']
- Databases.add_to_users_db(id, resp['data']['username'], resp['data']['name'])
+
+ if resp['meta']['code'] != 200
+ @shell.say_status :error, "can't get user #{id}'s data, skipping", :red
+ no_user[id] = true
+ next
+ end
+
+ the_username = resp['data']['username']
+ @shell.say_status :recording, "@#{the_username}", :green
+
+ usernames << "@" + the_username
+ Databases.add_to_users_db(id, the_username, resp['data']['name'])
+ else
+ the_username = "@" + db.keys.first
+ @shell.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