|
#!/usr/bin/env python |
|
''' |
|
File: tagshow.py |
|
Author: George Ang <[email protected]> |
|
Description: |
|
''' |
|
|
|
import os |
|
import sys |
|
import json |
|
import urllib2 |
|
import struct |
|
from urllib import urlencode |
|
from mutagen.mp4 import Open |
|
|
|
def rest_meta(filename): |
|
response = urllib2.urlopen('http://allshows.tv/showtags.json', |
|
data=urlencode(dict(filename=filename.encode('utf-8')))) |
|
return json.load(response) |
|
|
|
def main(): |
|
convert = False |
|
argv = sys.argv[1:] |
|
if argv: |
|
if argv[0] == '-t': |
|
convert = True |
|
argv = argv[1:] |
|
elif argv[0] == '-h': |
|
print >> sys.stderr, 'usage: tagshow [-t|-h] [dir1, dir2, ...]' |
|
|
|
if not argv: |
|
argv = ['.', ] |
|
|
|
for path in argv: |
|
path = os.path.abspath(path) |
|
for dirpath, _, files in os.walk(path): |
|
for fname in files: |
|
if not fname.lower().endswith('.mp4'): |
|
continue |
|
fname = fname.decode('utf-8') |
|
print fname, |
|
ep = rest_meta(fname) |
|
print ','.join(['%s=%s' % (k,v) for k,v in ep.iteritems()]) or '<NOT FOUND>' |
|
#continue |
|
|
|
if ep and convert: |
|
fullpath = os.path.join(dirpath, fname) |
|
track = Open(fullpath) |
|
track['trkn'] = [(ep['episodenumber'], 0), ] |
|
track['disk'] = [(1, 1), ] |
|
track['tves'] = struct.pack('>I', ep['episodenumber']) |
|
track['tvsn'] = struct.pack('>I', ep['seasonnumber']) |
|
track['tven'] = 'S%02dE%02d' % (int(ep['seasonnumber']), int(ep['episodenumber'])) |
|
track['tvsh'] = ep['showname'] |
|
track['aART'] = ep['showname'] |
|
track['\xa9ART'] = ep['showname'] |
|
track['\xa9alb'] = ep['showname'] + ', Season %s' % ep['seasonnumber'] |
|
track['\xa9nam'] = ep['episodename'] or 'S%02dE%02d' % (ep['seasonnumber'], ep['episodenumber']) |
|
print 'tagging: %s ...' % fname |
|
track.save() |
|
|
|
if __name__ == '__main__': |
|
main() |