import os, traceback, json import datetime base="/datas/media/video/mirror/youtube/" def run(c): for type in sorted(os.listdir(base)): ptype=os.path.join(base,type) for instance in sorted(os.listdir(ptype)): pinstance=os.path.join(ptype,instance) for file in sorted(os.listdir(pinstance)): pfile=os.path.join(pinstance,file) name, ext = os.path.splitext(file) if not ext or ext == '' or ext == '.part': continue if ext not in ('.mp4','.webm','.mkv','.ogg','.jpg','.png'): continue tname, tnum = os.path.splitext(name) if tnum != '' and tnum[1:].isdigit() and not os.path.isfile(os.path.join(pinstance,name+'.info.json')) and not os.path.isfile(os.path.join(pinstance,name+'.jpg')) and not os.path.isfile(os.path.join(pinstance,name+'.png')): name = tname date = None if ext in ('.mp4','.webm','.mkv','.ogg'): date = os.path.getmtime(pfile) data = None try: with open(os.path.join(pinstance,name+'.info.json')) as f: data = json.load(f) if 'upload_date' in data: date = datetime.datetime.strptime(data['upload_date'], '%Y%m%d').timestamp() except Exception: print("youtube collector, missing .info.json file for video, expected:", os.path.join(pinstance,name+'.info.json')) # traceback.print_exc() try: video = c.addVideo(name, { type: instance }, date) c.addSource(video, pfile) try: if data != None: categories=[] properties=[] if data['categories']: for category in data['categories']: categories.append(('category',category)) if data['tags']: for tag in data['tags']: properties.append(('tag',tag)) properties.append(('url', data['webpage_url'])) properties.append(('description', data['description'])) for name, value in categories: c.addProperty(video, name, value) for name, value in properties: c.addProperty(video, name, value, iscategory=False) except Exception: print("youtube collector, couldn't add extra infos for video:", pfile) traceback.print_exc() except Exception: print("youtube collector, couldn't add video:", pfile) traceback.print_exc()