File size: 7,724 Bytes
5cd2b89
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# Collectors Quick Start Guide

## Files Created

```
/home/user/crypto-dt-source/collectors/
β”œβ”€β”€ __init__.py              # Package exports
β”œβ”€β”€ market_data.py           # Market data collectors (16 KB)
β”œβ”€β”€ explorers.py             # Blockchain explorer collectors (17 KB)
β”œβ”€β”€ news.py                  # News aggregation collectors (13 KB)
β”œβ”€β”€ sentiment.py             # Sentiment data collectors (7.8 KB)
β”œβ”€β”€ onchain.py               # On-chain analytics (placeholder, 13 KB)
β”œβ”€β”€ demo_collectors.py       # Comprehensive demo script (6.6 KB)
β”œβ”€β”€ README.md                # Full documentation
└── QUICK_START.md          # This file
```

## Quick Test

### Test All Collectors

```bash
cd /home/user/crypto-dt-source
python collectors/demo_collectors.py
```

### Test Individual Modules

```bash
# Market Data (CoinGecko, CoinMarketCap, Binance)
python -m collectors.market_data

# Blockchain Explorers (Etherscan, BscScan, TronScan)
python -m collectors.explorers

# News (CryptoPanic, NewsAPI)
python -m collectors.news

# Sentiment (Alternative.me Fear & Greed)
python -m collectors.sentiment

# On-chain Analytics (Placeholder)
python -m collectors.onchain
```

## Import and Use

### Collect All Market Data

```python
import asyncio
from collectors import collect_market_data

results = asyncio.run(collect_market_data())

for result in results:
    print(f"{result['provider']}: {result['success']}")
```

### Collect All Data from All Categories

```python
import asyncio
from collectors import (
    collect_market_data,
    collect_explorer_data,
    collect_news_data,
    collect_sentiment_data,
    collect_onchain_data
)

async def main():
    # Run all collectors concurrently
    results = await asyncio.gather(
        collect_market_data(),
        collect_explorer_data(),
        collect_news_data(),
        collect_sentiment_data(),
        collect_onchain_data()
    )

    market, explorers, news, sentiment, onchain = results

    print(f"Market data: {len(market)} sources")
    print(f"Explorers: {len(explorers)} sources")
    print(f"News: {len(news)} sources")
    print(f"Sentiment: {len(sentiment)} sources")
    print(f"On-chain: {len(onchain)} sources (placeholder)")

asyncio.run(main())
```

### Individual Collector Example

```python
import asyncio
from collectors.market_data import get_coingecko_simple_price

async def get_prices():
    result = await get_coingecko_simple_price()

    if result['success']:
        data = result['data']
        print(f"BTC: ${data['bitcoin']['usd']:,.2f}")
        print(f"ETH: ${data['ethereum']['usd']:,.2f}")
        print(f"BNB: ${data['binancecoin']['usd']:,.2f}")
        print(f"Data age: {result['staleness_minutes']:.2f} minutes")
    else:
        print(f"Error: {result['error']}")

asyncio.run(get_prices())
```

## Collectors Summary

### 1. Market Data (market_data.py)

| Function | Provider | API Key Required | Description |
|----------|----------|------------------|-------------|
| `get_coingecko_simple_price()` | CoinGecko | No | BTC, ETH, BNB prices with market data |
| `get_coinmarketcap_quotes()` | CoinMarketCap | Yes | Professional market data |
| `get_binance_ticker()` | Binance | No | Real-time 24hr ticker |
| `collect_market_data()` | All above | - | Collects from all sources |

### 2. Blockchain Explorers (explorers.py)

| Function | Provider | API Key Required | Description |
|----------|----------|------------------|-------------|
| `get_etherscan_gas_price()` | Etherscan | Yes | Current Ethereum gas prices |
| `get_bscscan_bnb_price()` | BscScan | Yes | BNB price and BSC stats |
| `get_tronscan_stats()` | TronScan | Optional | TRON network statistics |
| `collect_explorer_data()` | All above | - | Collects from all sources |

