1+ import mysql .connector
2+ import requests
3+ import json
4+ from datetime import datetime
5+
6+ # Database configuration - Modify these values accordingly
7+ db_config = {
8+ 'host' : 'localhost' ,
9+ 'user' : 'your_username' ,
10+ 'password' : 'your_password' ,
11+ 'database' : 'your_database'
12+ }
13+
14+ # Connect to the MySQL database
15+ try :
16+ conn = mysql .connector .connect (** db_config )
17+ cursor = conn .cursor ()
18+
19+ # Step 1: Fetch all 'id' values from the 'Plans' table
20+ cursor .execute ("SELECT id FROM Plans" )
21+ ids = cursor .fetchall ()
22+
23+ # Step 2: Loop through each id and fetch data from the API
24+ for (plan_id ,) in ids :
25+ url = f"https://lcd.sentinel.co/sentinel/plans/{ plan_id } "
26+ print (f"Fetching data for plan ID: { plan_id } " )
27+
28+ try :
29+ response = requests .get (url )
30+ response .raise_for_status () # Raise exception for HTTP errors
31+
32+ # Step 3: Parse the JSON response
33+ data = response .json ()
34+ plan = data .get ('plan' )
35+
36+ if plan :
37+ # Step 4: Prepare data for insertion
38+ status_at_str = plan .get ('status_at' , '' )
39+ # Truncate the fractional seconds to 6 digits and remove 'T' and 'Z'
40+ status_at_str = status_at_str .replace ('T' , ' ' ).rstrip ('Z' ) # Replace T with space and remove 'Z'
41+ status_at_str = status_at_str [:23 ] # Keep only 6 digits in fractional seconds
42+
43+ # Now parse the ISO format string
44+ dt = datetime .fromisoformat (status_at_str )
45+
46+ # Convert to MySQL format
47+ status_at = dt .strftime ('%Y-%m-%d %H:%M:%S.%f' )
48+ price = plan ['prices' ][0 ] if plan ['prices' ] else {'denom' : None , 'amount' : None }
49+ amount = int (price ['amount' ]) if price ['amount' ] else None
50+ insert_query = """
51+ INSERT INTO plan_info (
52+ id,
53+ provider_address,
54+ duration,
55+ gigabytes,
56+ denom,
57+ amount,
58+ status,
59+ status_at
60+ ) VALUES (%s, %s, %s, %s, %s, %s, %s,%s)
61+
62+ """
63+
64+ values = (
65+ plan ['id' ],
66+ plan ['provider_address' ],
67+ plan ['duration' ],
68+ plan ['gigabytes' ],
69+ price ['denom' ],
70+ amount ,
71+ plan ['status' ],
72+ status_at
73+ )
74+
75+ # Step 5: Insert or update the plan data in the plan_info table
76+ cursor .execute (insert_query , values )
77+
78+ except requests .exceptions .RequestException as req_err :
79+ print (f"HTTP request failed for ID { plan_id } : { req_err } " )
80+ except KeyError as key_err :
81+ print (f"Missing expected key in JSON for ID { plan_id } : { key_err } " )
82+ except Exception as e :
83+ print (f"Error processing ID { plan_id } : { e } " )
84+ conn .rollback ()
85+
86+ # Commit all changes to the database
87+ conn .commit ()
88+
89+ except mysql .connector .Error as db_err :
90+ print (f"Database connection error: { db_err } " )
91+
92+ finally :
93+ # Ensure resources are released
94+ if 'cursor' in locals ():
95+ cursor .close ()
96+ if 'conn' in locals () and conn .is_connected ():
97+ conn .close ()
98+ print ("Database connection closed." )
0 commit comments