-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy patholdapi.py
More file actions
264 lines (216 loc) · 10.1 KB
/
oldapi.py
File metadata and controls
264 lines (216 loc) · 10.1 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
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
from datetime import datetime
from datetime import timedelta
from .utils import *
import requests
from bs4 import BeautifulSoup
import aiohttp
import re
Headers = {
"origin": "https://leetcode-cn.com",
"user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.107 Safari/537.36"
}
async def getLeetcodeDaily() -> dict:
async with aiohttp.ClientSession() as session:
async with session.post("https://leetcode.cn/graphql", json={"operationName": "questionOfToday", "variables": {}, "query": "query questionOfToday { todayRecord { question { questionFrontendId questionTitleSlug __typename } lastSubmission { id __typename } date userStatus __typename }}"}, headers=Headers, timeout=5) as response:
RawData = await response.json(content_type=None)
EnglishTitle = RawData["data"]["todayRecord"][0]["question"]["questionTitleSlug"]
Date = RawData["data"]["todayRecord"][0]["date"]
QuestionUrl = f"https://leetcode.cn/problems/{EnglishTitle}"
async with session.post("https://leetcode.cn/graphql", json={"operationName": "questionData", "variables": {"titleSlug": EnglishTitle}, "query": "query questionData($titleSlug: String!) { question(titleSlug: $titleSlug) { questionId questionFrontendId boundTopicId title titleSlug content translatedTitle translatedContent isPaidOnly difficulty likes dislikes isLiked similarQuestions contributors { username profileUrl avatarUrl __typename } langToValidPlayground topicTags { name slug translatedName __typename } companyTagStats codeSnippets { lang langSlug code __typename } stats hints solution { id canSeeDetail __typename } status sampleTestCase metaData judgerAvailable judgeType mysqlSchemas enableRunCode envInfo book { id bookName pressName source shortDescription fullDescription bookImgUrl pressImgUrl productUrl __typename } isSubscribed isDailyQuestion dailyRecordStatus editorType ugcQuestionId style __typename }}"}, headers=Headers, timeout=5) as response:
RawData = await response.json(content_type=None)
Data = RawData["data"]["question"]
ID = Data["questionFrontendId"]
Difficulty = Data["difficulty"]
ChineseTitle = Data["translatedTitle"]
Content = re.sub(r"(<\w+>|</\w+>)", "", Data["translatedContent"]).replace(" ", "").replace(
"<", "<").replace("\t", "").replace("\n\n", "\n").replace("\n\n", "\n")
data = {"id": ID, "title": ChineseTitle, "difficulty": Difficulty,
"content": Content, "url": QuestionUrl, "date": Date}
save_json("leetcode_daily.json", data)
async def getNiuKeSchool():
url = 'https://ac.nowcoder.com/acm/contest/vip-index?topCategoryFilter=14'
r = requests.get(url)
soup = BeautifulSoup(r.text, "html.parser")
div = soup.find_all("div", class_='platform-item js-item')
data = []
for it in div:
div1 = it.find("div", class_='platform-item-cont')
# print(div1)
a = div1('a')
i = div1('i')
li = div1.find("li", class_='match-time-icon')
if (len(i) > 0):
if ("加密" in i[0]['title']):
continue
name = a[0].string
link = "https://ac.nowcoder.com" + a[0]['href']
start = li.string[9:25]
end = li.string[33:49]
data.append({
'name': name,
'start_time': start,
'end_time': end,
'link': link,
})
save_json("niuke_school.json", data)
async def getAtcoder():
url = 'https://atcoder.jp/'
headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:66.0)Gecko/20100101 Firefox/66.0"
}
r = requests.get(url, timeout=30, headers=headers)
soup = BeautifulSoup(r.text, "html.parser")
div = soup.find("div", id="contest-table-upcoming")
if div is None:
print("最近没有比赛")
data = {"最近没有比赛": "......"}
save_json("atcoder.json", data)
else:
name = []
time = []
link = []
tbody = div.find("tbody")
for it in tbody.find_all("tr"):
a = it.find_all("a")
link.append("https://atcoder.jp" + a[1]['href'])
time.append(a[0].string)
name.append(a[1].string)
data = []
for i in range(0, len(name)):
ti = time[i][:-8]
date = datetime.strptime(ti, '%Y-%m-%d %H:%M')
data.append({
'name': name[i],
'start_time': (date - timedelta(hours=1)).strftime("%Y-%m-%d %H:%M"),
'end_time': "",
'link': link[i],
})
# print(data)
save_json("atcoder.json", data)
async def getCodeChef():
url = 'https://www.codechef.com/contests'
headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.82 Safari/537.36"
}
r = requests.get(url, timeout=30, headers=headers)
if (r.status_code != 200): # 由于codechef网站访问比较慢当出错的时候就在进行访问一次
r = requests.get(url, timeout=30, headers=headers)
soup = BeautifulSoup(r.text, "html.parser")
table = soup.find_all("table", class_="dataTable")
table = table[1]
tbody = table.find("tbody")
name = []
time = []
link = []
data_time = tbody.find_all("td", class_="start_date")
for it in tbody.find_all('td'):
# print(it)
a = it.find("a")
if a is not None:
name.append(a.string)
print(a['href'])
link.append("https://www.codechef.com" + a['href'])
for it in data_time:
time.append(it.text)
for i in range(0, len(time)):
date = datetime.strptime(time[i], '%d %b %Y %H:%M:%S')
time[i] = (date + timedelta(hours=2, minutes=30)
).strftime("%Y-%m-%d %H:%M") # 印度与中国时间相差2时30分
data = []
for i in range(0, len(time)):
data.append({
'name': name[i],
'start_time': time[i],
'end_time': "",
'link': link[i],
})
# 将获取的字典信息 导入json 不建议用数据库 因为信息比较少用json文件方便
save_json("codechef.json", data)
async def getCodefores():
contest_name = []
contest_time = []
contest_id = []
urls = 'https://codeforces.com/contests'
r = requests.get(urls, timeout=30)
if r.status_code != 200:
return
else:
soup = BeautifulSoup(r.text, "html.parser")
div = soup.find("div", class_="contestList")
table = div.find("table", class_="")
for tr in table.find_all('tr'):
for td in tr.find_all('td'):
if td.text.count('\n') <= 2:
contest_name.append(td.text.strip())
else:
contest_name.append(td.text[:td.text.find(
'\n', td.text.find('\n') + 1) + 1].strip())
contest_id.append(tr.get('data-contestid'))
break
for tr in table.find_all('tr'):
# print(tr)
for td in tr.find_all('td'):
span = td.find("span", class_='format-time')
if span:
contest_time.append(span.string)
t = []
for it in contest_time:
dete = datetime.strptime(it, '%b/%d/%Y %H:%M')
# print(dete)
times = dete + timedelta(hours=5, minutes=0)
t.append(times.strftime("%Y-%m-%d %H:%M"))
data = []
for i in range(0, len(contest_time)):
data.append({
'name': contest_name[i],
'start_time': t[i],
'end_time': "",
'link': "https://codeforces.com/contest/" + contest_id[i],
})
save_json("cf.json", data)
async def getNiuKe():
url = 'https://ac.nowcoder.com/acm/contest/vip-index?topCategoryFilter=13'
html = requests.get(url)
if html.status_code != 200:
return
else:
soup = BeautifulSoup(html.text, "html.parser")
data = []
div = soup.find(
"div", class_="nk-main with-banner-page clearfix js-container")
div_content = div.find("div", class_="nk-content")
div_contest = div_content.find("div", class_="platform-mod js-current")
for contest in div_contest.find_all("div", class_="platform-item js-item"):
next = contest.find("div", class_="platform-item-cont")
a = next.find("a")
li = next.find('li', class_='match-time-icon')
link = a['href']
name = a.string
tmp = li.string
start = tmp[9:25]
end = tmp[33:49]
link = "https://ac.nowcoder.com" + link
data.append({
'name': name,
'start_time': start,
'end_time': end,
'link': link,
})
save_json("niuke.json", data)
async def getNews():
url = 'https://news.cnblogs.com/'
html = requests.get(url)
soup = BeautifulSoup(html.text, "html.parser")
content = []
link = []
div = soup.find("div", id="news_list")
for h in div.find_all("h2"):
a = h.find('a')
content.append(a.string)
link.append(a['href'])
text = ""
for i in range(0, 10):
#cnt = random.randint(0, len(content))
text += str(i + 1) + "." + content[i] + "\n"
text += 'https://news.cnblogs.com' + link[i] + "\n"
return text