-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
67 lines (54 loc) · 1.89 KB
/
app.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
import requests
from bs4 import BeautifulSoup
from flask import Flask, render_template, request, redirect
import random
def scrapIt(url):
r = requests.get(url)
htmlContent = r.content
soup = BeautifulSoup(htmlContent, 'html.parser')
anchors = soup.find_all("option")
allLinks = []
linkDict = {}
for link in anchors:
if(link['value']):
ls = []
str = link['value']
if(str == 'combine-tags-by-or'):
continue
ls.append(str)
if(str.find(" ") > 0):
str = str.replace(" ", "%20")
ls.append(str)
linkUrl = "http://codeforces.com/problemset?tags=" + str
linkDict[str] = linkUrl
allLinks.append(ls)
return linkDict,allLinks
url = "http://codeforces.com/problemset#"
app = Flask(__name__)
linkDict,allLinks = scrapIt(url)
def problemScrap(url):
r = requests.get(url)
htmlContent = r.content
soup = BeautifulSoup(htmlContent, 'html.parser')
div = soup.find_all("td", {"class": "id"})
problems = []
for x in div:
anchor = x.find("a")
plink = "https://codeforces.com" + anchor['href']
problems.append(plink)
return problems
@app.route("/", methods=['GET', 'POST'])
def hello_world():
if request.method == 'POST':
topic = request.form['topic']
ques = problemScrap(linkDict[topic])
q1 = random.randint(0,len(ques)-1)
q2 = random.randint(0,len(ques)-1)
while(q2 == q1):
q2 = random.randint(0,len(ques))
problems = [ques[q1], ques[q2]]
qTopic = topic.replace("%20"," ")
return render_template('index.html', linkDict=linkDict,allLinks=allLinks, problems=problems, qTopic=qTopic)
return render_template('index.html', linkDict=linkDict,allLinks=allLinks)
if __name__ == "__main__":
app.run(debug=True, port=8000)