2016-08-12 15:45:28 +02:00
import json
import requests
misperrors = { ' error ' : ' Error ' }
2016-08-12 18:40:00 +02:00
mispattributes = { ' input ' : [ ' hostname ' , ' domain ' ] }
2016-08-12 15:45:28 +02:00
# possible module-types: 'expansion', 'hover' or both
2024-08-12 11:23:10 +02:00
moduleinfo = {
' version ' : ' 1 ' ,
' author ' : ' Hannah Ward ' ,
' description ' : ' Module to expand country codes. ' ,
' module-type ' : [ ' hover ' ] ,
' name ' : ' Country Code ' ,
' logo ' : ' ' ,
' requirements ' : [ ] ,
' features ' : ' The module takes a domain or a hostname as input, and returns the country it belongs to. \n \n For non country domains, a list of the most common possible extensions is used. ' ,
' references ' : [ ] ,
' input ' : ' Hostname or domain attribute. ' ,
' output ' : ' Text with the country code the input belongs to. ' ,
}
2016-08-12 15:45:28 +02:00
# config fields that your code expects from the site admin
moduleconfig = [ ]
2018-12-11 15:29:09 +01:00
common_tlds = { " com " : " Commercial (Worldwide) " ,
" org " : " Organisation (Worldwide) " ,
" net " : " Network (Worldwide) " ,
" int " : " International (Worldwide) " ,
" edu " : " Education (Usually USA) " ,
" gov " : " Government (USA) "
}
2016-08-12 15:45:28 +02:00
2019-10-08 15:45:06 +02:00
def parse_country_code ( extension ) :
# Retrieve a json full of country info
try :
codes = requests . get ( " http://www.geognos.com/api/en/countries/info/all.json " ) . json ( )
except Exception :
return " http://www.geognos.com/api/en/countries/info/all.json not reachable "
if not codes . get ( ' StatusMsg ' ) or not codes [ " StatusMsg " ] == " OK " :
return ' Not able to get the countrycode references from http://www.geognos.com/api/en/countries/info/all.json '
for country in codes [ ' Results ' ] . values ( ) :
if country [ ' CountryCodes ' ] [ ' tld ' ] == extension :
return country [ ' Name ' ]
return " Unknown "
2016-08-17 10:51:16 +02:00
2018-12-11 15:29:09 +01:00
2016-08-12 15:45:28 +02:00
def handler ( q = False ) :
if q is False :
return False
request = json . loads ( q )
2018-11-15 19:44:43 +01:00
domain = request [ " domain " ] if " domain " in request else request [ " hostname " ]
2016-08-12 15:45:28 +02:00
# Get the extension
ext = domain . split ( " . " ) [ - 1 ]
2018-11-15 19:44:43 +01:00
# Check if it's a common, non country one
2019-10-08 15:45:06 +02:00
val = common_tlds [ ext ] if ext in common_tlds . keys ( ) else parse_country_code ( ext )
2018-12-11 15:29:09 +01:00
r = { ' results ' : [ { ' types ' : [ ' text ' ] , ' values ' : [ val ] } ] }
2016-08-12 15:45:28 +02:00
return r
def introspection ( ) :
return mispattributes
def version ( ) :
moduleinfo [ ' config ' ] = moduleconfig
return moduleinfo