thejagstudio commited on
Commit
ea7959c
1 Parent(s): ef64631

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +365 -365
main.py CHANGED
@@ -1,365 +1,365 @@
1
- from src.routes.trailer import trailerBP
2
- from src.routes.subtitledownload import subtitledownloadBP
3
- from src.routes.streammap import streammapBP
4
- from src.routes.signup import signupBP
5
- from src.routes.restart import restartBP
6
- from src.routes.redirectdownload import redirectdownloadBP
7
- from src.routes.rebuild import rebuildBP
8
- from src.routes.ping import pingBP
9
- from src.routes.metadata import metadataBP
10
- from src.routes.image import imageBP
11
- from src.routes.environment import environmentBP
12
- from src.routes.download import downloadBP
13
- from src.routes.debug import debugBP
14
- from src.routes.config import configBP
15
- from src.routes.auth import authBP
16
- import datetime
17
- import io
18
- import json
19
- import logging
20
- import os
21
- import sys
22
- import threading
23
-
24
- import apscheduler.schedulers.background
25
- import bs4
26
- import colorama
27
- import flask
28
- import flask_cors
29
- import googleapiclient
30
- import requests
31
-
32
- import src.functions.config
33
- import src.functions.credentials
34
- import src.functions.metadata
35
- import src.functions.tests
36
-
37
- colorama.init()
38
- print(
39
- "====================================================\n\033[96m libDrive - v1.4.7\033[94m\n @eliasbenb\033[0m\n====================================================\n"
40
- )
41
-
42
- print("\033[32mREADING CONFIG...\033[0m")
43
- if os.getenv("LIBDRIVE_CONFIG"):
44
- config_str = os.getenv("LIBDRIVE_CONFIG")
45
- with open("config.json", "w+") as w:
46
- json.dump(obj=json.loads(config_str), fp=w, sort_keys=True, indent=4)
47
- config = src.functions.config.readConfig()
48
- print("DONE.\n")
49
-
50
- print("\033[32mREADING METADATA...\033[0m")
51
- metadata = src.functions.metadata.readMetadata(config)
52
- if os.getenv("LIBDRIVE_CLOUD") and config.get("refresh_token"):
53
- config, drive = src.functions.credentials.refreshCredentials(config)
54
- params = {
55
- "supportsAllDrives": True,
56
- "includeItemsFromAllDrives": True,
57
- "fields": "files(id,name)",
58
- "q": "'%s' in parents and trashed = false and mimeType = 'application/json'"
59
- % (os.getenv("LIBDRIVE_CLOUD")),
60
- }
61
- files = drive.files().list(**params).execute()["files"]
62
- config_file = next((i for i in files if i["name"] == "config.json"), None)
63
- metadata_file = next((i for i in files if i["name"] == "metadata.json"), None)
64
- if config_file:
65
- request = drive.files().get_media(fileId=config_file["id"])
66
- fh = io.BytesIO()
67
- downloader = googleapiclient.http.MediaIoBaseDownload(fh, request)
68
- done = False
69
- while done is False:
70
- status, done = downloader.next_chunk()
71
- config = json.loads(fh.getvalue())
72
- config, drive = src.functions.credentials.refreshCredentials(config)
73
- src.functions.config.updateConfig(config)
74
- if metadata_file:
75
- request = drive.files().get_media(fileId=metadata_file["id"])
76
- fh = io.BytesIO()
77
- downloader = googleapiclient.http.MediaIoBaseDownload(fh, request)
78
- done = False
79
- while done is False:
80
- status, done = downloader.next_chunk()
81
- metadata = json.loads(fh.getvalue())
82
- with open("metadata.json", "w+") as w:
83
- json.dump(metadata, w)
84
- print("DONE.\n")
85
-
86
- if not config.get("account_list"):
87
- config["account_list"] = []
88
- if config.get("account_list") == [] and config.get("signup") == False:
89
- config["auth"] = False
90
- if not config.get("auth"):
91
- config["auth"] = False
92
- if not config.get("build_interval"):
93
- config["build_interval"] = 360
94
- if not config.get("build_type"):
95
- config["build_type"] = "hybrid"
96
- if not config.get("category_list"):
97
- config["category_list"] = []
98
- if not config.get("cloudflare"):
99
- config["cloudflare"] = ""
100
- if not config.get("prefer_mkv"):
101
- config["prefer_mkv"] = False
102
- if not config.get("prefer_mp4"):
103
- config["prefer_mp4"] = True
104
- if not config.get("service_accounts"):
105
- config["service_accounts"] = []
106
- if not config.get("signup"):
107
- config["signup"] = False
108
- if not config.get("subtitles"):
109
- config["subtitles"] = False
110
- if not config.get("transcoded"):
111
- config["transcoded"] = False
112
- if not config.get("ui_config"):
113
- config["ui_config"] = {}
114
-
115
- with open("config.json", "w+") as w:
116
- json.dump(obj=config, fp=w, sort_keys=True, indent=4)
117
-
118
- print("\033[32mTESTING YOUR CONFIG...\033[0m")
119
- # src.functions.tests.tmdb_test(config)
120
- # src.functions.tests.category_list_test(config)
121
- # src.functions.tests.account_list_test(config)
122
- # src.functions.tests.cloudflare_test(config)
123
- print("DONE.\n")
124
-
125
-
126
- def threaded_metadata():
127
- for thread in threading.enumerate():
128
- if thread.name == "metadata_thread":
129
- print("DONE.\n")
130
- return (
131
- {
132
- "code": 500,
133
- "content": None,
134
- "message": "libDrive is already building metadata, please wait.",
135
- "success": False,
136
- },
137
- 500,
138
- )
139
- config = src.functions.config.readConfig()
140
- if len(config.get("category_list")) > 0:
141
- metadata_thread = threading.Thread(
142
- target=src.functions.metadata.writeMetadata,
143
- args=(config,),
144
- daemon=True,
145
- name="metadata_thread",
146
- )
147
- metadata_thread.start()
148
- else:
149
- with open("./metadata.json", "w+") as w:
150
- w.write(json.dumps([]))
151
- return (
152
- {
153
- "code": 200,
154
- "content": None,
155
- "message": "libDrive is building your new metadata.",
156
- "success": True,
157
- },
158
- 200,
159
- )
160
-
161
-
162
- def create_app():
163
- if os.path.exists("./build"):
164
- LIBDRIVE_DEBUG = os.getenv("LIBDRIVE_DEBUG")
165
- if LIBDRIVE_DEBUG:
166
- if LIBDRIVE_DEBUG.lower() == "true":
167
- LIBDRIVE_DEBUG = True
168
- else:
169
- LIBDRIVE_DEBUG = False
170
- else:
171
- LIBDRIVE_DEBUG = False
172
- r = open("./build/index.html", "r")
173
- soup = bs4.BeautifulSoup(r.read(), features="html.parser")
174
- if config.get("ui_config", {}).get("icon"):
175
- try:
176
- soup.find("meta", {"id": "@ld-meta-og-image"})["content"] = config.get(
177
- "ui_config", {}
178
- ).get("icon")
179
- except:
180
- pass
181
- try:
182
- soup.find("link", {"id": "@ld-link-icon"})["href"] = config.get(
183
- "ui_config", {}
184
- ).get("icon")
185
- except:
186
- pass
187
- else:
188
- try:
189
- soup.find("meta", {"id": "@ld-meta-og-image"})[
190
- "content"
191
- ] = "/images/icons/icon-512x512.png"
192
- except:
193
- pass
194
- try:
195
- soup.find("link", {"id": "@ld-link-icon"})["href"] = "/favicon.ico"
196
- except:
197
- pass
198
- if config.get("ui_config", {}).get("title"):
199
- try:
200
- soup.find("meta", {"id": "@ld-meta-og-title"})["content"] = config.get(
201
- "ui_config", {}
202
- ).get("title")
203
- except:
204
- pass
205
- try:
206
- soup.find("meta", {"id": "@ld-meta-og-site_name"})[
207
- "content"
208
- ] = config.get("ui_config", {}).get("title")
209
- except:
210
- pass
211
- try:
212
- soup.find("title", {"id": "@ld-title"}).string = config.get(
213
- "ui_config", {}
214
- ).get("title")
215
- except:
216
- pass
217
- else:
218
- try:
219
- soup.find("meta", {"id": "@ld-meta-og-title"})["content"] = "libDrive"
220
- except:
221
- pass
222
- try:
223
- soup.find("meta", {"id": "@ld-meta-og-site_name"})[
224
- "content"
225
- ] = "libDrive"
226
- except:
227
- pass
228
- try:
229
- soup.find("title", {"id": "@ld-title"}).string = "libDrive"
230
- except:
231
- pass
232
- if (
233
- config.get("arcio")
234
- and config.get("arcio") != ""
235
- and LIBDRIVE_DEBUG == False
236
- ):
237
- req = requests.get("https://arc.io/arc-sw.js")
238
- with open("./build/arc-sw.js", "wb") as wb:
239
- wb.write(req.content)
240
- code = config.get("arcio")
241
- if code == "dev":
242
- code = "tUUqUjhw"
243
- soup.find("script", {"id": "@ld-script-arcio"})[
244
- "src"
245
- ] = "//arc.io/widget.min.js#%s" % (code)
246
- else:
247
- if os.path.exists("./build/arc-sw.js"):
248
- os.remove("./build/arc-sw.js")
249
- soup.find("script", {"id": "@ld-script-arcio"})["src"] = ""
250
- with open("./build/index.html", "w+") as w:
251
- w.write(str(soup))
252
- r.close()
253
-
254
- app = flask.Flask(__name__, static_folder="build")
255
-
256
- build_interval = config.get("build_interval")
257
- if not build_interval:
258
- build_interval = 360
259
- if build_interval != 0:
260
- print("\033[32mCREATING CRON JOB...\033[0m")
261
- sched = apscheduler.schedulers.background.BackgroundScheduler(daemon=True)
262
- sched.add_job(
263
- threaded_metadata,
264
- "interval",
265
- minutes=build_interval,
266
- )
267
- sched.start()
268
- print("DONE.\n")
269
-
270
- config_categories = [d["id"] for d in config["category_list"]]
271
- metadata_categories = [d["id"] for d in metadata]
272
- if len(metadata) > 0 and sorted(config_categories) == sorted(metadata_categories):
273
- if build_interval == 0:
274
- return app
275
- elif datetime.datetime.utcnow() <= datetime.datetime.strptime(
276
- metadata[-1]["buildTime"], "%Y-%m-%d %H:%M:%S.%f"
277
- ) + datetime.timedelta(minutes=build_interval):
278
- return app
279
- else:
280
- threaded_metadata()
281
- else:
282
- threaded_metadata()
283
-
284
- return app
285
-
286
-
287
- app = create_app()
288
- flask_cors.CORS(app)
289
- app.secret_key = config.get("secret_key")
290
-
291
-
292
- app.register_blueprint(authBP)
293
- app.register_blueprint(configBP)
294
- app.register_blueprint(debugBP)
295
- app.register_blueprint(downloadBP)
296
- app.register_blueprint(environmentBP)
297
- app.register_blueprint(imageBP)
298
- app.register_blueprint(metadataBP)
299
- app.register_blueprint(pingBP)
300
- # app.register_blueprint(rebuildBP)
301
- app.register_blueprint(redirectdownloadBP)
302
- app.register_blueprint(restartBP)
303
- app.register_blueprint(signupBP)
304
- app.register_blueprint(streammapBP)
305
- app.register_blueprint(subtitledownloadBP)
306
- app.register_blueprint(trailerBP)
307
-
308
-
309
- @app.route("/", defaults={"path": ""})
310
- @app.route("/<path:path>")
311
- async def serve(path):
312
- if (path != "") and os.path.exists("%s/%s" % (app.static_folder, path)):
313
- return flask.send_from_directory(app.static_folder, path)
314
- else:
315
- return flask.send_from_directory(app.static_folder, "index.html")
316
-
317
-
318
- if __name__ == "__main__":
319
- print("\033[32mSERVING SERVER...\033[0m")
320
- LIBDRIVE_DEBUG = os.getenv("LIBDRIVE_DEBUG")
321
- if LIBDRIVE_DEBUG:
322
- if LIBDRIVE_DEBUG.lower() == "true":
323
- LIBDRIVE_DEBUG = True
324
- else:
325
- LIBDRIVE_DEBUG = False
326
- else:
327
- LIBDRIVE_DEBUG = False
328
- print("DONE.\n")
329
- app.run(
330
- host="0.0.0.0",
331
- port=31145,
332
- threaded=True,
333
- debug=LIBDRIVE_DEBUG,
334
- )
335
- else:
336
- print("\033[32mINITIALIZING LOGGER...\033[0m")
337
- if not os.path.exists("./logs"):
338
- os.mkdir("./logs")
339
- logs_path = os.path.abspath("./logs")
340
- logs_max_files = 5
341
-
342
- def sorted_ls(path):
343
- def mtime(f): return os.stat(os.path.join(path, f)).st_mtime
344
- return list(sorted(os.listdir(path), key=mtime))
345
-
346
- del_list = sorted_ls(logs_path)[0: (len(sorted_ls(logs_path)) - logs_max_files)]
347
- for del_file in del_list:
348
- try:
349
- os.remove(os.path.join(logs_path, del_file))
350
- except:
351
- pass
352
- logging.getLogger("googleapiclient").setLevel(logging.WARNING)
353
- logging.getLogger("oauth2client").setLevel(logging.WARNING)
354
- logging.getLogger("waitress").setLevel(logging.INFO)
355
- logging.basicConfig(
356
- filename="./logs/%s.log"
357
- % (datetime.datetime.utcnow().strftime("%Y%m%d-%H%M%S")),
358
- level=logging.INFO,
359
- )
360
- console_logger = logging.getLogger()
361
- console_logger.setLevel(logging.INFO)
362
- console_handler = logging.StreamHandler(sys.stdout)
363
- console_handler.setLevel(logging.INFO)
364
- console_logger.addHandler(console_handler)
365
- print("DONE.\n")
 
