-
Notifications
You must be signed in to change notification settings - Fork 14
/
hacks
executable file
·348 lines (319 loc) · 12.2 KB
/
hacks
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
#!/usr/bin/env python
import sys
import requests
import datetime
import calendar
import base64
no_of_arguments = len(sys.argv)
year = datetime.datetime.now().year
month = datetime.datetime.now().month
day = datetime.datetime.now().day
class Hackanswers:
"""object to hold information from our sources, to be dissected later."""
def fetch(self):
"""prepares the object to be queried for results by HackathonsList?(data),
where <data> is the city or location"""
print("Please wait a few seconds while we fetch the details for you...")
self.list1 = []
self.list2 = []
self.list3 = []
self.list4 = []
fetchalg = [self.fetchHackathonList,
self.fetchHackathonCal,
self.fetchHackathonWatch,
self.fetchHackathonIndia]
try:
import tqdm
fetchalg = tqdm.tqdm(fetchalg)
except ImportError:
pass
for alg in fetchalg:
alg()
def splitcontent(self, url):
"""possible abstraction for splitting request answeres"""
r = requests.get(url)
if r.status_code != 200:
raise RuntimeError
decoded_content = base64.b64decode(r.json()['content']).decode()
return decoded_content.split('\n')
# list1[r1,r2]
def fetchHackathonList(self):
url_1_1 = "http://www.hackalist.org/api/1.0/" + str(year) + "/" + "{:02}".format(month) + ".json"
url_1_2 = "http://www.hackalist.org/api/1.0/" + str(year) + "/" + "{:02}".format(month+1) + ".json"
if month == 12:
url_1_2 = "http://www.hackalist.org/api/1.0/" + str(year + 1) + "/" + "01" + ".json"
r1 = requests.get(url_1_1)
if r1.status_code != 200:
print("Couldn't fetch hackalist.org [1]")
else:
self.list1.append(r1.json())
r2 = requests.get(url_1_2)
if r2.status_code != 200:
print("Couldn't fetch hackalist.org [2]")
else:
self.list1.append(r2.json())
def fetchHackathonCal(self):
url_2 = "https://api.github.com/repos/japacible/Hackathon-Calendar/contents/README.md?ref=master"
try:
self.list2.append(self.splitcontent(url_2))
except RuntimeError:
print("Couldn't fetch Hackathon-Calendar")
def fetchHackathonWatch(self):
url_3 = "http://hackathonwatch.com:80/api//hackathons/coming.json?page=1"
r = requests.get(url_3)
if r.status_code != 200:
print("Couldn't fetch hackathonwatch.com")
else:
self.list3.append(r.json())
def fetchHackathonIndia(self):
url_4 = "https://api.github.com/repos/waseem18/Hackathons-In-India/contents/README.md"
try:
self.list4.append(self.splitcontent(url_4))
except RuntimeError:
print("Couldn't fetch Hackathons-In-India")
# initialization
hacks_result = []
hacks_answer = Hackanswers()
def HackathonsList1(city, source=hacks_answer, target=None):
data1 = source.list1[0]
data2 = source.list1[1]
if target is None:
target=hacks_result
for i in data1[str(calendar.month_name[month])]:
if city.lower() in (i['host'] + " " + i['city']).lower():
hack = {}
hack['Title'] = i['title']
hack['URL'] = i['url']
if i['highSchoolers'] == "yes":
hack['HighSchoolers'] = "Yes"
else:
hack['HighSchoolers'] = "No"
hack['Starts on'] = i['startDate'] + " " + i['year']
hack['Ends on'] = i['endDate'] + " " + i['year']
hack['location'] = i['host'] + " " + i['city']
if i['travel'] == "" or i['travel'] == "unknown":
hack['Travel'] = "Not mentioned"
elif i['travel'] == "yes":
hack['Travel'] = "Reimbursment provided!"
elif i['travel'] == "no":
hack['Travel'] = "No reimbursment!"
if i['facebookURL'] != "":
hack['Contact'] = "i['facebookURL']"
elif i['twitterURL'] != "":
hack['Contact'] = i['twitterURL']
target.append(hack)
for i in data2[str(calendar.month_name[1])]:
if city.lower() in (i['host'] + " " + i['city']).lower():
hack = {}
hack['Title'] = i['title']
hack['URL'] = i['url']
if i['highSchoolers'] == "yes":
hack['HighSchoolers'] = "Yes"
else:
hack['HighSchoolers'] = "No"
hack['Starts on'] = i['startDate'] + " " + i['year']
hack['Ends on'] = i['endDate'] + " " + i['year']
hack['location'] = i['host'] + " " + i['city']
if i['travel'] == "" or i['travel'] == "unknown":
hack['travel'] = "Not mentioned!"
elif i['travel'] == "yes":
hack['travel'] = "Reimbursement provided!"
elif i['travel'] == "no":
hack['travel'] = "No reimbursment"
if i['facebookURL'] != "":
hack['Contact'] = i['facebookURL']
elif i['twitterURL'] != "":
hack['Contact'] = i['twitterURL']
target.append(hack)
def HackathonsList2(city, source=hacks_answer, target=None):
if target is None:
target=hacks_result
decoded_content = source.list2[0]
for i in decoded_content:
hack = {}
if i[0:3] == "| [":
j = 3
title = ""
link = ""
place = ""
duration = ""
while str(i[j]) != "]":
title += i[j]
j += 1
hack['Title'] = title
j += 2
while str(i[j]) != ")":
link += i[j]
j += 1
hack['URL'] = link
j += 4
while str(i[j]) != "|":
place += i[j]
j += 1
hack['location'] = place
j += 2
while str(i[j]) != "|":
duration += i[j]
j += 1
duration = duration.split('-')
if len(duration) == 1:
start = duration[0].split('.')
hack['Starts on'] = str(calendar.month_name[int(start[0])]) + " " + str(start[1])
elif len(duration) == 2:
start = duration[0].split('.')
end = duration[1].split('.')
hack['Starts on'] = str(calendar.month_name[int(start[0])]) + " " + str(start[1])
hack['Ends on'] = str(calendar.month_name[int(end[0])]) + " " + str(end[1])
if city.lower() in hack['location'].lower():
target.append(hack)
def HackathonsList3(city, source=hacks_answer, target=None):
if target is None:
target=hacks_result
data = source.list3[0]
for i in data:
if city.lower() in i['full_address'].lower():
hack = {}
hack['Title'] = i['name']
hack['URL'] = i['public_url']
duration = int(i['finish_timestamp']) - int(i['start_timestamp'])
hack['Duration'] = str(duration)
# no_of_hours = duration / 3600
hack['Starts on'] = datetime.datetime.fromtimestamp(i['start_timestamp']).strftime(
'%Y-%m-%d %H:%M:%S')
hack['Ends on'] = datetime.datetime.fromtimestamp(i['finish_timestamp']).strftime(
'%Y-%m-%d %H:%M:%S')
hack['location'] = i['full_address']
target.append(hack)
def HackathonsList4(city, source=hacks_answer, target=None):
if target is None:
target=hacks_result
decoded_content = source.list4[0]
for i in decoded_content:
hack = {}
if str(i[0:2]) == "|[":
title = ""
link = ""
location = ""
start = ""
end = ""
j = 2
while i[j] != ']':
title += i[j]
j += 1
hack['Title'] = title
j += 2
while i[j] != ')':
link += i[j]
j += 1
hack['URL'] = link
j += 2
while i[j] != '|':
location += i[j]
j += 1
hack['location'] = location
j += 1
while i[j] != '|':
start += i[j]
j += 1
hack['Starts on'] = start
j += 1
while i[j] != '|':
end += i[j]
j += 1
hack['Ends on'] = end
if city.lower() in hack['location'].lower():
target.append(hack)
def print_help():
"""prints the help string"""
print("Usage:")
print(" 1. Just typing 'hacks' outputs the list of Hackathons in or near your location!\n")
print(" 2. Command 'hacks California' gives the list of Hackathons in California.\n")
print(" 3. We consider all arguments after first arguments as a single argument(city name)).\n")
print(" 4. So command 'hacks New York' too gives the list of Hackathons in New York.\n")
def runLists(data):
"""call this function with your location as <data>
it will append every valid Hackathon to hacks_result"""
for f in [HackathonsList1, HackathonsList2, HackathonsList3, HackathonsList4]:
try:
f(data)
except:
pass
def printResults():
"""This print all entries in hacks_result.
Consider running hacks_answer.fetch() beforehand.
"""
print("\n-----------------------------------------------------------------")
for i in hacks_result:
print("Title : " + i['Title'])
print("URL : " + i['URL'])
print("Starts on : " + i['Starts on'])
if 'Ends on' in i:
print("Ends on : " + i['Ends on'])
print("Location : " + i['location'])
print("-----------------------------------------------------------------")
print("")
def main():
global hacks_result
global hacks_answer
if no_of_arguments == 1:
# Outputs details of upcoming hackathons in the location of the user.
url = "http://ip-api.com/json"
r = requests.get(url)
data = r.json()
city = str(data['city'])
region = data['regionName']
country = data['country']
hacks_answer.fetch()
runLists(city)
runLists(region)
if len(hacks_result) == 0:
print("Looking for Hackathons in %s, %s..." % (region, country))
runLists(region) # is this line necessary?
runLists(country) # does this make L266-268 redundant?
if len(hacks_result) == 0:
print("Looking for Hackathons in %s..." % (country))
runLists(country) # can this possibly append something?
if len(hacks_result):
printResults()
else:
print(
"We couldn't find hackathons in %s, %s.\n"
"Try refining the search location Or try the command, 'hacks locationName'.\n"
"Eg: hacks California" % (city, country))
elif no_of_arguments == 2:
city = str(sys.argv[1])
if city == "-h" or city == "--help":
print_help()
sys.exit(0)
elif city == "--version":
print("Hacks - 0.1.1\n")
print("https://github.com/waseem18/hacks\n")
sys.exit(0)
elif city in ["-a", "--all"]:
hacks_answer.fetch()
runLists("")
printResults()
sys.exit(0)
hacks_answer.fetch()
runLists(city)
if len(hacks_result):
printResults()
else:
print(
"We couldn't find hackathons in %s.\n"
"Try refining the search location or find more hackathons at "
"https://github.com/japacible/Hackathon-Calendar" % (city))
elif no_of_arguments > 2:
city = " ".join(sys.argv[1:])
hacks_answer.fetch()
runLists(city)
if len(hacks_result):
printResults()
else:
print(
"We couldn't find hackathons in %s.\n"
"Try refining the search location or find more hackathons at "
"https://github.com/japacible/Hackathon-Calendar" % (city))
sys.exit(0)
if __name__ == "__main__":
main()