-
Notifications
You must be signed in to change notification settings - Fork 9
/
main.py
62 lines (57 loc) · 2.41 KB
/
main.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
from lxml import etree
from urlparse import urljoin
import json
import os
import webapp2
from google.appengine.api import urlfetch
from google.appengine.ext.webapp import template
class CheckWebsiteHandler(webapp2.RequestHandler):
def post(self):
website_url = self.request.get('websiteUrl')
response = {}
try:
# Fetch website URL.
website_result = urlfetch.fetch(website_url)
if website_result.status_code == 200:
website_url = website_result.final_url or website_url
# Parse the content
parser = etree.HTMLParser(encoding='utf-8')
# TODO Handle more encoding.
htmltree = etree.fromstring(website_result.content, parser)
# Try to find web manifest <link rel="manifest" href="...">.
value = htmltree.xpath("//link[@rel='manifest']/attribute::href")
if (len(value) > 0):
# Fetch web manifest.
manifest_url = value[0]
if "://" not in manifest_url:
manifest_url = urljoin(website_url, manifest_url)
try:
manifest_result = urlfetch.fetch(manifest_url)
response['manifestUrl'] = manifest_url
response['websiteUrl'] = website_url
if manifest_result.status_code == 200:
response['manifestUrl'] = manifest_result.final_url or manifest_url
if 'Access-Control-Allow-Origin' in manifest_result.headers:
response['originHeader'] = manifest_result.headers['Access-Control-Allow-Origin']
response['content'] = manifest_result.content
else:
response['error'] = 'Manifest %s is not HTTP 200.' % manifest_url
except (urlfetch.InvalidURLError, urlfetch.DownloadError) as e:
response['error'] = repr(e)
else:
response['error'] = 'No manifest detected at %s' % website_url
else:
response['error'] = 'Website %s is not HTTP 200.' % website_url
except (urlfetch.InvalidURLError, urlfetch.DownloadError) as e:
response['error'] = repr(e)
finally:
self.response.headers['Content-Type'] = 'application/json'
self.response.out.write(json.dumps(response))
class MainHandler(webapp2.RequestHandler):
def get(self):
self.response.write(template.render(
os.path.join(os.path.dirname(__file__), 'index.html'), {}))
app = webapp2.WSGIApplication([
('/', MainHandler),
('/check', CheckWebsiteHandler),
], debug=True)