@haoling haoling / ChinachuScanner.py
Created at Sun Jan 14 12:48:13 GMT 2018
ChinachuScanner.py
ChinachuScanner.py
Raw
  1. # coding:utf-8
  2. import os, json, re, sys, datetime
  3. import Media, VideoFiles, Stack, Utils
  4. reload(sys)
  5. sys.setdefaultencoding('utf-8')
  6. recordedList = None
  7. titleCount = {}
  8. log = open('/tmp/ChinachuScanner.log', 'a')
  9.  
  10. def Scan(path, files, mediaList, subdirs):
  11. global log
  12. VideoFiles.Scan(path, files, mediaList, subdirs)
  13. files.sort()
  14. if len(files) >= 1:
  15.  
  16. for file in files:
  17. log.write("file: " + file + "\n")
  18. filename = os.path.splitext(os.path.basename(file))[0]
  19. title = None
  20. episode = None
  21. release = None
  22. titleNormalizeRegexp = u'(\[.+?\]|【.+?】|(.+?)|\(.+?\)|「.+?」)'
  23. titleRegexp = u'([^\[\]0-9#  \(\)【】「」]{2,})'
  24. episodeRegexp = u'(^[  ]+|\[[^\]]+\]|【[^】]+】|(字)|\(字\)|(二)|\(二\))'
  25. mts = re.search(r'\[((?:cs|bs|gr)[0-9]+-[a-z0-9]+)\]', filename)
  26. if mts:
  27. log.write(" id: " + mts.groups()[0] + "\n")
  28. recorded = get_recorded(mts.groups()[0])
  29. if recorded:
  30. log.write(" json found.\n")
  31. #print json.dumps(recorded, indent=2)
  32. mts = re.search(titleRegexp, re.sub(titleNormalizeRegexp, '', recorded['title']))
  33. title = mts.groups()[0]
  34. episode = re.sub(episodeRegexp, '', recorded['title'].replace(title, ''))
  35. release = datetime.datetime.fromtimestamp(recorded['start'] / 1000).strftime("%Y-%m-%d")
  36. if title is None:
  37. mts = re.search(titleRegexp, re.sub(titleNormalizeRegexp, '', filename))
  38. title = mts.groups()[0]
  39. episode = re.sub(episodeRegexp, '', filename.replace(title, ''))
  40. if os.path.basename(os.path.dirname(file)) in title:
  41. title = os.path.basename(os.path.dirname(file))
  42. episode = re.sub(u'[#]', '', episode)
  43. if release is None:
  44. mts = re.search('\[(?:20)?(\d\d)-?(\d\d)-?(\d\d)(?:-\d\d:?\d\d(?::?\d\d)?)?\]', filename)
  45. if mts:
  46. release = "20" + mts.groups()[0] + "-" + mts.groups()[1] + "-" + mts.groups()[2]
  47. else:
  48. release = datetime.datetime.fromtimestamp(os.stat(file).st_mtime).strftime("%Y-%m-%d")
  49.  
  50. if title not in titleCount:
  51. titleCount[title] = 0
  52. titleCount[title] = titleCount[title] + 1
  53.  
  54. log.write(' title :' + title.encode('utf-8') + "\n")
  55. log.write(' episode: [' + str(titleCount[title]) + '] ' + episode.encode('utf-8') + "\n")
  56. log.write(' release: ' + release + "\n")
  57.  
  58. name, year = VideoFiles.CleanName(title.encode('utf-8'))
  59. tv_show = Media.Episode(name, None, None, episode.encode('utf-8'), year)
  60. tv_show.released_at = release
  61. tv_show.parts.append(file)
  62. mediaList.append(tv_show)
  63.  
  64. def get_recorded(id):
  65. global recordedList
  66. if recordedList is None:
  67. with open('/home/haoling/chinachu/data/recorded.json', 'r') as f:
  68. recordedList = json.loads(f.read(), 'utf-8')
  69.  
  70. for rec in recordedList:
  71. if rec['id'] == id:
  72. return rec
  73.  
Fei-YenScanner.py
Raw
  1. import re, os, os.path, datetime
  2. import Media, VideoFiles, Stack, Utils
  3.  
  4. def Scan(path, files, mediaList, subdirs):
  5. files.sort()
  6. if len(files) >= 1:
  7.  
  8. # Iterate through all the files
  9. for i,file in enumerate(files):
  10.  
  11. # Remove path and extension
  12. tsfile = os.path.basename(file)
  13. tsfile = os.path.splitext(tsfile)[0]
  14.  
  15. # Enigma2 files look like: YYYYMMDD HHMM - Channel - Programmename.ts
  16. #(date_time,channel,programme) = tsfile.split(' - ',2)
  17. #(date,time) = date_time.split(' ')
  18.  
  19. #year = int(date[0:4])
  20. #month = int(date[4:6])
  21. #day = int(date[6:8])
  22. #hour = int(time[0:2])
  23. #minute = int(time[2:4])
  24. #d = datetime.datetime(year, month, day, hour, minute)
  25. #t = d.timetuple()
  26.  
  27. show = os.path.basename(os.path.dirname(file))
  28.  
  29. tsfile = tsfile.replace(show, "")
  30.  
  31. #title = channel+" - "+date[6:8]+"/"+date[4:6]+"/"+date[0:4]+" "+time[0:2]+":"+time[2:4]
  32. title = tsfile
  33.  
  34. paths = Utils.SplitPath(path)
  35. season = None
  36. if len(paths) == 2:
  37. show = paths[0]
  38. match = re.search('(?P<season>[0-9]+)', paths[1])
  39. if match:
  40. season = match.group('season')
  41. else:
  42. title = paths[1] + '/' + title
  43.  
  44. tv_show = Media.Episode(show,season,None,title,None)
  45. tv_show.released_at = datetime.datetime.fromtimestamp(os.stat(file).st_mtime).strftime("%Y-%m-%d")
  46. tv_show.parts.append(file)
  47. mediaList.append(tv_show)