lib/ayadn/stream.rb in ayadn-3.0 vs lib/ayadn/stream.rb in ayadn-4.0

- old
+ new

@@ -3,35 +3,34 @@ class Stream require_relative("scroll") - def initialize api, view, workers + def initialize api, view, workers, check, status @api = api @view = view @workers = workers - @check = Check.new - @status = Status.new + @check = check + @status = status end def global settings - Settings.global[:force] = true if settings[:force] options = settings.dup options[:filter] = nicerank_true() @view.downloading(options) unless options[:scroll] - stream = @api.get_global(options) - Settings.global[:force] == true ? niceranks = {} : niceranks = NiceRank.new.get_ranks(stream) - @check.no_new_posts(stream, options, 'global') - Databases.save_max_id(stream, 'global') unless stream['meta']['max_id'].nil? - @view.render(stream, options, niceranks) + stream_object = StreamObject.new(@api.get_global(options)) + Settings.global.force ? niceranks = {} : niceranks = NiceRank.new.get_ranks(stream_object) + @check.no_new_posts(stream_object, options, 'global') + Databases.save_max_id(stream_object, 'global') unless stream_object.meta.max_id.nil? + @view.render(stream_object, options, niceranks) end if options[:scroll] @view.clear_screen() Scroll.new(@api, @view).global(options) end - puts "\n" if Settings.options[:timeline][:compact] && options[:raw].nil? + puts "\n" if Settings.options.timeline.compact && options[:raw].nil? end def method_missing(meth, options) case meth @@ -45,101 +44,102 @@ super end end def stream meth, options, target - Settings.global[:force] = true if options[:force] @view.downloading(options) unless options[:scroll] stream = @api.send("get_#{meth}".to_sym, options) - @check.no_new_posts(stream, options, target) - Databases.save_max_id(stream) - @view.render(stream, options) + stream_object = StreamObject.new(stream) + @check.no_new_posts(stream_object, options, target) + Databases.save_max_id(stream_object) + @view.render(stream_object, options) end if options[:scroll] @view.clear_screen() Scroll.new(@api, @view).send(meth, options) end - puts "\n" if Settings.options[:timeline][:compact] && options[:raw].nil? + puts "\n" if Settings.options.timeline.compact && options[:raw].nil? end def mentions username, options - Settings.global[:force] = true if options[:force] @check.no_username(username) username = @workers.add_arobase(username) @view.downloading(options) unless options[:scroll] stream = @api.get_mentions(username, options) - @check.no_user(stream, username) - Databases.save_max_id(stream) - @check.no_data(stream, 'mentions') + stream_object = StreamObject.new(stream) + @check.no_user(stream_object, username) + Databases.save_max_id(stream_object) + @check.no_data(stream_object, 'mentions') options = options.dup options[:in_mentions] = true - @view.render(stream, options) + @view.render(stream_object, options) end if options[:scroll] @view.clear_screen() Scroll.new(@api, @view).mentions(username, options) end - puts "\n" if Settings.options[:timeline][:compact] && options[:raw].nil? + puts "\n" if Settings.options.timeline.compact && options[:raw].nil? end def posts username, options - Settings.global[:force] = true if options[:force] @check.no_username(username) username = @workers.add_arobase(username) @view.downloading(options) stream = @api.get_posts(username, options) - @check.no_user(stream, username) - Databases.save_max_id(stream) unless stream['meta']['marker'].nil? - @check.no_data(stream, 'mentions') - unless options[:raw] || Settings.global[:force] + stream_object = StreamObject.new(stream) + @check.no_user(stream_object, username) + Databases.save_max_id(stream_object) unless stream_object.meta.marker.nil? + @check.no_data(stream_object, 'mentions') + unless options[:raw] || Settings.global.force # this is just to show a message rather than an empty screen - if Settings.options[:blacklist][:active] == true + if Settings.options.blacklist.active if Databases.is_in_blacklist?('mention', username) @status.no_force("#{username.downcase}") exit end end end - if stream['data'][0]['user']['you_muted'] || stream['data'][0]['user']['you_blocked'] - unless options[:raw] || Settings.global[:force] + if stream_object.posts[0].user.you_muted || stream_object.posts[0].user.you_blocked + unless options[:raw] || Settings.global.force @status.no_force("#{username.downcase}") exit end end - @view.render(stream, options) + @view.render(stream_object, options) Scroll.new(@api, @view).posts(username, options) if options[:scroll] - puts "\n" if Settings.options[:timeline][:compact] && options[:raw].nil? + puts "\n" if Settings.options.timeline.compact && options[:raw].nil? end def whatstarred(username, options) @check.no_username(username) username = @workers.add_arobase(username) @view.downloading(options) unless options["again"] - if options["again"] stream = FileOps.cached_list("whatstarred") + stream_object = StreamObject.new(stream) Errors.no_data('cached whatstarred') if stream.nil? else stream = @api.get_whatstarred(username, options) + stream_object = StreamObject.new(stream) end - @check.no_user(stream, username) - @check.no_data(stream, 'whatstarred') + @check.no_user(stream_object, username) + @check.no_data(stream_object, 'whatstarred') if options["cache"] && options["again"].nil? - FileOps.cache_list(stream, "whatstarred") + FileOps.cache_list(stream_object.input, "whatstarred") end if options[:extract] - @view.all_stars_links(stream) + @view.all_stars_links(stream_object) else - @view.render(stream, options) + @view.render(stream_object, options) end - puts "\n" if Settings.options[:timeline][:compact] == true + puts "\n" if Settings.options.timeline.compact end def followings(username, options) @check.no_username(username) username = @workers.add_arobase(username) @@ -153,19 +153,19 @@ end @check.auto_save_followings(list) Errors.no_data('followings') if list.empty? if options["lastpost"] && options["again"].nil? count = list.size - @workers.thor.say_status :downloading, "please wait, it may take a while...", :red + @status.thor.say_status :downloading, "please wait, it may take a while...", :red puts "\n" idx = 0 - list.each do |str_id,obj| + list.each do |str_id, obj| idx += 1 tmp_username = "@#{obj[0]}" - colored_username = tmp_username.color(Settings.options[:colors][:username]) + colored_username = tmp_username.color(Settings.options.colors.username) iter = "#{idx}/#{count}" - @workers.thor.say_status iter.to_sym, "last post from #{colored_username}", :cyan + @status.thor.say_status iter.to_sym, "last post from #{colored_username}", :cyan resp = @api.get_posts(tmp_username, {count: 1}) obj << resp["data"][0] end end if options["cache"] && options["again"].nil? @@ -242,11 +242,11 @@ @view.downloading(options) stream = @api.get_interactions @view.if_raw(stream, options) @view.clear_screen @view.show_interactions(stream['data']) - puts "\n" if Settings.options[:timeline][:compact] == true + puts "\n" if Settings.options.timeline.compact end def whoreposted(post_id, options) @check.bad_post_id(post_id) unless options[:force] @@ -259,11 +259,11 @@ Errors.no_data('cached whoreposted details') if details.nil? else details = @api.get_details(post_id, options) end - @check.no_post(details, post_id) + @check.no_details(details, post_id) id = @workers.get_original_id(post_id, details) if options["cache"] && options["again"].nil? FileOps.cache_list(details, "whoreposted_details") end @@ -299,11 +299,11 @@ Errors.no_data('cached whostarred details') if details.nil? else details = @api.get_details(post_id, options) end - @check.no_post(details, post_id) + @check.no_details(details, post_id) id = @workers.get_original_id(post_id, details) if options["cache"] && options["again"].nil? FileOps.cache_list(details, "whostarred_details") end @@ -327,58 +327,58 @@ @view.list(:whostarred, list['data'], id) end def convo(post_id, options) @check.bad_post_id(post_id) - if options[:force] - Settings.global[:force] = true - else + unless options[:force] post_id = @workers.get_real_post_id(post_id) end @view.downloading(options) details = @api.get_details(post_id, options) - @check.no_post(details, post_id) + @check.no_details(details, post_id) id = @workers.get_original_id(post_id, details) stream = @api.get_convo(id, options) - @check.no_post(stream, id) - Databases.pagination_insert("replies:#{id}", stream['meta']['max_id']) + stream_object = StreamObject.new(stream) + @check.no_post(stream_object, id) + Databases.pagination_insert("replies:#{id}", stream_object.meta.max_id) options = options.dup options[:reply_to] = details['data']['reply_to'].to_i unless details['data']['reply_to'].nil? options[:post_id] = post_id.to_i - @view.render(stream, options) + @view.render(stream_object, options) Scroll.new(@api, @view).convo(id, options) if options[:scroll] - puts "\n" if Settings.options[:timeline][:compact] && options[:raw].nil? + puts "\n" if Settings.options.timeline.compact && options[:raw].nil? end def messages(channel_id, options) if options[:silent] - Settings.options[:marker][:messages] = false + Settings.options.marker.messages = false end channel_id = @workers.get_channel_id_from_alias(channel_id) @view.downloading(options) resp = @api.get_messages(channel_id, options) + stream_object = StreamObject.new(resp) name = "channel:#{channel_id}" - @check.no_new_posts(resp, options, name) - if Settings.options[:marker][:messages] == true - unless resp['meta']['max_id'].nil? - marked = @api.update_marker(name, resp['meta']['max_id']) + @check.no_new_posts(stream_object, options, name) + if Settings.options.marker.messages + unless stream_object.meta.max_id.nil? + marked = @api.update_marker(name, stream_object.meta.max_id) updated = JSON.parse(marked) if updated['meta']['code'] != 200 raise "couldn't update channel #{channel_id} as read" end end end - Databases.save_max_id(resp) - @view.if_raw(resp, options) - @check.no_data(resp, 'messages') unless options[:scroll] - @view.render(resp, options) + Databases.save_max_id(stream_object) + @view.if_raw(stream_object, options) + @check.no_data(stream_object, 'messages') unless options[:scroll] + @view.render(stream_object, options) Scroll.new(@api, @view).messages(channel_id, options) if options[:scroll] - puts "\n" if Settings.options[:timeline][:compact] && options[:raw].nil? + puts "\n" if Settings.options.timeline.compact && options[:raw].nil? end def random_posts(options) - Settings.global[:force] = true + Settings.global.force = true #_, cols = @view.winsize #max_posts = cols / 16 max_posts = 6 @view.clear_screen @status.info("connected", "fetching random posts", "cyan") @@ -388,12 +388,14 @@ wait = options[:wait] || 10 loop do begin @random_post_id = rand(@max_id) @resp = @api.get_details(@random_post_id, {}) + next if @resp.nil? + next if @resp['data'].nil? || @resp['data'].empty? next if @resp['data']['is_deleted'] - @view.show_simple_post([@resp['data']], {}) + @view.show_simple_post([PostObject.new(@resp['data'])], {}) counter += 1 if counter == max_posts wait.downto(1) do |i| print "\r#{sprintf("%02d", i)} sec... ([CTRL+C] to quit)".color(:cyan) sleep 1 @@ -428,10 +430,10 @@ exit end end def nicerank_true - return true if Settings.options[:nicerank][:filter] == true + return true if Settings.options.nicerank.filter end end end