Advertisement
szymski

Untitled

Apr 25th, 2019
178
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.68 KB | None | 0 0
  1. from flask import Flask, jsonify, request, render_template, send_from_directory
  2. from funcs import *
  3. from flask_uploads import UploadSet, configure_uploads, AUDIO
  4. from flask_cors import CORS
  5. from tinytag import TinyTag
  6. import subprocess
  7. import signal
  8. import datetime
  9. import os
  10. import time
  11.  
  12. app = Flask(__name__, static_url_path='/static')
  13. cors = CORS(app, resources={r"/*": {"origins": "*"}})
  14.  
  15. music = UploadSet('music', AUDIO)
  16.  
  17. app.config["UPLOADED_MUSIC_DEST"] = "static/audio"
  18. app.config["JSON_AS_ASCII"] = False
  19. configure_uploads(app, music)
  20.  
  21. pifm_proc = None
  22. playing_file = None
  23. start_time = 0
  24. streaming = False
  25. radio_text = ""
  26.  
  27. @app.route('/start', methods=["POST"])
  28. def start():
  29. global pifm_proc
  30. global playing_file
  31. global start_time
  32. global streaming
  33.  
  34. streaming = False
  35.  
  36. json = request.get_json()
  37. file_name = json["file_name"]
  38. freq = json["freq"]
  39. radio_text = json["radio_text"]
  40. # radio_text = "web-rpi-fm"
  41. # m = subprocess.Popen("./pifmrds -audio " + file_name + " -freq " + freq + " -rt " + radio_text)
  42. # m.wait()
  43. if pifm_proc and not pifm_proc.poll():
  44. print("Killing")
  45. # os.killpg(os.getpgid(pifm_proc.pid), signal.SIGTERM)
  46. subprocess.Popen("sudo killall pifmrds", shell=True)
  47. print("Killed")
  48. pifm_proc = None
  49.  
  50. cmd = "sox -t mp3 {} -t wav - | sudo ./pifmrds -audio - -freq {} -rt {}".format(file_name, freq, radio_text)
  51. print("Cmd: {}".format(cmd))
  52. pifm_proc = subprocess.Popen(cmd, shell=True, cwd="static/audio", preexec_fn=os.setsid)
  53.  
  54. playing_file = file_name
  55. start_time = time.time()
  56.  
  57. return jsonify(), 200
  58.  
  59. @app.route('/starturl', methods=["POST"])
  60. def starturl():
  61. global pifm_proc
  62. global playing_file
  63. global start_time
  64. global streaming
  65. global radio_text
  66.  
  67. streaming = True
  68.  
  69. json = request.get_json()
  70. file_name = json["file_name"]
  71. freq = json["freq"]
  72. radio_text = json["radio_text"]
  73. # radio_text = "web-rpi-fm"
  74. # m = subprocess.Popen("./pifmrds -audio " + file_name + " -freq " + freq + " -rt " + radio_text)
  75. # m.wait()
  76. if pifm_proc and not pifm_proc.poll():
  77. print("Killing")
  78. # os.killpg(os.getpgid(pifm_proc.pid), signal.SIGTERM)
  79. subprocess.Popen("sudo killall pifmrds", shell=True)
  80. print("Killed")
  81. pifm_proc = None
  82.  
  83. cmd = "sox -t mp3 {} -t wav - | sudo ./pifmrds -audio - -freq {} -rt {}".format(file_name, freq, radio_text)
  84. print("Cmd: {}".format(cmd))
  85. pifm_proc = subprocess.Popen(cmd, shell=True, preexec_fn=os.setsid)
  86.  
  87. playing_file = file_name
  88. start_time = time.time()
  89.  
  90. return jsonify(), 200
  91.  
  92. @app.route('/<path:path>')
  93. def send_static(path):
  94. if not os.path.isfile("static/" + path):
  95. return app.send_static_file('index.html')
  96. return send_from_directory('static', path)
  97.  
  98. @app.route('/')
  99. def root():
  100. return app.send_static_file('index.html')
  101.  
  102. @app.route('/stop', methods=["POST"])
  103. def stop():
  104. global pifm_proc
  105.  
  106. if pifm_proc and not pifm_proc.poll():
  107. print("Killing")
  108. # os.killpg(os.getpgid(pifm_proc.pid), signal.SIGTERM)
  109. subprocess.Popen("sudo killall pifmrds", shell=True)
  110. print("Killed")
  111. pifm_proc = None
  112.  
  113. return jsonify(), 200
  114.  
  115. @app.route('/status', methods=["GET"])
  116. def status():
  117. global pifm_proc
  118. global playing_file
  119. global start_time
  120. global streaming
  121. global radio_text
  122.  
  123. running = pifm_proc and not pifm_proc.poll()
  124.  
  125. if not running:
  126. return jsonify({
  127. "running": False,
  128. }), 200
  129.  
  130. if streaming:
  131. return jsonify({
  132. "running": True,
  133. "filename": playing_file,
  134. "name": radio_text,
  135. "time_elapsed": time.time() - start_time,
  136. }), 200
  137.  
  138. file = playing_file
  139. f = TinyTag.get("static/audio/" + file, image=True)
  140. if not f.title:
  141. f.title = file
  142.  
  143. img_path = None
  144.  
  145. img_exists = os.path.isfile("static/img/" + file.split(".")[0] + ".png")
  146. if img_exists:
  147. img_path = file.split(".")[0] + ".png"
  148.  
  149. return jsonify({
  150. "running": True,
  151. "filename": file,
  152. "name": f.title,
  153. "length": f.duration,
  154. "author": f.artist,
  155. "img": img_path,
  156. "time_elapsed": time.time() - start_time,
  157. }), 200
  158.  
  159. @app.route('/sysinfo')
  160. def sysinfo():
  161. return jsonify(get_sysinfo()), 200
  162.  
  163. @app.route('/ls')
  164. def file_list():
  165. payload = {}
  166. # p = subprocess.Popen("ls", stdout=subprocess.PIPE, shell=True, cwd="static/audio")
  167. # p.wait()
  168. # files = p.stdout.readlines()
  169. # files = [x.strip() for x in files]
  170. i = 0
  171. for r, d, f in os.walk("static/audio"):
  172. for f2 in f:
  173. try:
  174. file = f2
  175. f = TinyTag.get("static/audio/" + file, image=True)
  176. if not f.title:
  177. f.title = file
  178. img_exists = os.path.isfile("static/img/" + file.split(".")[0] + ".png")
  179. if img_exists:
  180. img_path = file.split(".")[0] + ".png"
  181. else:
  182. img_path = None
  183. payload[i] = {
  184. "filename": file,
  185. "name": f.title,
  186. "length": f.duration,
  187. "author": f.artist,
  188. "img": img_path
  189. }
  190. i += 1
  191. except:
  192. pass
  193. return jsonify(payload)
  194.  
  195. @app.route('/upload', methods=["POST"])
  196. def upload():
  197. if request.method == "POST" and 'audio' in request.files:
  198. filename = music.save(request.files['audio'])
  199. f = TinyTag.get("static/audio/" + filename, image=True)
  200. image_data = f.get_image()
  201. if image_data:
  202. image_name = filename.split(".")[0]
  203. with file("static/img/" + image_name + ".png", 'wb') as x:
  204. x.write(image_data)
  205. return filename
  206.  
  207. @app.route('/delete', methods=["POST"])
  208. def delete():
  209. if request.method == "POST":
  210. json = request.get_json()
  211. filename = json["filename"]
  212. os.remove("static/audio/" + filename)
  213. try:
  214. image_name = filename.split(".")[0]
  215. os.remove("static/audio/" + image_name + ".png")
  216. except:
  217. pass
  218.  
  219. return jsonify(), 200
  220.  
  221. if __name__ == "__main__":
  222. subprocess.Popen("sudo killall pifmrds", shell=True)
  223. app.run(port=9000, host='0.0.0.0')
  224.  
  225. # p = subprocess.Popen("sox -t mp3 nighoffire.mp3 -t wav - | sudo ./pi_fm_adv --audio - --freq 88", stdout=subprocess.PIPE, shell=True, cwd="static/audio")
  226. # p.wait()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement