-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.py
121 lines (91 loc) · 3.74 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
__author__ = 'Ben'
import os
import json
from flask import Flask, render_template, request, jsonify, send_from_directory, url_for, send_file
from flask_socketio import SocketIO, send, emit
import utilities.globals as globals
from utilities.globals import log, HEADER_SIZE
import thread
import main
app = Flask(__name__, static_url_path='')
app.config['SECRET_KEY'] = 'secret!'
# app.debug = True
globals.init_globals(app)
# landing page
@app.route("/")
def hello():
return send_from_directory('static', 'landing_page.html')
# get static file
@app.route('/<path:path>')
def send_static_file(path):
print 'static file', path
return send_from_directory('static', path)
# download video
@app.route('/download_video/', methods=['POST'])
def download_video():
data = dict((key, request.form.getlist(key) if len(request.form.getlist(key)) > 1 else request.form.getlist(key)[0]) for key in request.form.keys())
url = data.get('url')
log('Downloading youtube URL: ', url, header=HEADER_SIZE)
# todo stop js from clicking twice.
name = main.download_video(url)
return name
# process video
@app.route('/process_video/', methods=['POST'])
def process_video():
data = dict((key, request.form.getlist(key) if len(request.form.getlist(key)) > 1 else request.form.getlist(key)[0]) for key in request.form.keys())
current_dir = os.path.dirname(os.path.realpath(__file__))
video_path = os.path.join(current_dir, 'example_images', data['name'], data['name'] + '.mp4') #TODO VERY CAREFUL HERE MIGHT NOT BE MP4
# Incase in future I want to specify multiplier
if data.get('multiplier'):
thread.start_new_thread(main.process_video, (video_path, data['url'], data['multiplier']))
else:
thread.start_new_thread(main.process_video, (video_path, data['url']))
# todo change
return ''
# get image
@app.route('/example_images/<path:path>')
def send_image(path):
current_dir = os.path.dirname(os.path.realpath(__file__))
full_path = os.path.join(current_dir, 'example_images', path)
# print full_path
return send_file(full_path, mimetype='image/png')
# get result_struct.json
@app.route("/get_json/<path:video_folder>", methods=['GET'])
def get_json_struct(video_folder):
current_dir = os.path.dirname(os.path.realpath(__file__))
json_struct_path = os.path.join(current_dir, 'example_images', video_folder, 'metadata', 'result_struct.json')
# print 'json_struct_path: ', json_struct_path
json_struct = {'images': []}
if os.path.isfile(json_struct_path):
with open(json_struct_path) as data_file:
json_struct = json.load(data_file)
# print json_struct
return jsonify(**json_struct)
return 'No json struct found!'
# get all_videos json
@app.route("/get_all_videos", methods=['GET'])
def get_json_videos():
current_dir = os.path.dirname(os.path.realpath(__file__))
json_videos_path = os.path.join(current_dir, 'example_images', 'all_videos.json')
# print 'json_struct_path: ', json_videos_path
json_videos = {'videos': []}
if os.path.isfile(json_videos_path):
with open(json_videos_path) as data_file:
json_videos = json.load(data_file)
# print json_videos
return jsonify(**json_videos)
return jsonify(**json_videos)
# ALL FLASH SOCKETS BELOW
def ack():
print 'message was received!'
@globals.socketio.on('connect')
def test_connect():
print('Client connected')
# emit('print_event', {'s': 'Connected to server', 'header' : 6})
log('Connected to server', header=4)
@globals.socketio.on('disconnect')
def test_disconnect():
print('Client disconnected')
if __name__ == "__main__":
print 'Running socketio on app'
globals.socketio.run(app, debug=True, log_output=False)