-
Notifications
You must be signed in to change notification settings - Fork 0
/
Inferno_api.py
171 lines (128 loc) · 4.17 KB
/
Inferno_api.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
171
import os, sys
import subprocess
from subprocess import Popen
from Inferno_db import influx
from flask import Flask
from flask import request, Response, make_response
from functools import wraps
import threading
from threading import Thread
dpmi = Flask(__name__)
interface="wlp2s0"
directory="/home/vivek/consumer-bitrate"
global mainstream
mainstream=[]
global streams
streams=[]
def auth(w):
@wraps(w)
def q(*args, **kwargs):
r = request.authorization
if r and r.username == 'dpmi' and r.password == 'dpmi':
return w(*args, **kwargs)
return make_response('\n...Could not verify...\nPlease check the credentials\n', 401, {'WWW-Authenticate' : 'Basic realm = "login required"'})
return q
def pkill():
os.system('sudo pkill bitrate')
return
def bitrate(str):
os.chdir(directory)
bitrate=subprocess.Popen(["unbuffer","./bitrate","-i",interface,str],stdout=subprocess.PIPE)
influx_thread=threading.Thread(target=influx,args=(bitrate.stdout,str,)).start()
@dpmi.route('/startstream/<stream>', methods=['GET'])
@auth
def main(stream):
global mainstream
mul=stream.split(',')
if len(mul)>1:
return '\n...start with only one stream, use "addstream" for adding multiple streams...\n\n'
else:
if not streams:
if stream in mainstream:
return '\n... bitrate stream %s is already running...\n\n' %stream
else:
streams.append(stream)
mainstream=mainstream+streams
bitrate_thread=threading.Thread(target=bitrate,args=(stream,)).start()
bitrate_thread.deamon=True
return '\n...bitrate stream %s started...\n\n' %stream
elif stream in mainstream:
return '\n...bitrate stream %s is already running...\n\n' %stream
else:
return '\n...a stream has already been started, use "addstream" to add the streams...\n\n'
@dpmi.route('/showstream', methods=['GET'])
@auth
def show():
if not mainstream:
return '\n...No streams available...\n\n'
else:
show=" ".join(str(S) for S in mainstream)
return '\n...running bitrate streams %s...\n\n' %show
@dpmi.route('/addstream/<add>', methods=['GET'])
@auth
def add(add):
global mainstream
addstream=add.split(',')
b=",".join(addstream)
already=[]
already=list(set(addstream).intersection(mainstream))
stralready=" ".join(str(j) for j in already)
new=[]
new=list(set(addstream)-set(already))
strnew=" ".join(str(i) for i in new)
mainstream=mainstream+new
for s in new:
bitrate_add_thread=threading.Thread(target=bitrate,args=(s,)).start()
bitrate_add_thread.deamon=True
if not already:
return '\n...adding bitrate streams %s...\n\n' %strnew
else:
return '\n...stream %s already running...\n...streams %s added...\n\n' %(stralready,strnew)
@dpmi.route('/deletestream/<delet>', methods=['GET'])
@auth
def delete(delet):
global mainstream
delet=delet.split(',')
suredel=[]
suredel=list(set(delet).intersection(mainstream))
strsuredel=" ".join(str(l) for l in suredel)
cantdel=[]
cantdel=list(set(delet)-set(suredel))
strcantdel=" ".join(str(m) for m in cantdel)
mainstream=list(set(mainstream)-set(suredel))
strmainstream=",".join(str(k) for k in mainstream)
if not suredel:
return '\n...stream(s) not available to delete...\n\n'
else:
pkill()
for h in mainstream:
bitrate_add_thread=threading.Thread(target=bitrate,args=(h,)).start()
bitrate_add_thread.deamon=True
if set(suredel).intersection(streams)!=0 :
del streams[:]
if not cantdel:
return "\n...bitrate stream %s deleted...\n\n" %(strsuredel)
else:
return "\n...bitrate stream %s deleted...\n...bitrate stream %s not available to delete...\n\n" %(strsuredel,strcantdel)
@dpmi.route('/changestream/<stream>', methods=['GET'])
@auth
def change(stream):
global ch
global mainstream
ch=stream
if ch in mainstream:
return '\n...bitrate stream %s already running, change to another stream...\n\n' %ch
else:
stop()
del streams[:]
mainstream=list(set(mainstream)-set(streams))
main(ch)
return '\n...bitrate stream changed to %s...\n\n' %ch
@dpmi.route('/stop', methods=['GET'])
@auth
def stop():
pkill()
del (mainstream[:],streams[:])
return "\n...bitrate stream killed...\n\n"
if __name__ == "__main__":
dpmi.run(host='localhost', port=5000, debug=True)