Class: YTAnalytics::YTAuth::Authentication

Inherits:
Object
  • Object
show all
Includes:
Logging
Defined in:
lib/yt_analytics/request/authentication.rb

Instance Method Summary collapse

Methods included from Logging

#logger

Constructor Details

#initialize(*params) ⇒ Authentication

Returns a new instance of Authentication.



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/yt_analytics/request/authentication.rb', line 11

def initialize *params
  if params.first.is_a?(Hash)
    hash_options = params.first
    @user                          = hash_options[:username]
    @password                      = hash_options[:password]
    @dev_key                       = hash_options[:dev_key]
    @access_token                  = hash_options[:access_token]
    @authsub_token                 = hash_options[:authsub_token]
    @client_id                     = hash_options[:client_id] || "youtube_it"
    @config_token                  = hash_options[:config_token]
  else
    puts "* warning: the method YTAnalytics::Auth::Authentication.new(username, password, dev_key) is deprecated, use YouTubeIt::Upload::VideoUpload.new(:username => 'user', :password => 'passwd', :dev_key => 'dev_key')"
    @user                          = params.shift
    @password                      = params.shift
    @dev_key                       = params.shift
    @access_token                  = params.shift
    @authsub_token                 = params.shift
    @client_id                     = params.shift || "youtube_it"
    @config_token                  = params.shift
  end
end

Instance Method Details

#demographic_percentages(user_id, options) ⇒ Object



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/yt_analytics/request/authentication.rb', line 88

def demographic_percentages(user_id, options)        
  opts = {'ids' => "channel==#{user_id}", 'dimensions' => 'ageGroup,gender'}
  opts['start-date'] = (options['start-date'].strftime("%Y-%m-%d") if options['start-date']) || 2.day.ago.strftime("%Y-%m-%d")
  opts['end-date'] = (options['end-date'].strftime("%Y-%m-%d") if options['end-date']) || 2.day.ago.strftime("%Y-%m-%d")
  if options['metrics'].class == Array
    opts['metrics'] = options['metrics'].join(",")
  elsif options['metrics'].class == String
    opts['metrics'] = options['metrics'].delete(" ")
  else
    opts['metrics'] = 'viewerPercentage'
  end
  
  get_url     = "/youtube/analytics/v1/reports?"
  get_url     << opts.collect { |k,p| [k,p].join '=' }.join('&')
  response    = yt_session('https://www.googleapis.com').get(get_url)
  content = JSON.parse(response.body)
  return YTAnalytics::Parser::DemographicParser.new(content).parse
end

#enable_http_debuggingObject



34
35
36
# File 'lib/yt_analytics/request/authentication.rb', line 34

def enable_http_debugging
  @http_debugging = true
end

#get_analytics(opts) ⇒ Object



47
48
49
50
51
52
53
54
55
# File 'lib/yt_analytics/request/authentication.rb', line 47

def get_analytics(opts)
  # max_results = opts[:per_page] || 50
  # start_index = ((opts[:page] || 1) -1) * max_results +1
  get_url     = "/youtube/analytics/v1/reports?"
  get_url     << opts.collect { |k,p| [k,p].join '=' }.join('&')
  response    = yt_session('https://www.googleapis.com').get(get_url)

  return YTAnalytics::Parser::AnalyticsParser.new(response.body).parse
end

#get_current_userObject



40
41
42
43
44
45
# File 'lib/yt_analytics/request/authentication.rb', line 40

def get_current_user
  current_user_url = "/feeds/api/users/default"
  response         = yt_session.get(current_user_url)

  return Nokogiri::XML(response.body).at("entry/author/name").text
end

#temporal_totals(dimension, user_id, options) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/yt_analytics/request/authentication.rb', line 57

def temporal_totals(dimension, user_id, options)
  #dimension is either day, 7DayTotals, 30DayTotals, or month
  opts = {'ids' => "channel==#{user_id}", 'dimensions' => dimension}

  start_date = options['start-date'] || 2.days.ago
  end_date = options['end-date'] || 2.days.ago

  if (dimension == "month")
    start_date = start_date.at_beginning_of_month
    end_date = end_date.at_beginning_of_month
  end

  opts['start-date'] = start_date.strftime("%Y-%m-%d")
  opts['end-date'] = end_date.strftime("%Y-%m-%d")
  opts['filters'] = options['filters']

  if options['metrics'].class == Array
    opts['metrics'] = options['metrics'].join(",")
  elsif options['metrics'].class == String
    opts['metrics'] = options['metrics'].delete(" ")
  else
    opts['metrics'] = 'views,comments,favoritesAdded,favoritesRemoved,likes,dislikes,shares,subscribersGained,subscribersLost,uniques'
  end
  
  get_url     = "/youtube/analytics/v1/reports?"
  get_url     << opts.collect { |k,p| [k,p].join '=' }.join('&')
  response    = yt_session('https://www.googleapis.com').get(get_url)
  content = JSON.parse(response.body)
  return YTAnalytics::Parser::TemporalParser.new(content).parse
end

#watch_time_metrics(dimension, user_id, options) ⇒ Object



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/yt_analytics/request/authentication.rb', line 107

def watch_time_metrics(dimension, user_id, options)
  #dimension is day
  opts = {'ids' => "channel==#{user_id}", 'dimensions' => dimension}
  opts['start-date'] = (options['start-date'].strftime("%Y-%m-%d") if options['start-date']) || 1.day.ago.strftime("%Y-%m-%d")
  opts['end-date'] = (options['end-date'].strftime("%Y-%m-%d") if options['end-date']) || 1.day.ago.strftime("%Y-%m-%d")
  if options['metrics'].class == Array
    opts['metrics'] = options['metrics'].join(",")
  elsif options['metrics'].class == String
    opts['metrics'] = options['metrics'].delete(" ")
  else
    opts['metrics'] = 'estimatedMinutesWatched,averageViewDuration,averageViewPercentage'
  end
  
  get_url     = "/youtube/analytics/v1/reports?"
  get_url     << opts.collect { |k,p| [k,p].join '=' }.join('&')
  response    = yt_session('https://www.googleapis.com').get(get_url)
  content = JSON.parse(response.body)
  return YTAnalytics::Parser::WatchTimeParser.new(content).parse
end