szymski

Untitled

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