You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

server.py 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. from os import path
  2. import json
  3. from flask_apscheduler import APScheduler
  4. from flask_socketio import SocketIO, emit
  5. from pony.flask import Pony
  6. from trackify.server.logger import Logger
  7. from trackify.server.views import HistoryView
  8. from trackify.tracker.tracker import scrape_current_track, \
  9. NotAuthorizedException
  10. def parse_json_config(config_path):
  11. if not path.isfile(config_path):
  12. Logger.get_logger().error('Config file not found')
  13. exit(1)
  14. with open(config_path) as f:
  15. try:
  16. return json.load(f)
  17. except json.JSONEncoder:
  18. Logger.get_logger().error('Configuration is not a valid JSON file.')
  19. exit(1)
  20. def get_server_config(config_path):
  21. config = parse_json_config(config_path)
  22. return config['server']
  23. def get_spotify_config(config_path):
  24. config = parse_json_config(config_path)['spotify']
  25. username = config['SPOTIPY_USER_NAME']
  26. client_id = config['SPOTIPY_CLIENT_ID']
  27. client_secret = config['SPOTIPY_CLIENT_SECRET']
  28. redirect_url = config['SPOTIPY_REDIRECT_URI']
  29. return username, client_id, client_secret, redirect_url
  30. def get_tracker_config(config_path):
  31. config = parse_json_config(config_path)
  32. return config['tracker']
  33. def run_tracker(socket, spotify_config):
  34. Logger.get_logger().info('Run tracker')
  35. username, client_id, client_secret, redirect_uri = spotify_config
  36. try:
  37. request = scrape_current_track(username,
  38. client_id,
  39. client_secret,
  40. redirect_uri)
  41. except NotAuthorizedException:
  42. Logger.get_logger().error('Failed to get user token')
  43. exit(1)
  44. if request is not None:
  45. Logger.get_logger().info('Got new track')
  46. socket.emit('response', json.dumps(request))
  47. else:
  48. Logger.get_logger().info('Currently not playing')
  49. def test_handler(data):
  50. print(data)
  51. def register_routes(flask_app):
  52. flask_app.add_url_rule('/api/history',
  53. view_func=HistoryView.as_view('history'))
  54. def register_event_handlers(flask_socket):
  55. flask_socket.on_event('history', test_handler)
  56. flask_socket.on_event('tracker', run_tracker)
  57. def register_jobs(flask_scheduler, socket, spotify_config, interval):
  58. flask_scheduler.add_job('run_tracker',
  59. run_tracker,
  60. trigger='interval',
  61. args=[socket, spotify_config],
  62. seconds=interval)
  63. def init_app(flask_app, config_path):
  64. server_config = get_server_config(config_path)
  65. spotify_config = get_spotify_config(config_path)
  66. tracker_interval = get_tracker_config(config_path)['INTERVAL']
  67. flask_app.config.update(
  68. DEBUG=server_config['DEBUG'],
  69. SECRET_KEY=server_config['SECRET_KEY'],
  70. )
  71. Pony(flask_app)
  72. flask_scheduler = APScheduler()
  73. flask_scheduler.api_enabled = True
  74. flask_scheduler.init_app(flask_app)
  75. flask_socket = SocketIO(flask_app)
  76. register_routes(flask_app)
  77. register_event_handlers(flask_socket)
  78. register_jobs(flask_scheduler,
  79. flask_socket,
  80. spotify_config,
  81. tracker_interval)
  82. return flask_scheduler, flask_socket