-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.py
170 lines (144 loc) · 5.15 KB
/
server.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
from __future__ import division
from flask import Flask, jsonify
from apscheduler.schedulers.background import BackgroundScheduler
from flask_pymongo import PyMongo
import pymongo
import logging
from Downloader import Downloader
from Parser import Parser
from ParserTest import ParserTest
from configparser import ConfigParser
from shutil import copy2
import sys
import datetime
import os
#https://www.youtube.com/watch?v=u3DVs2XS50A messenger
#https://www.youtube.com/watch?v=60WNEb6dHsM acho
app = Flask(__name__)
scheduler = None
# init log levels
logLevels = {"DEBUG":10, "INFO":20, "WARNING":30, "ERROR":40,"CRITICAL":50}
# read conf
Config = ConfigParser()
Config.read("conf.ini")
''' check if conf is valid '''
try:
# get mongo conf
if Config.get('DB', 'mongoHost') == "" or Config.get('DB', 'mongoPort') == "" or Config.get('DB', 'mongoDbName') == "":
print("please configure mongo connection in conf.ini")
sys.exit()
# check conf parser
if Config.get('PARSER', 'videoFolder') == "" or Config.get('PARSER', 'characterFolder') == "" or Config.get('PARSER', 'templateFolder') == "":
print("please configure work folders for the parser in conf.ini")
sys.exit()
if not os.path.isdir(Config.get('PARSER', 'videoFolder')) or not os.path.isdir(Config.get('PARSER', 'characterFolder')) or not os.path.isdir(Config.get('PARSER', 'templateFolder')):
print("please configure acual folders for the parser in conf.ini")
sys.exit()
# get log level from conf if specified
level = Config.get('LOG', 'level')
except Exception as e:
print("Error reading conf: "+str(e))
sys.exit()
# loggers
log = logging.getLogger('apscheduler.executors.default')
log2 = logging.getLogger('apscheduler.scheduler')
log.setLevel(level)
log2.setLevel(level)
fmt = logging.Formatter('%(levelname)s:%(name)s:%(message)s')
h = logging.StreamHandler()
h.setFormatter(fmt)
log.addHandler(h)
log2.addHandler(h)
#mongodb
app.config["MONGO_URI"] = "mongodb://"+Config.get('DB', 'mongoHost')+":"+Config.get('DB', 'mongoPort')+"/"+Config.get('DB', 'mongoDbName')
mongo = PyMongo(app)
# objects doing the work
downloader = Downloader(mongo, Config.get('PARSER', 'videoFolder'))
parser = Parser(mongo, Config.get('PARSER', 'videoFolder'), Config.get('PARSER', 'characterFolder'), Config.get('PARSER', 'templateFolder'))
parserTest = ParserTest(mongo, Config.get('PARSER', 'videoFolder'), Config.get('PARSER', 'characterFolder'), Config.get('PARSER', 'templateFolder'))
# fire the main job
@app.route('/firemainjob')
def index():
print("fire scheduler")
scheduler.get_job("mainJob").modify(next_run_time=datetime.datetime.now())
return jsonify({"fire": "ok"})
# download only
@app.route('/onlydl/<idvid>')
def onlydl(idvid):
print("download:"+idvid)
downloader.downloadVideo(idvid)
return jsonify({"download only": idvid})
# local test
@app.route('/testlocal')
def testLocal():
print("test local")
if mongo.db.videos.count_documents({"_id": "test"}) > 0:
mongo.db.videos.update_one({
"_id": "test"
}, {
"$set": {
"status": "DOWNLOADED",
"parsing": 0
}})
else:
mongo.db.videos.insert_one({
"_id" : "test",
"status": "DOWNLOADED",
"addDate": datetime.datetime.now(),
"dl": 100,
"parsing": 0,
"length": "99mn",
})
mongo.db.matches.delete_many({"idVideo": "test"})
print("db ok")
copy2(Config.get('PARSER', 'videoFolder')+"dHAZGFJ7YME.mp4", Config.get('PARSER', 'videoFolder')+"test.mp4")
print("copy ok")
parser.parsevideo("test")
return jsonify({"test": "ok"})
# Launched by scheduler
# Get next video to DL from DB
# Download it then parse it
# Repeat
def work():
print("work")
# si il y a des video a traiter
id = getNextVideoId()
while id != "":
print("new video to downlaod:"+id)
downloader.downloadVideo(id)
parser.parsevideo(id)
id = getNextVideoId()
print("fin work")
# get the next video id to download
def getNextVideoId():
print("getnextvideo")
id = ""
result = mongo.db.videos.find({"status": "WAITING"}).sort("addDate", pymongo.ASCENDING)
print(result.count())
if result.count() > 0:
id = result[0]["_id"]
print("_id:"+id)
return id
# In case the process has been shutdown during download or parsing, set video to redo the interrupted process
def correctDB():
print("correct DB")
mongo.db.videos.update_one({
"status": "DOWNLOADING"
}, {
"$set": {
"status": "WAITING",
"dl": 0
}})
mongo.db.videos.update_one({
"status": "PARSING"
}, {
"$set": {
"status": "DOWNLOADED",
"frameCounter": 0
}})
if __name__ == '__main__':
scheduler = BackgroundScheduler()
correctDB()
scheduler.add_job(work, 'interval', hours=24, id="mainJob", misfire_grace_time=None)
scheduler.start()
app.run()