forked from Kozea/pystil
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmigrate.py
executable file
·51 lines (46 loc) · 1.53 KB
/
migrate.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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Copyright (C) 2011 by Florian Mounier, Kozea
# This file is part of pystil, licensed under a 3-clause BSD license.
from pystil import config
config.freeze()
from pystil.corns import Visit
from pygeoip import GeoIP, MMAP_CACHE
import re
gip = GeoIP(config.CONFIG['IP_DB'], MMAP_CACHE)
IPV4RE = re.compile(r"(\d{1,3}(\.|$)){4}")
for visit in Visit.all.execute():
lat = None
lng = None
country_code = None
ip = visit['ip']
ip = ip.replace('::ffff:', '')
if IPV4RE.match(ip):
if (ip == '127.0.0.1'
or ip.startswith('192.168.')
or ip.startswith('10.')):
city = 'Local'
country = 'Local'
else:
location = gip.record_by_addr(ip)
city = (location.get('city', 'Unknown')
.decode('iso-8859-1')
if location else 'Unknown')
country = (location.get('country_name', 'Unknown')
.decode('iso-8859-1')
if location else 'Unknown')
country_code = (location.get('country_code', 'Unknown')
.decode('iso-8859-1')
if location else 'Unknown')
lat = location.get('latitude', None)
lng = location.get('longitude', None)
else:
country = 'ipv6'
city = 'ipv6'
visit['country'] = country
visit['country_code'] = country_code
visit['city'] = city
visit['lat'] = lat
visit['lng'] = lng
visit.save()
print city, " done"