db.py 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. def uploaded(self):
  16. ret = [ jsonable(f) for f in self.ul_files.find() ]
  17. return ret
  18. def raw(self, _id=None):
  19. ret = None
  20. if _id:
  21. ret = jsonable(self.raw_files.find_one({"_id": ObjectId(_id)}))
  22. else:
  23. ret = [ jsonable(fix_date(r)) for r in self.raw_files.find() ]
  24. return ret
  25. def processed(self, _id=None):
  26. ret = None
  27. if _id:
  28. ret = jsonable(self.cut_files.find_one({"_id": ObjectId(_id)}))
  29. else:
  30. ret = [ jsonable(fix_date(c)) for c in self.cut_files.find() ]
  31. return ret
  32. def insert_uploaded(self, data):
  33. try:
  34. self.ul_files.insert_one(data)
  35. return True
  36. except errors.DuplicateKeyError:
  37. print("Dup")
  38. return False
  39. def insert_processed(self, data):
  40. try:
  41. self.cut_files.insert_one(data)
  42. return True
  43. except errors.DuplicateKeyError:
  44. print("Dup")
  45. return False
  46. def insert_raw(self, rawfile):
  47. try:
  48. self.raw_files.insert_one(rawfile)
  49. return True
  50. except errors.DuplicateKeyError:
  51. print("Dup")
  52. return False
  53. def insert_raw_list(self):
  54. for fname in os.listdir(RAW_PATH):
  55. if fname.endswith("mp4"):
  56. self.insert_raw(file_info(os.path.join(RAW_PATH,fname)))
  57. def fix_date(obj):
  58. ret = obj
  59. #ret["date"]=ret["date"].strftime('%Y-%m-%dT%H:%M:%S')
  60. return ret
  61. def get_video_length(fname):
  62. process="ffprobe -v error -show_entries format=duration -of default=noprint_wrappers=1:nokey=1 -sexagesimal %s" % fname
  63. process=process.split(" ")
  64. out,err= subprocess.Popen(process,stdout=subprocess.PIPE).communicate()
  65. out=out.decode('utf-8')
  66. if "." in out:
  67. out = out.split(".")[0]
  68. return out
  69. def file_info(fname):
  70. return {'file':fname,
  71. 'length': get_video_length(fname),
  72. 'date': time.ctime(os.path.getctime(fname))
  73. }
  74. def jsonable(el):
  75. if el is None or "_id" not in el:
  76. return el
  77. el["_id"] = str(el["_id"])
  78. return el