Skip to content

Instantly share code, notes, and snippets.

@dlopes7
Created March 23, 2018 13:29
Show Gist options
  • Save dlopes7/0badcb5cc1f0441bd9a9e09f25141fbe to your computer and use it in GitHub Desktop.
Save dlopes7/0badcb5cc1f0441bd9a9e09f25141fbe to your computer and use it in GitHub Desktop.
Creates the Geo Location XML with data from dbip
import csv
from unicodedata import normalize
root = """<mappings>
{elements}</mappings>"""
element = """<mapping>
<ip-range from="{ip_from}" to="{ip_to}"/>
<location country="{country}" region="{region}" city="{city}"/>
</mapping>"""
regions = {'Acre': 'Acre',
'Alagoas': 'Alagoas',
'Am': 'Amazonas',
'Amapa': 'Amapa',
'Amazonas': 'Amazonas',
'Ba': 'Bahia',
'Bahia': 'Bahia',
'Ce': 'Ceara',
'Ceara': 'Ceara',
'Df': 'Federal District',
'Distrito Federal': 'Federal District',
'Espirito Santo': 'Espirito Santo',
'Federal District': 'Federal District',
'Go': 'Goias',
'Goias': 'Goias',
'Maranhao': 'Maranhao',
'Mato Grosso': 'Mato Grosso',
'Mato Grosso Do Sul': 'Mato Grosso do Sul',
'Mg': 'Minas Gerais',
'Minas Gerais': 'Minas Gerais',
'Ms': 'Mato Grosso do Sul',
'Para': 'Para',
'Paraiba': 'Paraiba',
'Parana': 'Parana',
'Pe': 'Pernambuco',
'Pernambuco': 'Pernambuco',
'Piaui': 'Piaui',
'Pr': 'Parana',
'Rio De Janeiro': 'Rio De Janeiro',
'Rio Grande Do Norte': 'Rio Gramde do Norte',
'Rio Grande Do Sul': 'Rio Grande do Sul',
'Rj': 'Rio de Janeiro',
'Rn': 'Rio Grande do Norte',
'Rondonia': 'Rondonia',
'Roraima': 'Roraima',
'Rs': 'Rio Grande do Sul',
'Santa Catarina': 'Santa Catarina',
'Sao Paulo': 'Sao Paulo',
'Sc': 'Santa Catarina',
'Sergipe': 'Sergipe',
'Sp': 'Sao Paulo',
'Tocantins': 'Tocantins'
}
def remove_accents(txt):
return normalize('NFKD', txt).encode('ASCII', 'ignore').decode('ASCII')
def main():
mappings = ''
lines = 1
with open('dbip-city-2018-03.csv', "r") as csvfile:
datareader = csv.reader(csvfile)
for row in datareader:
ip_from, ip_to, country, region, city = row
if country == 'BR' and ':' not in ip_from:
country = 'Brazil'
region = remove_accents(region).replace('State of ', '')
region = regions[region]
lines += 1
new_location = element.format(ip_from=ip_from,
ip_to=ip_to,
country=country,
region=region,
city=city)
mappings += new_location
if lines % 100 == 0:
print(lines / 128055 * 100, '%')
print(lines)
mappings += """<mapping>
<ip-range from="172.20.0.0" to="172.20.255.255"/>
<location country="Brazil" region="Sao Paulo" city="Escritorio Porto"/>
</mapping>"""
final_xml = root.format(elements=mappings)
with open('geo-ip-mappings.xml', 'w') as outfile:
outfile.write(final_xml)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment