Guest User

Untitled

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