File size: 1,838 Bytes
5901908
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import os
import json
import requests
import pandas as pd
from dotenv import load_dotenv

# Load environment variables
load_dotenv()
ETHERSCAN_API_KEY = os.getenv("API_KEY_ETHERSCAN")
TOKENS_TO_WATCH = "ressources/dict_tokens_addr.json"  # Path to the file with tokens to watch
INFLUENCERS_FILE = "ressources/dict_influencers_addr.json"  # Path to the file with influencers' addresses

def fetch_token_balance(address, token_contract):
    url = f"https://api.etherscan.io/api?module=account&action=tokenbalance&contractaddress={token_contract}&address={address}&tag=latest&apikey={ETHERSCAN_API_KEY}"
    response = requests.get(url)
    if response.status_code == 200:
        data = response.json()
        if data["status"] == "1":
            balance = int(data["result"])
            return balance
        else:
            print(f"Error fetching token balance: {data.get('message')}")
    else:
        print(f"Error: HTTP {response.status_code}")
    return 0

def main():
    # Load tokens and influencers
    with open(TOKENS_TO_WATCH, "r") as file:
        tokens_to_watch = json.load(file)
    with open(INFLUENCERS_FILE, "r") as file:
        influencers = json.load(file)

    balances = []
    for name, address in influencers.items():
        for token_name, token_contract in tokens_to_watch.items():
            balance = fetch_token_balance(address, token_contract)
            if balance > 0:  # Optionally check if balance is above a certain threshold
                balances.append({
                    "Influencer": name,
                    "Token": token_name,
                    "Balance": balance / (10 ** 18)  # Convert from Wei
                })

    # Save to CSV
    df = pd.DataFrame(balances)
    df.to_csv("output/influencers_token_balances.csv", index=False)

if __name__ == "__main__":
    main()