db.py 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. import time
  2. import re
  3. import os
  4. import subprocess
  5. from bson.objectid import ObjectId
  6. from constants import RAW_PATH
  7. from pymongo import MongoClient, errors, DESCENDING, ASCENDING
  8. class db():
  9. DB = "videos"
  10. def __init__(self):
  11. self.client = MongoClient('localhost', 27017)
  12. self.raw_files = self.client[self.DB]["raw"]
  13. self.cut_files = self.client[self.DB]["cut"]
  14. self.ul_files = self.client[self.DB]["ul"]
  15. self.jobs = self.client[self.DB]["jobs"]
  16. def getJobs(self):
  17. ret = [ jsonable(r) for r in self.jobs.find() ]
  18. return ret
  19. def raw(self, _id=None):
  20. ret = None
  21. if _id:
  22. ret = jsonable(self.raw_files.find_one({"_id": ObjectId(_id)}))
  23. else:
  24. ret = [ jsonable(fix_date(r)) for r in self.raw_files.find() ]
  25. return ret
  26. def cut(self, _id=None):
  27. ret = None
  28. if _id:
  29. ret = jsonable(self.cut_files.find_one({"_id": ObjectId(_id)}))
  30. else:
  31. ret = [ jsonable(fix_date(c)) for c in self.cut_files.find() ]
  32. return ret
  33. def insert_processed(self, data):
  34. try:
  35. self.cut_files.insert_one(data)
  36. return True
  37. except errors.DuplicateKeyError:
  38. print("Dup")
  39. return False
  40. def insert_raw(self, rawfile):
  41. try:
  42. self.raw_files.insert_one(rawfile)
  43. return True
  44. except errors.DuplicateKeyError:
  45. print("Dup")
  46. return False
  47. def insert_raw_list(self):
  48. for fname in os.listdir(RAW_PATH):
  49. if fname.endswith("mp4"):
  50. self.insert_raw(file_info(os.path.join(RAW_PATH,fname)))
  51. def fix_date(obj):
  52. ret = obj
  53. #ret["date"]=ret["date"].strftime('%Y-%m-%dT%H:%M:%S')
  54. return ret
  55. def get_video_length(fname):
  56. process="ffprobe -v error -show_entries format=duration -of default=noprint_wrappers=1:nokey=1 -sexagesimal %s" % fname
  57. process=process.split(" ")
  58. out,err= subprocess.Popen(process,stdout=subprocess.PIPE).communicate()
  59. out=out.decode('utf-8')
  60. if "." in out:
  61. out = out.split(".")[0]
  62. return out
  63. def file_info(fname):
  64. return {'file':fname,
  65. 'length': get_video_length(fname),
  66. 'date': time.ctime(os.path.getctime(fname))
  67. }
  68. def jsonable(el):
  69. if el is None or "_id" not in el:
  70. return el
  71. el["_id"] = str(el["_id"])
  72. return el