1
1
import requests
2
2
import json
3
- import bs4
3
+ import bs4
4
4
import re
5
5
6
- CLEANR = re .compile ('<.*?>' )
6
+ CLEANR = re .compile ("<.*?>" )
7
+
7
8
8
9
def cleanhtml (raw_html ):
9
- cleantext = re .sub (CLEANR , '' , raw_html )
10
- return cleantext
10
+ cleantext = re .sub (CLEANR , "" , raw_html )
11
+ return cleantext
12
+
11
13
12
14
def split_value_and_unit (soup ):
13
- l = soup .split ()
14
- return {
15
- "value" : int (l [0 ]),
16
- "unit" : l [1 ]
17
- }
15
+ length = soup .split ()
16
+ return {"value" : int (length [0 ]), "unit" : length [1 ]}
18
17
19
18
20
19
def test_group (lst ):
21
20
return [{"input" : _in , "output" : _out } for _in , _out in pairwise (lst )]
22
21
23
22
24
23
def test_sample (souped_html ):
25
- return test_group (get_tags_contents (souped_html , ' pre' ))
24
+ return test_group (get_tags_contents (souped_html , " pre" ))
26
25
27
26
28
27
def get_tags_contents (souped_html , tag_name , class_name = None ):
29
- return [concat_contents (tag .contents ) for tag in souped_html .find_all (tag_name , class_name )]
28
+ return [
29
+ concat_contents (tag .contents )
30
+ for tag in souped_html .find_all (tag_name , class_name )
31
+ ]
30
32
31
33
32
34
def pairwise (iterable ):
@@ -35,11 +37,11 @@ def pairwise(iterable):
35
37
36
38
37
39
def get_statement (soup ):
38
- return concat_contents (soup .find (' div' , ' header' ).next_sibling .contents )
40
+ return concat_contents (soup .find (" div" , " header" ).next_sibling .contents )
39
41
40
42
41
- def get_content (soup , _class = '' ):
42
- element = soup .find (' div' , _class )
43
+ def get_content (soup , _class = "" ):
44
+ element = soup .find (" div" , _class )
43
45
if not element :
44
46
return None
45
47
tags = element .contents
@@ -48,84 +50,93 @@ def get_content(soup, _class=''):
48
50
49
51
50
52
def concat_contents (ls ):
51
- return '' .join ([str (i ) for i in ls ])
53
+ return "" .join ([str (i ) for i in ls ])
54
+
52
55
53
56
def scrap_wraper (problem_link ):
54
57
markup = requests .get (problem_link ).text
55
58
soup = bs4 .BeautifulSoup (markup , "html.parser" )
56
59
problem = {
57
- "title" : soup .find ('div' , 'title' ).string ,
58
- "timeLimit" : split_value_and_unit (soup .find ('div' , 'time-limit' ).contents [1 ].string ),
59
- "memoryLimit" : split_value_and_unit (soup .find ('div' , 'memory-limit' ).contents [1 ].string ),
60
+ "title" : soup .find ("div" , "title" ).string ,
61
+ "timeLimit" : split_value_and_unit (
62
+ soup .find ("div" , "time-limit" ).contents [1 ].string
63
+ ),
64
+ "memoryLimit" : split_value_and_unit (
65
+ soup .find ("div" , "memory-limit" ).contents [1 ].string
66
+ ),
60
67
"statement" : get_statement (soup ),
61
- "inputSpecification" : get_content (soup , ' input-specification' ),
62
- "outputSpecification" : get_content (soup , ' output-specification' ),
68
+ "inputSpecification" : get_content (soup , " input-specification" ),
69
+ "outputSpecification" : get_content (soup , " output-specification" ),
63
70
"samples" : test_sample (soup ),
64
- "note" : get_content (soup , ' note' ),
71
+ "note" : get_content (soup , " note" ),
65
72
}
66
73
return problem
67
74
68
75
69
-
70
- def get_all_problems () :
71
- url = 'https://codeforces.com/api/problemset.problems'
72
- print (url )
76
+ def get_all_problems ():
77
+ url = "https://codeforces.com/api/problemset.problems"
78
+ print (url )
73
79
74
80
r = requests .get (url )
75
81
76
- if ( r .status_code == 200 ) :
82
+ if r .status_code == 200 :
77
83
data = r .json ()
78
84
print (json .dumps (data ["result" ]["problems" ], sort_keys = True , indent = 4 ))
79
85
else :
80
86
print ("SORRY! SERVER ERROR EXISTS" )
81
-
82
- def get_all_problems_by_tag (tag ):
83
- url = 'https://codeforces.com/api/problemset.problems'
87
+
88
+
89
+ def get_all_problems_by_tag (tag ):
90
+ url = "https://codeforces.com/api/problemset.problems"
84
91
85
92
r = requests .get (url )
86
93
87
- if ( r .status_code == 200 ) :
94
+ if r .status_code == 200 :
88
95
data = r .json ()
89
96
list_of_all_problems = data ["result" ]["problems" ]
90
97
for index in list_of_all_problems :
91
98
tags_of_problem = index ["tags" ]
92
- if tags_of_problem .count (tag ) :
99
+ if tags_of_problem .count (tag ):
93
100
print (index )
94
-
95
-
96
- else :
101
+
102
+ else :
97
103
print ("SORRY! SERVER ERROR EXISTS" )
98
104
99
- def get_problem_statement_by_id_and_index ( id , index ):
100
- url = "https://codeforces.com/problemset/problem/" + id + "/" + index
105
+
106
+ def get_problem_statement_by_id_and_index (id , index ):
107
+ url = "https://codeforces.com/problemset/problem/" + id + "/" + index
101
108
data = scrap_wraper (url )
102
- print (cleanhtml (data ["statement" ]))
103
- print (cleanhtml (data ["inputSpecification" ]))
104
- print (cleanhtml (data ["outputSpecification" ]))
109
+ print (cleanhtml (data ["statement" ]))
110
+ print (cleanhtml (data ["inputSpecification" ]))
111
+ print (cleanhtml (data ["outputSpecification" ]))
112
+
105
113
106
114
def main ():
107
115
ch = "YES"
108
116
while ch == "YES" :
109
- print ( "PLEASE SELECT ANY ONE OF THE BELOW : \n 1. GET ALL PROBLEMS \n 2. GET ALL PROBLEMS BY TAGS \n 3. GET PROBLEM STATEMENT " )
117
+ print ("PLEASE SELECT ANY ONE OF THE BELOW :" )
118
+ print ("\n 1. GET ALL PROBLEMS" )
119
+ print ("\n 2. GET ALL PROBLEMS BY TAGS \n 3. GET PROBLEM STATEMENT " )
110
120
111
121
answer = int (input ())
112
-
113
- if ( answer == 1 ) :
122
+
123
+ if answer == 1 :
114
124
get_all_problems ()
115
-
116
- elif ( answer == 2 ) :
117
- print ("\n Please Enter Your Tag : " )
125
+
126
+ elif answer == 2 :
127
+ print ("\n Please Enter Your Tag : " )
118
128
tag = input ()
119
129
get_all_problems_by_tag (tag )
120
-
121
- elif ( answer == 3 ) :
122
- print ("\n Please Enter Id and Index as Follows : \n Id : " )
130
+
131
+ elif answer == 3 :
132
+ print ("\n Please Enter Id and Index as Follows : \n Id : " )
123
133
id = input ()
124
- print ("\n Index : " )
134
+ print ("\n Index : " )
125
135
index = input ()
126
136
get_problem_statement_by_id_and_index (id , index )
127
-
137
+
128
138
ch = input ("WOULD YOU LIKE TO CONTINUE : " )
129
-
139
+
140
+
130
141
if __name__ == "__main__" :
131
142
main ()
0 commit comments