cron.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. import json
  2. import requests
  3. import sqlite3
  4. import time
  5. import settings
  6. from pprint import pprint
  7. def send_notification(maintainer, player, property, old_value, new_value):
  8. query = """SELECT discord FROM discord WHERE maintainer = ?;"""
  9. conn = sqlite3.connect(settings.db_path + '/slork.sqlite')
  10. conn.row_factory = sqlite3.Row
  11. cur = conn.cursor()
  12. cur.execute(query, (maintainer))
  13. discord = cur.fetchone()['discord']
  14. query = """SELECT value FROM player
  15. WHERE maintainer = ?
  16. AND player = ?
  17. AND property = 'name';"""
  18. cur.execute(query, (maintainer, player))
  19. name = cur.fetchone()['name']
  20. message = f"{name} [{player}] property '{property}' changed from {old_value} to {new_value}"
  21. message_json = {"content": message}
  22. response = requests.post(url, json=message_json)
  23. if response.status_code in [200, 204]:
  24. print("Webhook executed")
  25. else:
  26. print("status code {}: {}".format(response.status_code, response.content.decode("utf-8")))
  27. if response.status_code == int(429):
  28. sys.exit(1)
  29. def update_value(maintainer, player, property, new_value):
  30. query = """INSERT OR REPLACE INTO player (maintainer, player, property, value)
  31. VALUES (?, ?, ?, ?)"""
  32. conn = sqlite3.connect(settings.db_path + '/slork.sqlite')
  33. conn.row_factory = sqlite3.Row
  34. cur = conn.cursor()
  35. cur.execute(query, (maintainer, player, property, value))
  36. def cron():
  37. query = """SELECT player_properties.maintainer,
  38. maintainer.api, player_properties.player,
  39. player_properties.property
  40. FROM player_properties
  41. JOIN maintainer ON player_properties.maintainer = maintainer.maintainer;"""
  42. conn = sqlite3.connect(settings.db_path + '/slork.sqlite')
  43. conn.row_factory = sqlite3.Row
  44. cur = conn.cursor()
  45. for row in cur.execute(query):
  46. print(f"{row['maintainer']} - {row['api']} - {row['player']} - {row['property']}")
  47. url = f"https://api.torn.com/user/{row['player']}?selections=personalstats,basic&key={row['api']}"
  48. print(url)
  49. response = requests.get(url)
  50. player_json = None
  51. print(response.text[:10])
  52. if response and response.text:
  53. player_json = json.loads(response.text)
  54. # pprint(faction_json)
  55. if 'error' in player_json:
  56. pprint(faction_json)
  57. break
  58. # Check for changes
  59. properties = json.loads(row['property'])
  60. if 'name' not in properties:
  61. properties.append(name)
  62. query = """SELECT player.value, properties.parent
  63. FROM player
  64. JOIN properties ON player.property = properties.property
  65. WHERE maintainer = ?
  66. AND player = ?
  67. AND player.property = ?;"""
  68. for property in properties:
  69. for p_row in cur.execute(query, (row['maintainer'], row['player'], property)):
  70. print(f"property: '{property}' parent: '{p_row['parent']}' -> value: '{p_row['value']}'")
  71. new_value = ''
  72. if (p_row['parent']):
  73. new_value = player_json[p_row['parent']][property]
  74. else:
  75. new_value = player_json[property]
  76. print(f"new_value: '{new_value}'")
  77. if p_row['value'] != new_value:
  78. send_notification(row['maintainer'], row['player'], property, p_row['value'], new_value)
  79. update_value(row['maintainer'], row['player'], property, new_value)
  80. time.sleep(1)
  81. if __name__ == '__main__':
  82. cron()