-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathleetcode.py
More file actions
57 lines (43 loc) · 1.48 KB
/
leetcode.py
File metadata and controls
57 lines (43 loc) · 1.48 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
import json
import httpx
import asyncio
from bs4 import BeautifulSoup
from datetime import datetime
try:
from helpers.format_time import secondsToTime, timeToSeconds
except ImportError:
from .helpers.format_time import secondsToTime, timeToSeconds
def extract_data(r):
soup = BeautifulSoup(r, "lxml")
a = soup.find("script", id="__NEXT_DATA__")
data = json.loads(a.text)[
"props"]["pageProps"]["dehydratedState"]["queries"][-1]["state"]["data"]["topTwoContests"]
return data
async def getContests(ses: httpx.AsyncClient):
r = (await ses.get("https://leetcode.com/contest/"))
loop = asyncio.get_event_loop()
data = await loop.run_in_executor(None, extract_data, r.text)
allContests = []
for i in data:
name = i["title"]
url = "https://leetcode.com/contest/" + i["titleSlug"]
startSec = i["startTime"]
startTime = datetime.strftime(
datetime.utcfromtimestamp(startSec),
"%d-%m-%Y %H:%M:%S") + " UTC"
durationSec = i["duration"]
duration = secondsToTime(durationSec)
contest = {
"name": name,
"url": url,
"startTime": startTime,
"duration": duration,
"durationSeconds": durationSec
}
allContests.append(contest)
return allContests
if __name__ == "__main__":
print("Only running one file.\n")
a = asyncio.run(getContests(httpx.AsyncClient(timeout=13)))
for j in a:
print(j)