Skip to content

Instantly share code, notes, and snippets.

@aliva
Last active August 29, 2015 13:56
Show Gist options
  • Save aliva/9226768 to your computer and use it in GitHub Desktop.
Save aliva/9226768 to your computer and use it in GitHub Desktop.
sync rhythmboxdb.xml with last.fm info (play count/rating)
#!/usr/bin/python3
import json
import os
from datetime import datetime
from shutil import copy
from urllib.error import HTTPError
from urllib.request import quote
from urllib.request import urlopen
from xml.etree import ElementTree
# you should create a last fm app for api key, and fill your user name here
api_key = ""
username = ""
if api_key == "":
if os.path.exists("api_key"):
api_key = open("api_key").read().strip()
if username == "":
if os.path.exists("username"):
username = open("username").read().strip()
if not username or not api_key:
print ("username or api_key is empty")
exit()
rb_db_file = os.path.join(os.path.expanduser("~"), ".local/share/rhythmbox/rhythmdb.xml")
base_url = "http://ws.audioscrobbler.com/2.0/?method=track.getInfo&api_key=%s&username=%s&artist=%s&track=%s&format=json"
now = datetime.now()
copy(rb_db_file, rb_db_file + "_" + now.strftime("%Y-%m-%d-%H-%M-%S") + ".bak")
tree = ElementTree.parse(rb_db_file)
root = tree.getroot()
counter = 0
total = len(root)
for child in root:
counter += 1
if child.attrib["type"] == "ignore":
continue
if counter % 100 == 0:
tree.write(rb_db_file)
track = child.find("title").text
artist = child.find("artist").text
album = child.find("album").text
try:
play_count = int(child.find("play-count").text)
except AttributeError:
play_count = 0
try:
rating
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment