Przeglądaj źródła

add db support. initially for raw files

David 10 lat temu
rodzic
commit
fa7e9abaf3
2 zmienionych plików z 67 dodań i 5 usunięć
  1. 63 0
      back/db.py
  2. 4 5
      back/web.py

+ 63 - 0
back/db.py

@@ -0,0 +1,63 @@
+from pymongo import MongoClient, errors, DESCENDING, ASCENDING
+from bson.objectid import ObjectId
+import time
+import re
+import os
+import subprocess
+
+RAW_DIRECTORY="/home/david"
+
+class db():
+    DB = "videos"
+
+    def __init__(self):
+        self.client = MongoClient('localhost', 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 jsonable(self, el):
+        if el is None or "_id" not in el:
+            return el
+
+        el["_id"] = str(el["_id"])
+        return el
+
+    def raw(self, _id=None):
+        ret = None
+        if _id:
+            ret = self.raw_files.find_one({"_id": ObjectId(id)})
+        else:
+            ret = [ self.jsonable(r) for r in self.raw_files.find() ]
+
+        return ret
+
+    def insert_raw(self, rawfile):
+        try:
+            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_DIRECTORY):
+            if fname.endswith("flv"):
+                self.insert_raw(file_info(os.path.join(RAW_DIRECTORY,fname)))
+
+
+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': time.ctime(os.path.getctime(fname))
+           }
+
+

+ 4 - 5
back/web.py

@@ -1,6 +1,8 @@
 #!/usr/bin/env python3
+import json
 import tasks
 
+from db import db
 from flask import Flask, jsonify
 from rq import Queue
 from rq.job import Job
@@ -9,17 +11,14 @@ from worker import conn
 PORT = 8981
 BASE_PATH="/back" #changes based on webserver
 app = Flask(__name__)
-
+d=db()
 @app.route(BASE_PATH + '/')
 def index():
     return "Hello world"
 
 @app.route(BASE_PATH + '/raw')
 def raw_list():
-    ret={'list':
-            [{'file':'movie-1231547519.flv', 'length':'1:14:32', 'date': "2016,7,13"},
-         {'file':'movie-1231111111.flv', 'length':'0:14:32', 'date': "2016-7-12"} ]
-        }
+    ret={'list': d.raw() }
     return jsonify(ret)
 
 @app.route(BASE_PATH + '/job')