-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathleague_entry.py
146 lines (122 loc) · 5.22 KB
/
league_entry.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
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
import asyncio
import datetime
import os
import asyncpg
import aiohttp
class LeagueEntryCrawler:
def __init__(self):
self.session = aiohttp.ClientSession()
self.base_url = (
"https://kr.api.riotgames.com/lol/league/v4/entries/RANKED_SOLO_5x5"
)
self.api_key = os.environ.get("RIOT_API_KEY")
self.tiers = ["IRON", "BRONZE", "SILVER", "GOLD", "PLATINUM", "DIAMOND"]
self.divisions = ["I", "II", "III", "IV"]
self.db_user = os.environ.get("POSTGRES_USER")
self.db_password = os.environ.get("POSTGRES_PASSWORD")
self.db_host = "localhost"
self.db_port = os.environ.get("POSTGRES_PORT")
self.db_name = os.environ.get("POSTGRES_WEB_DB")
self.pool = None
async def create_db_pool(self):
self.pool = await asyncpg.create_pool(
user=self.db_user,
password=self.db_password,
host=self.db_host,
port=self.db_port,
database=self.db_name,
)
async def run(self):
await self.create_db_pool()
for tier in self.tiers:
for division in self.divisions:
await self.get_league_entries(tier, division)
await self.session.close()
await self.pool.close()
async def get_league_entries(self, tier: str, division: str):
url = f"{self.base_url}/{tier}/{division}?api_key={self.api_key}"
page = 1
league_entries = await self._fetch(url + f"&page={page}")
while league_entries:
summoners = [
(
league_entry["summonerId"],
league_entry["summonerName"],
datetime.datetime.now(),
datetime.datetime.now(),
)
for league_entry in league_entries
]
async with self.pool.acquire() as connection:
async with connection.transaction():
await connection.executemany(
"INSERT INTO app_summoner (id, name, created_at, updated_at)"
"VALUES ($1, $2, $3, $4)"
"ON CONFLICT (id)"
"""
DO UPDATE SET
name = EXCLUDED.name,
updated_at = EXCLUDED.updated_at
""",
summoners,
)
entries = [
(
league_entry["tier"],
league_entry["rank"],
league_entry["leaguePoints"],
league_entry["wins"],
league_entry["losses"],
league_entry["veteran"],
league_entry["inactive"],
league_entry["freshBlood"],
league_entry["hotStreak"],
league_entry["queueType"],
league_entry["leagueId"],
league_entry["summonerId"],
datetime.datetime.now(),
datetime.datetime.now(),
)
for league_entry in league_entries
]
async with self.pool.acquire() as connection:
async with connection.transaction():
await connection.executemany(
"INSERT INTO app_leagueentry (tier, rank, league_points, wins, losses, veteran, inactive, fresh_blood, hot_streak, queue_type, league_id, summoner_id, created_at, updated_at)"
"VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14)"
"ON CONFLICT (summoner_id, queue_type)"
"""
DO UPDATE SET
tier = EXCLUDED.tier,
rank = EXCLUDED.rank,
league_points = EXCLUDED.league_points,
wins = EXCLUDED.wins,
losses = EXCLUDED.losses,
veteran = EXCLUDED.veteran,
inactive = EXCLUDED.inactive,
fresh_blood = EXCLUDED.fresh_blood,
hot_streak = EXCLUDED.hot_streak,
queue_type = EXCLUDED.queue_type,
league_id = EXCLUDED.league_id,
summoner_id = EXCLUDED.summoner_id,
updated_at = EXCLUDED.updated_at
""",
entries,
)
page += 1
league_entries = await self._fetch(url + f"&page={page}")
return []
async def _fetch(self, url: str) -> list:
async with self.session.get(url) as response:
response = await self.session.get(url)
while response.status == 429:
retry_after = response.headers.get("Retry-After")
await asyncio.sleep(int(retry_after))
response = await self.session.get(url)
return await response.json()
async def main():
crawler = LeagueEntryCrawler()
await crawler.run()
loop = asyncio.get_event_loop()
loop.run_until_complete(main())
loop.close()