Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- '''
- This program is free software: you can redistribute it and/or modify
- it under the terms of the GNU General Public License as published by
- the Free Software Foundation, either version 3 of the License, or
- (at your option) any later version.
- This program is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- GNU General Public License for more details.
- You should have received a copy of the GNU General Public License
- along with this program. If not, see <http://www.gnu.org/licenses/>.
- '''
- #!/usr/bin/python
- import sys
- import shutil
- import xml.etree.ElementTree as ET
- print "FixRhy 0.3"
- reload(sys)
- sys.setdefaultencoding( "UTF-8" )
- #check cli arguments
- if len(sys.argv) < 5:
- print "Error too little arguments", len(sys.argv)
- print "Usage: fixrhy artist_name album_name artist_sort_name album_sort_name"
- print "Ex: fixrhy \"David Bowie\" \"The Best of\" \"Bowie, David\" \"2002\""
- print "You can add force at the end to override existing sort information."
- exit()
- lk_art = sys.argv[1]
- lk_alb = sys.argv[2]
- sr_art = sys.argv[3]
- sr_alb = sys.argv[4]
- if len(sys.argv) > 5:
- flag = sys.argv[5]
- else:
- flag = ""
- print "Looking for", lk_alb, "by",lk_art
- print "Sorting as", sr_alb, "by", sr_art
- filename = 'rhythmdb.xml'
- backup = filename + ".backup"
- tree = ET.parse(filename)
- root = tree.getroot()
- count = 0
- ignored = 0
- for entry in root:
- if entry.attrib == {'type': 'song'}:
- if entry.find('artist').text == lk_art:
- if entry.find('album').text == lk_alb:
- count += 1
- # check if sort is not already there
- f_art = entry.find('mb-artistsortname')
- if f_art == None:
- se = ET.SubElement(entry, 'mb-artistsortname')
- se.text = sr_art
- print "Set sort artist",sr_art
- else:
- if f_art.text == sr_art:
- print "Sort artist already OK"
- else:
- if flag == "force":
- entry.remove(f_art)
- se = ET.SubElement(entry, 'mb-artistsortname')
- se.text = sr_art
- print "Sort artist different, force changed"
- else:
- print "Sort artist different, left unchanged"
- f_alb = entry.find('album-sortname')
- if f_alb == None:
- se = ET.SubElement(entry, 'album-sortname')
- se.text = sr_alb
- print "Set sort album",sr_alb
- else:
- if f_alb.text == sr_alb:
- print "Sort album already OK"
- else:
- if flag == "force":
- entry.remove(f_alb)
- se = ET.SubElement(entry, 'album-sortname')
- se.text = sr_alb
- print "Sort album different, force changed"
- else:
- print "Sort album different, left unchanged"
- else:
- ignored += 1
- # print "Ignore"
- #backup file
- shutil.copy2(filename,backup)
- #write modified one
- tree.write(filename, xml_declaration=True)
- print "Found",count,"entries."
Add Comment
Please, Sign In to add comment