-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathimport_wallet_data.py
More file actions
229 lines (198 loc) · 9.77 KB
/
import_wallet_data.py
File metadata and controls
229 lines (198 loc) · 9.77 KB
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
import pandas as pd
from db_connection import get_db_connection
import os
from sqlalchemy import text
def create_wallet_tables(engine):
"""Create necessary tables if they don't exist"""
with engine.connect() as connection:
# Create users table to track usernames across platforms
connection.execute(text("""
CREATE TABLE IF NOT EXISTS users (
id SERIAL PRIMARY KEY,
username VARCHAR(255) NOT NULL,
name VARCHAR(255),
bio TEXT,
is_verified BOOLEAN,
avatar_url TEXT,
twitter VARCHAR(255),
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
)
"""))
# Create wallets table to track all wallets and their platforms
connection.execute(text("""
CREATE TABLE IF NOT EXISTS wallets (
id SERIAL PRIMARY KEY,
user_id INTEGER REFERENCES users(id),
address VARCHAR(42) NOT NULL,
platform VARCHAR(50) NOT NULL, -- 'blub', 'wink', 'salvor'
initial_balance_usd DECIMAL(20, 2),
total_usd_sold DECIMAL(20, 2),
total_collections INTEGER,
total_nft_items INTEGER,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
)
"""))
# Commit the changes
connection.commit()
def import_blub_data(engine):
"""Import data from blub wallet CSV file"""
try:
# Read the CSV file
df = pd.read_csv('blub/wallet_data.csv')
# Clean and prepare the data
df['address'] = df['address'].str.lower() # Normalize addresses to lowercase
df['username_norm'] = df['name'].astype(str).str.strip().str.lower() # Normalize usernames
with engine.connect() as connection:
for _, row in df.iterrows():
# First, ensure user exists
user_result = connection.execute(text("""
INSERT INTO users (username, name, twitter)
VALUES (:username, :name, :twitter)
ON CONFLICT (username) DO UPDATE
SET name = EXCLUDED.name,
twitter = EXCLUDED.twitter,
updated_at = CURRENT_TIMESTAMP
RETURNING id
"""), {
'username': row['username_norm'],
'name': row['name'],
'twitter': row['social']
}).fetchone()
# Then insert wallet
connection.execute(text("""
INSERT INTO wallets (user_id, address, platform, initial_balance_usd, total_usd_sold)
VALUES (:user_id, :address, 'blub', :initial_balance_usd, :total_usd_sold)
ON CONFLICT (address, platform) DO UPDATE
SET initial_balance_usd = EXCLUDED.initial_balance_usd,
total_usd_sold = EXCLUDED.total_usd_sold,
updated_at = CURRENT_TIMESTAMP
"""), {
'user_id': user_result[0],
'address': row['address'],
'initial_balance_usd': row['initial_balance_usd'],
'total_usd_sold': row['total_usd_sold']
})
connection.commit()
print(f"Successfully processed {len(df)} records from blub wallet data")
except Exception as e:
print(f"Error importing blub data: {str(e)}")
def import_wink_data(engine):
"""Import data from wink wallet CSV file"""
try:
# Read the CSV file
df = pd.read_csv('wink/wink_wallet_data.csv')
# Clean and prepare the data
df['address'] = df['address'].str.lower() # Normalize addresses to lowercase
df['username_norm'] = df['name'].astype(str).str.strip().str.lower() # Normalize usernames
with engine.connect() as connection:
for _, row in df.iterrows():
# First, ensure user exists
user_result = connection.execute(text("""
INSERT INTO users (username, name, twitter)
VALUES (:username, :name, :twitter)
ON CONFLICT (username) DO UPDATE
SET name = EXCLUDED.name,
twitter = EXCLUDED.twitter,
updated_at = CURRENT_TIMESTAMP
RETURNING id
"""), {
'username': row['username_norm'],
'name': row['name'],
'twitter': row['social']
}).fetchone()
# Then insert wallet
connection.execute(text("""
INSERT INTO wallets (user_id, address, platform, initial_balance_usd, total_usd_sold)
VALUES (:user_id, :address, 'wink', :initial_balance_usd, :total_usd_sold)
ON CONFLICT (address, platform) DO UPDATE
SET initial_balance_usd = EXCLUDED.initial_balance_usd,
total_usd_sold = EXCLUDED.total_usd_sold,
updated_at = CURRENT_TIMESTAMP
"""), {
'user_id': user_result[0],
'address': row['address'],
'initial_balance_usd': row['initial_balance_usd'],
'total_usd_sold': row['total_usd_sold']
})
connection.commit()
print(f"Successfully processed {len(df)} records from wink wallet data")
except Exception as e:
print(f"Error importing wink data: {str(e)}")
def import_salvor_data(engine):
"""Import data from salvor wallet CSV file"""
try:
# Read the CSV file
df = pd.read_csv('salvor/salvor_mass_collection.csv')
# Clean and prepare the data
df['wallet_address'] = df['wallet_address'].str.lower() # Normalize addresses to lowercase
df['username_norm'] = df['username'].astype(str).str.strip().str.lower() # Normalize usernames
# Handle is_verified field - convert NaN to False
df['is_verified'] = df['is_verified'].fillna(False)
df['is_verified'] = df['is_verified'].map({'True': True, 'False': False, True: True, False: False})
# Convert numeric columns to integers
df['total_collections'] = pd.to_numeric(df['total_collections'], errors='coerce').fillna(0).astype(int)
df['total_nft_items'] = pd.to_numeric(df['total_nft_items'], errors='coerce').fillna(0).astype(int)
with engine.connect() as connection:
for _, row in df.iterrows():
# First, ensure user exists
user_result = connection.execute(text("""
INSERT INTO users (username, name, bio, is_verified, avatar_url, twitter)
VALUES (:username, :name, :bio, :is_verified, :avatar_url, :twitter)
ON CONFLICT (username) DO UPDATE
SET name = EXCLUDED.name,
bio = EXCLUDED.bio,
is_verified = EXCLUDED.is_verified,
avatar_url = EXCLUDED.avatar_url,
twitter = EXCLUDED.twitter,
updated_at = CURRENT_TIMESTAMP
RETURNING id
"""), {
'username': row['username_norm'],
'name': row['name'],
'bio': row['bio'] if pd.notna(row['bio']) else None,
'is_verified': bool(row['is_verified']),
'avatar_url': row['avatar_url'] if pd.notna(row['avatar_url']) else None,
'twitter': row['twitter'] if pd.notna(row['twitter']) else None
}).fetchone()
# Then insert wallet
connection.execute(text("""
INSERT INTO wallets (user_id, address, platform, total_collections, total_nft_items)
VALUES (:user_id, :address, 'salvor', :total_collections, :total_nft_items)
ON CONFLICT (address, platform) DO UPDATE
SET total_collections = EXCLUDED.total_collections,
total_nft_items = EXCLUDED.total_nft_items,
updated_at = CURRENT_TIMESTAMP
"""), {
'user_id': user_result[0],
'address': row['wallet_address'],
'total_collections': row['total_collections'],
'total_nft_items': row['total_nft_items']
})
connection.commit()
print(f"Successfully processed {len(df)} records from salvor wallet data")
except Exception as e:
print(f"Error importing salvor data: {str(e)}")
def main():
# Get database connection
engine = get_db_connection()
if not engine:
print("Failed to connect to database. Please check your database configuration.")
return
try:
# Create tables
print("Creating database tables...")
create_wallet_tables(engine)
# Import data
print("\nImporting blub wallet data...")
import_blub_data(engine)
print("\nImporting wink wallet data...")
import_wink_data(engine)
print("\nImporting salvor wallet data...")
import_salvor_data(engine)
print("\nData import completed successfully!")
except Exception as e:
print(f"An error occurred: {str(e)}")
if __name__ == "__main__":
main()