forked from micah-weitzman/mlh-api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
executable file
·106 lines (91 loc) · 3.24 KB
/
app.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
#!flask/bin/python
from flask import Flask, jsonify
from bs4 import BeautifulSoup
import requests, time
import urllib
import datetime
app = Flask(__name__)
year = str(datetime.date.today().year)
def request_stuff(season, events):
mlh_url = "https://mlh.io/seasons/%s/events" % (season)
mlh_html = requests.get(mlh_url)
soup = BeautifulSoup(mlh_html.content)
event_list = soup.find_all('div', {'class':'col-lg-3'})
for event_for in event_list:
event_id = str(event_for)
event_id = event_id[(event_id.find("event")+12):(event_id.find("event")+15)]
if event_id in events:
pass
else:
link_tag = str(event_for.find_all('a'))
index = link_tag.find('"') + 1
link = link_tag[index:]
index = link.find('"')
link = link[:index]
event_head = str(event_for.find_all('h3'))
index = event_head.find(">") + 1
event_head = event_head[index:]
index = event_head.find("<")
event_head = event_head[:index]
event_date = str(event_for.find_all('p'))
index = event_date.find(">") + 1
event_date = event_date[index:]
index = event_date.find("<")
event_date = event_date[:index]
event_loc = str(event_for.find_all('p')[1])
index = event_loc.find(">") + 1
event_loc = event_loc[index:]
index = event_loc.find("<")
event_loc = event_loc[:index]
event = {}
event["location"] = event_loc
event["date"] = event_date
event["name"] = event_head
event["link"] = link
event["id"] = event_id
events[event_head] = event
@app.route('/')
def index():
us_event = {}
request_stuff("s" + year, us_event)
eu_event = {}
request_stuff("s"+year+"-eu", eu_event)
event_all = {"us_event":us_event,"eu_event":eu_event}
return jsonify(event_all)
@app.route('/<string:mlh_season>/')
def select_season(mlh_season):
events_all = {}
while True:
request_stuff(mlh_season, events_all)
return jsonify(events_all)
# time to wait until refresh
time.sleep(1800)
@app.route('/event/<string:mlh_event>/')
def search_event(mlh_event):
us_event = {}
request_stuff("s"+year, us_event)
eu_event = {}
request_stuff("s"+year+"-eu", eu_event)
for evnt in us_event:
if urllib.unquote(mlh_event.lower()) == evnt.lower():
return jsonify(us_event[evnt])
else:
for i in eu_event:
if urllib.unquote(mlh_event.lower()) == i.lower():
return jsonify(eu_event[i])
@app.route('/search/<string:mlh_event>/<string:key_>/')
def search_by_key(mlh_event, key_):
us_event = {}
request_stuff("s"+year, us_event)
eu_event = {}
request_stuff("s"+year+"-eu", eu_event)
for evnt in us_event:
if urllib.unquote(mlh_event.lower()) == evnt.lower():
return us_event[evnt][key_]
else:
for i in eu_event:
if urllib.unquote(mlh_event.lower()) == i.lower():
return eu_event[i][key_]
if __name__ == '__main__':
app.run(debug=True, port=50981
)