Skip to content

Instantly share code, notes, and snippets.

@Fedjmike
Created July 26, 2016 23:47
Show Gist options
  • Save Fedjmike/9478627d8dd16cd2496eaacdc3b649c1 to your computer and use it in GitHub Desktop.
Save Fedjmike/9478627d8dd16cd2496eaacdc3b649c1 to your computer and use it in GitHub Desktop.
Fix release cover arts whose canonical URL has changed
import requests
from multiprocessing import Pool
from model import Model, NotFound
from mb_api_import import get_album_art_urls
def safe_print(*args):
try:
print(*args)
except:
pass
def is_url_valid_and_live(url):
try:
return requests.head(url).status_code == 200
except requests.RequestException:
return False
def fix_release_art(mbid, release_id, title, full, thumb, n):
if n % 50 == 0:
print(n)
if not mbid:
safe_print("No mbid:", title)
return
if not (full and thumb):
pass#return
if all(is_url_valid_and_live(url) for url in [full, thumb]):
return
safe_print("Getting art for ", title)
full, thumb = get_album_art_urls(mbid, group=False)
print(full, thumb)
with Model() as model:
model.execute("update releases set full_art_url=?, thumb_art_url=?"
"where id=?", full, thumb, release_id)
if __name__ == '__main__':
with Model() as model:
with Pool(processes=100) as pool:
done = 0
for release_id, title, full, thumb \
in model.query("select id, title, full_art_url, thumb_art_url from releases"):
mbid = model.get_link(release_id, "musicbrainz")
pool.apply_async(fix_release_art, (mbid, release_id, title, full, thumb, done))
done += 1
pool.close()
pool.join()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment