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

- old
+ new

@@ -130,11 +130,11 @@ def interactions(options) begin doing(options) stream = @api.get_interactions - option_show_raw(stream, options) + if_raw_show(stream, options) @view.clear_screen @view.show_interactions(stream['data']) rescue => e Errors.global_error({error: e, caller: caller, data: [options]}) end @@ -160,11 +160,11 @@ doing(options) details = @api.get_details(post_id, options) stop_if_404(details, post_id) id = get_original_id(post_id, details) list = @api.get_whoreposted(id) - option_show_raw(list, options) + if_raw_show(list, options) unless list['data'].empty? get_list(:whoreposted, list['data'], post_id) else puts Status.nobody_reposted end @@ -179,11 +179,11 @@ doing(options) details = @api.get_details(post_id, options) stop_if_404(details, post_id) id = get_original_id(post_id, details) list = @api.get_whostarred(id) - option_show_raw(list, options) + if_raw_show(list, options) unless list['data'].empty? get_list(:whostarred, list['data'], id) else puts Status.nobody_starred end @@ -199,10 +199,15 @@ details = @api.get_details(post_id, options) stop_if_404(details, post_id) id = get_original_id(post_id, details) stream = get_convo id, options Databases.pagination["replies:#{id}"] = stream['meta']['max_id'] + options = options.dup + unless details['data']['reply_to'].nil? + options[:reply_to] = details['data']['reply_to'].to_i + end + options[:post_id] = post_id.to_i render_view(stream, options) Scroll.new(@api, @view).convo(id, options) if options[:scroll] rescue => e Errors.global_error({error: e, caller: caller, data: [post_id, id, options]}) end @@ -236,73 +241,97 @@ rescue => e Errors.global_error({error: e, caller: caller, data: [message_id]}) end end - def unfollow(username) + def unfollow(usernames) begin - stop_if_no_username(username) - username = add_arobase(username) - puts Status.unfollowing(username) - check_has_been_unfollowed(username, @api.unfollow(username)) + stop_if_no_username(usernames) + users = all_but_me(usernames) + puts Status.unfollowing(users.join(',')) + users.each do |user| + resp = @api.unfollow(user) + check_has_been_unfollowed(user, resp) + sleep 1 unless users.length == 1 + end rescue => e - Errors.global_error({error: e, caller: caller, data: [username]}) + Errors.global_error({error: e, caller: caller, data: [usernames]}) end end - def follow(username) + def follow(usernames) begin - stop_if_no_username(username) - username = add_arobase(username) - puts Status.following(username) - check_has_been_followed(username, @api.follow(username)) + stop_if_no_username(usernames) + users = all_but_me(usernames) + puts Status.following(users.join(',')) + users.each do |user| + resp = @api.follow(user) + check_has_been_followed(user, resp) + sleep 1 unless users.length == 1 + end rescue => e - Errors.global_error({error: e, caller: caller, data: [username]}) + Errors.global_error({error: e, caller: caller, data: [usernames]}) end end - def unmute(username) + def unmute(usernames) begin - stop_if_no_username(username) - username = add_arobase(username) - puts Status.unmuting(username) - check_has_been_unmuted(username, @api.unmute(username)) + stop_if_no_username(usernames) + users = all_but_me(usernames) + puts Status.unmuting(users.join(',')) + users.each do |user| + resp = @api.unmute(user) + check_has_been_unmuted(user, resp) + sleep 1 unless users.length == 1 + end rescue => e - Errors.global_error({error: e, caller: caller, data: [username]}) + Errors.global_error({error: e, caller: caller, data: [usernames]}) end end - def mute(username) + def mute(usernames) begin - stop_if_no_username(username) - username = add_arobase(username) - puts Status.muting(username) - check_has_been_muted(username, @api.mute(username)) + stop_if_no_username(usernames) + users = all_but_me(usernames) + puts Status.muting(users.join(',')) + users.each do |user| + resp = @api.mute(user) + check_has_been_muted(user, resp) + sleep 1 unless users.length == 1 + end rescue => e - Errors.global_error({error: e, caller: caller, data: [username]}) + Errors.global_error({error: e, caller: caller, data: [usernames]}) end end - def unblock(username) + def unblock(usernames) begin - stop_if_no_username(username) - username = add_arobase(username) - puts Status.unblocking(username) - check_has_been_unblocked(username, @api.unblock(username)) + stop_if_no_username(usernames) + users = all_but_me(usernames) + puts Status.unblocking(users.join(',')) + users.each do |user| + resp = @api.unblock(user) + check_has_been_unblocked(user, resp) + sleep 1 unless users.length == 1 + end rescue => e - Errors.global_error({error: e, caller: caller, data: [username]}) + Errors.global_error({error: e, caller: caller, data: [usernames]}) end end - def block(username) + def block(usernames) begin - stop_if_no_username(username) - username = add_arobase(username) - puts Status.blocking(username) - check_has_been_blocked(username, @api.block(username)) + stop_if_no_username(usernames) + users = all_but_me(usernames) + puts Status.blocking(users.join(',')) + users.each do |user| + resp = @api.block(user) + check_has_been_blocked(user, resp) + sleep 1 unless users.length == 1 + end rescue => e - Errors.global_error({error: e, caller: caller, data: [username]}) + Errors.global_error({error: e, caller: caller, data: [usernames]}) end end def repost(post_id) begin @@ -399,11 +428,11 @@ stop_if_no_data(stream, 'search') if options[:users] stream['data'].sort_by! {|obj| obj['counts']['followers']} stream['data'].each do |obj| puts @view.big_separator - @view.show_userinfos(obj, nil) + @view.show_userinfos(obj, nil, false) end elsif options[:channels] @view.show_channels stream, options else if options[:extract] @@ -420,11 +449,11 @@ def followings(username, options) begin stop_if_no_username(username) username = add_arobase(username) doing(options) - option_show_raw(@api.get_raw_list(username, :followings), options) + if_raw_list(username, :followings, options) list = @api.get_followings(username) auto_save_followings(list) no_data('followings') if list.empty? get_list(:followings, list, username) Databases.add_to_users_db_from_list(list) @@ -436,11 +465,11 @@ def followers(username, options) begin stop_if_no_username(username) username = add_arobase(username) doing(options) - option_show_raw(@api.get_raw_list(username, :followers), options) + if_raw_list(username, :followers, options) list = @api.get_followers(username) auto_save_followers(list) no_data('followers') if list.empty? get_list(:followers, list, username) Databases.add_to_users_db_from_list(list) @@ -450,11 +479,11 @@ end def muted(options) begin doing(options) - option_show_raw(@api.get_raw_list(nil, :muted), options) + if_raw_list(nil, :muted, options) list = @api.get_muted auto_save_muted(list) no_data('muted') if list.empty? get_list(:muted, list, nil) Databases.add_to_users_db_from_list(list) @@ -464,11 +493,11 @@ end def blocked(options) begin doing(options) - option_show_raw(@api.get_raw_list(nil, :blocked), options) + if_raw_list(nil, :blocked, options) list = @api.get_blocked no_data('blocked') if list.empty? get_list(:blocked, list, nil) Databases.add_to_users_db_from_list(list) rescue => e @@ -487,11 +516,15 @@ def userinfo(username, options) begin stop_if_no_username(username) username = add_arobase(username) doing(options) - option_show_raw(@api.get_user(username), options) + if options[:raw] + resp = @api.get_user(username) + @view.show_raw(resp, options) + exit + end stream = @api.get_user(username) stop_if_no_user(stream, username) same_username?(stream) ? token = @api.get_token_info['data'] : token = nil get_infos(stream['data'], token) rescue => e @@ -501,11 +534,15 @@ def postinfo(post_id, options) begin stop_if_bad_post_id(post_id) doing(options) - option_show_raw(@api.get_details(post_id, options), options) + if options[:raw] + details = @api.get_details(post_id, options) + @view.show_raw(details, options) + exit + end @view.clear_screen response = @api.get_details(post_id, options) stop_if_no_post(response, post_id) resp = response['data'] response = @api.get_user("@#{resp['user']['username']}") @@ -518,23 +555,26 @@ Errors.repost(post_id, resp['repost_of']['id']) @view.show_simple_post([resp['repost_of']], options) end puts "AUTHOR:\n".inverse if response['data']['username'] == Settings.config[:identity][:username] - @view.show_userinfos(stream, @api.get_token_info['data']) + @view.show_userinfos(stream, @api.get_token_info['data'], true) else - @view.show_userinfos(stream, nil) + @view.show_userinfos(stream, nil, true) end rescue => e Errors.global_error({error: e, caller: caller, data: [post_id, options]}) end end def files(options) begin doing(options) - option_show_raw(@api.get_files_list(options), options) + if options[:raw] + @view.show_raw(@api.get_files_list(options), options) + exit + end list = @api.get_files_list(options) @view.clear_screen list.empty? ? no_data('files') : @view.show_files_list(list) rescue => e Errors.global_error({error: e, caller: caller, data: [options]}) @@ -567,11 +607,11 @@ channel_id = get_channel_id_from_alias(channel_id) doing(options) resp = @api.get_messages(channel_id, options) stop_if_no_new_posts(resp, options, "channel:#{channel_id}") Databases.save_max_id(resp) - option_show_raw(resp, options) + if_raw_show(resp, options) stop_if_no_data(resp, 'messages') render_view(resp, options) Scroll.new(@api, @view).messages(channel_id, options) if options[:scroll] rescue => e Errors.global_error({error: e, caller: caller, data: [channel_id, options]}) @@ -621,18 +661,12 @@ def post(args, options) begin writer = Post.new @view.clear_screen if options['embed'] - if options['embed'].length > 1 # if args are inversed on the cl - opts = options['embed'].dup - embed = [opts.shift] - text = opts.join(" ") - else - embed = options['embed'] - text = args.join(" ") - end + embed = options['embed'] + text = args.join(" ") puts Status.uploading(embed) resp = writer.send_embedded(text, FileOps.make_paths(embed)) else puts Status.posting resp = writer.post(args) @@ -750,11 +784,18 @@ resp = poster.send_reply(reply, post_id) end FileOps.save_post(resp) if Settings.options[:backup][:auto_save_sent_posts] @view.clear_screen puts Status.done - render_view(@api.get_convo(post_id)) + + options = options.dup + unless resp['data']['reply_to'].nil? + options[:reply_to] = resp['data']['reply_to'].to_i + end + options[:post_id] = resp['data']['id'].to_i + + render_view(@api.get_convo(post_id), options) rescue => e Errors.global_error({error: e, caller: caller, data: [post_id, options]}) end end @@ -791,25 +832,47 @@ rescue => e Errors.global_error({error: e, caller: caller, data: [@max_id, @random_post_id, @resp, options]}) end end + def version + begin + puts "\nAYADN\n".color(:red) + puts "Version:\t".color(:cyan) + "#{VERSION}\n".color(:green) + puts "Changelog:\t".color(:cyan) + "https://github.com/ericdke/na/blob/master/CHANGELOG.md\n".color(Settings.options[:colors][:link]) + puts "Docs:\t\t".color(:cyan) + "http://ayadn-app.net/doc/".color(Settings.options[:colors][:link]) + puts "\n" + rescue => e + Errors.global_error({error: e, caller: caller, data: []}) + end + end + private + def if_raw_list username, what, options + if options[:raw] + @view.show_raw(@api.get_raw_list(username, what), options) + exit + end + end + + def if_raw_show what, options + if options[:raw] + @view.show_raw(what, options) + exit + end + end + + def all_but_me usernames + all_but_me = usernames.select {|user| user != 'me'} + Workers.at(all_but_me) + end + def np_lastfm options require 'rss' begin - if Settings.options[:nowplaying] - if Settings.options[:nowplaying][:lastfm] - user = Settings.options[:nowplaying][:lastfm] - else - user = create_lastfm_user() - end - else - Settings.options[:nowplaying] = {} - user = create_lastfm_user() - end + user = Settings.options[:nowplaying][:lastfm] || create_lastfm_user() puts Status.fetching_from('Last.fm') artist, track = get_lastfm_track_infos(user) puts Status.itunes_store store = lastfm_istore_request(artist, track) unless options['no_url'] text_to_post = "#nowplaying\n \nTitle: ‘#{track}’\nArtist: #{artist}" @@ -850,30 +913,37 @@ def post_nowplaying text_to_post, store, options begin @view.clear_screen puts Status.writing show_nowplaying("\n#{text_to_post}", options, store) - unless options['no_url'] || store['code'] != 200 - #text_to_post += "\n \n[> Listen](#{store['preview']})" + unless options['no_url'] || store.nil? text_to_post += "\n \n[iTunes Store](#{store['link']})" end abort(Status.canceled) unless STDIN.getch == ("y" || "Y") puts "\n#{Status.yourpost}" - unless options['no_url'] || store['code'] != 200 - visible, track, artwork, artwork_thumb = true, store['track'], store['artwork'], store['artwork_thumb'] + unless store.nil? || options['no_url'] + visible, track, artwork, artwork_thumb, link, artist = true, store['track'], store['artwork'], store['artwork_thumb'], store['link'], store['artist'] else - visible, track, artwork, artwork_thumb = false + visible, track, artwork, artwork_thumb, link, artist = false end + if options['lastfm'] + source = 'Last.fm' + else + source = 'iTunes' + end dic = { 'text' => text_to_post, 'title' => track, + 'artist' => artist, 'artwork' => artwork, 'artwork_thumb' => artwork_thumb, 'width' => 1200, 'height' => 1200, 'width_thumb' => 200, 'height_thumb' => 200, + 'link' => link, + 'source' => source, 'visible' => visible } @view.show_posted(Post.new.send_nowplaying(dic)) rescue => e puts Status.wtf @@ -956,28 +1026,17 @@ if options['no_url'] || store['code'] != 200 puts text + "\n\n\n" else puts text + "\n\n\nThe iTunes Store thinks this track is: ".color(:green) + "'#{store['track']}'".color(:magenta) + " by ".color(:green) + "'#{store['artist']}'".color(:magenta) + ".\n\nAyadn will use these elements to insert album artwork and a link to the track.\n\n".color(:green) end - puts "Do you confirm? (y/N) ".color(:yellow) + puts "Is it ok? (y/N) ".color(:yellow) end def nicerank_true - if Settings.options[:nicerank] - if Settings.options[:nicerank][:filter] == true - return true - end - end + return true if Settings.options[:nicerank][:filter] == true end - def option_show_raw stream, options - if options[:raw] - @view.show_raw(stream) - exit - end - end - def stop_if_bad_post_id post_id missing_post_id() unless post_id.is_integer? end def stop_if_no_data stream, target @@ -1218,10 +1277,10 @@ @view.show_simple_stream(stream) end def get_infos(stream, token) @view.clear_screen - @view.show_userinfos(stream, token) + @view.show_userinfos(stream, token, true) end def get_list(what, list, target) @view.clear_screen case what