app.py 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. import os
  2. import sys
  3. sys.path.append(os.path.dirname(sys.path[0]))
  4. from flask import Flask, send_from_directory, make_response
  5. from utils.tools import get_result_file_content, get_ip_address, resource_path
  6. from utils.config import config
  7. import utils.constants as constants
  8. app = Flask(__name__)
  9. @app.route("/")
  10. def show_index():
  11. return get_result_file_content()
  12. @app.route("/favicon.ico")
  13. def favicon():
  14. return send_from_directory(resource_path('static/images'), 'favicon.ico',
  15. mimetype='image/vnd.microsoft.icon')
  16. @app.route("/txt")
  17. def show_txt():
  18. return get_result_file_content(file_type="txt")
  19. @app.route("/m3u")
  20. def show_m3u():
  21. return get_result_file_content(file_type="m3u")
  22. @app.route("/content")
  23. def show_content():
  24. return get_result_file_content(show_content=True)
  25. @app.route("/log")
  26. def show_log():
  27. log_path = resource_path(constants.sort_log_path)
  28. if os.path.exists(log_path):
  29. with open(log_path, "r", encoding="utf-8") as file:
  30. content = file.read()
  31. else:
  32. content = constants.waiting_tip
  33. response = make_response(content)
  34. response.mimetype = "text/plain"
  35. return response
  36. def run_service():
  37. try:
  38. if not os.environ.get("GITHUB_ACTIONS"):
  39. ip_address = get_ip_address()
  40. print(f"📄 Result content: {ip_address}/content")
  41. print(f"📄 Log content: {ip_address}/log")
  42. print(f"🚀 M3u api: {ip_address}/m3u")
  43. print(f"🚀 Txt api: {ip_address}/txt")
  44. print(f"✅ You can use this url to watch IPTV 📺: {ip_address}")
  45. app.run(host="0.0.0.0", port=config.app_port)
  46. except Exception as e:
  47. print(f"❌ Service start failed: {e}")
  48. if __name__ == "__main__":
  49. run_service()