-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
71 lines (50 loc) · 1.63 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
import time
import calendar
import eventlet
import requests
from flask import *
from config import *
from datetime import datetime
from lxml.html import fromstring
from main import main, configured_servers
time.sleep(STARTUP_DELAY) # Wait for the startup delay so the other applications have time to boot up
main() # initialize configured_servers
app = Flask(__name__)
configured_urls = []
short_urls = []
short_url_use = []
titles = []
for server in configured_servers:
configured_urls += server.get_link_urls()
short_urls += server.get_short_urls()
short_url_use += server.get_short_url_use()
# Pull the title of the webpage from the URL
def get_title(url):
global title
try:
with eventlet.Timeout(2):
r = requests.get(url) # 2 second ping timeout in case of invalid url
tree = fromstring(r.content)
title = tree.findtext('.//title')
if r.status_code != 200:
title = ''
except:
title = ''
return title
eventlet.monkey_patch() # Necessary to set timeouts on requests
for index in range(0, len(configured_urls)):
titles.append(get_title(configured_urls[index]))
@app.context_processor
def inject_now():
return {'now': datetime.utcnow()}
@app.context_processor
def inject_time_to_all_templates():
return dict(time=get_time())
@app.route('/')
def index():
return render_template("index.html", urls=configured_urls, short_urls=short_urls, short_url_use=short_url_use,
titles=titles)
def get_time():
return calendar.timegm(datetime.now().utctimetuple())
if __name__ == '__main__':
app.run()