cron.py 3.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. pprint(properties)
  63. query = """SELECT player.value, properties.parent
  64. FROM player
  65. JOIN properties ON player.property = properties.property
  66. WHERE maintainer = ?
  67. AND player = ?
  68. AND player.property = ?;"""
  69. for property in properties:
  70. for p_row in cur.execute(query, (row['maintainer'], row['player'], property)):
  71. print(f"property: '{property}' parent: '{p_row['parent']}' -> value: '{p_row['value']}'")
  72. new_value = ''
  73. if (p_row['parent']):
  74. new_value = player_json[p_row['parent']][property]
  75. else:
  76. new_value = player_json[property]
  77. print(f"new_value: '{new_value}'")
  78. if p_row['value'] != new_value:
  79. send_notification(row['maintainer'], row['player'], property, p_row['value'], new_value)
  80. update_value(row['maintainer'], row['player'], property, new_value)
  81. time.sleep(1)
  82. if __name__ == '__main__':
  83. cron()