|
|
@@ -0,0 +1,90 @@
|
|
|
+import json
|
|
|
+import requests
|
|
|
+import sqlite3
|
|
|
+import time
|
|
|
+import settings
|
|
|
+from pprint import pprint
|
|
|
+
|
|
|
+
|
|
|
+def send_notification(maintainer, player, property, old_value, new_value):
|
|
|
+ query = """SELECT discord FROM discord WHERE maintainer = ?;"""
|
|
|
+ conn = sqlite3.connect(settings.db_path + '/slork.sqlite')
|
|
|
+ conn.row_factory = sqlite3.Row
|
|
|
+ cur = conn.cursor()
|
|
|
+ cur.execute(query, (maintainer))
|
|
|
+ discord = cur.fetchone()['discord']
|
|
|
+ query = """SELECT value FROM player
|
|
|
+ WHERE maintainer = ?
|
|
|
+ AND player = ?
|
|
|
+ AND property = 'name';"""
|
|
|
+ cur.execute(query, (maintainer, player))
|
|
|
+ name = cur.fetchone()['name']
|
|
|
+ message = f"{name} [{player}] property '{property}' changed from {old_value} to {new_value}"
|
|
|
+ message_json = {"content": message}
|
|
|
+ response = requests.post(url, json=message_json)
|
|
|
+ if response.status_code in [200, 204]:
|
|
|
+ print("Webhook executed")
|
|
|
+ else:
|
|
|
+ print("status code {}: {}".format(response.status_code, response.content.decode("utf-8")))
|
|
|
+ if response.status_code == int(429):
|
|
|
+ sys.exit(1)
|
|
|
+
|
|
|
+def update_value(maintainer, player, property, new_value):
|
|
|
+ query = """INSERT OR REPLACE INTO player (maintainer, player, property, value)
|
|
|
+ VALUES (?, ?, ?, ?)"""
|
|
|
+ conn = sqlite3.connect(settings.db_path + '/slork.sqlite')
|
|
|
+ conn.row_factory = sqlite3.Row
|
|
|
+ cur = conn.cursor()
|
|
|
+ cur.execute(query, (maintainer, player, property, value))
|
|
|
+
|
|
|
+def cron():
|
|
|
+ query = """SELECT player_properties.maintainer,
|
|
|
+ maintainer.api, player_properties.player,
|
|
|
+ player_properties.property
|
|
|
+ FROM player_properties
|
|
|
+ JOIN maintainer ON player_properties.maintainer = maintainer.maintainer;"""
|
|
|
+ conn = sqlite3.connect(settings.db_path + '/slork.sqlite')
|
|
|
+ conn.row_factory = sqlite3.Row
|
|
|
+ cur = conn.cursor()
|
|
|
+
|
|
|
+ for row in cur.execute(query):
|
|
|
+ print(f"{row['maintainer']} - {row['api']} - {row['player']} - {row['property']}")
|
|
|
+ url = f"https://api.torn.com/user/{row['player']}?selections=personalstats,basic&key={row['api']}"
|
|
|
+ print(url)
|
|
|
+ response = requests.get(url)
|
|
|
+ player_json = None
|
|
|
+ print(response.text[:10])
|
|
|
+ if response and response.text:
|
|
|
+ player_json = json.loads(response.text)
|
|
|
+ # pprint(faction_json)
|
|
|
+ if 'error' in player_json:
|
|
|
+ pprint(faction_json)
|
|
|
+ break
|
|
|
+
|
|
|
+ # Check for changes
|
|
|
+ properties = json.loads(row['property'])
|
|
|
+ if 'name' not in properties:
|
|
|
+ properties.append(name)
|
|
|
+ query = """SELECT player.value, properties.parent
|
|
|
+ FROM player
|
|
|
+ JOIN properties ON player.property = properties.property
|
|
|
+ WHERE maintainer = ?
|
|
|
+ AND player = ?
|
|
|
+ AND player.property = ?;"""
|
|
|
+ for property in properties:
|
|
|
+ for p_row in cur.execute(query, (row['maintainer'], row['player'], property)):
|
|
|
+ print(f"property: '{property}' parent: '{p_row['parent']}' -> value: '{p_row['value']}'")
|
|
|
+ new_value = ''
|
|
|
+ if (p_row['parent']):
|
|
|
+ new_value = player_json[p_row['parent']][property]
|
|
|
+ else:
|
|
|
+ new_value = player_json[property]
|
|
|
+ print(f"new_value: '{new_value}'")
|
|
|
+ if p_row['value'] != new_value:
|
|
|
+ send_notification(row['maintainer'], row['player'], property, p_row['value'], new_value)
|
|
|
+ update_value(row['maintainer'], row['player'], property, new_value)
|
|
|
+
|
|
|
+ time.sleep(1)
|
|
|
+
|
|
|
+if __name__ == '__main__':
|
|
|
+ cron()
|