diff --git a/lib/geolocProfiles.py b/lib/geolocProfiles.py index 0777516..4cb82e6 100755 --- a/lib/geolocProfiles.py +++ b/lib/geolocProfiles.py @@ -1,12 +1,12 @@ -import requests, sys +import requests from time import time from lib.cesiumCommon import CesiumCommon from lib.gvaWallets import ListWallets class GeolocProfiles(CesiumCommon): - # Configure JSON document POST ready to send def getCesiumProfiles(self): + # Send a POST request to the Cesium profiles API response = requests.post( "https://g1.data.e-is.pro/user/profile/_search?scroll=2m", json={ @@ -45,24 +45,24 @@ class GeolocProfiles(CesiumCommon): finalResult = response.json()["hits"]["hits"] while True: - # Effectuez une requête de défilement pour obtenir la page suivante + # Send a scroll request to get the next page response_scroll = requests.post( "https://g1.data.e-is.pro/_search/scroll", json={"scroll_id": scroll_id, "scroll": "2m"}, ) - # Vérifiez si la réponse est vide (aucun résultat) ou si la durée du défilement est écoulée + # Check if the response is empty (no results) or if there's an error if ( not response_scroll.json()["hits"]["hits"] or "error" in response_scroll.json() ): break else: - finalResult = finalResult + response_scroll.json()["hits"]["hits"] + finalResult.extend(response_scroll.json()["hits"]["hits"]) - # Traitez les résultats ici + # Process the results here - # Une fois terminé, supprimez le contexte de défilement + # Delete the scroll context when done requests.delete( "https://g1.data.e-is.pro/_search/scroll", json={"scroll_id": [scroll_id]} ) @@ -70,7 +70,8 @@ class GeolocProfiles(CesiumCommon): return finalResult def getGVAProfiles(self, node): - gva = ListWallets(node, False, False, False, False, True) + # Retrieve GVA profiles using the ListWallets class + gva = ListWallets(node, map=True) return gva.sendDoc() def formatProfiles(self, cesiumProfiles, gvaProfiles): @@ -78,52 +79,20 @@ class GeolocProfiles(CesiumCommon): for profile in cesiumProfiles: source = profile["_source"] pubkey = profile["_id"] - # if pubkey == "JEGCJSfKbFyxUnnHBQUqe9U4fhMMpdWvr7zdU56EZHsa": - # print(pubkey) - # sys.exit(0) if pubkey not in gvaProfiles: continue - try: - isMember = gvaProfiles[pubkey]["id"]["isMember"] - except: - isMember = False - - try: - userId = gvaProfiles[pubkey]["id"]["username"] - except: - userId = None - - try: - title = source["title"] - except: - title = None - - try: - city = source["city"] - except: - city = None - - try: - avatar = source["avatar"] - except: - avatar: None - - try: - socials = source["socials"] - except: - socials = None - - try: - description = source["description"] - except: - description = None - - try: - address = source["address"] - except: - address = None + # Extract necessary information from the profiles + id_info = gvaProfiles[pubkey].get("id") or {} + isMember = id_info.get("isMember", False) + userId = id_info.get("username") + title = source.get("title") + city = source.get("city") + avatar = source.get("avatar") + socials = source.get("socials") + description = source.get("description") + address = source.get("address") walletsResult.append( { @@ -140,6 +109,4 @@ class GeolocProfiles(CesiumCommon): } ) - finalResult = {"wallets": walletsResult, "time": int(time())} - - return finalResult + return {"wallets": walletsResult, "time": int(time())} diff --git a/lib/gvaWallets.py b/lib/gvaWallets.py index bce7226..6888ad4 100755 --- a/lib/gvaWallets.py +++ b/lib/gvaWallets.py @@ -1,28 +1,29 @@ #!/usr/bin/env python3 -import sys, re, os.path, json, ast -from termcolor import colored -from lib.natools import fmt, sign, get_privkey +import sys +import json from gql import gql, Client from gql.transport.aiohttp import AIOHTTPTransport - -PUBKEY_REGEX = "(?![OIl])[1-9A-Za-z]{42,45}" +from lib.natools import fmt, sign, get_privkey class ListWallets: - def __init__(self, node, brut, mbr, nonMbr, larf, map=False): - self.mbr = mbr - self.larf = larf - self.nonMbr = nonMbr - self.brut = brut - self.map = map + def __init__( + self, node=False, brut=False, mbr=False, nonMbr=False, larf=False, map=False + ): + # Initialize the ListWallets class with optional filters + self.mbr = mbr # Filter for members + self.larf = larf # Filter for non-empty identities + self.nonMbr = nonMbr # Filter for non-members + self.brut = brut # Output format flag (brut or JSON) + self.map = map # Output format flag (map or list) + # Define Duniter GVA node transport = AIOHTTPTransport(url=node) self.client = Client(transport=transport, fetch_schema_from_transport=True) def sendDoc(self): - # Build wallets generation document - + # Define the GraphQL query to retrieve wallet information queryBuild = gql( """ { @@ -46,58 +47,54 @@ class ListWallets: } } } - """ + """ ) - # Send wallets document try: + # Execute the GraphQL query queryResult = self.client.execute(queryBuild) except Exception as e: - sys.stderr.write("Echec de récupération de la liste:\n" + str(e) + "\n") + # Handle any exceptions that occur during the query + sys.stderr.write("Failed to retrieve the list:\n" + str(e) + "\n") sys.exit(1) jsonBrut = queryResult["wallets"]["edges"] walletList = [] walletMap = {} + for i, trans in enumerate(jsonBrut): dataWork = trans["node"] - if self.mbr and ( - dataWork["idty"] == None or dataWork["idty"]["isMember"] == False - ): - continue - if self.nonMbr and ( - dataWork["idty"] == None or dataWork["idty"]["isMember"] == True - ): - continue - if self.larf and (dataWork["idty"] != None): + identity = dataWork["idty"] + is_member = identity and identity["isMember"] + + # Apply filters based on member status and larf flag + member_filter = self.mbr and not is_member + non_member_filter = self.nonMbr and is_member + larf_filter = self.larf and identity + if member_filter or non_member_filter or larf_filter: continue + wallet_data = { + "pubkey": dataWork["script"], + "balance": dataWork["balance"]["amount"] / 100, + "id": identity, + } + if self.map: - walletMap[dataWork["script"]] = { - "balance": dataWork["balance"]["amount"] / 100, - "id": dataWork["idty"], - } + walletMap[dataWork["script"]] = wallet_data else: - walletList.append( - { - "pubkey": dataWork["script"], - "balance": dataWork["balance"]["amount"] / 100, - "id": dataWork["idty"], - } - ) + walletList.append(wallet_data) if self.brut: - names = [] - for dataWork in walletList: - if self.mbr or self.nonMbr: - names.append(dataWork["pubkey"] + " " + dataWork["id"]["username"]) - else: - names.append(dataWork["pubkey"]) - + # Generate a list of formatted wallet names using list comprehension + names = [ + wallet["pubkey"] + if not (self.mbr or self.nonMbr) or wallet["id"] is None + else f'{wallet["pubkey"]} {wallet["id"]["username"]}' + for wallet in walletList + ] return "\n".join(names) else: - if self.map: - return json.dumps(walletMap, indent=2) - else: - return json.dumps(walletList, indent=2) + # Return JSON data in either map or list format + return json.dumps(walletMap if self.map else walletList, indent=2)