Kaynağa Gözat

[Refactor] Split out maintainers

Foppe Hemminga 5 yıl önce
ebeveyn
işleme
f57da78618
1 değiştirilmiş dosya ile 82 ekleme ve 73 silme
  1. 82 73
      cron.py

+ 82 - 73
cron.py

@@ -63,86 +63,95 @@ def update_missing_value(maintainer, player, property, value):
     conn.close()
 
 def cron():
-    query_first = """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')
+    query_zero = """SELECT DISTINCT maintainer FROM maintainer;"""
+     conn = sqlite3.connect(settings.db_path + '/slork.sqlite')
     conn.row_factory = sqlite3.Row
     cur = conn.cursor()
-    cur.execute(query_first)
+    
+    query_first = """SELECT player_properties.maintainer,
+        maintainer.api, player_properties.player,
+        player_properties.property
+        FROM player_properties
+        JOIN maintainer ON player_properties.maintainer = maintainer.maintainer
+        WHERE player_properties.maintainer = ?;"""
 
-    for row in cur.fetchall():
-        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:
-            print(f"{row['maintainer']}")
-            pprint(player_json)
-            _send(row['maintainer'], '```'+pformat(player_json)+'```')
-            break
+    cur.execute(query_zero)
+    maintainers = cur.fetchall()
+    for maintainer in maintainers:
+        # maintainers
+        cur.execute(query_first, (maintainer['maintainer'],))
+        players = cur.fetchall()
+        for player in players:
+            # Rows for this maintainer
+            print(f"{player['maintainer']} - {player['api']} - {player['player']} - {player['property']}")
+            url = f"https://api.torn.com/user/{player['player']}?selections=personalstats,basic&key={player['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:
+                print(f"{player['maintainer']}")
+                pprint(player_json)
+                _send(player['maintainer'], '```'+pformat(player_json)+'```')
+                break
 
-        # Check for changes
-        properties = json.loads(row['property'])
-        if 'name' not in properties:
-            properties.append(name)
-        # pprint(properties)
-        query_second = """SELECT player.value, properties.parent
-            FROM player
-            JOIN properties ON player.property = properties.property
-            WHERE maintainer = ?
-            AND player = ?
-            AND player.property = ?;"""
-        retrieved_properties = []
-        for property in properties:
-            # pprint((row['maintainer'], row['player'], property,))
-            cur.execute(query_second, (row['maintainer'], row['player'], property,))
-            p_row = cur.fetchone()
-            if not p_row:
-                continue
-            # print(f"property: '{property}' parent: '{p_row['parent']}' -> value: '{p_row['value']}'")
-            retrieved_properties.append(property)
-            new_value = ''
-            try:
-                if (p_row['parent']):
-                    new_value = player_json[p_row['parent']][property]
-                else:
-                    new_value = player_json[property]
-            except KeyError:
-                new_value = 0
-            # print(f"p_row['value']: {p_row['value']} != new_value: '{new_value}'")
-            if str(p_row['value']) != str(new_value):
-                send_notification(row['maintainer'], row['player'], property, p_row['value'], new_value)
-                update_value(row['maintainer'], row['player'], property, new_value)
-        new_properties = []
-        # print(f"len(properties): {len(properties)}")
-        # print(f"len(retrieved_properties): {len(retrieved_properties)}")
-        for item in properties:
-            if item not in retrieved_properties:
-                new_properties.append(item)
-        if len(new_properties) > 0:
-            # print(f"len(new_properties): {len(new_properties)}")
-            for item in new_properties:
-                query_third = """SELECT parent FROM properties where property = ?;"""
-                cur.execute(query_third, (item,))
-                parent = cur.fetchone()['parent']
+            # Check for changes
+            properties = json.loads(player['property'])
+            if 'name' not in properties:
+                properties.append(name)
+            # pprint(properties)
+            query_second = """SELECT player.value, properties.parent
+                FROM player
+                JOIN properties ON player.property = properties.property
+                WHERE maintainer = ?
+                AND player = ?
+                AND player.property = ?;"""
+            retrieved_properties = []
+            for property in properties:
+                # pprint((player['maintainer'], player['player'], property,))
+                cur.execute(query_second, (player['maintainer'], player['player'], property,))
+                p_row = cur.fetchone()
+                if not p_row:
+                    continue
+                # print(f"property: '{property}' parent: '{p_row['parent']}' -> value: '{p_row['value']}'")
+                retrieved_properties.append(property)
+                new_value = ''
                 try:
-                    if (parent):
-                        value = player_json[parent][item]
+                    if (p_row['parent']):
+                        new_value = player_json[p_row['parent']][property]
                     else:
-                         value = player_json[item]
+                        new_value = player_json[property]
                 except KeyError:
-                    value = 0
-                print(f"item: {item} -> value: {value}")
-                update_missing_value(row['maintainer'], row['player'], item, value)
-        time.sleep(2)
+                    new_value = 0
+                # print(f"p_row['value']: {p_row['value']} != new_value: '{new_value}'")
+                if str(p_row['value']) != str(new_value):
+                    send_notification(row['maintainer'], player['player'], property, p_row['value'], new_value)
+                    update_value(player['maintainer'], player['player'], property, new_value)
+            new_properties = []
+            # print(f"len(properties): {len(properties)}")
+            # print(f"len(retrieved_properties): {len(retrieved_properties)}")
+            for item in properties:
+                if item not in retrieved_properties:
+                    new_properties.append(item)
+            if len(new_properties) > 0:
+                # print(f"len(new_properties): {len(new_properties)}")
+                for item in new_properties:
+                    query_third = """SELECT parent FROM properties where property = ?;"""
+                    cur.execute(query_third, (item,))
+                    parent = cur.fetchone()['parent']
+                    try:
+                        if (parent):
+                            value = player_json[parent][item]
+                        else:
+                             value = player_json[item]
+                    except KeyError:
+                        value = 0
+                    print(f"item: {item} -> value: {value}")
+                    update_missing_value(player['maintainer'], player['player'], item, value)
+            time.sleep(2)
     conn.close()
 
 if __name__ == '__main__':