<commit_before>require 'open-uri'

module UsersHelper

  def user_display_name user
    first = user.try(:first_name)
    last = user.try(:last_name)
    str = first.to_s + last.to_s
    if first && last
      [first, last].join(' ')
    elsif !first && !last
      # User has not filled in their profile
      user.email.split('@').first
    else
      str
    end
  end


  def link_to_youtube_button(origin_url)
    link_to raw('<i class="fa fa-large fa-youtube"></i> Sync with YouTube'),
            "/auth/gplus/?youtube=true&origin=#{origin_url}", class: "btn btn-danger btn-lg", type: "button"
  end

  def unlink_from_youtube_button(origin_url)
    link_to raw('<i class="fa fa-large fa-youtube"></i> Disconnect YouTube'),
            "/auth/destroy/0?youtube=true&origin=#{origin_url}", class: "btn btn-danger btn-lg", type: "button"
  end

  def video_link(video)
    link_to video[:title], video[:url], id: video[:id], class: 'yt_link', data: { content: video[:content] }
  end

  def video_embed_link(video)
    "http://www.youtube.com/embed/#{video[:id]}?enablejsapi=1"
  end

end

#TODO YA move to a separate helper module
module Youtube
  class << self

    def user_videos(user)
      if user_id = user.youtube_id
        parse_response(open("http://gdata.youtube.com/feeds/api/users/#{user_id}/uploads?alt=json").read, user)
      end
    end

    def project_videos(user)
      parse_response(open("https://www.googleapis.com/youtube/v3/search?part=snippet&channelId=#{user_id}&order=date&q=#{search_term}&type=video&key=#{api_key}").read)
    end

    def parse_response(response, user)
      begin
        json = JSON.parse(response)
        videos = json['feed']['entry']
        videos.map do |hash|
          {
              id:         hash['id']['$t'].split('/').last,
              published:  hash['published']['$t'].to_date,
              title:      hash['title']['$t'],
              content:    hash['content']['$t'],
              url:        hash['link'].first['href'],
              thumbs:     hash['media$group']['media$thumbnail'],
              player_url: hash['media$group']['media$player'].first['url'],
              user: user
          }
        end
      rescue JSON::JSONError
        Rails.logger.warn('Attempted to decode invalid JSON')
        nil
      end
    end

    def channel_id(token)
      # API v3
      json = JSON.load(open("https://www.googleapis.com/youtube/v3/channels?access_token=#{token}&part=id&mine=true").read)
      json['items'].first['id']
    end<commit_msg>commented out code that will not be used for now in users_helper.rb<commit_after>require 'open-uri'

module UsersHelper

  def user_display_name user
    first = user.try(:first_name)
    last = user.try(:last_name)
    str = first.to_s + last.to_s
    if first && last
      [first, last].join(' ')
    elsif !first && !last
      # User has not filled in their profile
      user.email.split('@').first
    else
      str
    end
  end


  def link_to_youtube_button(origin_url)
    link_to raw('<i class="fa fa-large fa-youtube"></i> Sync with YouTube'),
            "/auth/gplus/?youtube=true&origin=#{origin_url}", class: "btn btn-danger btn-lg", type: "button"
  end

  def unlink_from_youtube_button(origin_url)
    link_to raw('<i class="fa fa-large fa-youtube"></i> Disconnect YouTube'),
            "/auth/destroy/0?youtube=true&origin=#{origin_url}", class: "btn btn-danger btn-lg", type: "button"
  end

  def video_link(video)
    link_to video[:title], video[:url], id: video[:id], class: 'yt_link', data: { content: video[:content] }
  end

  def video_embed_link(video)
    "http://www.youtube.com/embed/#{video[:id]}?enablejsapi=1"
  end

end

#TODO YA move to a separate helper module
module Youtube
  class << self

    def user_videos(user)
      if user_id = user.youtube_id
        parse_response(open("http://gdata.youtube.com/feeds/api/users/#{user_id}/uploads?alt=json").read, user)
      end
    end

    #def project_videos(user)
    #  parse_response(open("https://www.googleapis.com/youtube/v3/search?part=snippet&channelId=#{user_id}&order=date&q=#{search_term}&type=video&key=#{api_key}").read)
    #end

    def parse_response(response, user)
      begin
        json = JSON.parse(response)
        videos = json['feed']['entry']
        return if videos.nil?
        videos.map do |hash|
          {
              id:         hash['id']['$t'].split('/').last,
              published:  hash['published']['$t'].to_date,
              title:      hash['title']['$t'],
              content:    hash['content']['$t'],
              url:        hash['link'].first['href'],
              thumbs:     hash['media$group']['media$thumbnail'],
              player_url: hash['media$group']['media$player'].first['url'],
              user: user
          }
        end
      rescue JSON::JSONError
        Rails.logger.warn('Attempted to decode invalid JSON')
        nil
      end
    end

    def channel_id(token)
      # API v3
      json = JSON.load(open("https://www.googleapis.com/youtube/v3/channels?access_token=#{token}&part=id&mine=true").read)
      json['items'].first['id']
    end
{'fraction_non_alphanumeric': 0.1541448262473306, 'fraction_numerical': 0.0019413706076490001, 'mean_word_length': 2.752367079388201, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 11, 'https://': 4, 'lorem ipsum': 0, 'www.': 6, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4523964', 'n_tokens_mistral': 1671, 'n_tokens_neox': 1634, 'n_words': 329}