Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
import argparse
import sqlite3 as lite
import os
from collections import defaultdict
def _open_db(db):
con = lite.connect(db)
cur = con.cursor()
return (cur,con)
except lite.Error as e:
print("Could not open database %s: %s" % (db,e))
def detect_missing_artwork(db, movie_folder, arts):
""" detect missing fanart
lookup all movies from a folder in video db
and report if any artwork from 'arts' is missing
db: path to 'MyVideos**.db' (XBMC video library)
movie_folder: path to directory with movies in separate folders
arts: list of missing art to report, i.e., ['fanart', 'poster']
flaws: dictionary of flaws.
key: movie name
value: list of missing artwork
(cur,con) = _open_db(db)
flaws = defaultdict(list)
movies = os.listdir(movie_folder)
# iterate over all subdirectories of movies, i.e., over all movies on disk
for i, movie in enumerate(movies):
# show progress
print "\r{0:>3}/{1:<3} {2:<100}:".format(i, len(movies), movie),
path = os.path.join(movie_folder, movie)
sql = """
SELECT type FROM art WHERE media_id=(
SELECT idMovie FROM movie WHERE idFile=(
SELECT idFile FROM files WHERE idPath=(
SELECT idPath FROM path WHERE strPath=?
AND media_type='movie';
if os.path.exists(os.path.join(path,'VIDEO_TS')):
strPath = path + "\\VIDEO_TS\\"
strPath = path + "\\"
cur.execute(sql, (strPath,))
present = [r[0] for r in cur.fetchall()]
for type_ in arts:
if type_ not in present:
# clean progress line
print "\r" + 100*" " + "\r",
return flaws
if __name__=="__main__":
parser = argparse.ArgumentParser(description='find flaws in XBMC video database')
parser.add_argument('db', help='path to myvideos**.db library')
parser.add_argument('movie_folder', help='path to movie folder')
parser.add_argument('-a', '--artwork', help='one or more artwork to report is missing',
nargs='+', default=['fanart', 'poster'])
args = parser.parse_args()
flaws = detect_missing_artwork(args.db, args.movie_folder, args.artwork)
for movie, flaw in sorted(flaws.items()):
print("{0} missing {1} ".format(movie, ' and '.join(flaw)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment