DotaNoobs main site.
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.

101 lines
2.9 KiB

  1. #!venv/bin/python
  2. from flask import Flask
  3. from flask.ext.script import Manager, Server
  4. from flask.ext.migrate import Migrate, MigrateCommand
  5. from app import app, db, models
  6. #SQLALCHEMY_DATABASE_URI = 'mysql://root:$perwePP@localhost/dotanoobs'
  7. migrate = Migrate(app, db)
  8. manager = Manager(app)
  9. manager.add_command('db', MigrateCommand)
  10. def createTeamspeakInstance():
  11. import ts3
  12. s = ts3.TS3Server(app.config['TS3_HOST'], app.config['TS3_PORT'])
  13. s.login(app.config['TS3_USERNAME'], app.config['TS3_PASSWORD'])
  14. s.use(1)
  15. return s
  16. @manager.command
  17. def install_cronjobs():
  18. from os import path
  19. from crontab import CronTab
  20. cron = CronTab(user=True)
  21. # Clear out existing jobs
  22. cron.remove_all(comment='DOOBSAUTO')
  23. def make_job(job):
  24. p = path.realpath(__file__)
  25. c = cron.new(command='{}/venv/bin/python {} {}'.format(path.split(p)[0],\
  26. p, job), comment='DOOBSAUTO')
  27. return c
  28. # Create the jobs
  29. winrate = make_job('calc_winrates')
  30. ts3_move_afk = make_job('ts3_move_afk')
  31. ts3_snapshot = make_job('ts3_snapshot')
  32. ts3_award_points = make_job('ts3_award_points')
  33. ts3_process_events = make_job('ts3_process_events')
  34. # Set their frequency to run
  35. winrate.every(1).day()
  36. ts3_move_afk.every(app.config['MOVE_AFK_FREQUENCY']).minute()
  37. ts3_snapshot.every(app.config['SNAPSHOT_FREQUENCY']).hour()
  38. ts3_award_points.every(app.config['AWARD_POINTS_FREQUENCY']).minute()
  39. ts3_process_events.every(app.config['PROCESS_EVENTS_FREQUENCY']).hour()
  40. try:
  41. assert True == winrate.is_valid()
  42. assert True == ts3_move_afk.is_valid()
  43. assert True == ts3_snapshot.is_valid()
  44. assert True == ts3_award_points.is_valid()
  45. assert True == ts3_process_events.is_valid()
  46. except AssertionError as e:
  47. print "Problem installing cronjobs: {}".format(e)
  48. else:
  49. cron.write()
  50. print "Cron jobs written succesfully"
  51. @manager.command
  52. def admin(name):
  53. u = models.User.query.filter_by(nickname=name).first()
  54. if u and not u.admin:
  55. u.admin = True
  56. db.session.commit()
  57. print "User {} has been granted admin access.".format(name)
  58. @manager.command
  59. def calc_winrates():
  60. from app.analytics import calculate_winrates
  61. calculate_winrates()
  62. @manager.command
  63. def ts3_move_afk():
  64. from app.teamspeak import idle_mover
  65. tsServer = createTeamspeakInstance()
  66. idle_mover(tsServer)
  67. @manager.command
  68. def ts3_snapshot():
  69. from app.teamspeak import store_active_data
  70. tsServer = createTeamspeakInstance()
  71. store_active_data(tsServer)
  72. @manager.command
  73. def ts3_award_points():
  74. from app.teamspeak import award_idle_ts3_points
  75. tsServer = createTeamspeakInstance()
  76. award_idle_ts3_points(tsServer)
  77. @manager.command
  78. def ts3_process_events():
  79. from app.teamspeak import process_ts3_events
  80. tsServer = createTeamspeakInstance()
  81. process_ts3_events(tsServer)
  82. if __name__ == '__main__':
  83. manager.run()