lib/ayadn/nowplaying.rb in ayadn-1.8.2 vs lib/ayadn/nowplaying.rb in ayadn-2.0

- old
+ new

@@ -3,43 +3,76 @@ class NowPlaying require 'rss' - def initialize api, view, workers + def initialize api, view, workers, options = {} @api = api @view = view @workers = workers + @status = Status.new + unless options[:hashtag] + @hashtag = "#nowplaying" + else + @hashtag = "##{options[:hashtag].join()}" + end + unless options[:text] + @custom_text = nil + else + @custom_text = "\n \n#{options[:text].join(' ')}" + end end def lastfm options begin user = Settings.options[:nowplaying][:lastfm] || create_lastfm_user() - puts Status.fetching_from('Last.fm') + @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}" + @status.itunes_store + store = [] + unless options['no_url'] + store = lastfm_istore_request(artist, track) + if store['code'] == 404 && artist =~ /(and)/ + artist.gsub!('and', '&') + store = lastfm_istore_request(artist, track) + end + end + text_to_post = "#{@hashtag}\n \nTitle: ‘#{track}’\nArtist: #{artist}#{@custom_text}" post_nowplaying(text_to_post, store, options) rescue => e - puts Status.wtf + @status.wtf Errors.global_error({error: e, caller: caller, data: [store, options]}) end end def itunes options begin - abort(Status.error_only_osx) unless Settings.config[:platform] =~ /darwin/ - puts Status.fetching_from('iTunes') + unless Settings.config[:platform] =~ /darwin/ + @status.error_only_osx + exit + end + @status.fetching_from('iTunes') itunes = get_itunes_track_infos() - itunes.each {|el| abort(Status.empty_fields) if el.length == 0} - puts Status.itunes_store - store = itunes_istore_request(itunes) unless options['no_url'] - text_to_post = "#nowplaying\n \nTitle: ‘#{itunes.track}’\nArtist: #{itunes.artist}\nfrom ‘#{itunes.album}’" + itunes.each do |el| + if el.length == 0 + @status.empty_fields + exit + end + end + @status.itunes_store + store = [] + unless options['no_url'] + store = itunes_istore_request(itunes) + if store['code'] == 404 && itunes.artist =~ /(and)/ + itunes.artist.gsub!('and', '&') + store = itunes_istore_request(itunes) + end + end + text_to_post = "#{@hashtag}\n \nTitle: ‘#{itunes.track}’\nArtist: #{itunes.artist}\nfrom ‘#{itunes.album}’#{@custom_text}" post_nowplaying(text_to_post, store, options) rescue => e - puts Status.wtf + @status.wtf Errors.global_error({error: e, caller: caller, data: [itunes, store, options]}) end end private @@ -49,28 +82,43 @@ url = "http://ws.audioscrobbler.com/2.0/user/#{user}/recenttracks.rss" feed = RSS::Parser.parse(CNX.download(url)) lfm = feed.items[0].title.split(' – ') return lfm[0], lfm[1] rescue Interrupt - abort(Status.canceled) + @status.canceled + exit end end def post_nowplaying text_to_post, store, options begin - @view.clear_screen - puts Status.writing - show_nowplaying("\n#{text_to_post}", options, store) + before = text_to_post 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}" + poster = Post.new + poster.post_size_error(text_to_post) if poster.post_size_ok?(text_to_post) == false + @view.clear_screen + @status.writing + show_nowplaying("\n#{before}", options, store) + unless STDIN.getch == ("y" || "Y") + @status.canceled + exit + end + @view.clear_screen + @status.yourpost + puts "\n\n" if store.nil? || options[:no_url] + text_to_post = before visible, track, artwork, artwork_thumb, link, artist = false else - visible, track, artwork, artwork_thumb, link, artist = true, store['track'], store['artwork'], store['artwork_thumb'], store['link'], store['artist'] + if store['link'].nil? || store['code'] == 404 + text_to_post = before + visible, track, artwork, artwork_thumb, link, artist = false + else + visible, track, artwork, artwork_thumb, link, artist = true, store['track'], store['artwork'], store['artwork_thumb'], store['link'], store['artist'] + end end options = options.dup options[:nowplaying] = true if options[:lastfm] source = 'Last.fm' @@ -90,78 +138,91 @@ height_thumb: 200, link: link, source: source, visible: visible } - resp = Post.new.post(dic) - FileOps.save_post(resp) if Settings.options[:backup][:auto_save_sent_posts] + resp = poster.post(dic) + FileOps.save_post(resp) if Settings.options[:backup][:posts] @view.show_posted(resp) rescue => e - puts Status.wtf + @status.wtf Errors.global_error({error: e, caller: caller, data: [dic, store, options]}) end end def ask_lastfm_user - puts "\nPlease enter your Last.fm username:\n".color(:cyan) + @status.info("please", "enter your Last.fm username", "yellow") + print "> " begin STDIN.gets.chomp! rescue Interrupt - abort(Status.canceled) + @status.canceled + exit end end def create_lastfm_user Settings.options[:nowplaying][:lastfm] = ask_lastfm_user() Settings.save_config return Settings.options[:nowplaying][:lastfm] end def itunes_istore_request itunes - infos = itunes_reg([itunes.artist, itunes.track, itunes.album]) - itunes_url = "https://itunes.apple.com/search?term=#{infos[0]}&term=#{infos[1]}&term=#{infos[2]}&media=music&entity=musicTrack" + itunes_url = "https://itunes.apple.com/search?term=#{itunes.artist}&term=#{itunes.track}&term=#{itunes.album}&media=music&entity=musicTrack" get_itunes_store(itunes_url, itunes.artist, itunes.track) end def lastfm_istore_request artist, track - infos = itunes_reg([artist, track]) - itunes_url = "https://itunes.apple.com/search?term=#{infos[0]}&term=#{infos[1]}&media=music&entity=musicTrack" + itunes_url = "https://itunes.apple.com/search?term=#{artist}&term=#{track}&media=music&entity=musicTrack" get_itunes_store(itunes_url, artist, track) end def get_itunes_store url, artist, track results = JSON.load(CNX.download(URI.escape(url)))['results'] - + # puts results.inspect unless results.empty? || results.nil? - - results.keep_if do |obj| - unless obj['artistName'].nil? + # results.each {|obj| puts obj['trackName']} + # puts "-" + # puts track + one = results.select do |obj| + next if obj['trackName'].nil? + obj['trackName'].downcase == track.downcase + end + # puts one.inspect + if one.empty? + by_artist = results.select do |obj| + next if obj['artistName'].nil? obj['artistName'].downcase == artist.downcase end - end + # puts by_artist + by_exact_track = by_artist.select do |obj| + next if obj['trackName'].nil? + obj['trackName'].downcase == track.downcase + end + # puts by_exact_track + if by_exact_track.empty? + splitted = track.split(" ").first.downcase + results = by_artist.select do |obj| + next if obj['trackName'].nil? + obj['trackName'].split(" ").first.downcase == splitted + end + else + results = by_exact_track + end - if results.empty? - return { - 'code' => 404, - 'request' => url - } - end - - if results.length > 1 - resp = results.select {|obj| obj['trackName'].downcase == track.downcase} else - resp = results + results = one end - if resp.empty? + if results.empty? return { 'code' => 404, 'request' => url } end - candidate = resp[0] || results[0] + candidate = results[0] return { 'code' => 200, 'artist' => candidate['artistName'], 'track' => candidate['trackName'], @@ -178,37 +239,39 @@ 'request' => url } end end - def itunes_reg arr_of_itunes - regex_exotics = /[~:-;,?!\'&`^=+<>*%()\/"“”’°£$€.…]/ - arr_of_itunes.map do |itune| - itune.gsub(regex_exotics, ' ').split(' ').join('+') - end - end - def get_itunes_track_infos track = `osascript -e 'tell application "iTunes"' -e 'set trackName to name of current track' -e 'return trackName' -e 'end tell'` if track.empty? - puts Status.no_itunes + @status.no_itunes Errors.warn "Nowplaying canceled: unable to get info from iTunes." exit end album = `osascript -e 'tell application "iTunes"' -e 'set trackAlbum to album of current track' -e 'return trackAlbum' -e 'end tell'` 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, options, store) - puts "\nYour post:\n".color(:cyan) - 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) + # @status.to_be_posted + thor = Thor::Shell::Basic.new + text.split("\n").each do |line| + thor.say_status(nil, line.color(Settings.options[:colors][:excerpt])) end - puts "Is it ok? (y/N) ".color(:yellow) + puts "\n" + unless options['no_url'] || store['code'] != 200 + thor.say_status(nil, "[iTunes link](#1)") + thor.say_status(nil, "[album art](#2)") + puts "\n\n" + thor.say_status(:'#1', store['link']) + thor.say_status(:'#2', store['artwork']) + puts "\n" + @status.itunes_store_track(store) + end + @status.ok? end end end