-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
78 lines (67 loc) · 2.29 KB
/
Copy pathmain.py
File metadata and controls
78 lines (67 loc) · 2.29 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
import requests
import pandas as pd
import sqlite3
import os
import json
# Create data folder if not exists
os.makedirs("data", exist_ok=True)
# Step 1: Fetch from API
url = "https://fakestoreapi.com/products"
try:
response = requests.get(url, timeout=10)
response.raise_for_status() # Raise an exception for bad status codes
data = response.json()
except requests.exceptions.RequestException as e:
print(f"❌ Error fetching data from API: {e}")
exit(1)
except json.JSONDecodeError as e:
print(f"❌ Error parsing JSON response: {e}")
exit(1)
# Save raw data
try:
with open("data/raw_data.json", "w") as f:
json.dump(data, f, indent=2)
except IOError as e:
print(f"❌ Error saving raw data: {e}")
exit(1)
# Step 2: Convert to DataFrame
try:
df = pd.DataFrame(data)
if df.empty:
print("❌ No data received from API")
exit(1)
# Flatten nested dictionary columns for SQLite compatibility
if 'rating' in df.columns:
df['rating_rate'] = df['rating'].apply(lambda x: x.get('rate', 0) if isinstance(x, dict) else 0)
df['rating_count'] = df['rating'].apply(lambda x: x.get('count', 0) if isinstance(x, dict) else 0)
df = df.drop('rating', axis=1) # Remove the original nested column
except Exception as e:
print(f"❌ Error creating DataFrame: {e}")
exit(1)
# Step 3: Sort by rating (descending) and take top 5
try:
# Use the flattened rating_rate column for sorting
if 'rating_rate' not in df.columns:
print("❌ 'rating_rate' column not found in data")
exit(1)
top5 = df.sort_values(by="rating_rate", ascending=False).head(5)
except Exception as e:
print(f"❌ Error processing rating data: {e}")
exit(1)
# Step 4: Save full data to SQLite
try:
conn = sqlite3.connect("data/products.db")
df.to_sql("products", conn, if_exists="replace", index=False)
conn.close()
print("✅ Data saved to SQLite database")
except Exception as e:
print(f"❌ Error saving to SQLite: {e}")
exit(1)
# Step 5: Export top 5 to CSV
try:
top5.to_csv("data/top5_products.csv", index=False)
print("✅ Top 5 products exported to CSV")
except Exception as e:
print(f"❌ Error saving CSV: {e}")
exit(1)
print("✅ Done! Data saved to SQLite and top 5 to CSV.")