def get_country_from_code(code): """Get the country code from a coutry name.""" try: name = transformations.cc_to_cn(code) return SUBNAMES.get(name, name) except KeyError: pass
def get_country_from_code(code): """Get the country code from a coutry name.""" try: name = transformations.cc_to_cn(code) return SUBNAMES.get(name, name) except KeyError: pass
def _data_for_A(self, _type, record): # record meta (which would include geo information is only # returned when getting a record's detail, not from zone detail geo = defaultdict(list) data = { 'ttl': record['ttl'], 'type': _type, } values, codes = [], [] if 'answers' not in record: values = record['short_answers'] for answer in record.get('answers', []): meta = answer.get('meta', {}) if meta: # country + state and country + province are allowed # in that case though, supplying a state/province would # be redundant since the country would supercede in when # resolving the record. it is syntactically valid, however. country = meta.get('country', []) us_state = meta.get('us_state', []) ca_province = meta.get('ca_province', []) for cntry in country: cn = transformations.cc_to_cn(cntry) con = transformations.cn_to_ctca2(cn) key = '{}-{}'.format(con, cntry) geo[key].extend(answer['answer']) for state in us_state: key = 'NA-US-{}'.format(state) geo[key].extend(answer['answer']) for province in ca_province: key = 'NA-CA-{}'.format(province) geo[key].extend(answer['answer']) for code in meta.get('iso_region_code', []): key = code geo[key].extend(answer['answer']) else: values.extend(answer['answer']) codes.append([]) values = [unicode(x) for x in values] geo = OrderedDict( {unicode(k): [unicode(x) for x in v] for k, v in geo.items()}) data['values'] = values data['geo'] = geo return data
from incf.countryutils import transformations Current_db = 'MergedData' db = MySQLdb.connect(host=DB_configuration.host, user=DB_configuration.user, passwd=DB_configuration.passwd, db=Current_db) cur = db.cursor() continent = 'AF' query = "select IXP, IXPName , CC from AllRouteCollectors where Continent = '" + continent + "';" cur.execute(query) data = cur.fetchall() #print data ##path to the location of the outputs of the IXP view computations in the /var directory create_folder_command = 'mkdir /var/www/html/.../outputs' os.system(create_folder_command) Python_list_IXPs = [] ##path to the location of the final list of IXPs IXP_list_file = open('/var/www/html/.../outputs/list_IXPs.txt', 'w') for line in data: if line[0] not in Python_list_IXPs: country_name = transformations.cc_to_cn(str(line[2]).strip()) print line[2], country_name IXP_list_file.write(line[0] + ';' + line[2] + ';' + country_name + '\n') Python_list_IXPs.append(line[0])
def get_location(query, format, api_key): """Get geographic data of a lab in a coherent way for all labs.""" # Play nice with the API... sleep(1) geolocator = OpenCage(api_key=api_key, timeout=10) # Variables for storing the data data = { "city": None, "address_1": None, "postal_code": None, "country": None, "county": None, "state": None, "country_code": None, "latitude": None, "longitude": None, "continent": None } road = "" number = "" # Default None values location_data = { "city": None, "road": None, "house_number": None, "postcode": None, "country": None, "county": None, "state": None, "ISO_3166-1_alpha-2": None, "country_code": None, "lat": None, "lng": None } # Reverse geocoding ... from coordinates to address if format == "reverse": # If the query (coordinates) is not empty if query is None or len(query) < 3: pass else: location = geolocator.reverse(query) if location is not None: location_data = location[0].raw[u'components'] location_data["lat"] = location[0].raw[u'geometry']["lat"] location_data["lng"] = location[0].raw[u'geometry']["lng"] # Direct geocoding ... from address to coordinates and full address if format == "direct": # If the query (address) is not empty if query is None or len(query) < 3: pass else: location = geolocator.geocode(query) if location is not None: location_data = location.raw[u'components'] location_data["lat"] = location.raw[u'geometry']["lat"] location_data["lng"] = location.raw[u'geometry']["lng"] # Extract the meaningful data for component in location_data: if component == "town" or component == "city": data["city"] = location_data[component] if component == "road": road = location_data[component] if component == "house_number": number = location_data[component] if component == "postcode": data["postal_code"] = location_data[component] if component == "country": data["country"] = location_data[component] if component == "county": data["county"] = location_data[component] if component == "state": data["state"] = location_data[component] if component == "ISO_3166-1_alpha-2": data["country_code"] = location_data[component] # The address need to be reconstructed data["address_1"] = unicode(road) + " " + unicode(number) data["latitude"] = location_data["lat"] data["longitude"] = location_data["lng"] # Format the country code to three letters try: country_data = transformations.cca2_to_ccn(data["country_code"]) data["country_code"] = transformations.ccn_to_cca3(country_data) except: data["country_code"] = None # Get the continent try: country_data = transformations.cc_to_cn(data["country_code"]) data["continent"] = transformations.cn_to_ctn(country_data) except: data["continent"] = None # Return the final data return data