lib/ayadn/workers.rb in ayadn-1.4.6 vs lib/ayadn/workers.rb in ayadn-1.5.0

- old
+ new

@@ -23,11 +23,11 @@ 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'] - users_list << {:username => obj['username'], :name => name, :you_follow => obj['you_follow'], :follows_you => obj['follows_you']} + users_list << {:username => obj['username'], :name => name, :you_follow => obj['you_follow'], :follows_you => obj['follows_you'], :id => obj['id']} end table.style = {border_x: ' ', border_i: ' ', border_y: ' '} return users_list, table end @@ -35,11 +35,11 @@ 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'] - users_list << {:username => obj['username'], :name => name, :you_follow => obj['you_follow'], :follows_you => obj['follows_you']} + users_list << {:username => obj['username'], :name => name, :you_follow => obj['you_follow'], :follows_you => obj['follows_you'], :id => obj['id']} end table.style = {border_x: ' ', border_i: ' ', border_y: ' '} return users_list, table end @@ -79,44 +79,75 @@ table.style = {border_x: ' ', border_i: ' ', border_y: ' '} build_users_list(build_users_array(list), table) end def build_users_list(list, table) + users = Workers.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/DAY'.color(:red)] + table << :separator list.each_with_index do |obj, index| - obj[:username].length > 35 ? username = "#{obj[:username][0...32]}..." : username = obj[:username] - unless obj[:name].nil? - obj[:name].length > 35 ? name = "#{obj[:name][0...32]}..." : name = obj[:name] - table << [ "@#{username} ".color(Settings.options[:colors][:username]), "#{name}" ] + 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 - table << [ "@#{username} ".color(Settings.options[:colors][:username]), "" ] + posts_day = 'unknown' end + obj[:username].length > 23 ? username = "#{obj[:username][0..20]}..." : username = obj[:username] + unless obj[:name].nil? || obj[:name].empty? + obj[:name].length > 23 ? name = "#{obj[:name][0..20]}..." : name = obj[:name] + table << [ "@#{username} ".color(Settings.options[:colors][:username]), "#{name}", posts_day ] + else + table << [ "@#{username} ".color(Settings.options[:colors][:username]), "", posts_day ] + end table << :separator unless index + 1 == list.length end table end def build_posts(data, niceranks = {}) # builds a hash of hashes, each hash is a normalized post with post id as a key posts = {} - data.each.with_index(1) do |post, index| - next if Databases.blacklist[post['source']['name'].downcase] - next if Databases.blacklist["-@#{post['user']['username'].downcase}"] + if Databases.blacklist[post['source']['name'].downcase] + Debug.skipped({source: post['source']['name']}) + next + end + if Databases.blacklist["-@#{post['user']['username'].downcase}"] + Debug.skipped({user: post['user']['username']}) + next + end hashtags = extract_hashtags(post) @skip = false hashtags.each do |h| if Databases.blacklist[h.downcase] @skip = true + Debug.skipped({hashtag: h}) break end end next if @skip mentions= [] post['entities']['mentions'].each { |m| mentions << m['name'] } mentions.each do |m| if Databases.blacklist["@#{m.downcase}"] @skip = true + Debug.skipped({mention: m}) break end end next if @skip @@ -269,10 +300,22 @@ def parsed_time(string) "#{string[0...10]} #{string[11...19]}" end + def self.at usernames + usernames.map do |user| + if user == 'me' + 'me' + elsif user[0] == '@' + user + else + "@#{user}" + end + end + end + def self.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 @@ -317,11 +360,10 @@ users_hash end def colorize_text(text, mentions, hashtags) reg_split = '[~:-;,?!\'&`^=+<>*%()\/"“”’°£$€.…]' - #reg_tag = '#([A-Za-z0-9_]{1,255})(?![\w+])' 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}"} @@ -402,11 +444,11 @@ end def build_users_array(list) users_list = [] list.each do |key, value| - users_list << {:username => value[0], :name => value[1], :you_follow => value[2], :follows_you => value[3]} + users_list << {:username => value[0], :name => value[1], :you_follow => value[2], :follows_you => value[3], :id => key} end users_list end def extract_checkins(post) @@ -443,12 +485,9 @@ checkins[:title] = obj['value']['title'] end unless obj['value']['region'].nil? checkins[:region] = obj['value']['region'] end - #when "net.app.core.oembed" - #has_checkins = true - #checkins[:embeddable_url] = obj['value']['embeddable_url'] end end end return checkins, has_checkins end