db.py 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. import time
  2. import re
  3. import os
  4. import subprocess
  5. from bson.objectid import ObjectId
  6. from constants import RAW_PATH, DB_IP
  7. from pymongo import MongoClient, errors, DESCENDING, ASCENDING
  8. class db():
  9. DB = "videos"
  10. def __init__(self):
  11. self.client = MongoClient(DB_IP, 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. print(rawfile)
  49. self.raw_files.insert_one(rawfile)
  50. return True
  51. except errors.DuplicateKeyError:
  52. print("Dup")
  53. return False
  54. def insert_raw_list(self):
  55. for fname in os.listdir(RAW_PATH):
  56. if fname.endswith("mp4"):
  57. self.insert_raw(file_info(os.path.join(RAW_PATH,fname)))
  58. def fix_date(obj):
  59. ret = obj
  60. #ret["date"]=ret["date"].strftime('%Y-%m-%dT%H:%M:%S')
  61. return ret
  62. def get_video_length(fname):
  63. process="ffprobe -v error -show_entries format=duration -of default=noprint_wrappers=1:nokey=1 -sexagesimal %s" % fname
  64. process=process.split(" ")
  65. out,err= subprocess.Popen(process,stdout=subprocess.PIPE).communicate()
  66. out=out.decode('utf-8')
  67. if "." in out:
  68. out = out.split(".")[0]
  69. return out
  70. def file_info(fname):
  71. return {'file':fname,
  72. 'length': get_video_length(fname),
  73. 'date': time.ctime(os.path.getctime(fname))
  74. }
  75. def jsonable(el):
  76. if el is None or "_id" not in el:
  77. return el
  78. el["_id"] = str(el["_id"])
  79. return el