-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWebFunctions.py
More file actions
195 lines (115 loc) · 6.86 KB
/
WebFunctions.py
File metadata and controls
195 lines (115 loc) · 6.86 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
import urllib
from bs4 import BeautifulSoup
def DownloadChallenge(textType, year, num, part):
ChallengeText = "Unable to download that challenge yet."
#if year == 2019:
if year == 2020:
extension = ""
if num == 0:
extension = "introduction"
print ("Downloading introduction...")
else:
#print ("Downloading challenge " + str(num) + part + "...")
#print ("Downloading challenge " + str(year) + " " + str(num) + part + "...")
#print ("Need to add it to work for challenges other than introduction!")
#extension = "challenge-" + str(num)
if num <= 3:
extension = "practice-challenge-" + str(num)
else:
extension = "competition-challenge-" + str(num)
url = "https://www.cipherchallenge.org/challenge/" + extension
print ("URL:", url)
user_agent = 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.0.7) Gecko/2009021910 Firefox/3.0.7'
headers = {'User-Agent': user_agent}
html = urllib.request.urlopen(urllib.request.Request(url, None, headers)).read()
soup = BeautifulSoup(html, features = "html.parser")
try:
if num != 0:
#if part.lower() == "a":
#
# ChallengeText = soup.body.find("div", attrs = {"class": "challenge__content " + part.lower()}).text.strip("\n")
#else:
# ChallengeText = soup.body.find("div", attrs = {"class": "challenge__content"}).text.strip("\n")
if textType == "ciphertext":
ChallengeText = soup.body.find_all("div", attrs = {"class": "challenge__content"})[["a", "b"].index(part.lower())].text.strip("\n")
else:
#ChallengeText = "Cannot retrieve 2019 plaintexts yet."
ChallengeText = soup.body.find_all("div", attrs = {"class": "challenge__answer"})[["a", "b"].index(part.lower())].text.strip("\n").lstrip("Correct Answer").lstrip("\n")
else:
#ChallengeText = soup.body.find("div", attrs = {"class": "challenge__contenta"}).text.strip("\n")
#ChallengeText = soup.body.find("div", attrs = {"class": "challenge__content a"}).text.strip("\n").lstrip("Correct Answer").lstrip("\n")
ChallengeText = soup.body.find("div", attrs = {"class": "challenge__content a"}).text.strip("\n")
#print (ChallengeText)
except Exception as exc:
#ChallengeText = "Unable to download."
ChallengeText = "Unable to download.\n\nException: " + str(exc)
#elif year in [2018, 2017, 2016]:
elif False:
extension = ""
if year == 2016 and num == 0:
ChallengeText = "2016 has no introduction / challenge 0."
else:
if year != 2018 or num != 10:
extension = "challenge-" + str(num)
else:
#extensions = "competition-challenge-"
extension = "competition-challenge-10"
#print ("Downloading challenge " str(year) + " " + str(num) + part + "...")
print ("Downloading challenge " + str(year) + " " + str(num) + part + "...")
#url = "https://www.cipherchallenge.org/challenges/" + extension
url = "https://" + str(year) + ".cipherchallenge.org/challenges/" + extension
print ("URL:", url)
user_agent = 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.0.7) Gecko/2009021910 Firefox/3.0.7'
headers = {'User-Agent': user_agent}
html = urllib.request.urlopen(urllib.request.Request(url, None, headers)).read()
soup = BeautifulSoup(html, features = "html.parser")
try:
if num != 0:
#if part.lower() == "a":
#
# ChallengeText = soup.body.find("div", attrs = {"class": "challenge__content " + part.lower()}).text.strip("\n")
#
#else:
#ChallengeText = soup.body.find("div", attrs = {"class": "challenge__content"}).text.strip("\n")
#ChallengeText = soup.body.find("div", attrs = {"class": "challenge__content", "id": "#challenge-b__target"}).text.strip("\n")
#ChallengeText = soup.body.find_all("div", attrs = {"class": "challenge__content", "id": "#challenge-b__target"})[1].text.strip("\n")
# ChallengeText = soup.body.find_all("div", attrs = {"class": "challenge__content"})[1].text.strip("\n")
ChallengeText = soup.body.find_all("div", attrs = {"class": "challenge__content"})[["a", "b"].index(part.lower())].text.strip("\n")
else:
#ChallengeText = soup.body.find("div", attrs = {"class": "challenge__content a"}).text.strip("\n")
ChallengeText = soup.body.find("div", attrs = {"class": "challenge__content"}).text.strip("\n")
except Exception as exc:
#ChallengeText = "Unable to download."
ChallengeText = "Unable to download.\n\nException: " + str(exc)
else:
#ChallengeText = "Cannot yet retrieve that year's challenges."
#ChallengeText = open("..\\Archive\\Updated 7-Oct-2019\\" + year + "\\" + str(num) + part.upper() + "\\ciphertext.txt").read()
if textType == "ciphertext":
ChallengeText = open("..\\Archive\\Updated 7-Oct-2019\\" + str(year) + "\\" + str(num) + part.upper() + "\\ciphertext.txt").read()
else:
ChallengeText = open("..\\Archive\\Updated 7-Oct-2019\\" + str(year) + "\\" + str(num) + part.upper() + "\\plaintext.txt").read()
print ("Download complete.")
return ChallengeText
def UploadChallenge(text, year, num, part):
if year == 2019:
if num <= 3:
extension = "practice-challenge-" + str(num)
else:
extension = "competition-challenge-" + str(num)
url = "https://www.cipherchallenge.org/challenge/" + extension
#print ("Uploading to " + str(year) + " Challenge " + str(num) + str(part))
print ("Uploading to " + str(year) + " Challenge " + str(num) + str(part) + "...")
print ("URL:", url)
values = {"answer": text}
# pretend to be a chrome 47 browser on a windows 10 machine
headers = {"User-Agent": "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.0.7) Gecko/2009021910 Firefox/3.0.7"}
# encode values for the url
params = urllib.parse.urlencode(values).encode("utf-8")
# create the url
targetUrl = urllib.request.Request(url=url, data=params, headers=headers)
# open the url
x = urllib.request.urlopen(targetUrl)
# read the response
respone = x.read()
#print(respone)
print ("Uploaded. (I hope.)")