Class: Aural::Grabber

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

Class Method Summary collapse

Class Method Details

.announce(s) ⇒ Object

debug function



58
59
60
61
62
# File 'lib/aural.rb', line 58

def announce(s)
  puts "*"*100
  puts s
  puts "*"*100
end

.grab(url) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/aural.rb', line 21

def grab(url)
  agent = Mechanize.new
  page = agent.get url
  title = page.title.strip.split("\n").first
  filename = "#{title}.mp3"

  regex = /,url=(http%3A%2F%2F.*?\.youtube.com%2Fvideoplayback%3Fsparams.*?id%3D.*?)\\u0026/

  page.content.scan(regex) do |m|
    m = m.first
    next unless m =~ /algorithm/
    next if @written

    puts "looking for your video. this might take a minute or two."
    response = agent.get(CGI.unescape(m))
    if response.is_a?(Mechanize::File) && response.header["content-type"] == "video/x-flv"
      puts "ok found it. creating the mp3 now."
      temp_file do |file|
        file.write(response.content)
        transcode(file.path, filename)
      end
      exit
    end
  end
end

.temp_fileObject



11
12
13
14
15
16
17
18
19
# File 'lib/aural.rb', line 11

def temp_file
  file = Tempfile.new('foo')
  begin
    yield file
  ensure
    file.close
    file.unlink
  end
end

.transcode(path, filename) ⇒ Object



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

def transcode(path,filename)
  cmd = "ffmpeg -i \"#{path}\" -acodec libmp3lame -ac 2 -ab 128K \"#{filename}\""
  announce cmd
  if system cmd
    puts "done! enjoy your shiny new mp3: #{filename}"
  else
    puts "shit. something broke. ask ian to fix this."
  end
end