@@ -0,0 +1,2501 @@
+#!/usr/bin/env python%0A%0Aimport re%0Aimport sys%0Aimport os%0Afrom os.path import abspath, dirname, join%0Aimport urllib%0Afrom MySQLdb import IntegrityError%0Afrom datetime import datetime%0Aimport pinax%0A%0Afrom django.conf import settings%0Afrom django.core.management import setup_environ, execute_from_command_line%0A%0Asys.path.append(os.path.dirname(os.path.abspath(__file__ + '/../..')))%0Atry:%0A    import settings as settings_mod # Assumed to be in the same directory.%0Aexcept ImportError:%0A    sys.stderr.write(%22Error: Can't find the file 'settings.py' in the directory containing %25r. It appears you've customized things.%5CnYou'll have to run django-admin.py, passing it your settings module.%5Cn(If the file settings.py does indeed exist, it's causing an ImportError somehow.)%5Cn%22 %25 __file__)%0A    sys.exit(1)%0A%0A# setup the environment before we start accessing things in the settings.%0Asetup_environ(settings_mod)%0A%0Asys.path.insert(0, join(settings.PINAX_ROOT, %22apps%22))%0Asys.path.insert(0, join(settings.PROJECT_ROOT, %22apps%22))%0Afrom turan.models import Location%0A%0Aif __name__ == '__main__':%0A    countries = %7B%7D%0A    if os.path.exists(%22countryInfo.txt%22):%0A        data = file(%22countryInfo.txt%22)%0A    else:%0A        print %22Using network%22%0A        countries_url = %22http://download.geonames.org/export/dump/countryInfo.txt%22%0A        data = urllib.urlopen(countries_url, %22r%22)%0A%0A    for line in data.readlines():%0A        if line%5B0%5D in %5B%22#%22, %22%5Cn%22, %22%5Cr%22%5D:%0A            continue%0A        fields = line.decode(%22UTF-8%22).strip(%22%5Cr%22).split(%22%5Ct%22)%0A        try:%0A            countries%5Bfields%5B0%5D%5D = fields%5B4%5D%0A        except IndexError:%0A            print line%0A%0A    if os.path.exists(%22cities1000.txt%22):%0A        data = file(%22cities1000.txt%22)%0A    else:%0A        print %22Need to download cities1000%22%0A        sys.exit(1)%0A%0A    Location.objects.filter(url__startswith=%22http://www.geonames.org%22).delete()%0A%0A    for line in data.readlines():%0A#        print line%0A        fields = line.decode(%22UTF-8%22).strip(%22%5Cr%22).split(%22%5Ct%22)%0A#        fields = %5Bx.decode(%22UTF-8%22) for x in fields%5D%0A        l = Location()%0A        l.town = fields%5B1%5D%0A        l.lat = fields%5B4%5D%0A        l.lon = fields%5B5%5D%0A        l.url = %22http://www.geonames.org/%25s/%22 %25 (fields%5B0%5D)%0A        l.country = countries%5Bfields%5B8%5D%5D%0A        # Hack until we get Location.country%0A        l.town = l.town + %22, %22 + l.country%0A        try:%0A            l.save()%0A        except ValueError, e:%0A            pass%0A        except IntegrityError:%0A            pass%0A        except Exception, e:%0A            print line%0A            raise e%0A

Add geonames import filter