jriver-fix-date-imported.py 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. #!/usr/bin/env python3
  2. import sys
  3. import os
  4. from pathlib import PureWindowsPath
  5. in_file = sys.argv[1]
  6. out_file = sys.argv[2]
  7. def getAlbumPath(line):
  8. filename = line.lstrip('<Field Name="Filename">')
  9. filename = filename.rstrip('</Field>\n')
  10. path = PureWindowsPath(filename)
  11. path = path.parents[0]
  12. return str(path)
  13. def getDate(line):
  14. date = line.lstrip('<Field Name="Date Imported">')
  15. date = date.rstrip('</Field>\n')
  16. return int(date)
  17. f = open(in_file, "r")
  18. lines = f.readlines()
  19. f.close()
  20. albums = {}
  21. for lnum,line in enumerate(lines):
  22. if '<Field Name="Filename">' in line:
  23. album = getAlbumPath(line)
  24. elif '<Field Name="Date Imported">' in line:
  25. date = getDate(line)
  26. if album in albums:
  27. albums[album].append((lnum, date))
  28. else:
  29. albums[album] = [(lnum, date)]
  30. earliest = {}
  31. for album in albums:
  32. tracks = albums[album]
  33. earliest_date = min(tracks, key = lambda t: t[1])[1]
  34. for track in tracks:
  35. lines[track[0]] = f'<Field Name="Date Imported">{earliest_date}</Field>\n'
  36. f = open(out_file, 'w')
  37. f_string = "".join(lines)
  38. f.write(f_string)
  39. f.close()
  40. exit()