### 3. News Aggregation (news.py)

| Function | Provider | API Key Required | Description |
|----------|----------|------------------|-------------|
| `get_cryptopanic_posts()` | CryptoPanic | No | Latest crypto news posts |
| `get_newsapi_headlines()` | NewsAPI | Yes | Crypto-related headlines |
| `collect_news_data()` | All above | - | Collects from all sources |

### 4. Sentiment Analysis (sentiment.py)

| Function | Provider | API Key Required | Description |
|----------|----------|------------------|-------------|
| `get_fear_greed_index()` | Alternative.me | No | Market Fear & Greed Index |
| `collect_sentiment_data()` | All above | - | Collects from all sources |

### 5. On-Chain Analytics (onchain.py)

| Function | Provider | Status | Description |
|----------|----------|--------|-------------|
| `get_the_graph_data()` | The Graph | Placeholder | GraphQL blockchain data |
| `get_blockchair_data()` | Blockchair | Placeholder | Blockchain statistics |
| `get_glassnode_metrics()` | Glassnode | Placeholder | Advanced on-chain metrics |
| `collect_onchain_data()` | All above | - | Collects from all sources |

## API Keys Setup

Create a `.env` file or set environment variables:

```bash
# Market Data
export COINMARKETCAP_KEY_1="your_key_here"

# Blockchain Explorers
export ETHERSCAN_KEY_1="your_key_here"
export BSCSCAN_KEY="your_key_here"
export TRONSCAN_KEY="your_key_here"

# News
export NEWSAPI_KEY="your_key_here"
```

## Output Format

All collectors return standardized format:

```python
{
    "provider": "CoinGecko",                    # Provider name
    "category": "market_data",                  # Category
    "data": {...},                              # Raw API response
    "timestamp": "2025-11-11T00:20:00Z",       # Collection time
    "data_timestamp": "2025-11-11T00:19:30Z",  # Data timestamp
    "staleness_minutes": 0.5,                   # Data age
    "success": True,                            # Success flag
    "error": None,                              # Error message
    "error_type": None,                         # Error type
    "response_time_ms": 342.5                   # Response time
}
```

## Key Features

βœ“ **Async/Concurrent** - All collectors run asynchronously
βœ“ **Error Handling** - Comprehensive error handling and logging
βœ“ **Staleness Tracking** - Calculates data age in minutes
βœ“ **Rate Limiting** - Respects API rate limits
βœ“ **Retry Logic** - Automatic retries with exponential backoff
βœ“ **Structured Logging** - JSON-formatted logs
βœ“ **API Key Management** - Secure key handling from environment
βœ“ **Standardized Output** - Consistent response format
βœ“ **Production Ready** - Ready for production deployment

## Common Issues

### 1. Missing API Keys

```
Error: API key required but not configured for CoinMarketCap
```

**Solution:** Set the required environment variable:
```bash
export COINMARKETCAP_KEY_1="your_api_key"
```

### 2. Rate Limit Exceeded

```
Error Type: rate_limit
```

**Solution:** Collectors automatically retry with backoff. Check rate limits in provider documentation.

### 3. Network Timeout

```
Error Type: timeout
```

**Solution:** Collectors automatically increase timeout and retry. Check network connectivity.

## Next Steps

1. Run the demo: `python collectors/demo_collectors.py`
2. Configure API keys for providers requiring authentication
3. Integrate collectors into your monitoring system
4. Implement on-chain collectors (currently placeholders)
5. Add custom collectors following the existing patterns

## Support

- Full documentation: `collectors/README.md`
- Demo script: `collectors/demo_collectors.py`
- Configuration: `config.py`
- API Client: `utils/api_client.py`
- Logger: `utils/logger.py`

---

**Total Collectors:** 14 functions across 5 modules
**Total Code:** ~75 KB of production-ready Python code
**Status:** Ready for production use (except on-chain placeholders)