Skip to content

Instantly share code, notes, and snippets.

@dzen
Created September 5, 2010 15:59
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dzen/566110 to your computer and use it in GitHub Desktop.
Save dzen/566110 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
"""
arrange your films (with a name like "american_history_x.avi") on your filesystem:
BASEDIR/
./date/1998/american_history_x.avi (symlink)
./genre/drama/american_history_x.avi (symlink)
./genre/crime/american_history_x.avi (symlink)
./actor/edward furlong/american_history_x.avi (symlink)
./actor/edward norton/american_history_x.avi (symlink)
./actor/beverly d'angelo/american_history_x.avi (symlink)
./all/american_history_x.avi (real file)
note: I hate getopt, so it's un finished.
example:
$> ./rename.py --bulk w.avi waist_deep.avi wanted.avi waterworld.avi wild_wild_west.avi
depends:
imdb.py
"""
import sys
import os
import shutil
import getopt
import time
import unicodedata
import imdb
VERBOSE=True
DRYRUN=False
BASEDIR="/stuff/video/films/"
def strip_accents(s):
return ''.join((c for c in unicodedata.normalize('NFD', s) if unicodedata.category(c) != 'Mn'))
class Film(object):
def __init__(self, old_name, new_name):
self.old_name = old_name
self.new_name = new_name
self.genres = None
self.actors = None
self.year = None
def guess_film(old_name, new_name):
ia = imdb.IMDb()
filmname = new_name.replace('_', ' ').rstrip('.avi')
result = ia.search_movie(filmname)[0]
ia.update(result)
film = Film(old_name, new_name)
film.genres = ["genre/" + genre.lower() for genre in result['genres']]
film.actors = ["actor/" + actor['name'].lower() for actor in result['cast'][:3]]
film.year = "date/%d" % (result['year'])
return film
def fs_op(film):
newfname = BASEDIR + os.sep + "all"
if not os.path.exists(newfname):
print "os.mkdir(%s)" % newfname
if not DRYRUN:
os.mkdir(newfname)
newfname = newfname + os.sep + film.new_name
if not DRYRUN:
shutil.move(film.old_name, newfname)
print "shutil.move(%s, %s)" % (film.old_name, newfname)
copylist = list()
copylist.extend(film.genres)
copylist.extend(film.actors)
copylist.append(str(film.year))
for dest in copylist:
base = BASEDIR + os.sep + dest
base = strip_accents(unicode(base))
if not os.path.exists(base):
print "os.mkdir(%s)" % base
if not DRYRUN:
os.mkdir(base)
if not DRYRUN:
os.symlink(newfname, base + os.sep + film.new_name)
print "os.symlink(%s, %s)" % (newfname, base + os.sep + film.new_name)
def bulk(film_list):
for film_name in film_list:
print "film : ", film_name
film = guess_film(film_name, film_name)
fs_op(film)
time.sleep(5)
def usage():
print """
-h --help Show this help
-b --bulk <film, film, film> Tidy a list of films
-v --verbose Enable debug
-d --dry-run Does nothing on the FS
-o --output <film_name> Tidy a film using a new output filename
"""
def main():
optlist, args = getopt.getopt(sys.argv[1:],
'hdbvo:',
["help", "dry-run", "bulk", "verbose", "output="])
for opt, arg in optlist:
if opt == "-v":
VERBOSE = True
elif opt in ("-d", "--dry-run"):
DRYRUN=True
elif opt in ("-h", "--help"):
usage()
sys.exit()
elif opt in ("-b", "--bulk"):
bulk(args)
elif opt in ("-o", "--output"):
new_name = arg
old_name = args[0]
# old_name: MyFuckin'Realease-XViD-blablabla.avi
# new_name: my_fuckin.avi
film = guess_film(old_name, new_name)
else:
assert False, "unhandled option"
# ...
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment