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.

113 lines
3.3 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 successfully"
  51. @manager.command
  52. def delete_cronjobs():
  53. from os import path
  54. from crontab import CronTab
  55. cron = CronTab(user=True)
  56. cron.remove_all(comment='DOOBSAUTO')
  57. print "Existing cronjobs deleted successfully"
  58. @manager.command
  59. def admin(name):
  60. u = models.User.query.filter_by(nickname=name).first()
  61. if u and not u.admin:
  62. u.admin = True
  63. db.session.commit()
  64. print "User {} has been granted admin access.".format(name)
  65. @manager.command
  66. def calc_winrates():
  67. from app.analytics import calculate_winrates
  68. calculate_winrates()
  69. @manager.command
  70. def ts3_move_afk():
  71. from app.teamspeak import idle_mover
  72. tsServer = createTeamspeakInstance()
  73. idle_mover(tsServer)
  74. @manager.command
  75. def ts3_snapshot():
  76. from app.teamspeak import store_active_data
  77. tsServer = createTeamspeakInstance()
  78. store_active_data(tsServer)
  79. @manager.command
  80. def ts3_award_points():
  81. from app.teamspeak import award_idle_ts3_points
  82. tsServer = createTeamspeakInstance()
  83. award_idle_ts3_points(tsServer)
  84. @manager.command
  85. def ts3_process_events():
  86. from app.teamspeak import process_ts3_events
  87. tsServer = createTeamspeakInstance()
  88. process_ts3_events(tsServer)
  89. @manager.command
  90. def forum_award_points():
  91. for user in models.User.query.filter(models.User.forum_id != None).all():
  92. user.update_forum_posts()
  93. if __name__ == '__main__':
  94. manager.run()