48 lines
1.4 KiB
Python
48 lines
1.4 KiB
Python
|
# utility functions for countries
|
||
|
# import pycountry
|
||
|
import socket
|
||
|
from urllib.request import urlopen
|
||
|
import json
|
||
|
from urllib.parse import urlparse
|
||
|
|
||
|
def getEuTlds():
|
||
|
# map tld to alpha_2
|
||
|
return { '.ad': 'AD', '.at': 'AT', '.be':'BE', '.ch':'CH', '.cz':'CZ',
|
||
|
'.de':'DE', '.dk':'DK', '.ee':'EE', '.es':'ES', '.eu':'EU', '.fi':'FI',
|
||
|
'.fr':'FR', '.gr':'GR', '.hr':'HR', '.hu':'HU', '.ie':'IE', '.it':'IT',
|
||
|
'.li':'LI', '.lt':'LT', '.lu':'LU', '.lv':'LV', '.nl':'NL', '.no':'NO',
|
||
|
'.pl':'PL', '.pt':'PT', '.ro':'RO', '.se':'SE', '.si':'SI', '.sk':'SK',
|
||
|
'.bg':'BG', '.cy':'CY', '.mt':'MT'}
|
||
|
|
||
|
def getCountryOfUrl(url):
|
||
|
ip = socket.gethostbyname(url)
|
||
|
api_url = 'https://ipinfo.io/' + ip + '/json'
|
||
|
response = urlopen(api_url)
|
||
|
data = json.load(response)
|
||
|
return data['country']
|
||
|
|
||
|
def isCountryGerman(self, url):
|
||
|
return 'DE' == getCountryOfUrl(url)
|
||
|
|
||
|
def isGermanTLD(url):
|
||
|
parts = urlparse(url)
|
||
|
tld = parts.hostname[-3:]
|
||
|
return tld == '.de'
|
||
|
|
||
|
def isGerman(self, url):
|
||
|
if not isGermanTLD(url):
|
||
|
return isCountryGerman(url)
|
||
|
return True
|
||
|
|
||
|
def isEuropean(url):
|
||
|
eu_tlds = getEuTlds()
|
||
|
parts = urlparse(url)
|
||
|
tld = parts.hostname[-3:]
|
||
|
if tld in eu_tlds:
|
||
|
return eu_tlds[tld]
|
||
|
country = getCountryOfUrl(url)
|
||
|
if country in eu_tlds.values():
|
||
|
return country
|
||
|
return False
|
||
|
|