# coding:utf-8
import os, json, re, sys, datetime
import Media, VideoFiles, Stack, Utils
reload(sys)
sys.setdefaultencoding('utf-8')
recordedList = None
titleCount = {}
log = open('/tmp/ChinachuScanner.log', 'a')
def Scan(path, files, mediaList, subdirs):
global log
VideoFiles.Scan(path, files, mediaList, subdirs)
files.sort()
if len(files) >= 1:
for file in files:
log.write("file: " + file + "\n")
filename = os.path.splitext(os.path.basename(file))[0]
title = None
episode = None
release = None
titleNormalizeRegexp = u'(\[.+?\]|【.+?】|(.+?)|\(.+?\)|「.+?」)'
titleRegexp = u'([^\[\]0-9# \(\)【】「」]{2,})'
episodeRegexp = u'(^[ ]+|\[[^\]]+\]|【[^】]+】|(字)|\(字\)|(二)|\(二\))'
mts = re.search(r'\[((?:cs|bs|gr)[0-9]+-[a-z0-9]+)\]', filename)
if mts:
log.write(" id: " + mts.groups()[0] + "\n")
recorded = get_recorded(mts.groups()[0])
if recorded:
log.write(" json found.\n")
#print json.dumps(recorded, indent=2)
mts = re.search(titleRegexp, re.sub(titleNormalizeRegexp, '', recorded['title']))
title = mts.groups()[0]
episode = re.sub(episodeRegexp, '', recorded['title'].replace(title, ''))
release = datetime.datetime.fromtimestamp(recorded['start'] / 1000).strftime("%Y-%m-%d")
if title is None:
mts = re.search(titleRegexp, re.sub(titleNormalizeRegexp, '', filename))
title = mts.groups()[0]
episode = re.sub(episodeRegexp, '', filename.replace(title, ''))
if os.path.basename(os.path.dirname(file)) in title:
title = os.path.basename(os.path.dirname(file))
episode = re.sub(u'[#]', '', episode)
if release is None:
mts = re.search('\[(?:20)?(\d\d)-?(\d\d)-?(\d\d)(?:-\d\d:?\d\d(?::?\d\d)?)?\]', filename)
if mts:
release = "20" + mts.groups()[0] + "-" + mts.groups()[1] + "-" + mts.groups()[2]
else:
release = datetime.datetime.fromtimestamp(os.stat(file).st_mtime).strftime("%Y-%m-%d")
if title not in titleCount:
titleCount[title] = 0
titleCount[title] = titleCount[title] + 1
log.write(' title :' + title.encode('utf-8') + "\n")
log.write(' episode: [' + str(titleCount[title]) + '] ' + episode.encode('utf-8') + "\n")
log.write(' release: ' + release + "\n")
name, year = VideoFiles.CleanName(title.encode('utf-8'))
tv_show = Media.Episode(name, None, None, episode.encode('utf-8'), year)
tv_show.released_at = release
tv_show.parts.append(file)
mediaList.append(tv_show)
def get_recorded(id):
global recordedList
if recordedList is None:
with open('/home/haoling/chinachu/data/recorded.json', 'r') as f:
recordedList = json.loads(f.read(), 'utf-8')
for rec in recordedList:
if rec['id'] == id:
return rec