app.py 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. #! /usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. # vim:fenc=utf-8
  4. #
  5. # Copyright © 2024 StrayWarrior <i@straywarrior.com>
  6. from flask import Flask, request, render_template
  7. from flaskext.mysql import MySQL
  8. from datetime import datetime, timedelta
  9. app = Flask(__name__)
  10. app.config['MYSQL_DATABASE_HOST'] = 'rm-bp17q95335a99272b.mysql.rds.aliyuncs.com'
  11. app.config['MYSQL_DATABASE_USER'] = 'crawler'
  12. app.config['MYSQL_DATABASE_PASSWORD'] = 'crawler123456@'
  13. app.config['MYSQL_DATABASE_DB'] = 'growth'
  14. mysql = MySQL()
  15. mysql.init_app(app)
  16. @app.route('/', methods=['GET', 'POST'])
  17. def index():
  18. if request.method == 'POST':
  19. staff_id = request.form['staff_id']
  20. video_id = request.form['video_id']
  21. if video_id:
  22. condition = "b.miniprogram_video_id = %s"
  23. if staff_id:
  24. condition += " AND a.carrier_id = %s"
  25. query = f"""
  26. SELECT a.carrier_id, a.remark, b.miniprogram_video_id, b.send_time,
  27. DATEDIFF(NOW(), b.send_time) as days
  28. FROM we_com_staff a JOIN we_com_message_attachment b
  29. ON a.id = b.staff_id
  30. WHERE {condition} ORDER BY send_time DESC
  31. """
  32. cursor = mysql.get_db().cursor()
  33. if staff_id:
  34. cursor.execute(query, (video_id, staff_id))
  35. else:
  36. cursor.execute(query, (video_id))
  37. result = cursor.fetchall()
  38. cursor.close()
  39. elif staff_id:
  40. query = """
  41. SELECT a.carrier_id, a.remark, b.miniprogram_video_id, b.send_time,
  42. DATEDIFF(NOW(), b.send_time) as days
  43. FROM we_com_staff a JOIN we_com_message_attachment b
  44. ON a.id = b.staff_id
  45. WHERE a.carrier_id = %s ORDER BY send_time DESC
  46. """
  47. cursor = mysql.get_db().cursor()
  48. cursor.execute(query, (staff_id,))
  49. result = cursor.fetchall()
  50. cursor.close()
  51. else:
  52. return render_template('index.html', results=[], form=request.form)
  53. return render_template('index.html', results=result, form=request.form)
  54. return render_template('index.html', results=[], form={'staff_id': '', 'video_id': ''})
  55. if __name__ == '__main__':
  56. app.run(debug=True)