Briga

FixRhy

Feb 17th, 2018
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.10 KB | None | 0 0
  1. '''
  2. This program is free software: you can redistribute it and/or modify
  3. it under the terms of the GNU General Public License as published by
  4. the Free Software Foundation, either version 3 of the License, or
  5. (at your option) any later version.
  6. This program is distributed in the hope that it will be useful,
  7. but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  9. GNU General Public License for more details.
  10.  
  11. You should have received a copy of the GNU General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. '''
  14. #!/usr/bin/python
  15.  
  16. import sys
  17. import shutil
  18. import xml.etree.ElementTree as ET
  19.  
  20. print "FixRhy 0.3"
  21. reload(sys)
  22. sys.setdefaultencoding( "UTF-8" )
  23.  
  24. #check cli arguments
  25. if len(sys.argv) < 5:
  26.   print "Error too little arguments", len(sys.argv)
  27.   print "Usage: fixrhy artist_name album_name artist_sort_name album_sort_name"
  28.   print "Ex: fixrhy \"David Bowie\" \"The Best of\" \"Bowie, David\" \"2002\""
  29.   print "You can add force at the end to override existing sort information."
  30.   exit()
  31.  
  32. lk_art = sys.argv[1]
  33. lk_alb = sys.argv[2]
  34. sr_art = sys.argv[3]
  35. sr_alb = sys.argv[4]
  36.  
  37. if len(sys.argv) > 5:
  38.   flag = sys.argv[5]
  39. else:
  40.   flag = ""
  41.  
  42. print "Looking for", lk_alb, "by",lk_art
  43. print "Sorting as", sr_alb, "by", sr_art
  44.  
  45. filename = 'rhythmdb.xml'
  46. backup = filename + ".backup"
  47. tree = ET.parse(filename)
  48. root = tree.getroot()
  49. count = 0
  50. ignored = 0
  51.  
  52. for entry in root:
  53.   if entry.attrib == {'type': 'song'}:
  54.     if entry.find('artist').text == lk_art:
  55.       if entry.find('album').text == lk_alb:
  56.         count += 1
  57.         # check if sort is not already there
  58.         f_art = entry.find('mb-artistsortname')
  59.         if f_art == None:
  60.           se = ET.SubElement(entry, 'mb-artistsortname')
  61.           se.text = sr_art
  62.           print "Set sort artist",sr_art
  63.         else:
  64.           if f_art.text == sr_art:
  65.             print "Sort artist already OK"
  66.           else:
  67.             if flag == "force":
  68.               entry.remove(f_art)
  69.               se = ET.SubElement(entry, 'mb-artistsortname')
  70.               se.text = sr_art
  71.               print "Sort artist different, force changed"
  72.             else:
  73.               print "Sort artist different, left unchanged"
  74.         f_alb = entry.find('album-sortname')
  75.         if f_alb == None:
  76.           se = ET.SubElement(entry, 'album-sortname')
  77.           se.text = sr_alb
  78.           print "Set sort album",sr_alb
  79.         else:
  80.           if f_alb.text == sr_alb:
  81.             print "Sort album already OK"
  82.           else:
  83.             if flag == "force":
  84.               entry.remove(f_alb)
  85.               se = ET.SubElement(entry, 'album-sortname')
  86.               se.text = sr_alb
  87.               print "Sort album different, force changed"
  88.             else:
  89.               print "Sort album different, left unchanged"
  90.       else:
  91.         ignored += 1
  92.         # print "Ignore"
  93.  
  94. #backup file
  95. shutil.copy2(filename,backup)
  96.  
  97. #write modified one
  98. tree.write(filename, xml_declaration=True)
  99.  
  100. print "Found",count,"entries."
Add Comment
Please, Sign In to add comment