diff --git a/ChangeLog b/ChangeLog index 02d4ac4..5c7e13b 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,19 @@ +1.2.5 2011-05-10 + * Remove Confidence and Accuracy Database it is unsued anyway. ( Boris + Zentner ) + * Add new methods enable_teredo, teredo, time_zone_by_country_and_region + and lib_version ( Boris Zentner ) + * Add support for Confidence and Accuracy Database. + * Add support for Netspeed Database. via: + id_by_addr + id_by_name + * Add IPv6 glue *** needs libGeoIP 1.4.7 *** + country_code_by_name_v6 + country_name_by_name_v6 + country_code_by_addr_v6 + country_name_by_addr_v6 ( Boris Zentner ) 1.2.4 2008-12-16 - * Add charset and set_charset methods , as well as + * Add charset and set_charset methods, as well as the new attributes GeoIP.GEOIP_CHARSET_ISO_8859_1 and GeoIP.GEOIP_CHARSET_UTF8 ( Boris Zentner ) * Add test_city_charset.py script showing howto use diff --git a/README b/README index dce7b8e..535d51a 100644 --- a/README +++ b/README @@ -1,6 +1,6 @@ Requirements: Python 2.0 or greater - GeoIP C Library 1.4.5 or greater + GeoIP C Library 1.4.7 or greater To Install: python setup.py build @@ -11,6 +11,9 @@ Usage: See test_org.py for example usage with GeoIP ISP and Organization See test_city.py for example usage with GeoIP City See test_region.py for example usage with GeoIP Region + See test_netspeed.py for example usage with GeoIP Netspeed + See test_v6.py for example usage with GeoIP v6 Country Database + See test_city_acc.py for example usage with GeoIP Confidence and Accuracy Database Troubleshooting: @@ -20,7 +23,7 @@ directory" error, add /usr/local/lib to /etc/ld.so.conf then run License: -Copyright (c) 2008 MaxMind LLC +Copyright (c) 2010 MaxMind LLC All rights reserved. This package is free software; it is licensed under the LGPL. diff --git a/py_GeoIP.c b/py_GeoIP.c index 21f70c4..7f3a9a5 100644 --- a/py_GeoIP.c +++ b/py_GeoIP.c @@ -83,6 +83,52 @@ GeoIP_GeoIP_dealloc(PyObject* self) PyObject_Del(self); } +static PyObject * GeoIP_country_code_by_name_v6_Py(PyObject *self, PyObject *args) { + char * name; + const char * retval; + GeoIP_GeoIPObject* GeoIP = (GeoIP_GeoIPObject*)self; + if (!PyArg_ParseTuple(args, "s", &name)) { + return NULL; + } + retval = GeoIP_country_code_by_name_v6(GeoIP->gi, name); + return Py_BuildValue("s", retval ); +} + +static PyObject * GeoIP_country_name_by_name_v6_Py(PyObject *self, PyObject *args) { + char * name; + const char * retval; + GeoIP_GeoIPObject* GeoIP = (GeoIP_GeoIPObject*)self; + if (!PyArg_ParseTuple(args, "s", &name)) { + return NULL; + } + retval = GeoIP_country_name_by_name_v6(GeoIP->gi, name); + return Py_BuildValue("s", retval); +} + +static PyObject * GeoIP_country_code_by_addr_v6_Py(PyObject *self, PyObject *args) { + char * name; + const char * retval; + GeoIP_GeoIPObject* GeoIP = (GeoIP_GeoIPObject*)self; + if (!PyArg_ParseTuple(args, "s", &name)) { + return NULL; + } + retval = GeoIP_country_code_by_addr_v6(GeoIP->gi, name); + return Py_BuildValue("s", retval); +} + +static PyObject * GeoIP_country_name_by_addr_v6_Py(PyObject *self, PyObject *args) { + char * name; + const char * retval; + PyObject * ret; + GeoIP_GeoIPObject* GeoIP = (GeoIP_GeoIPObject*)self; + if (!PyArg_ParseTuple(args, "s", &name)) { + return NULL; + } + retval = GeoIP_country_name_by_addr_v6(GeoIP->gi, name); + ret = Py_BuildValue("s", retval); + return ret; +} + static PyObject * GeoIP_country_code_by_name_Py(PyObject *self, PyObject *args) { char * name; const char * retval; @@ -155,6 +201,28 @@ static PyObject * GeoIP_org_by_name_Py(PyObject *self, PyObject *args) { return ret; } +static PyObject * GeoIP_id_by_addr_Py(PyObject *self, PyObject *args) { + char * name; + int i; + GeoIP_GeoIPObject* GeoIP = (GeoIP_GeoIPObject*)self; + if (!PyArg_ParseTuple(args, "s", &name)) { + return NULL; + } + i = GeoIP_id_by_addr(GeoIP->gi, name); + return Py_BuildValue("i", i); +} + +static PyObject * GeoIP_id_by_name_Py(PyObject *self, PyObject *args) { + char * name; + int i; + GeoIP_GeoIPObject* GeoIP = (GeoIP_GeoIPObject*)self; + if (!PyArg_ParseTuple(args, "s", &name)) { + return NULL; + } + i = GeoIP_id_by_name(GeoIP->gi, name); + return Py_BuildValue("i", i); +} + void GeoIP_SetItemString(PyObject *dict, const char * name, const char * value) { PyObject * nameObj; PyObject * valueObj; @@ -185,6 +253,30 @@ void GeoIP_SetItemInt(PyObject *dict, const char * name, int value) { Py_DECREF(valueObj); } +void GeoIP_SetConfItemInt(PyObject *dict, const char * name, int value) { + PyObject * nameObj; + PyObject * valueObj; + nameObj = Py_BuildValue("s",name); + valueObj = value == GEOIP_UNKNOWN_CONF + ? Py_BuildValue("") + : Py_BuildValue("i",value); + PyDict_SetItem(dict,nameObj,valueObj); + Py_DECREF(nameObj); + Py_DECREF(valueObj); +} + +void GeoIP_SetAccuracyItemInt(PyObject *dict, const char * name, int value) { + PyObject * nameObj; + PyObject * valueObj; + nameObj = Py_BuildValue("s",name); + valueObj = value == GEOIP_UNKNOWN_ACCURACY_RADIUS + ? Py_BuildValue("") + : Py_BuildValue("i",value); + PyDict_SetItem(dict,nameObj,valueObj); + Py_DECREF(nameObj); + Py_DECREF(valueObj); +} + static PyObject * GeoIP_region_populate_dict(GeoIPRegion * gir) { PyObject * retval; const char * region_name = NULL; @@ -199,7 +291,7 @@ static PyObject * GeoIP_region_populate_dict(GeoIPRegion * gir) { return retval; } -static PyObject * GeoIP_populate_dict(GeoIPRecord *gir) { +static PyObject * GeoIP_populate_dict(GeoIP* gi, GeoIPRecord *gir) { PyObject * retval; retval = PyDict_New(); GeoIP_SetItemString(retval,"country_code",gir->country_code); @@ -208,22 +300,39 @@ static PyObject * GeoIP_populate_dict(GeoIPRecord *gir) { GeoIP_SetItemString(retval,"region",gir->region); GeoIP_SetItemString(retval,"city",gir->city); GeoIP_SetItemString(retval,"postal_code",gir->postal_code); - GeoIP_SetItemFloat(retval,"latitude",gir->latitude); - GeoIP_SetItemFloat(retval,"longitude",gir->longitude); - /* - * metro_code is a alias for the depreciated dma_code. - * we use the depreciated gir->dma_code since the CAPI - * wrapper might be outdated and does not supply metro_code - */ - GeoIP_SetItemInt(retval,"dma_code",gir->dma_code); - /* we did __NOT__ use gir->metro_code here, since metro_code is - * somewhat new */ - GeoIP_SetItemInt(retval,"metro_code",gir->dma_code); - GeoIP_SetItemInt(retval,"area_code",gir->area_code); + GeoIP_SetItemFloat(retval,"latitude", gir->latitude); + GeoIP_SetItemFloat(retval,"longitude", gir->longitude); GeoIP_SetItemString(retval, "region_name", GeoIP_region_name_by_code(gir->country_code, gir->region)); GeoIP_SetItemString(retval, "time_zone", GeoIP_time_zone_by_country_and_region(gir->country_code, gir->region)); + if ( gi->databaseType != GEOIP_CITY_EDITION_REV0 ){ + /* + * metro_code is a alias for the depreciated dma_code. + * we use the depreciated gir->dma_code since the CAPI + * wrapper might be outdated and does not supply metro_code + */ + GeoIP_SetItemInt(retval,"dma_code",gir->dma_code); + /* we did __NOT__ use gir->metro_code here, since metro_code is + * somewhat new */ + GeoIP_SetItemInt(retval,"metro_code",gir->dma_code); + GeoIP_SetItemInt(retval,"area_code",gir->area_code); + } + +/* + * drop support for Confidence and Accuracy Database + * for now + + if ( gi->databaseType != GEOIP_CITY_EDITION_REV1 ){ + GeoIP_SetConfItemInt(retval, "country_conf", gir->country_conf ); + GeoIP_SetConfItemInt(retval, "region_conf", gir->region_conf ); + GeoIP_SetConfItemInt(retval, "city_conf", gir->city_conf ); + GeoIP_SetConfItemInt(retval, "postal_conf", gir->postal_conf ); + + GeoIP_SetAccuracyItemInt(retval, "accuracy_radius", gir->accuracy_radius ); + +*/ + GeoIPRecord_delete(gir); return retval; } @@ -240,7 +349,7 @@ static PyObject * GeoIP_record_by_addr_Py(PyObject *self, PyObject *args) { Py_INCREF(Py_None); return Py_None; } - return GeoIP_populate_dict(gir); + return GeoIP_populate_dict(GeoIP->gi, gir); } static PyObject * GeoIP_record_by_name_Py(PyObject *self, PyObject *args) { @@ -255,7 +364,7 @@ static PyObject * GeoIP_record_by_name_Py(PyObject *self, PyObject *args) { Py_INCREF(Py_None); return Py_None; } - return GeoIP_populate_dict(gir); + return GeoIP_populate_dict(GeoIP->gi, gir); } static PyObject * GeoIP_region_by_name_Py(PyObject *self, PyObject * args) { @@ -328,6 +437,36 @@ static PyObject * GeoIP_last_netmask_Py(PyObject *self, PyObject * args) { return Py_BuildValue("i", GeoIP_last_netmask(GeoIP->gi) ); } + +static PyObject * GeoIP_teredo_Py(PyObject *self, PyObject * args) { + GeoIP_GeoIPObject* GeoIP = (GeoIP_GeoIPObject*)self; + return Py_BuildValue("i", GeoIP_teredo(GeoIP->gi) ); +} + +static PyObject * GeoIP_enable_teredo_Py(PyObject *self, PyObject * args) { + GeoIP_GeoIPObject* GeoIP = (GeoIP_GeoIPObject*)self; + int teredo; + if (!PyArg_ParseTuple(args, "i", &teredo)) { + return NULL; + } + return Py_BuildValue("i", GeoIP_enable_teredo(GeoIP->gi, teredo)); +} + +static PyObject* +GeoIP_lib_version_Py(PyObject* self, PyObject *args) { + return Py_BuildValue("s", GeoIP_lib_version()); +} + +static PyObject * GeoIP_time_zone_by_country_and_region_Py(PyObject *self, PyObject * args) { + char * country_code, *region; + if (!PyArg_ParseTuple(args, "ss", &country_code, ®ion)) { + return NULL; + } + return Py_BuildValue("s", GeoIP_time_zone_by_country_and_region(country_code, region)); +} + + + static PyMethodDef GeoIP_Object_methods[] = { {"country_code_by_name", GeoIP_country_code_by_name_Py, 1, "Lookup Country Code By Name"}, {"country_name_by_name", GeoIP_country_name_by_name_Py, 1, "Lookup Country Name By Name"}, @@ -342,7 +481,15 @@ static PyMethodDef GeoIP_Object_methods[] = { {"range_by_ip", GeoIP_range_by_ip_Py, 1, "Lookup start and end IP's for a given IP"}, {"charset", GeoIP_charset_Py, 1, "Return the current charset ( either GEOIP_CHARSET_ISO_8859_1 or GEOIP_CHARSET_UTF8 )"}, {"set_charset", GeoIP_set_charset_Py, 1, "Set the charset for city records"}, - {"last_netmask", GeoIP_last_netmask_Py, 1, "return the netmask depth of the last lookup"}, + {"last_netmask", GeoIP_last_netmask_Py, 1, "Return the netmask depth of the last lookup"}, + {"country_code_by_name_v6", GeoIP_country_code_by_name_v6_Py, 1, "Lookup IPv6 Country Code By Name"}, + {"country_name_by_name_v6", GeoIP_country_name_by_name_v6_Py, 1, "Lookup IPv6 Country Name By Name"}, + {"country_code_by_addr_v6", GeoIP_country_code_by_addr_v6_Py, 1, "Lookup IPv6 Country Code By IP Address"}, + {"country_name_by_addr_v6", GeoIP_country_name_by_addr_v6_Py, 1, "Lookup IPv6 Country Name By IP Address"}, + {"enable_teredo", GeoIP_enable_teredo_Py, 1, "Enable / disable teredo"}, + {"teredo", GeoIP_teredo_Py, 1, "Returns true if teredo is enabled"}, + {"id_by_addr", GeoIP_id_by_addr_Py, 1, "Lookup Netspeed By IP Address"}, + {"id_by_name", GeoIP_id_by_name_Py, 1, "Lookup Netspeed By Name"}, {NULL, NULL, 0, NULL} }; @@ -386,6 +533,8 @@ static PyTypeObject GeoIP_GeoIPType = { static PyMethodDef GeoIP_Class_methods[] = { {"new", GeoIP_new_Py, 1, "GeoIP Constructor"}, {"open", GeoIP_open_Py, 1, "GeoIP Constructor with database filename argument"}, + {"lib_version", GeoIP_lib_version_Py, 1, "Returns the CAPI version"}, + {"time_zone_by_country_and_region", GeoIP_time_zone_by_country_and_region_Py, 1, "Returns time_zone for country, region"}, {NULL, NULL, 0, NULL} }; @@ -453,4 +602,19 @@ initGeoIP(void) PyDict_SetItemString(d, "GEOIP_CHARSET_UTF8", tmp); Py_DECREF(tmp); + tmp = PyInt_FromLong(GEOIP_UNKNOWN_SPEED); + PyDict_SetItemString(d, "GEOIP_UNKNOWN_SPEED", tmp); + Py_DECREF(tmp); + + tmp = PyInt_FromLong(GEOIP_DIALUP_SPEED); + PyDict_SetItemString(d, "GEOIP_DIALUP_SPEED", tmp); + Py_DECREF(tmp); + + tmp = PyInt_FromLong(GEOIP_CABLEDSL_SPEED); + PyDict_SetItemString(d, "GEOIP_CABLEDSL_SPEED", tmp); + Py_DECREF(tmp); + + tmp = PyInt_FromLong(GEOIP_CORPORATE_SPEED); + PyDict_SetItemString(d, "GEOIP_CORPORATE_SPEED", tmp); + Py_DECREF(tmp); } diff --git a/setup.py b/setup.py index beaa7de..4a8c9a3 100644 --- a/setup.py +++ b/setup.py @@ -7,6 +7,6 @@ include_dirs = ['/usr/local/include']) setup (name = 'GeoIP-Python', - version = '1.2.4', + version = '1.2.5', description = 'This is a python wrapper to GeoIP', ext_modules = [module1]) diff --git a/test.py b/test.py index 5247b1b..774e97a 100755 --- a/test.py +++ b/test.py @@ -4,7 +4,7 @@ #gi = GeoIP.new(GeoIP.GEOIP_STANDARD) gi = GeoIP.new(GeoIP.GEOIP_MEMORY_CACHE) -#gi = GeoIP.open("/usr/local/share/GeoIP/GeoIP.data",GeoIP.GEOIP_STANDARD) +#gi = GeoIP.open("/usr/local/share/GeoIP/GeoIP.dat",GeoIP.GEOIP_STANDARD) print gi.country_code_by_name("yahoo.com") print gi.last_netmask() diff --git a/test_city.py b/test_city.py index ef86b3d..b0b3f76 100755 --- a/test_city.py +++ b/test_city.py @@ -20,3 +20,4 @@ print gir['area_code'] print gir['time_zone'] print gir['metro_code'] + print str(gir) diff --git a/test_netspeed.py b/test_netspeed.py new file mode 100755 index 0000000..3665523 --- /dev/null +++ b/test_netspeed.py @@ -0,0 +1,12 @@ +#!/usr/bin/python + +import GeoIP + +gi = GeoIP.open("/usr/local/share/GeoIP/GeoIPNetspeed.dat",GeoIP.GEOIP_STANDARD) + +# GEOIP_UNKNOWN_SPEED, GEOIP_DIALUP_SPEED, GEOIP_CABLEDSL_SPEED or +# GEOIP_CORPORATE_SPEED + +print gi.id_by_name("yahoo.com") +print gi.id_by_name("www.maxmind.com") == GeoIP.GEOIP_UNKNOWN_SPEED +print gi.id_by_addr("203.195.93.0") diff --git a/test_org.py b/test_org.py index a646172..99b40ab 100755 --- a/test_org.py +++ b/test_org.py @@ -5,5 +5,5 @@ gi = GeoIP.open("/usr/local/share/GeoIP/GeoIPOrg.dat",GeoIP.GEOIP_STANDARD) print gi.org_by_name("yahoo.com") -print gi.org_by_name("www.government.de") -print gi.org_by_addr("203.195.93.0") +print gi.org_by_name("www.google.com") +print gi.org_by_addr("24.24.24.24") diff --git a/test_v6.py b/test_v6.py new file mode 100755 index 0000000..be2d484 --- /dev/null +++ b/test_v6.py @@ -0,0 +1,13 @@ +#!/usr/bin/python + +import GeoIP + +#gi = GeoIP.new(GeoIP.GEOIP_STANDARD) +#gi = GeoIP.new(GeoIP.GEOIP_MEMORY_CACHE) +gi = GeoIP.open("/usr/local/share/GeoIP/GeoIPv6.dat",GeoIP.GEOIP_STANDARD) + +print gi.country_code_by_name_v6("ipv6.google.com") +print gi.country_name_by_name_v6("ipv6.google.com") +print gi.country_code_by_addr_v6("2001:4860:0:1001::68") +print gi.country_name_by_addr_v6("2001:4860:0:1001::68") +