<commit_before># encoding: utf-8
module Ayadn

  class NowPlaying

    begin
      require 'rss'
    rescue LoadError => e
      puts "\nAYADN: Error while loading an external resource\n\n"
      puts "RUBY: #{e}\n\n"
      exit
    end

    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
      @affiliate_suffix = "&at=1l3vtb8&ct=ayadn"
      @deezer_auth_url = "https://connect.deezer.com/oauth/auth.php?app_id=150971&redirect_uri=http://aya.io/ayadn/deezer.html&response_type=token&perms=basic_access,listening_history,offline_access"
    end

    def deezer options
      begin
        @deezer_code = if Settings.options[:nowplaying][:deezer].nil?
            create_deezer_user()
          else
            if Settings.options[:nowplaying][:deezer][:code].nil?
              create_deezer_user()
            else
              Settings.options[:nowplaying][:deezer][:code]
            end
          end
        @deezer_user_url = "http://api.deezer.com/user/me"
        @deezer_token_suffix = "?access_token=#{@deezer_code}"
        @status.fetching_from('Deezer')
        req = "#{@deezer_user_url}/history#{@deezer_token_suffix}"
        res = JSON.parse(CNX.download(req))
        res["data"].sort_by! { |obj| obj["timestamp"] }
        candidate = res["data"].last
        maker = Struct.new(:artist, :album, :track)
        itunes = maker.new(candidate["artist"]["name"], candidate["album"]["title"], candidate["title"])
        post_itunes(options, itunes)
      rescue => e
        @status.wtf
        Errors.global_error({error: e, caller: caller, data: [store, options]})
      end
    end

    def lastfm options
      begin
        user = Settings.options[:nowplaying][:lastfm] || create_lastfm_user()
        @status.fetching_from('Last.fm')
        artist, track = get_lastfm_track_infos(user)
        @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
        @status.wtf
        Errors.global_error({error: e, caller: caller, data: [store, options]})
      end
    end

    def itunes options
      begin
        unless Settings.config[:platform] =~ /darwin/
          @status.error_only_osx
          exit
        end
        @status.fetching_from('iTunes')
        itunes = get_itunes_track_infos()
        itunes.each do |el|
          if el.length == 0
            @status.empty_fields
            exit
          end
        end
        post_itunes(options, itunes)
      rescue => e
        @status.wtf
        Errors.global_error({error: e, caller: caller, data: [itunes, store, options]})
      end
    end

    private

    def post_itunes options, itunes
      @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)
    end

    def get_lastfm_track_infos user
      begin
        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
        @status.canceled
        exit
      end
    end

    def post_nowplaying text_to_post, store, options
      begin
        before = text_to_post
        unless options[:no_url] || store.nil?
          text_to_post += "\n \n[iTunes Store](#{store['link']})"
        end
        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
          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'
        else
          source = 'iTunes'
        end
        dic = {
          options: options,
          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
        }
        resp = poster.post(dic)
        FileOps.save_post(resp) if Settings.options[:backup][:posts]
        @view.show_posted(resp)
      rescue => e
        @status.wtf
        Errors.global_error({error: e, caller: caller, data: [dic, store, options]})
      end<commit_msg>nowplaying deezer annotation<commit_after># encoding: utf-8
module Ayadn

  class NowPlaying

    begin
      require 'rss'
    rescue LoadError => e
      puts "\nAYADN: Error while loading an external resource\n\n"
      puts "RUBY: #{e}\n\n"
      exit
    end

    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
      @affiliate_suffix = "&at=1l3vtb8&ct=ayadn"
      @deezer_auth_url = "https://connect.deezer.com/oauth/auth.php?app_id=150971&redirect_uri=http://aya.io/ayadn/deezer.html&response_type=token&perms=basic_access,listening_history,offline_access"
    end

    def deezer options
      begin
        @deezer_code = if Settings.options[:nowplaying][:deezer].nil?
            create_deezer_user()
          else
            if Settings.options[:nowplaying][:deezer][:code].nil?
              create_deezer_user()
            else
              Settings.options[:nowplaying][:deezer][:code]
            end
          end
        @deezer_user_url = "http://api.deezer.com/user/me"
        @deezer_token_suffix = "?access_token=#{<PASSWORD>}"
        @status.fetching_from('Deezer')
        req = "#{@deezer_user_url}/history#{@deezer_token_suffix}"
        res = JSON.parse(CNX.download(req))
        res["data"].sort_by! { |obj| obj["timestamp"] }
        candidate = res["data"].last
        maker = Struct.new(:artist, :album, :track)
        itunes = maker.new(candidate["artist"]["name"], candidate["album"]["title"], candidate["title"])
        post_itunes(options, itunes)
      rescue => e
        @status.wtf
        Errors.global_error({error: e, caller: caller, data: [store, options]})
      end
    end

    def lastfm options
      begin
        user = Settings.options[:nowplaying][:lastfm] || create_lastfm_user()
        @status.fetching_from('Last.fm')
        artist, track = get_lastfm_track_infos(user)
        @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
        @status.wtf
        Errors.global_error({error: e, caller: caller, data: [store, options]})
      end
    end

    def itunes options
      begin
        unless Settings.config[:platform] =~ /darwin/
          @status.error_only_osx
          exit
        end
        @status.fetching_from('iTunes')
        itunes = get_itunes_track_infos()
        itunes.each do |el|
          if el.length == 0
            @status.empty_fields
            exit
          end
        end
        post_itunes(options, itunes)
      rescue => e
        @status.wtf
        Errors.global_error({error: e, caller: caller, data: [itunes, store, options]})
      end
    end

    private

    def post_itunes options, itunes
      @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)
    end

    def get_lastfm_track_infos user
      begin
        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
        @status.canceled
        exit
      end
    end

    def post_nowplaying text_to_post, store, options
      begin
        before = text_to_post
        unless options[:no_url] || store.nil?
          text_to_post += "\n \n[iTunes Store](#{store['link']})"
        end
        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
          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
        source = if options[:lastfm]
          'Last.fm'
        elsif options[:deezer]
          'Deezer'
        else
          'iTunes'
        end
        dic = {
          options: options,
          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
        }
        resp = poster.post(dic)
        FileOps.save_post(resp) if Settings.options[:backup][:posts]
        @view.show_posted(resp)
      rescue => e
        @status.wtf
        Errors.global_error({error: e, caller: caller, data: [dic, store, options]})
      end
{'fraction_non_alphanumeric': 0.1262348178137652, 'fraction_numerical': 0.00631578947368421, 'mean_word_length': 2.1001506024096384, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 14, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5074489', 'n_tokens_mistral': 3999, 'n_tokens_neox': 3910, 'n_words': 779}