-
Notifications
You must be signed in to change notification settings - Fork 0
/
registration_API.py
189 lines (154 loc) · 6.67 KB
/
registration_API.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
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
# Author: Josuel Musambaghani
# Email: joshlixmus@gmail.com
# ____________________________________________________________________________________________________________________
#############################################---------PART ONE----------##############################################
# This is the foundation of my work, a solid reference from which all challenges will have their resources.
import urllib2
import json
# First, let us start by the registration ...
def register(email,github): # email="joshlixmus@gmail.com and github="https://github.com/Joshlix95"
url = 'http://challenge.code2040.org/api/register'
values = { 'email': email, 'github':github}
req = urllib2.Request(url)
req.add_header('Content-Type', 'Application/json')
rsp = urllib2.urlopen(req, json.dumps(values))
output = rsp.read()
return output
# Now it’s time for the challenge.
# The value of token is given in my previous code.
def getchallenge(token,url):
info = {'token':token}
request = urllib2.Request(url)
request.add_header('Content-Type', 'application/json')
response = urllib2.urlopen(request, json.dumps(info))
output = response.read()
return output
# Now it comes to precise what stage of the challenge I want to deal with.
# validatechallenge() takes two arguments:
# 1. a JSON string with the answer for the previous challenge, and
# 2. an url (as described and given in the guide)
def validatechallenge(answer,url):
request = urllib2.Request(url)
request.add_header('Content-Type','application\json')
response = urllib2.urlopen(request,json.dumps(answer))
content = response.read()
print ""
print "*************** API result ****************** "
return json.loads(content)['result']
# Whenever I need to check my grades, a function Grades is defined here below.
def Grades(token):
info = {'token':token}
request = urllib2.Request('http://challenge.code2040.org/api/status')
request.add_header('Content-Type', 'application/json')
response = urllib2.urlopen(request, json.dumps(info))
content = response.read()
result = json.loads(content)['result']
print
print "************** Grades ****************************"
for challenge in sorted(result.keys()):
print challenge + '\t\t' + str(result[challenge])
#############################################------------PART TWO-----------###########################################
# this part deals with the four challenges challenges, and the way they are linked with the json for code executions.
from datetime import datetime
from datetime import timedelta
import json
def reversed_string(data,token):
print "**************************"
print "Reversed String Challenge"
print "**************************"
string = json.loads(data)['result']
reversed_str = string[::-1]
print "The initial string: ", string
print "The reversed String: ", reversed_str
return {'token':token, 'string':reversed_str}
def prefix(data,token):
print "*******************************"
print "Prefix"
print "*******************************"
result = json.loads(data)['result']
array = result['array']
prefix = result['prefix']
new_array = []
print "Array: ", array
print "Prefix: ", prefix
for item in array:
if item.find(str(prefix)) < 0:
new_array.append(item)
print "The array without the given prefix"
print new_array
return {'token':token,'array':new_array}
def findneedle(data,token):
print "************************"
print "Needle In The Haystack"
print "************************"
json_data = json.loads(data)
needle = json_data['result']['needle']
haystack = json_data['result']['haystack']
index = 0
print "Needle: ", needle
print "Haystack: ", haystack
for stuff in haystack:
if needle == stuff:
print "Index of needle: ", index
return {'token':token,'needle':index}
index += 1
def datingGame(data,token):
print "************************"
print "The Dating Game"
print "************************"
result = json.loads(data)['result']
datestamp = result['datestamp']
interval = result['interval']
time = iso8601.parse_date(datestamp)
dt = datetime.strptime((dico["datestamp"]), "%Y-%m-%d %H:%M")
return {'token':token, 'datestamp':str(dt+timedelta(seconds=(dico["interval"])))}
#####################################-----------------PART THREE----------------#######################################
# My principal function for navigation! This part will help me to go through all my built code.
import json
def main():
my_token = 'rmUQt128vF'
challenge_urls = [
'http://challenge.code2040.org/api/getstring',
'http://challenge.code2040.org/api/prefix',
'http://challenge.code2040.org/api/time',
'http://challenge.code2040.org/api/haystack',
]
validation_urls= [
'http://challenge.code2040.org/api/validatestring',
'http://challenge.code2040.org/api/validateprefix',
'http://challenge.code2040.org/api/validatetime',
'http://challenge.code2040.org/api/validateneedle'
]
valid_choice = True
while(valid_choice == True):
print "++++++++++++++++++++++++++++++++++++++++++"
print "What challenge do you want to solve? *"
print "1.- Reverse String "
print "2.- Prefix "
print "3.- Haystack "
print "4.- The Dating Game "
print " "
print "Write 'Grades' to check grade on the API *"
print "++++++++++++++++++++++++++++++++++++++++++"
choice = raw_input("Enter your choice: ")
if choice == '1':
challenge_data = getchallenge(my_token,challenge_urls[0])
challenge_answer = reversed_string(challenge_data,my_token)
print validatechallenge(challenge_answer,validation_urls[0])
elif choice == '2':
challenge_data = getchallenge(my_token,challenge_urls[1])
challenge_answer = prefix(challenge_data,my_token)
print validatechallenge(challenge_answer,validation_urls[1])
elif choice == '3':
challenge_data = getchallenge(my_token,challenge_urls[2])
challenge_answer = datingGame(challenge_data,my_token)
print validatechallenge(challenge_answer,validation_urls[2])
elif choice == '4':
challenge_data = getchallenge(my_token,challenge_urls[3])
challenge_answer = findneedle(challenge_data,my_token)
print validatechallenge(challenge_answer,validation_urls[3])
elif choice.lower() == 'grades':
Grades(my_token)
else:
valid_choice = False
main()