-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathteams.py
106 lines (97 loc) · 3.89 KB
/
teams.py
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
import pymysql
import hashlib
import requests
import sys
import os
from dotenv import load_dotenv
load_dotenv()
# Connect to the database
cnx = pymysql.connect(
host=os.getenv('DB_HOST'),
user=os.getenv('DB_USER'),
passwd=os.getenv('DB_PASSWD'),
db=os.getenv('DB_NAME'),
cursorclass=pymysql.cursors.DictCursor,
use_unicode=True,
charset=os.getenv('DB_CHARSET')
)
cursor = cnx.cursor()
querySelect = "SELECT * FROM racingmike_motogp.categories_by_season"
cursor.execute(querySelect)
result = cursor.fetchall()
print(result)
for row in result:
category_id = row['id']
name = row['name']
year = row['year']
print("RUNNING YEAR "+str(year))
#url = "https://api.motogp.com/riders-api/season/" + str(year) + "/teams?category=" + str(category_id)
url = "https://api.motogp.pulselive.com/motogp/v1/teams?categoryUuid="+ str(category_id)+"&seasonYear="+str(year)
print(url)
response = requests.get(url)
data = response.json()
for team in data:
for rider in team['riders']:
team_id = None
if rider.get('current_career_step') and rider['current_career_step'].get('team'):
team_id = rider['current_career_step']['team'].get('id')
values = (
team['id'],
team['name'],
team['legacy_id'],
team['color'],
team['text_color'],
team['picture'],
team['constructor']['id'],
team['constructor']['name'],
team['constructor']['legacy_id'],
rider['id'],
rider['name'],
rider['surname'],
rider['nickname'],
rider['current_career_step']['season'],
rider['current_career_step']['number'],
rider['current_career_step']['sponsored_team'],
#rider['current_career_step']['team']['id'],
team_id,
rider['current_career_step']['category']['id'],
rider['current_career_step']['category']['name'],
rider['current_career_step']['category']['legacy_id'],
rider['current_career_step']['in_grid'],
rider['current_career_step']['short_nickname'],
rider['current_career_step']['current'],
rider['current_career_step']['pictures']['profile']['main'],
rider['current_career_step']['pictures']['bike']['main'],
rider['current_career_step']['pictures']['helmet']['main'],
rider['current_career_step']['pictures']['number'],
rider['current_career_step']['pictures']['portrait'],
rider['current_career_step']['type'],
rider['country']['iso'],
rider['country']['name'],
rider['country']['flag'],
rider['birth_city'],
rider['birth_date'],
rider['years_old'],
rider['published'],
rider['legacy_id'],
year
)
# Create a string representation of the entry
entry_str = ''.join(map(str, (
year,rider['legacy_id']
)))
# Generate the MD5 hash
md5_hash = hashlib.md5(entry_str.encode()).hexdigest()
# Extend your insertion values and query to include the MD5 hash
values += (md5_hash,)
try:
insert_query = "INSERT INTO TeamRiders VALUES (%s,%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)"
cursor.execute(insert_query, values)
except Exception as e:
print(e)
print(values)
print(insert_query)
continue
cnx.commit()
cursor.close()
cnx.close()