Guest User

Untitled

a guest
May 27th, 2018
183
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.24 KB | None | 0 0
  1. #!/usr/bin/env ruby
  2.  
  3. require 'rubygems'
  4. require 'hpricot'
  5. require 'cgi'
  6.  
  7. class Track
  8. attr_accessor :name
  9. attr_accessor :album
  10. attr_accessor :artist
  11. attr_accessor :location
  12.  
  13. ITUNES_ROOT = %r[(.*iTunes2|.*iTunes Music)]
  14. XBOX_ROOT = 'smb://xbox:xbox@10.0.1.65/media/Audio/iTunes'
  15.  
  16. def initialize(hpr_doc)
  17. parse_xml hpr_doc.to_s
  18. end
  19.  
  20. def to_m3u(index)
  21. return nil unless location
  22. rtn = "#EXTINF:0,%02d. %s\n" % [ index, name ]
  23. rtn << remap_location
  24. rtn
  25. end
  26.  
  27. private
  28.  
  29. def parse_xml(xml)
  30. lines = xml.split(/\n/)
  31. lines.each do |line|
  32. case line
  33. when />Name</ then @name = get_string line
  34. when />Album</ then @album = get_string line
  35. when />Artist</ then @artist = get_string line
  36. when />Location</ then @location = get_string line
  37. end
  38. end
  39. end
  40.  
  41. def get_string(xml_string)
  42. xml_string.match(/string>(.*)<.string/)[1]
  43. end
  44.  
  45. def remap_location
  46. location.sub! ITUNES_ROOT, XBOX_ROOT
  47. CGI.unescape location
  48. end
  49.  
  50. end
  51.  
  52. doc = Hpricot File.read(ARGV[0])
  53. tracks = doc/"//dict/dict"
  54.  
  55. puts '#EXTM3U'
  56.  
  57. tracks.each_with_index do |track, i|
  58. t = Track.new(track)
  59. if line = t.to_m3u(i)
  60. puts line
  61. end
  62. end
Add Comment
Please, Sign In to add comment