-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
executable file
·49 lines (38 loc) · 1.16 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
# -*- coding: utf-8 -*-
import cherrypy
import cherrypy_cors
import json
import os
import requests
from urllib.parse import urlparse, urlencode
from newsplease import NewsPlease
cherrypy_cors.install()
class DatetimeEncoder(json.JSONEncoder):
def default(self, obj):
try:
return super(DatetimeEncoder, obj).default(obj)
except TypeError:
return str(obj)
class NewsParserAPI(object):
@cherrypy.expose
@cherrypy.tools.json_out()
def default(self, *args, **params):
response = {}
if 'url' in params:
article = NewsPlease.from_url(params['url'])
response = article.get_dict()
response = json.loads(json.dumps(response, cls=DatetimeEncoder))
return response
config = {
'global': {
'server.socket_host': '0.0.0.0',
'server.socket_port': int(os.environ.get('PORT', 5000)),
'cors.expose.on': True,
},
'/assets': {
'tools.staticdir.root': os.path.dirname(os.path.abspath(__file__)),
'tools.staticdir.on': True,
'tools.staticdir.dir': 'assets',
}
}
cherrypy.quickstart(NewsParserAPI(), '/', config=config)