-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathData_Extractor
More file actions
428 lines (342 loc) · 15.9 KB
/
Copy pathData_Extractor
File metadata and controls
428 lines (342 loc) · 15.9 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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
#!/usr/bin/env python3
"""
IMEA Direct Data Extractor - Simplified Version
Extracts agricultural data from IMEA API and saves to CSV files
Usage: python imea_extractor.py
Output: CSV files in ./datasets/ directory
"""
import os
import sys
import pandas as pd
import requests
import ssl
import urllib3
import logging
from datetime import datetime, timedelta
import concurrent.futures
from typing import Dict, List, Optional, Any
# Load environment variables
try:
from dotenv import load_dotenv
load_dotenv()
except ImportError:
print("⚠️ python-dotenv not installed. Install with: pip install python-dotenv")
# Suppress SSL warnings
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
# Setup logging
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s - %(levelname)s - %(message)s'
)
logger = logging.getLogger(__name__)
class TLSAdapter(requests.adapters.HTTPAdapter):
"""Custom TLS adapter for IMEA's SSL configuration"""
def init_poolmanager(self, *args, **kwargs):
ctx = ssl.create_default_context()
ctx.check_hostname = False
ctx.verify_mode = ssl.CERT_NONE
ctx.options |= 0x4
kwargs['ssl_context'] = ctx
return super(TLSAdapter, self).init_poolmanager(*args, **kwargs)
class IMEAExtractor:
"""Simplified IMEA Data Extractor"""
def __init__(self):
self.base_url = 'https://api1.imea.com.br'
self.access_token = None
# Setup session with custom TLS adapter
self.session = requests.Session()
self.session.mount('https://', TLSAdapter())
# IMEA data mapping
self.chains = {
'1': {'name': 'Cotton', 'code': 'CTN'},
'3': {'name': 'Corn', 'code': 'CRN'},
'4': {'name': 'Soy', 'code': 'SOY'}
}
self.groups = {
'697311317415952386': {'name': 'Planting', 'code': 'PLT'},
'697311317415952384': {'name': 'Harvest', 'code': 'HRV'},
'697311317415952385': {'name': 'Commercialization', 'code': 'COM'}
}
self.states = {
'51': {'name': 'Mato Grosso', 'code': 'MT'}
}
# Ensure datasets directory exists
os.makedirs('datasets', exist_ok=True)
def authenticate(self) -> bool:
"""Authenticate with IMEA API"""
username = os.getenv('IMEA_USERNAME')
password = os.getenv('IMEA_PASSWORD')
if not username or not password:
logger.error("❌ IMEA credentials not found in environment variables")
logger.error("Please set IMEA_USERNAME and IMEA_PASSWORD in .env file")
return False
try:
logger.info("🔐 Authenticating with IMEA API...")
headers = {
'content-type': 'application/x-www-form-urlencoded',
'authorization': 'bearer undefined'
}
auth_data = {
'username': username,
'password': password,
'grant_type': 'password',
'client_id': '2',
}
response = self.session.post(
f'{self.base_url}/token',
headers=headers,
data=auth_data,
verify=False
)
if response.status_code == 200:
token_data = response.json()
self.access_token = token_data.get('access_token')
if self.access_token:
logger.info("✅ Authentication successful!")
return True
else:
logger.error("❌ No access token in response")
else:
logger.error(f"❌ Authentication failed: {response.status_code}")
logger.error(f"Response: {response.text}")
except Exception as e:
logger.error(f"❌ Authentication error: {e}")
return False
def extract_historical_series(self) -> Optional[pd.DataFrame]:
"""Extract historical series data"""
if not self.access_token:
logger.error("❌ No access token available")
return None
logger.info("📈 Extracting historical agricultural data...")
# Define date range (from 2021 to present + 3 months)
start_dt = datetime(2021, 10, 1)
end_dt = datetime.now() + timedelta(days=90)
# Generate all requests
all_requests = []
for chain_id in ['4', '3', '1']: # Soy, Corn, Cotton
for group_id in ['697311317415952386', '697311317415952384', '697311317415952385']:
crop_name = self.chains.get(chain_id, {}).get('name', f'Chain {chain_id}')
activity_name = self.groups.get(group_id, {}).get('name', f'Group {group_id}')
# Generate yearly requests
for year in range(start_dt.year, end_dt.year + 1):
year_start = datetime(year, 1, 1)
year_end = datetime(year, 12, 31)
if year_start < start_dt:
year_start = start_dt
if year_end > end_dt:
year_end = end_dt
request_info = {
'chain_id': chain_id,
'group_id': group_id,
'crop_name': crop_name,
'activity_name': activity_name,
'start_date': year_start.strftime('%Y-%m-%d'),
'end_date': year_end.strftime('%Y-%m-%d'),
'year': year
}
all_requests.append(request_info)
logger.info(f"🚀 Making {len(all_requests)} parallel requests...")
# Execute requests in parallel
all_data = []
def make_request(request_info):
payload = {
'inicio': request_info['start_date'],
'fim': request_info['end_date'],
'cadeia': [request_info['chain_id']],
'grupo': [request_info['group_id']],
'indicador': [],
'tipolocalidade': ['1'],
'estado': ['51'],
'regiao': [],
'cidade': [],
'tipoDestino': [],
'estadoDestino': [],
'regiaoDestino': [],
'cidadeDestino': [],
'safra': [],
}
headers = {
'authorization': f'bearer {self.access_token}',
'content-type': 'application/json;charset=UTF-8',
'accept': 'application/json, text/plain, */*',
}
try:
response = self.session.post(
f'{self.base_url}/api/seriehistorica/export',
headers=headers,
json=payload,
verify=False,
timeout=30
)
if response.status_code == 200:
data = response.json()
if isinstance(data, list) and data:
return {'success': True, 'data': data}
else:
return {'success': True, 'data': []}
return {'success': False, 'error': f"HTTP {response.status_code}"}
except Exception as e:
return {'success': False, 'error': str(e)}
# Execute with ThreadPoolExecutor
with concurrent.futures.ThreadPoolExecutor(max_workers=8) as executor:
futures = [executor.submit(make_request, req) for req in all_requests]
completed = 0
for future in concurrent.futures.as_completed(futures):
result = future.result()
completed += 1
if result['success']:
all_data.extend(result['data'])
if completed % 10 == 0:
logger.info(f"✅ Completed {completed}/{len(all_requests)} requests...")
logger.info(f"🎯 Requests completed: {completed}/{len(all_requests)}")
if all_data:
df = pd.DataFrame(all_data)
logger.info(f"✅ Retrieved: {len(df)} historical records")
# Transform data
return self._transform_data(df)
else:
logger.warning("⚠️ No historical data retrieved")
return None
def _transform_data(self, df: pd.DataFrame) -> pd.DataFrame:
"""Transform raw data into clean format"""
try:
# Convert date column
df['Data'] = pd.to_datetime(df['Data'])
df['date'] = df['Data'].dt.date
df['year'] = df['Data'].dt.year
df['month'] = df['Data'].dt.month
# Convert numeric values (percentages)
df['percentage'] = pd.to_numeric(df['Valor'], errors='coerce')
# Add readable names
df['crop'] = df['CadeiaId'].map(lambda x: self.chains.get(x, {}).get('name', f'Chain {x}'))
df['activity'] = df['IndicadorGrupoId'].map(lambda x: self.groups.get(x, {}).get('name', f'Group {x}'))
df['state'] = df['EstadoId'].map(lambda x: self.states.get(x, {}).get('name', f'State {x}'))
df['harvest_season'] = df['SafraDescricao']
# Filter only percentage data
if 'UnidadeDescricao' in df.columns:
df = df[df['UnidadeDescricao'] == 'Percentual'].copy()
# Create final structure
final_df = df[[
'date', 'year', 'month', 'crop', 'activity', 'percentage',
'harvest_season', 'state'
]].copy()
# Add metadata
final_df['extraction_date'] = datetime.now()
final_df['data_source'] = 'IMEA_API'
logger.info(f"✅ Data transformed: {len(final_df)} percentage records")
return final_df
except Exception as e:
logger.error(f"❌ Error transforming data: {e}")
return df
def create_summary(self, df: pd.DataFrame) -> pd.DataFrame:
"""Create summary with all activities in one row"""
try:
# Pivot data
summary = df.pivot_table(
index=['date', 'year', 'month', 'crop', 'state', 'harvest_season'],
columns='activity',
values='percentage',
aggfunc='mean'
).reset_index()
# Flatten column names
summary.columns.name = None
# Rename columns
column_rename = {
'Planting': 'planted_percentage',
'Harvest': 'harvested_percentage',
'Commercialization': 'commercialized_percentage'
}
summary = summary.rename(columns=column_rename)
# Fill missing values with 0
for col in ['planted_percentage', 'harvested_percentage', 'commercialized_percentage']:
if col not in summary.columns:
summary[col] = 0.0
else:
summary[col] = summary[col].fillna(0.0)
# Reorder columns
final_columns = [
'date', 'year', 'month', 'crop', 'state', 'harvest_season',
'planted_percentage', 'harvested_percentage', 'commercialized_percentage'
]
summary = summary[final_columns]
summary = summary.sort_values(['date', 'crop']).reset_index(drop=True)
logger.info(f"✅ Created summary with {len(summary)} records")
return summary
except Exception as e:
logger.error(f"❌ Error creating summary: {e}")
return df
def save_datasets(self, df: pd.DataFrame) -> None:
"""Save data as CSV files"""
try:
# Save main summary file
summary_df = self.create_summary(df)
summary_path = 'datasets/BR_IMEA_CROP_PERCENTAGE_PROGRESS.csv'
summary_df.to_csv(summary_path, index=False)
logger.info(f"📊 Saved main summary: {len(summary_df)} records → {summary_path}")
# Save individual crop/activity files
crops = ['Soy', 'Corn', 'Cotton']
activities = [
('Planting', 'planted_percentage', 'PLANTING'),
('Harvest', 'harvested_percentage', 'HARVEST'),
('Commercialization', 'commercialized_percentage', 'COMMERCIALIZATION')
]
saved_files = []
for crop in crops:
crop_data = summary_df[summary_df['crop'] == crop].copy()
if crop_data.empty:
continue
for activity_name, percentage_col, file_suffix in activities:
activity_data = crop_data[crop_data[percentage_col] > 0].copy()
if activity_data.empty:
continue
# Create clean dataset
clean_data = activity_data[[
'date', 'year', 'month', 'crop', 'state', 'harvest_season', percentage_col
]].copy()
clean_data = clean_data.rename(columns={percentage_col: 'percentage'})
clean_data = clean_data.sort_values('date').reset_index(drop=True)
# Save file
filename = f'BR_IMEA_{crop.upper()}_{file_suffix}_PERCENTAGE.csv'
file_path = f'datasets/{filename}'
clean_data.to_csv(file_path, index=False)
saved_files.append((filename, len(clean_data)))
logger.info(f"📄 Saved {crop} {activity_name}: {len(clean_data)} records → {filename}")
# Print summary
total_files = len(saved_files) + 1 # +1 for main summary
total_records = sum(count for _, count in saved_files) + len(summary_df)
logger.info(f"\n📋 EXTRACTION SUMMARY:")
logger.info(f"✅ Total files created: {total_files}")
logger.info(f"✅ Total records: {total_records:,}")
logger.info(f"✅ Date range: {summary_df['date'].min()} to {summary_df['date'].max()}")
logger.info(f"✅ All files saved to: ./datasets/")
except Exception as e:
logger.error(f"❌ Error saving datasets: {e}")
def run(self) -> bool:
"""Main extraction process"""
logger.info("🌾 Starting IMEA Data Extraction...")
# Authenticate
if not self.authenticate():
return False
# Extract data
df = self.extract_historical_series()
if df is None or df.empty:
logger.error("❌ No data extracted")
return False
# Save datasets
self.save_datasets(df)
logger.info("🎉 Data extraction completed successfully!")
return True
def main():
"""Main function"""
print("🌾 IMEA Direct Data Extractor")
print("=" * 40)
extractor = IMEAExtractor()
success = extractor.run()
if success:
print("\n✅ Success! Check the ./datasets/ folder for your CSV files")
print("📊 Files ready for analysis in Excel, Python, R, or any data tool")
else:
print("\n❌ Extraction failed. Check the logs above for details.")
sys.exit(1)
if __name__ == "__main__":
main()