1
+ from src.routes.trailer import trailerBP
2
+ from src.routes.subtitledownload import subtitledownloadBP
3
+ from src.routes.streammap import streammapBP
4
+ from src.routes.signup import signupBP
5
+ from src.routes.restart import restartBP
6
+ from src.routes.redirectdownload import redirectdownloadBP
7
+ from src.routes.rebuild import rebuildBP
8
+ from src.routes.ping import pingBP
9
+ from src.routes.metadata import metadataBP
10
+ from src.routes.image import imageBP
11
+ from src.routes.environment import environmentBP
12
+ from src.routes.download import downloadBP
13
+ from src.routes.debug import debugBP
14
+ from src.routes.config import configBP
15
+ from src.routes.auth import authBP
16
+ import datetime
17
+ import io
18
+ import json
19
+ import logging
20
+ import os
21
+ import sys
22
+ import threading
23
+
24
+ import apscheduler.schedulers.background
25
+ import bs4
26
+ import colorama
27
+ import flask
28
+ import flask_cors
29
+ import googleapiclient
30
+ import requests
31
+
32
+ import src.functions.config
33
+ import src.functions.credentials
34
+ import src.functions.metadata
35
+ import src.functions.tests
36
+
37
+ colorama.init()
38
+ print(
39
+ "====================================================\n\033[96m libDrive - v1.4.7\033[94m\n @eliasbenb\033[0m\n====================================================\n"
40
+ )
41
+
42
+ print("\033[32mREADING CONFIG...\033[0m")
43
+ if os.getenv("LIBDRIVE_CONFIG"):
44
+ config_str = os.getenv("LIBDRIVE_CONFIG")
45
+ with open("config.json", "w+") as w:
46
+ json.dump(obj=json.loads(config_str), fp=w, sort_keys=True, indent=4)
47
+ config = src.functions.config.readConfig()
48
+ print("DONE.\n")
49
+
50
+ print("\033[32mREADING METADATA...\033[0m")
51
+ metadata = src.functions.metadata.readMetadata(config)
52
+ if os.getenv("LIBDRIVE_CLOUD") and config.get("refresh_token"):
53
+ config, drive = src.functions.credentials.refreshCredentials(config)
54
+ params = {
55
+ "supportsAllDrives": True,
56
+ "includeItemsFromAllDrives": True,
57
+ "fields": "files(id,name)",
58
+ "q": "'%s' in parents and trashed = false and mimeType = 'application/json'"
59
+ % (os.getenv("LIBDRIVE_CLOUD")),
60
+ }
61
+ files = drive.files().list(**params).execute()["files"]
62
+ config_file = next((i for i in files if i["name"] == "config.json"), None)
63
+ metadata_file = next((i for i in files if i["name"] == "metadata.json"), None)
64
+ if config_file:
65
+ request = drive.files().get_media(fileId=config_file["id"])
66
+ fh = io.BytesIO()
67
+ downloader = googleapiclient.http.MediaIoBaseDownload(fh, request)
68
+ done = False
69
+ while done is False:
70
+ status, done = downloader.next_chunk()
71
+ config = json.loads(fh.getvalue())
72
+ config, drive = src.functions.credentials.refreshCredentials(config)
73
+ src.functions.config.updateConfig(config)
74
+ if metadata_file:
75
+ request = drive.files().get_media(fileId=metadata_file["id"])
76
+ fh = io.BytesIO()
77
+ downloader = googleapiclient.http.MediaIoBaseDownload(fh, request)
78
+ done = False
79
+ while done is False:
80
+ status, done = downloader.next_chunk()
81
+ metadata = json.loads(fh.getvalue())
82
+ with open("metadata.json", "w+") as w:
83
+ json.dump(metadata, w)
84
+ print("DONE.\n")
85
+
86
+ if not config.get("account_list"):
87
+ config["account_list"] = []
88
+ if config.get("account_list") == [] and config.get("signup") == False:
89
+ config["auth"] = False
90
+ if not config.get("auth"):
91
+ config["auth"] = False
92
+ if not config.get("build_interval"):
93
+ config["build_interval"] = 360
94
+ if not config.get("build_type"):
95
+ config["build_type"] = "hybrid"
96
+ if not config.get("category_list"):
97
+ config["category_list"] = []
98
+ if not config.get("cloudflare"):
99
+ config["cloudflare"] = ""
100
+ if not config.get("prefer_mkv"):
101
+ config["prefer_mkv"] = False
102
+ if not config.get("prefer_mp4"):
103
+ config["prefer_mp4"] = True
104
+ if not config.get("service_accounts"):
105
+ config["service_accounts"] = []
106
+ if not config.get("signup"):
107
+ config["signup"] = False
108
+ if not config.get("subtitles"):
109
+ config["subtitles"] = False
110
+ if not config.get("transcoded"):
111
+ config["transcoded"] = False
112
+ if not config.get("ui_config"):
113
+ config["ui_config"] = {}
114
+
115
+ with open("config.json", "w+") as w:
116
+ json.dump(obj=config, fp=w, sort_keys=True, indent=4)
117
+
118
+ print("\033[32mTESTING YOUR CONFIG...\033[0m")
119
+ # src.functions.tests.tmdb_test(config)
120
+ # src.functions.tests.category_list_test(config)
121
+ # src.functions.tests.account_list_test(config)
122
+ # src.functions.tests.cloudflare_test(config)
123
+ print("DONE.\n")
124
+
125
+
126
+ def threaded_metadata():
127
+ for thread in threading.enumerate():
128
+ if thread.name == "metadata_thread":
129
+ print("DONE.\n")
130
+ return (
131
+ {
132
+ "code": 500,
133
+ "content": None,
134
+ "message": "libDrive is already building metadata, please wait.",
135
+ "success": False,
136
+ },
137
+ 500,
138
+ )
139
+ config = src.functions.config.readConfig()
140
+ if len(config.get("category_list")) > 0:
141
+ metadata_thread = threading.Thread(
142
+ target=src.functions.metadata.writeMetadata,
143
+ args=(config,),
144
+ daemon=True,
145
+ name="metadata_thread",
146
+ )
147
+ metadata_thread.start()
148
+ else:
149
+ with open("./metadata.json", "w+") as w:
150
+ w.write(json.dumps([]))
151
+ return (
152
+ {
153
+ "code": 200,
154
+ "content": None,
155
+ "message": "libDrive is building your new metadata.",
156
+ "success": True,
157
+ },
158
+ 200,
159
+ )
160
+
161
+
162
+ def create_app():
163
+ if os.path.exists("./build"):
164
+ LIBDRIVE_DEBUG = os.getenv("LIBDRIVE_DEBUG")
165
+ if LIBDRIVE_DEBUG:
166
+ if LIBDRIVE_DEBUG.lower() == "true":
167
+ LIBDRIVE_DEBUG = True
168
+ else:
169
+ LIBDRIVE_DEBUG = False
170
+ else:
171
+ LIBDRIVE_DEBUG = False
172
+ r = open("./build/index.html", "r")
173
+ soup = bs4.BeautifulSoup(r.read(), features="html.parser")
174
+ if config.get("ui_config", {}).get("icon"):
175
+ try:
176
+ soup.find("meta", {"id": "@ld-meta-og-image"})["content"] = config.get(
177
+ "ui_config", {}
178
+ ).get("icon")
179
+ except:
180
+ pass
181
+ try:
182
+ soup.find("link", {"id": "@ld-link-icon"})["href"] = config.get(
183
+ "ui_config", {}
184
+ ).get("icon")
185
+ except:
186
+ pass
187
+ else:
188
+ try:
189
+ soup.find("meta", {"id": "@ld-meta-og-image"})[
190
+ "content"
191
+ ] = "/images/icons/icon-512x512.png"
192
+ except:
193
+ pass
194
+ try:
195
+ soup.find("link", {"id": "@ld-link-icon"})["href"] = "/favicon.ico"
196
+ except:
197
+ pass
198
+ if config.get("ui_config", {}).get("title"):
199
+ try:
200
+ soup.find("meta", {"id": "@ld-meta-og-title"})["content"] = config.get(
201
+ "ui_config", {}
202
+ ).get("title")
203
+ except:
204
+ pass
205
+ try:
206
+ soup.find("meta", {"id": "@ld-meta-og-site_name"})[
207
+ "content"
208
+ ] = config.get("ui_config", {}).get("title")
209
+ except:
210
+ pass
211
+ try:
212
+ soup.find("title", {"id": "@ld-title"}).string = config.get(
213
+ "ui_config", {}
214
+ ).get("title")
215
+ except:
216
+ pass
217
+ else:
218
+ try:
219
+ soup.find("meta", {"id": "@ld-meta-og-title"})["content"] = "libDrive"
220
+ except:
221
+ pass
222
+ try:
223
+ soup.find("meta", {"id": "@ld-meta-og-site_name"})[
224
+ "content"
225
+ ] = "libDrive"
226
+ except:
227
+ pass
228
+ try:
229
+ soup.find("title", {"id": "@ld-title"}).string = "libDrive"
230
+ except:
231
+ pass
232
+ if (
233
+ config.get("arcio")
234
+ and config.get("arcio") != ""
235
+ and LIBDRIVE_DEBUG == False
236
+ ):
237
+ req = requests.get("https://arc.io/arc-sw.js")
238
+ with open("./build/arc-sw.js", "wb") as wb:
239
+ wb.write(req.content)
240
+ code = config.get("arcio")
241
+ if code == "dev":
242
+ code = "tUUqUjhw"
243
+ soup.find("script", {"id": "@ld-script-arcio"})[
244
+ "src"
245
+ ] = "//arc.io/widget.min.js#%s" % (code)
246
+ else:
247
+ if os.path.exists("./build/arc-sw.js"):
248
+ os.remove("./build/arc-sw.js")
249
+ soup.find("script", {"id": "@ld-script-arcio"})["src"] = ""
250
+ with open("./build/index.html", "w+") as w:
251
+ w.write(str(soup))
252
+ r.close()
253
+
254
+ app = flask.Flask(__name__, static_folder="build")
255
+
256
+ build_interval = config.get("build_interval")
257
+ if not build_interval:
258
+ build_interval = 360
259
+ if build_interval != 0:
260
+ print("\033[32mCREATING CRON JOB...\033[0m")
261
+ sched = apscheduler.schedulers.background.BackgroundScheduler(daemon=True)
262
+ sched.add_job(
263
+ threaded_metadata,
264
+ "interval",
265
+ minutes=build_interval,
266
+ )
267
+ sched.start()
268
+ print("DONE.\n")
269
+
270
+ config_categories = [d["id"] for d in config["category_list"]]
271
+ metadata_categories = [d["id"] for d in metadata]
272
+ if len(metadata) > 0 and sorted(config_categories) == sorted(metadata_categories):
273
+ if build_interval == 0:
274
+ return app
275
+ elif datetime.datetime.utcnow() <= datetime.datetime.strptime(
276
+ metadata[-1]["buildTime"], "%Y-%m-%d %H:%M:%S.%f"
277
+ ) + datetime.timedelta(minutes=build_interval):
278
+ return app
279
+ else:
280
+ threaded_metadata()
281
+ else:
282
+ threaded_metadata()
283
+
284
+ return app
285
+
286
+
287
+ app = create_app()
288
+ flask_cors.CORS(app)
289
+ app.secret_key = config.get("secret_key")
290
+
291
+
292
+ app.register_blueprint(authBP)
293
+ app.register_blueprint(configBP)
294
+ app.register_blueprint(debugBP)
295
+ app.register_blueprint(downloadBP)
296
+ app.register_blueprint(environmentBP)
297
+ app.register_blueprint(imageBP)
298
+ app.register_blueprint(metadataBP)
299
+ app.register_blueprint(pingBP)
300
+ # app.register_blueprint(rebuildBP)
301
+ app.register_blueprint(redirectdownloadBP)
302
+ app.register_blueprint(restartBP)
303
+ app.register_blueprint(signupBP)
304
+ app.register_blueprint(streammapBP)
305
+ app.register_blueprint(subtitledownloadBP)
306
+ app.register_blueprint(trailerBP)
307
+
308
+
309
+ @app.route("/", defaults={"path": ""})
310
+ @app.route("/<path:path>")
311
+ async def serve(path):
312
+ if (path != "") and os.path.exists("%s/%s" % (app.static_folder, path)):
313
+ return flask.send_from_directory(app.static_folder, path)
314
+ else:
315
+ return flask.send_from_directory(app.static_folder, "index.html")
316
+
317
+
318
+ if __name__ == "__main__":
319
+ print("\033[32mSERVING SERVER...\033[0m")
320
+ LIBDRIVE_DEBUG = os.getenv("LIBDRIVE_DEBUG")
321
+ if LIBDRIVE_DEBUG:
322
+ if LIBDRIVE_DEBUG.lower() == "true":
323
+ LIBDRIVE_DEBUG = True
324
+ else:
325
+ LIBDRIVE_DEBUG = False
326
+ else:
327
+ LIBDRIVE_DEBUG = False
328
+ print("DONE.\n")
329
+ app.run(
330
+ host="0.0.0.0",
331
+ port=7860,
332
+ threaded=True,
333
+ debug=LIBDRIVE_DEBUG,
334
+ )
335
+ else:
336
+ print("\033[32mINITIALIZING LOGGER...\033[0m")
337
+ if not os.path.exists("./logs"):
338
+ os.mkdir("./logs")
339
+ logs_path = os.path.abspath("./logs")
340
+ logs_max_files = 5
341
+
342
+ def sorted_ls(path):
343
+ def mtime(f): return os.stat(os.path.join(path, f)).st_mtime
344
+ return list(sorted(os.listdir(path), key=mtime))
345
+
346
+ del_list = sorted_ls(logs_path)[0: (len(sorted_ls(logs_path)) - logs_max_files)]
347
+ for del_file in del_list:
348
+ try:
349
+ os.remove(os.path.join(logs_path, del_file))
350
+ except:
351
+ pass
352
+ logging.getLogger("googleapiclient").setLevel(logging.WARNING)
353
+ logging.getLogger("oauth2client").setLevel(logging.WARNING)
354
+ logging.getLogger("waitress").setLevel(logging.INFO)
355
+ logging.basicConfig(
356
+ filename="./logs/%s.log"
357
+ % (datetime.datetime.utcnow().strftime("%Y%m%d-%H%M%S")),
358
+ level=logging.INFO,
359
+ )
360
+ console_logger = logging.getLogger()
361
+ console_logger.setLevel(logging.INFO)
362
+ console_handler = logging.StreamHandler(sys.stdout)
363
+ console_handler.setLevel(logging.INFO)
364
+ console_logger.addHandler(console_handler)
365
+ print("DONE.\n")