app.py 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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. from argparse import ArgumentParser
  10. app = Flask(__name__)
  11. app.config['MYSQL_DATABASE_HOST'] = 'rm-bp17q95335a99272b.mysql.rds.aliyuncs.com'
  12. app.config['MYSQL_DATABASE_USER'] = 'crawler'
  13. app.config['MYSQL_DATABASE_PASSWORD'] = 'crawler123456@'
  14. app.config['MYSQL_DATABASE_DB'] = 'growth'
  15. mysql = MySQL()
  16. mysql.init_app(app)
  17. @app.route('/', methods=['GET', 'POST'])
  18. def index():
  19. if request.method == 'POST':
  20. staff_id = request.form['staff_id']
  21. video_id = request.form['video_id']
  22. if video_id:
  23. condition = "b.miniprogram_video_id = %s"
  24. if staff_id:
  25. condition += " AND a.carrier_id = %s"
  26. query = f"""
  27. SELECT a.carrier_id, a.remark, b.miniprogram_video_id, b.send_time,
  28. DATEDIFF(NOW(), b.send_time) as days
  29. FROM we_com_staff a JOIN we_com_message_attachment b
  30. ON a.id = b.staff_id
  31. WHERE {condition} ORDER BY send_time DESC
  32. """
  33. cursor = mysql.get_db().cursor()
  34. if staff_id:
  35. cursor.execute(query, (video_id, staff_id))
  36. else:
  37. cursor.execute(query, (video_id))
  38. result = cursor.fetchall()
  39. cursor.close()
  40. elif staff_id:
  41. query = """
  42. SELECT a.carrier_id, a.remark, b.miniprogram_video_id, b.send_time,
  43. DATEDIFF(NOW(), b.send_time) as days
  44. FROM we_com_staff a JOIN we_com_message_attachment b
  45. ON a.id = b.staff_id
  46. WHERE a.carrier_id = %s ORDER BY send_time DESC
  47. """
  48. cursor = mysql.get_db().cursor()
  49. cursor.execute(query, (staff_id,))
  50. result = cursor.fetchall()
  51. cursor.close()
  52. else:
  53. return render_template('index.html', results=[], form=request.form)
  54. return render_template('index.html', results=result, form=request.form)
  55. return render_template('index.html', results=[], form={'staff_id': '', 'video_id': ''})
  56. if __name__ == '__main__':
  57. parser = ArgumentParser()
  58. parser.add_argument('--prod', action='store_true')
  59. parser.add_argument('--host', default='127.0.0.1')
  60. parser.add_argument('--port', type=int, default=8083)
  61. args = parser.parse_args()
  62. app.run(debug=not args.prod, host=args.host, port=args.port)