Guest User

Untitled

a guest
Jul 17th, 2018
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.96 KB | None | 0 0
  1. task :default do
  2. blogs = Blog.all
  3. blogs.each do |blog|
  4. scrape = Hpricot(open(blog.url))
  5. scrape.search('//a[@href$=.mp3]').each do |link|
  6. url = link.attributes['href'].scan(/http:\/\/(?:www\.)?([^\/]+?)(\/.*?\.mp3)/) do |host, path|
  7. request = "GET #{path} HTTP/1.0\r\n\r\n"
  8. socket = TCPSocket.open(host,80)
  9. socket.print(request)
  10.  
  11. # find beginning of response body
  12. buffer = ""
  13. while !buffer.match("\r\n\r\n") do
  14. buffer += socket.read(1)
  15. end
  16.  
  17. @id3_tags = socket.read(100) #read first 100 bytes of body
  18.  
  19. Mp3Info.open(@id3_tags) do |mp3Info|
  20. @artist = mp3Info.tag.artist
  21. @title = mp3Info.tag.title
  22. end
  23.  
  24. end
  25.  
  26. @mp3 = Mp3File.new(:url => link.attributes['href'], :file_name => link.attributes['href'].split('/').last, :band => @artist, :title => @title)
  27. @mp3.save
  28. end
  29. end
  30. end
Add Comment
Please, Sign In to add comment