@haoling haoling / ChinachuScanner.py
Created at Sun Jan 14 12:48:13 GMT 2018
ChinachuScanner.py
ChinachuScanner.py
Raw
# 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

Fei-YenScanner.py
Raw
import re, os, os.path, datetime
import Media, VideoFiles, Stack, Utils

def Scan(path, files, mediaList, subdirs):
  files.sort()
  if len(files) >= 1:

    # Iterate through all the files
    for i,file in enumerate(files):

      # Remove path and extension
      tsfile = os.path.basename(file)
      tsfile = os.path.splitext(tsfile)[0]

      # Enigma2 files look like: YYYYMMDD HHMM - Channel - Programmename.ts
      #(date_time,channel,programme) = tsfile.split(' - ',2)
      #(date,time) = date_time.split(' ')

      #year = int(date[0:4])
      #month = int(date[4:6])
      #day = int(date[6:8])
      #hour = int(time[0:2])
      #minute = int(time[2:4])
      #d = datetime.datetime(year, month, day, hour, minute)
      #t = d.timetuple()

      show = os.path.basename(os.path.dirname(file))

      tsfile = tsfile.replace(show, "")

      #title = channel+" - "+date[6:8]+"/"+date[4:6]+"/"+date[0:4]+" "+time[0:2]+":"+time[2:4]
      title = tsfile

      paths = Utils.SplitPath(path)
      season = None
      if len(paths) == 2:
        show = paths[0]
        match = re.search('(?P<season>[0-9]+)', paths[1])
        if match:
          season = match.group('season')
        else:
          title = paths[1] + '/' + title

      tv_show = Media.Episode(show,season,None,title,None)
      tv_show.released_at = datetime.datetime.fromtimestamp(os.stat(file).st_mtime).strftime("%Y-%m-%d")
      tv_show.parts.append(file)
      mediaList.append(tv_show)