Skip to content

Instantly share code, notes, and snippets.

@kmr
Created April 1, 2011 14:48
Show Gist options
  • Save kmr/898258 to your computer and use it in GitHub Desktop.
Save kmr/898258 to your computer and use it in GitHub Desktop.
The simple movie scanner for Plex Nine
import re, os, os.path
import Media, VideoFiles, Stack
from mp4file import mp4file, atomsearch
# Look for episodes.
def Scan(path, files, mediaList, subdirs):
# Scan for video files.
VideoFiles.Scan(path, files, mediaList, subdirs)
anonep = dict()
for i in files:
done = False
file = os.path.basename(i)
(file, ext) = os.path.splitext(file)
if ext.lower() in ['.mp4', '.m4v', '.mov']:
m4season = m4ep = m4year = 0
m4show = title = ''
try:
mp4fileTags = mp4file.Mp4File(i)
# Show.
try: m4show = find_data(mp4fileTags, 'moov/udta/meta/ilst/album').encode('utf-8')
except: m4show = 'other'
# Season.
m4season = 1
# Episode.
m4ep = None
try:
# tracknum (can be 101)
m4ep = int(find_data(mp4fileTags, 'moov/udta/meta/ilst/tracknum'))
except:
if anonep.has_key(m4show):
m4ep = anonep[m4show] + 1
else:
m4ep = 101
anonep[m4show] = m4ep
# Title.
try: title = find_data(mp4fileTags, 'moov/udta/meta/ilst/title').encode('utf-8')
except: pass
# Year.
m4year = 1999
# If we have all the data we need, add it.
if len(m4show) > 0:
tv_show = Media.Episode(m4show, m4season, m4ep, title, m4year)
tv_show.parts.append(i)
mediaList.append(tv_show)
continue
except:
pass
# Stack the results.
Stack.Scan(path, files, mediaList, subdirs)
def find_data(atom, name):
child = atomsearch.find_path(atom, name)
data_atom = child.find('data')
if data_atom and 'data' in data_atom.attrs:
return data_atom.attrs['data']
import sys
if __name__ == '__main__':
print "Hello, world!"
path = sys.argv[1]
files = [os.path.join(path, file) for file in os.listdir(path)]
media = []
Scan(path[1:], files, media, [])
print "Media:", media
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment