lib/ayadn/action.rb in ayadn-1.3.2 vs lib/ayadn/action.rb in ayadn-1.4.0

- old
+ new

@@ -730,26 +730,10 @@ ensure Databases.close_all end end - def post(args) - begin - @view.clear_screen - puts Status.posting - resp = Post.new.post(args) - FileOps.save_post(resp) if Settings.options[:backup][:auto_save_sent_posts] - @view.clear_screen - puts Status.yourpost - @view.show_posted(resp) - rescue => e - Errors.global_error("action/post", args, e) - ensure - Databases.close_all - end - end - def auto(options) begin @view.clear_screen puts Status.auto poster = Post.new @@ -767,50 +751,87 @@ ensure Databases.close_all end end - def write + def post(args, options) begin writer = Post.new + @view.clear_screen + if options['embed'] + puts Status.uploading(options['embed']) + resp = writer.send_embedded(args.join(" "), FileOps.make_paths(options['embed'])) + else + puts Status.posting + resp = writer.post(args) + end + FileOps.save_post(resp) if Settings.options[:backup][:auto_save_sent_posts] + @view.clear_screen + puts Status.yourpost + @view.show_posted(resp) + rescue => e + Errors.global_error("action/post", [args, options], e) + ensure + Databases.close_all + end + end + + def write(options) + begin + files = FileOps.make_paths(options['embed']) if options['embed'] + writer = Post.new puts Status.writing puts Status.post lines_array = writer.compose writer.check_post_length(lines_array) + text = lines_array.join("\n") + if options['embed'] + @view.clear_screen + puts Status.uploading(options['embed']) + resp = writer.send_embedded(text, files) + else + resp = writer.send_post(text) + end @view.clear_screen puts Status.posting - resp = writer.send_post(lines_array.join("\n")) FileOps.save_post(resp) if Settings.options[:backup][:auto_save_sent_posts] @view.clear_screen puts Status.yourpost @view.show_posted(resp) rescue => e - Errors.global_error("action/write", lines_array.join(" "), e) + Errors.global_error("action/write", [text, options], e) ensure Databases.close_all end end - def pmess(username) + def pmess(username, options = {}) begin + files = FileOps.make_paths(options['embed']) if options['embed'] missing_username if username.empty? temp = Workers.add_arobase_if_missing(username) username = [temp] messenger = Post.new puts Status.message_from(username) puts Status.message lines_array = messenger.compose messenger.check_message_length(lines_array) + text = lines_array.join("\n") @view.clear_screen - puts Status.posting - resp = messenger.send_pm(username, lines_array.join("\n")) + if options['embed'] + puts Status.uploading(options['embed']) + resp = messenger.send_pm_embedded(username, text, files) + else + puts Status.posting + resp = messenger.send_pm(username, text) + end FileOps.save_message(resp) if Settings.options[:backup][:auto_save_sent_messages] @view.clear_screen puts Status.yourmessage @view.show_posted(resp) rescue => e - Errors.global_error("action/pmess", username, e) + Errors.global_error("action/pmess", [username, options], e) ensure Databases.close_all end end @@ -834,12 +855,13 @@ ensure Databases.close_all end end - def reply(post_id) + def reply(post_id, options = {}) begin + files = FileOps.make_paths(options['embed']) if options['embed'] post_id = get_real_post_id(post_id) puts Status.replying_to(post_id) replied_to = @api.get_details(post_id) post_404(post_id) if meta_404(replied_to) post_id = get_original_id(post_id, replied_to) @@ -854,40 +876,62 @@ puts Status.reply lines_array = poster.compose poster.check_post_length(lines_array) @view.clear_screen reply = poster.reply(lines_array.join("\n"), Workers.new.build_posts([replied_to['data']])) - puts Status.posting - resp = poster.send_reply(reply, post_id) + if options['embed'] + puts Status.uploading(options['embed']) + resp = poster.send_reply_embedded(reply, post_id, files) + else + puts Status.posting + 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, {}), {}) rescue => e - Errors.global_error("action/reply", post_id, e) + Errors.global_error("action/reply", [post_id, options], e) ensure Databases.close_all end end - def nowplaying + def nowplaying(options = {}) begin Databases.close_all abort(Status.error_only_osx) unless Settings.config[:platform] =~ /darwin/ - itunes = get_track_infos + itunes = get_track_infos() itunes.each {|el| abort(Status.empty_fields) if el.length == 0} @view.clear_screen - text_to_post = "#nowplaying\nTitle: ‘#{itunes.track}’\nArtist: #{itunes.artist}\nfrom ‘#{itunes.album}’" + unless options['no_url'] + store = itunes_request(itunes) + end + text_to_post = "#nowplaying\n \nTitle: ‘#{itunes.track}’\nArtist: #{itunes.artist}\nfrom ‘#{itunes.album}’" puts Status.writing - show_nowplaying(text_to_post) + show_nowplaying("\n#{text_to_post}", options, store) + text_to_post += "\n \n[> Listen](#{store['preview']})" unless options['no_url'] unless STDIN.getch == ("y" || "Y") puts "\nCanceled.\n\n".color(:red) exit end puts "\n" puts Status.yourpost - @view.show_posted(Post.new.post([text_to_post])) + if options['no_url'].nil? + visible, track, artwork = true, store['track'], store['artwork'] + else + visible, track, artwork = false, false, false + end + dic = { + 'text' => text_to_post, + 'title' => track, + 'artwork' => artwork, + 'width' => 1200, + 'height' => 1200, + 'visible' => visible + } + @view.show_posted(Post.new.send_nowplaying(dic)) rescue => e puts Status.wtf Errors.global_error("action/nowplaying", itunes, e) end end @@ -925,10 +969,28 @@ end end private + def itunes_request itunes + regex_exotics = /[~:-;,?!\'&`^=+<>*%()\/"“”’°£$€.…]/ + store_artist = itunes.artist.gsub(regex_exotics, ' ').split(' ').join('+') + store_track = itunes.track.gsub(regex_exotics, ' ').split(' ').join('+') + #store_album = itunes.album.gsub(regex_exotics, ' ').split(' ').join('+') + itunes_url = "https://itunes.apple.com/search?term=#{store_artist}&term=#{store_track}&media=music&entity=musicTrack" + results = JSON.load(CNX.download(itunes_url))['results'] + candidate = results[0] + { + 'artist' => candidate['artistName'], + 'track' => candidate['trackName'], + 'preview' => candidate['previewUrl'], + 'artwork' => candidate['artworkUrl100'].gsub('100x100', '1200x1200'), + 'request' => itunes_url, + 'results' => results + } + end + def splitter_all words [words].collect {|w| w.split(' ')} end def get_original_id(post_id, resp) @@ -1226,12 +1288,16 @@ artist = `osascript -e 'tell application "iTunes"' -e 'set trackArtist to artist of current track' -e 'return trackArtist' -e 'end tell'` maker = Struct.new(:artist, :album, :track) maker.new(artist.chomp!, album.chomp!, track.chomp!) end - def show_nowplaying(text) + def show_nowplaying(text, options, store) puts "\nYour post:\n".color(:cyan) - puts text + "\n\n" + if options['no_url'] + puts text + "\n\n\n" + else + puts text + "\n\nAlbum artwork + 30 sec preview for track '#{store['track']}' by '#{store['artist']}' will be inserted in the post.\n\n\n".color(:green) + end puts "Do you confirm? (y/N) ".color(:yellow) end def countdown(wait) wait.downto(1) do |i|