import datetime import time import re import os import subprocess from bson.objectid import ObjectId from constants import RAW_PATH, DB_IP from pymongo import MongoClient, errors, DESCENDING, ASCENDING class db(): DB = "videos" def __init__(self): self.client = MongoClient(DB_IP, 27017) self.raw_files = self.client[self.DB]["raw"] self.cut_files = self.client[self.DB]["cut"] self.ul_files = self.client[self.DB]["ul"] def uploaded(self): ret = [] for f in self.ul_files.find().sort("finished_at",-1): item = jsonable(f) item["enqueued_at"]=item["enqueued_at"].strftime("%d/%m/%Y %H:%M") item["finished_at"]=item["finished_at"].strftime("%d/%m/%Y %H:%M") ret.append(item) return ret def raw(self, _id=None): ret = None if _id: ret = jsonable(self.raw_files.find_one({"_id": ObjectId(_id)})) else: ret=[] for r in self.raw_files.find(): item = jsonable(r) #item["date"]=item["date"].strftime("%d/%m/%Y %H:%M") ret.append(item) return ret def processed(self, _id=None): ret = None if _id: ret = jsonable(self.cut_files.find_one({"_id": ObjectId(_id)})) else: ret = [ jsonable(c) for c in self.cut_files.find().sort("job_end",-1) ] return ret def insert_uploaded(self, data): try: self.ul_files.insert_one(data) return True except errors.DuplicateKeyError: print("Dup") return False def insert_processed(self, data): try: self.cut_files.insert_one(data) return True except errors.DuplicateKeyError: print("Dup") return False def insert_raw(self, rawfile): try: print(rawfile) self.raw_files.insert_one(rawfile) return True except errors.DuplicateKeyError: print("Dup") return False def insert_raw_list(self): for fname in os.listdir(RAW_PATH): if fname.endswith("mp4"): self.insert_raw(file_info(os.path.join(RAW_PATH,fname))) def videoData(self,v_id): ret = jsonable(self.ul_files.find_one({"_id": ObjectId(v_id)})) return ret def findCutId(self,video): ret = self.cut_files.find_one({"out_fname": video})["_id"] return ret def get_video_length(fname): process="ffprobe -v error -show_entries format=duration -of default=noprint_wrappers=1:nokey=1 -sexagesimal %s" % fname process=process.split(" ") out,err= subprocess.Popen(process,stdout=subprocess.PIPE).communicate() out=out.decode('utf-8') if "." in out: out = out.split(".")[0] return out def file_info(fname): return {'file':fname, 'length': get_video_length(fname), 'date': os.path.getctime(fname) } def jsonable(el): if el is None or "_id" not in el: return el el["_id"] = str(el["_id"]) return el