forked from john-kurkowski/tldextract
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandlers.py
36 lines (24 loc) · 854 Bytes
/
handlers.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
'''web.py handlers for making a JSON-over-HTTP API around tldextract.'''
import json
# pylint: disable=import-error
import tldextract
import web
URLS = (
'/api/extract', 'Extract',
'/api/re', 'TLDSet',
'/test', 'Test',
)
class Extract(object):
def GET(self): # pylint: disable=invalid-name,no-self-use
url = web.input(url='').url
if not url:
return web.webapi.badrequest()
ext = tldextract.extract(url)._asdict()
web.header('Content-Type', 'application/json')
return json.dumps(ext) + '\n'
class TLDSet(object):
def GET(self): # pylint: disable=invalid-name,no-self-use
web.header('Content-Type', 'text/html; charset=utf-8')
return '<br/>'.join(sorted(tldextract.tldextract.TLD_EXTRACTOR.tlds))
APP = web.application(URLS, globals())
main = APP.cgirun()