forked from darthfrazier/API-Challenge
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchallenges.py
152 lines (109 loc) · 4.4 KB
/
challenges.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
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
__author__ = 'Darthfrazier'
import requests
import json
import datetime
#------------------------Register-----------------------------#
login = {"email":"[email protected]","github":"https://github.com/darthfrazier"}
login = json.dumps(login)
r = requests.post("http://challenge.code2040.org/api/register", data=login)
stringtoken = json.loads(r.text)
stringtoken = stringtoken["result"]
print("Your token is " + stringtoken + "\n")
#--------------------------Challenge One------------------------#
print("Running Challenge One\n")
token = {"token":stringtoken}
token = json.dumps(token)
r = requests.post("http://challenge.code2040.org/api/getstring", data=token)
string = json.loads(r.text)
print("Input string is " + string["result"])
outputstring = string["result"][::-1]
print("Reversed string is " + outputstring)
jsonstring = {"token":stringtoken, "string": outputstring}
jsonstring = json.dumps(jsonstring)
r = requests.post("http://challenge.code2040.org/api/validatestring", data=jsonstring)
results = json.loads(r.text)
results = results["result"]
print("Did I pass the test? \n" + results)
if results == "PASS: stage1. Enrollment record updated!":
print("Congratulations, on to the next one\n")
else:
print("Uh oh, there may be something wrong with the code\n")
#-----------------------------Challenge Two---------------------------#
print("Running Challenge Two\n")
r = requests.post("http://challenge.code2040.org/api/haystack", data=token)
jsonresult = json.loads(r.text)
result = jsonresult["result"]
needle = result["needle"]
haystack = result["haystack"]
print("Needle is " + needle)
print("Haystack is ")
for i in range (0,len(haystack)):
print(haystack[i])
index = haystack.index(needle)
jsonstring = {"token":stringtoken, "needle": index}
jsonstring = json.dumps(jsonstring)
r = requests.post("http://challenge.code2040.org/api/validateneedle", data=jsonstring)
results = json.loads(r.text)
results = results["result"]
print("Did I pass the test? \n" + results)
if results == "PASS: stage2. Enrollment record updated!":
print("Congratulations, on to the next one\n")
else:
print("Uh oh, there may be something wrong with the code\n")
#------------------------------Challenge Three------------------------#
print("Running Challenge Prefix\n")
r = requests.post("http://challenge.code2040.org/api/prefix", data=token)
jsonresult = json.loads(r.text)
result = jsonresult["result"]
prefix = result["prefix"]
array = result["array"]
temp = []
print("Prefix is " + prefix)
print("Array is ")
for i in range(0,len(array)):
print(array[i])
for j in range(0, len(array)):
if array[j].startswith(prefix) == False:
temp.append(array[j])
len = len(temp)
print(len)
jsonstring = {"token":stringtoken, "array": temp}
jsonstring = json.dumps(jsonstring)
r = requests.post("http://challenge.code2040.org/api/validateprefix", data=jsonstring)
results = json.loads(r.text)
results = results["result"]
print("Did I pass the test? \n" + results)
if results == "PASS: stage3. Enrollment record updated!":
print("Congratulations, on to the next one\n")
else:
print("Uh oh, there may be something wrong with the code\n")
#---------------------------Challenge Four---------------------------#
print("Running Challenge Four\n")
r = requests.post("http://challenge.code2040.org/api/time", data=token)
jsonresult = json.loads(r.text)
result = jsonresult["result"]
date = result["datestamp"]
interval = result["interval"]
print("Datestamp is " + date)
datestamp = datetime.datetime.strptime(date, "%Y-%m-%dT%H:%M:%S.%fZ")
datestampplus = datestamp + datetime.timedelta(0,interval)
datestampplus = datestampplus.isoformat()
print("New datestamp is " + datestampplus)
jsonstring = {"token":stringtoken, "datestamp": datestampplus}
jsonstring = json.dumps(jsonstring)
r = requests.post("http://challenge.code2040.org/api/validatetime", data=jsonstring)
results = json.loads(r.text)
results = results["result"]
print("Did I pass the test? \n" + results)
if results == "PASS: stage4. Enrollment record updated!":
print("Congratulations, all tests complete!\n")
else:
print("Uh oh, there may be something wrong with the code\n")
#-----------------------Status-----------------------------#
print("Checking Status\n")
r = requests.post("http://challenge.code2040.org/api/status", data=token)
jsonresult = json.loads(r.text)
result = jsonresult["result"]
print("Your results... ")
for k,v in result.items():
print(k,v)