forked from vladimir-tutin/Plex-Auto-Collections
-
Notifications
You must be signed in to change notification settings - Fork 0
/
image_server.py
58 lines (48 loc) · 1.52 KB
/
image_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
from flask import Flask
from flask import send_from_directory
from flask import abort
from config_tools import ImageServer
import requests
import logging
import os
import time
class Server:
def __init__(self):
self.server = ImageServer()
try:
self.host = self.server.host
except AttributeError:
self.host = "0.0.0.0"
try:
self.port = self.server.port
except AttributeError:
self.port = "5000"
def check_running():
time.sleep(1)
srv = Server()
try:
r = requests.get("http://" + srv.host + ":" + str(srv.port), verify=False, timeout=1)
return "IMAGE SERVER RUNNING ON http://{}:{}/images/".format(srv.host, srv.port)
except (requests.exceptions.ConnectTimeout, requests.exceptions.ConnectionError):
return "IMAGE SERVER NOT RUNNING"
def start_srv():
server = Server()
app = Flask(__name__)
app.upload_folder = "images"
log = logging.getLogger("werkzeug")
os.environ['WERKZEUG_RUN_MAIN'] = 'true'
log.setLevel(logging.ERROR)
@app.route('/images/<path:c_name>')
def send_file(c_name):
cwd = os.getcwd()
images = os.listdir(os.path.join(cwd, "images"))
for img in images:
if (c_name + ".") in img:
return send_from_directory(app.upload_folder, img)
return abort(404)
try:
app.run(host=server.host, port=server.port)
except (OSError, TypeError) as e:
print(e)
if __name__ == '__main__':
start_srv()