cron.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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 update_missing_value(maintainer, player, property, value):
  37. query = """INSERT OR REPLACE INTO player (maintainer, player, property, value)
  38. VALUES (?, ?, ?, ?);"""
  39. conn = sqlite3.connect(settings.db_path + '/slork.sqlite')
  40. cur = conn.cursor()
  41. cur.execute(query, (maintainer, player, property, value))
  42. conn.commit()
  43. cur = Null
  44. conn = Null
  45. def cron():
  46. query = """SELECT player_properties.maintainer,
  47. maintainer.api, player_properties.player,
  48. player_properties.property
  49. FROM player_properties
  50. JOIN maintainer ON player_properties.maintainer = maintainer.maintainer;"""
  51. conn = sqlite3.connect(settings.db_path + '/slork.sqlite')
  52. conn.row_factory = sqlite3.Row
  53. cur = conn.cursor()
  54. for row in cur.execute(query):
  55. print(f"{row['maintainer']} - {row['api']} - {row['player']} - {row['property']}")
  56. url = f"https://api.torn.com/user/{row['player']}?selections=personalstats,basic&key={row['api']}"
  57. print(url)
  58. response = requests.get(url)
  59. player_json = None
  60. print(response.text[:10])
  61. if response and response.text:
  62. player_json = json.loads(response.text)
  63. # pprint(faction_json)
  64. if 'error' in player_json:
  65. pprint(faction_json)
  66. break
  67. # Check for changes
  68. properties = json.loads(row['property'])
  69. if 'name' not in properties:
  70. properties.append(name)
  71. pprint(properties)
  72. query = """SELECT player.value, properties.parent
  73. FROM player
  74. JOIN properties ON player.property = properties.property
  75. WHERE maintainer = ?
  76. AND player = ?
  77. AND player.property = ?;"""
  78. retrieved_properties = []
  79. for property in properties:
  80. p_rows = cur.execute(query, (row['maintainer'], row['player'], property))
  81. for p_row in p_rows:
  82. print(f"property: '{property}' parent: '{p_row['parent']}' -> value: '{p_row['value']}'")
  83. retrieved_properties.append(property)
  84. new_value = ''
  85. if (p_row['parent']):
  86. new_value = player_json[p_row['parent']][property]
  87. else:
  88. new_value = player_json[property]
  89. print(f"new_value: '{new_value}'")
  90. if p_row['value'] != new_value:
  91. send_notification(row['maintainer'], row['player'], property, p_row['value'], new_value)
  92. update_value(row['maintainer'], row['player'], property, new_value)
  93. new_properties = []
  94. for item in properties:
  95. if item not in retrieved_properties:
  96. new_properties.append(item)
  97. if len(new_properties) > 0:
  98. for item in new_properties:
  99. query = """SELECT parent FROM properties where property = ?;"""
  100. cur.execute(query, (item,))
  101. parent = cur.fetchone()['parent']
  102. if (parent):
  103. value = player_json[parent][property]
  104. else:
  105. value = player_json[property]
  106. print(f"item: {item} -> value: {value}")
  107. update_missing_value(row['maintainer'], row['player'], item, value)
  108. time.sleep(1)
  109. if __name__ == '__main__':
  110. cron()