szymski

Untitled

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