Advertisement
lagranzotto

app.rb

Oct 7th, 2014
166
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 4.06 KB | None | 0 0
  1. require_relative "dependencies.rb"
  2. require 'active_support/core_ext'
  3.  
  4. $started = false;
  5. $collection = []
  6. $artists = []
  7. $albums = []
  8.  
  9. def getTrackInfoNumeric(line)
  10.     inicio = line.index("<integer>") + 9
  11.     fim = line.index("</i")
  12.     line[inicio..fim].to_i
  13. end
  14.  
  15. def getTrackInfo(line)
  16.     inicio = line.index("<string>") + 8
  17.     fim = line.index("</s") - 1
  18.     line[inicio..fim]
  19. end
  20.  
  21. def getLocation(arr)
  22.     arr[0..arr.length-2].join("/") << "/"
  23. end
  24.  
  25. def getFilename(arr)
  26.     arr[arr.length-1]
  27. end
  28.  
  29. def getCountry(arr)
  30.     arr[arr.length-1].strip!
  31. end
  32.  
  33. def getGrouping(arr)
  34.     arr.each do |t|
  35.         t.strip!
  36.     end
  37.     arr[0..arr.length-2].sort.join(", ")
  38. end
  39.  
  40. def getTime(time)
  41.     seconds = time / 1000
  42.     Time.at(seconds).strftime("%M:%S")
  43. end
  44.  
  45. def getTags(line)
  46.     bkp = line.split(", ")
  47.     retorno = Hash.new
  48.     retorno[:acoustic] = false
  49.     retorno[:another] = false
  50.     retorno[:live] = false
  51.     retorno[:cover] = false
  52.     retorno[:medley] = false
  53.     bkp.each do |t|
  54.         retorno[:acoustic] = true if t == 'Acoustic'
  55.         retorno[:another] = true if t == 'Another'
  56.         retorno[:live] = true if t == 'Live'
  57.         retorno[:cover] = true if t == 'Cover'
  58.         retorno[:medley] = true if t == 'Medley'
  59.     end
  60.     retorno
  61. end
  62.  
  63. File.open("Biblioteca.xml","r:UTF-8").each_line do |line|
  64.     if line.include? "<key>Tracks</key>"
  65.         $started = true;       
  66.     elsif not $started
  67.     elsif line.include? "</dicta>"
  68.         break
  69.     elsif line.include? "<dict>"
  70.         $current = Song.new
  71.     elsif line.include? "<key>Year</key>"
  72.         $current.year = getTrackInfoNumeric(line)
  73.     elsif line.include? "<key>Name</key>"
  74.         $current.title = getTrackInfo(line)
  75.     elsif line.include? "<key>Artist</key>"
  76.         $current.artist = getTrackInfo(line)
  77.     elsif line.include? "<key>Album Artist</key>"
  78.         $current.album_artist = getTrackInfo(line)
  79.     elsif line.include? "<key>Album</key>"
  80.         $current.album = getTrackInfo(line)
  81.     elsif line.include? "<key>Total Time</key>"
  82.         $current.time = getTime(getTrackInfoNumeric(line))
  83.     elsif line.include? "<key>Track Number</key>"
  84.         $current.track_number = getTrackInfoNumeric(line)
  85.     elsif line.include? "<key>Track Count</key>"
  86.         $current.track_count = getTrackInfoNumeric(line)
  87.     elsif line.include? "<key>Location</key>"
  88.         array = URI::decode(getTrackInfo(line)).split("/")
  89.         $current.location = getLocation(array)
  90.         $current.filename = getFilename(array)
  91.     elsif line.include? "<key>Compilation</key>"
  92.         $current.tags[:compilation] = true
  93.     elsif line.include? "<key>Grouping</key>"
  94.         array = getTrackInfo(line).split(",")
  95.         $current.country = getCountry(array)
  96.         $current.grouping = getGrouping(array)
  97.         $current.tags = $current.tags.merge(getTags($current.grouping))
  98.     elsif line.include? "<key>Genre</key>"
  99.         $current.genre = getTrackInfo(line)
  100.     elsif line.include? "<key>Size</key>"
  101.         $current.size = getTrackInfoNumeric(line)
  102.     elsif line.include? "<key>Bit Rate</key>"
  103.         $current.bit_rate = getTrackInfoNumeric(line)
  104.     elsif line.include? "<key>Sample Rate</key>"
  105.         $current.sample_rate = getTrackInfoNumeric(line)
  106.     elsif line.include? "<key>Comments</key>"
  107.         $current.comments = getTrackInfo(line)
  108.     elsif line.include? "<key>Composer</key>"
  109.         $current.composer = getTrackInfo(line)
  110.     elsif line.include? "</dict>"
  111.         $collection << $current
  112.         #break
  113.     end
  114. end
  115.  
  116. #$artists << Artist.new(true)
  117. $collection.each do |song|
  118.     if song.artist == 'Helloween'
  119.         artist = Artist.new(song)
  120.             $artists << artist unless $artists.include? artist
  121.     end
  122. end
  123.  
  124. $collection.each do |song|
  125.     if song.artist == 'Helloween'
  126.         album = Album.new(song)
  127.         puts album
  128.         $albums << album unless $albums.include? album
  129.     end
  130. end
  131.  
  132. $collection.each do |song|
  133.     if song.artist == 'Helloween'
  134.         album = Album.new(song)
  135.         track = Track.new(song)
  136.         index = $albums.find_index(album)
  137.         puts album.to_s
  138.             puts track.to_s
  139.                 puts index.to_s
  140.         $albums.at(index).trackList << track
  141.     end
  142. end
  143.  
  144. #File.open("artists.txt","w:UTF-8") do |f|
  145. #   $artists.each do |a|
  146. #       f.puts(a.to_json)
  147. #   end
  148. #end
  149.  
  150. File.open("albums.txt","w:UTF-8") do |f|
  151.     f.puts($albums.to_json)
  152. #   $albums.each do |a|
  153. #       f.puts(a.to_json)
  154. #   end
  155. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement