web.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. #!/usr/bin/env python3
  2. from constants import BASE_PATH,PORT
  3. import datetime
  4. import json
  5. import tasks
  6. import audio_timings
  7. from db import db
  8. from flask import Flask, jsonify, request
  9. from rq import Queue
  10. app = Flask(__name__)
  11. d=db()
  12. @app.route(BASE_PATH + '/')
  13. def index():
  14. return "Hello world"
  15. @app.route(BASE_PATH + '/list/uploaded/')
  16. @app.route(BASE_PATH + '/list/uploaded')
  17. def uploaded_list():
  18. ret={'list': d.uploaded()}
  19. return jsonify(ret)
  20. @app.route(BASE_PATH + '/list/raw/')
  21. @app.route(BASE_PATH + '/list/raw')
  22. def raw_list():
  23. ret={'list': d.raw() }
  24. return jsonify(ret)
  25. @app.route(BASE_PATH + '/list/processed/')
  26. @app.route(BASE_PATH + '/list/processed')
  27. def processed_list():
  28. ret={'list': d.processed() }
  29. return jsonify(ret)
  30. @app.route(BASE_PATH + '/list/jobs/')
  31. @app.route(BASE_PATH + '/list/jobs')
  32. def jobs():
  33. working = tasks.jobs()
  34. return json.dumps({"working":working})
  35. @app.route(BASE_PATH + '/raw/<video_id>')
  36. def raw_file(video_id):
  37. ret=d.raw(video_id)
  38. print(ret)
  39. if ret is not None:
  40. ret["file"]=ret["file"].split("front")[1] #FIXME, path relativo al webserver
  41. return jsonify(ret)
  42. return "{}"
  43. @app.route(BASE_PATH + '/processed/<video_id>')
  44. def processed_file(video_id):
  45. ret=d.processed(video_id)
  46. ret["out_fname"]=ret["out_fname"].split("front")[1] #FIXME, path relativo al webserver
  47. return jsonify(ret)
  48. @app.route(BASE_PATH + '/timings/<video_id>')
  49. def get_timings(video_id):
  50. ret=d.raw(video_id)
  51. if ret is not None:
  52. timings = audio_timings.parse_file(ret["file"])
  53. return jsonify({'timings': timings})
  54. return jsonify({})
  55. @app.route(BASE_PATH + '/crop/', methods=["POST"])
  56. def crop():
  57. ret={"status":"Procesando"}
  58. today=datetime.date.today()
  59. data = request.get_json()
  60. video=d.raw(data["id"])
  61. if "censuras" not in data:
  62. data["censuras"] = []
  63. out_filename="%s_%s_%s.mp4" % (data["curso"],
  64. today.strftime('%Y%m%d'),
  65. data["desc"])
  66. job = tasks.crop_video.delay(
  67. video["file"],
  68. str(data["inicio"]),
  69. str(data["fin"]),
  70. data["censuras"],
  71. out_filename)
  72. job.meta["type"]="Cortar"
  73. job.meta["target"]=out_filename
  74. job.save()
  75. ret["job_id"]=job.get_id()
  76. return jsonify(ret)
  77. @app.route(BASE_PATH + '/upload/', methods=["POST"])
  78. def upload():
  79. ret={"status":"Procesando"}
  80. today=datetime.date.today()
  81. data = request.get_json()
  82. video=data["id"]
  83. prof=data["prof"]
  84. texto1=data["texto1"]
  85. texto2=data["texto2"]
  86. if video is None or prof is None or texto1 is None or texto2 is None:
  87. return jsonify({"status":"Error, faltan datos"})
  88. tasks.upload_video(video,prof,texto1,texto2)
  89. return jsonify(ret)
  90. @app.route(BASE_PATH + '/cargar/<video_id>')
  91. def cargar(video_id):
  92. return jsonify(tasks.cargar_plataforma(video_id))
  93. @app.route(BASE_PATH + '/reupload/<video_id>')
  94. def reupload(video_id):
  95. today=datetime.date.today()
  96. data = d.videoData(video_id)
  97. video=data["video"]
  98. tsplit=data["title"].split("-")
  99. prof=tsplit[0]
  100. texto1=tsplit[1]
  101. texto2=tsplit[2]
  102. cutid = d.findCutId(video)
  103. tasks.upload_video(cutid,prof,texto1,texto2)
  104. ret={"status":"Procesando"}
  105. return jsonify(ret)
  106. @app.route(BASE_PATH + '/job')
  107. def job():
  108. job = tasks.long_job.delay("test")
  109. ret = {"id":job.get_id()}
  110. return jsonify(ret)
  111. @app.route(BASE_PATH+"/results/<job_key>", methods=['GET'])
  112. def get_results(job_key):
  113. try:
  114. ret=tasks.result(job_key)
  115. except Exception as e:
  116. print(e)
  117. return "Error. Probablemente job_key es invalido", 400
  118. return ret
  119. if __name__ == '__main__':
  120. app.run(host="0.0.0.0",port=PORT, debug=True)