Class: YoutubeCrawler::Search

Inherits:
Object
  • Object
show all
Defined in:
lib/youtube/crawler.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts) ⇒ Search

Returns a new instance of Search.



13
14
15
16
17
18
# File 'lib/youtube/crawler.rb', line 13

def initialize(opts)
  @options = opts
  @options[:search] = opts[:search] || 'default search'
  @options[:max_results] = opts[:max_results] || 5
  @options[:player] = opts[:player] || 'vlc'
end

Instance Attribute Details

#clientObject (readonly)

Returns the value of attribute client.



11
12
13
# File 'lib/youtube/crawler.rb', line 11

def client
  @client
end

#optionsObject (readonly)

Returns the value of attribute options.



11
12
13
# File 'lib/youtube/crawler.rb', line 11

def options
  @options
end

Instance Method Details

#authObject



38
39
40
41
42
43
44
# File 'lib/youtube/crawler.rb', line 38

def auth
  # Change to single acc, pw prompting and save to file
  @client = YouTubeIt::Client.new(
      :username => KEYS['username'],
      :password => KEYS['password'],
      :dev_key => KEYS['key'])
end

#launch_player(player = 'vlc') ⇒ Object



51
52
53
# File 'lib/youtube/crawler.rb', line 51

def launch_player(player = 'vlc')
  system("#{player} #{@hash[@num.to_i]}")
end

#query(query, max_results = 5) ⇒ Object



27
28
29
30
31
32
33
34
35
36
# File 'lib/youtube/crawler.rb', line 27

def query(query, max_results = 5)
  @number = 0
  @hash = Hash.new
  @result = @client.videos_by(:query => query, :max_results => max_results)
  @result.videos.each do |x|
    @number += 1
    puts "#{@number}: #{x.title}"
    @hash[@number] = x.player_url
  end
end

#runObject



20
21
22
23
24
25
# File 'lib/youtube/crawler.rb', line 20

def run
  auth
  query(@options[:search], @options[:max_results])
  user_input
  launch_player(options[:player])
end

#user_inputObject



46
47
48
49
# File 'lib/youtube/crawler.rb', line 46

def user_input
  puts 'What video(by number)?: '
  @num = $stdin.gets
end