-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathdata_export.py
More file actions
309 lines (239 loc) Β· 9.23 KB
/
Copy pathdata_export.py
File metadata and controls
309 lines (239 loc) Β· 9.23 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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
"""
Data export example for GMGN API.
This example demonstrates:
- Real-time data export to files
- CSV, JSON, and Database export formats
- Data filtering before export
- File rotation and compression
Built with Chipa Editor - https://chipaeditor.com/?utm_source=code&utm_medium=example&utm_campaign=gmgn_api&utm_term=export&utm_content=docstring
"""
import asyncio
import logging
from decimal import Decimal
from pathlib import Path
from gmgnapi import (
GmGnEnhancedClient,
TokenFilter,
DataExportConfig,
)
# Configure logging
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s'
)
logger = logging.getLogger(__name__)
async def json_export_example():
"""Example of exporting data to JSON files."""
logger.info("ποΈ Starting JSON Export Example")
# Configure JSON export
export_config = DataExportConfig(
enabled=True,
format="json",
file_path="./exports/json_data",
max_file_size_mb=10,
rotation_interval_hours=1,
compress=False,
include_metadata=True,
)
# Filter for high-value tokens only
token_filter = TokenFilter(
min_market_cap=Decimal("25000"),
min_liquidity=Decimal("5000"),
min_holder_count=5,
)
client = GmGnEnhancedClient(
export_config=export_config,
token_filter=token_filter,
)
async def on_new_pool(pool_info):
logger.info(f"π Exported new pool data to JSON")
client.on_new_pool(on_new_pool)
await client.connect()
await client.subscribe_new_pools()
# Run for 5 minutes
logger.info("β±οΈ Collecting data for 5 minutes...")
await asyncio.sleep(300)
await client.disconnect()
# Check exported files
export_path = Path("./exports/json_data")
if export_path.exists():
files = list(export_path.glob("*.json"))
logger.info(f"β
Created {len(files)} JSON export files")
for file in files:
size_kb = file.stat().st_size / 1024
logger.info(f" π {file.name}: {size_kb:.1f} KB")
async def csv_export_example():
"""Example of exporting data to CSV files."""
logger.info("π Starting CSV Export Example")
# Configure CSV export
export_config = DataExportConfig(
enabled=True,
format="csv",
file_path="./exports/csv_data",
max_file_size_mb=5,
rotation_interval_hours=2,
compress=True, # Enable compression for CSV
include_metadata=True,
)
# Filter for active tokens with trading volume
token_filter = TokenFilter(
min_volume_24h=Decimal("1000"),
exchanges=["raydium", "orca"],
exclude_symbols=["TEST", "FAKE"],
)
client = GmGnEnhancedClient(
export_config=export_config,
token_filter=token_filter,
)
async def on_pair_update(pair_data):
logger.info(f"π Exported pair update to CSV")
client.on_pair_update(on_pair_update)
await client.connect()
await client.subscribe_pair_updates()
# Run for 3 minutes
logger.info("β±οΈ Collecting data for 3 minutes...")
await asyncio.sleep(180)
await client.disconnect()
# Check exported files
export_path = Path("./exports/csv_data")
if export_path.exists():
files = list(export_path.glob("*.csv*")) # Include compressed files
logger.info(f"β
Created {len(files)} CSV export files")
for file in files:
size_kb = file.stat().st_size / 1024
logger.info(f" π {file.name}: {size_kb:.1f} KB")
async def database_export_example():
"""Example of exporting data to SQLite database."""
logger.info("ποΈ Starting Database Export Example")
# Configure database export
export_config = DataExportConfig(
enabled=True,
format="database",
file_path="./exports/db_data",
include_metadata=True,
)
# No filtering - capture everything for analysis
client = GmGnEnhancedClient(export_config=export_config)
async def on_message(message):
# Log every 50th message to avoid spam
if hasattr(on_message, 'count'):
on_message.count += 1
else:
on_message.count = 1
if on_message.count % 50 == 0:
logger.info(f"πΎ Exported {on_message.count} messages to database")
client.on_message(on_message)
await client.connect()
await client.subscribe_all_channels()
# Run for 2 minutes
logger.info("β±οΈ Collecting data for 2 minutes...")
await asyncio.sleep(120)
await client.disconnect()
# Check database file
db_path = Path("./exports/db_data/gmgn_data.db")
if db_path.exists():
size_kb = db_path.stat().st_size / 1024
logger.info(f"β
Created database: {size_kb:.1f} KB")
# Simple query to show data
import sqlite3
conn = sqlite3.connect(str(db_path))
cursor = conn.cursor()
cursor.execute("SELECT COUNT(*) FROM messages")
count = cursor.fetchone()[0]
logger.info(f" π Total messages in database: {count:,}")
cursor.execute("SELECT channel, COUNT(*) FROM messages GROUP BY channel")
channel_counts = cursor.fetchall()
for channel, count in channel_counts:
logger.info(f" π‘ {channel}: {count:,} messages")
conn.close()
async def filtered_export_example():
"""Example of advanced filtering before export."""
logger.info("π Starting Filtered Export Example")
# Configure export with strict filtering
export_config = DataExportConfig(
enabled=True,
format="json",
file_path="./exports/filtered_data",
include_metadata=True,
)
# Very strict filtering for high-quality tokens only
token_filter = TokenFilter(
min_market_cap=Decimal("100000"), # $100k minimum
min_liquidity=Decimal("50000"), # $50k minimum
min_volume_24h=Decimal("10000"), # $10k minimum
min_holder_count=50, # 50+ holders
exchanges=["raydium"], # Only Raydium
exclude_symbols=[ # Exclude potential scams
"SCAM", "TEST", "FAKE", "MEME",
"SHIT", "TRASH", "PUMP", "DUMP"
],
max_risk_score=0.3, # Very low risk only
)
client = GmGnEnhancedClient(
export_config=export_config,
token_filter=token_filter,
)
high_quality_pools = []
async def on_new_pool(pool_info):
"""Only high-quality pools pass the filter."""
if pool_info.pools:
pool = pool_info.pools[0]
token_info = pool.bti
if token_info:
symbol = getattr(token_info, 's', 'Unknown')
market_cap = getattr(token_info, 'mc', 0)
high_quality_pools.append({
'symbol': symbol,
'market_cap': market_cap,
'pool_address': pool.a,
})
logger.warning(
f"β HIGH QUALITY: {symbol} with ${market_cap:,.0f} market cap"
)
client.on_new_pool(on_new_pool)
await client.connect()
await client.subscribe_new_pools()
# Run for 10 minutes to find quality tokens
logger.info("β±οΈ Searching for high-quality tokens for 10 minutes...")
await asyncio.sleep(600)
await client.disconnect()
# Report results
logger.info(f"β
Found {len(high_quality_pools)} high-quality pools")
if high_quality_pools:
logger.info("π Top quality pools found:")
for pool in high_quality_pools[:5]: # Show top 5
logger.info(f" {pool['symbol']}: ${pool['market_cap']:,.0f}")
async def main():
"""Run all export examples."""
logger.info("π Starting Data Export Examples")
logger.info("="*60)
try:
# Create export directories
Path("./exports").mkdir(exist_ok=True)
# Run examples sequentially
await json_export_example()
await asyncio.sleep(2)
await csv_export_example()
await asyncio.sleep(2)
await database_export_example()
await asyncio.sleep(2)
await filtered_export_example()
logger.info("="*60)
logger.info("β
All export examples completed successfully!")
# Summary of exports
export_path = Path("./exports")
if export_path.exists():
total_files = len(list(export_path.rglob("*")))
logger.info(f"π Total export files created: {total_files}")
# Calculate total size
total_size = sum(
f.stat().st_size for f in export_path.rglob("*") if f.is_file()
)
total_size_kb = total_size / 1024
logger.info(f"πΎ Total export size: {total_size_kb:.1f} KB")
except Exception as e:
logger.error(f"β Error in export examples: {e}")
raise
if __name__ == "__main__":
# Run the data export examples
asyncio.run(main())