forked from omaha-consulting/omaha-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
uncensorp_out.py
29 lines (21 loc) · 857 Bytes
/
uncensorp_out.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
from twisted.web import resource
from db_helper import DbHelper
from config import Config
import json
from twisted.web.client import getPage
from twisted.web import server
import pycountry
import pygeoip
class UncensorPOutResource(resource.Resource):
isLeaf = True
pathFromRoot = '/service/uncensorp_domains'
def render_GET(self, request):
gi = pygeoip.GeoIP('GeoIP.dat', pygeoip.MEMORY_CACHE)
country_code = gi.country_code_by_addr(request.getHeader('x-forwarded-for'))
macdb = DbHelper()
res = macdb.uncensorp_fetch_by_iso(country_code)
res = { 'domains': [rec['domain'] for rec in res], 'country_code': country_code,
'country_name': pycountry.countries.get(alpha2=country_code).name.encode('utf-8') }
macdb.cleanup()
request.setHeader('Content-Type', 'application/json')
return json.dumps(res)