[Include] GeoIP - lite version (without any databases)
#1

GeoIP - lite version

Yesterday i found an API witch provide you to get geoip stats for an IP.
The script is simple and small. Just an HTTP request to https://freegeoip.net.

Here are the available functions:

Код:
- Geo_GetIP(playerid);	        	        // Get the IP for a player (same as GetPlayerIP)				
- Geo_GetAllData(playerid); 	        	// Get a string of all player data.		
- Geo_GetCountry(playerid);	                // Get the player country as a string		
- Geo_GetCountryCode(playerid);	                // Get the player country Code as a string (Ex: RO, US, PL, NL)
- Geo_GetCity(playerid);		        // Get the player city as a string
- Geo_GetRegionCode(playerid) ;			// Get the player region code as a string (Ex: CA, LA, LS)	
- Geo_GetRegion(playerid);		        // Get the player region as a string
- Geo_GetZipCode(playerid); 			// Get the player Zip code as a integer		
- Geo_GetTimeZone(playerid); 			// Get the player time zone as a string (Ex: Europe/Bucharest)	
- Geo_GetLatitude(playerid); 			// Get the player latitude as a float 
- Geo_GetLongitude(playerid);		        // Get the player longitude as a float	
- Geo_GetMetroCode(playerid);			// Get the player metro code as a integer
I recommend to use this include for better time execution and stability: https://sampforum.blast.hk/showthread.php?tid=296171.
This is a "lite" version and it can have problems with the API or others factors.

Here are the download links:
https://github.com/Catalin-Mihai/Geo...leases/tag/1.0 // Github
http://pastebin.com/uQ7gZgpT // Pastebin

Enjoy! Please post your feedback below.
P.S: This is my first include.

Credits:
- https://freegeoip.net for the API
- ****** for Encoding function.
Reply
#2

Is this working?
Reply
#3

Quote:
Originally Posted by SecretBoss
Посмотреть сообщение
Is this working?
At least, for me is working well.
Reply
#4

Very nice work, +rep.
Reply
#5

It's a good idea however if that site is down then this isn't going to work which would likely be an exceptional circumstance. So yeah I would say it's pretty cool with only limited issues.
Reply
#6

Quote:
Originally Posted by Pottus
Посмотреть сообщение
It's a good idea however if that site is down then this isn't going to work which would likely be an exceptional circumstance. So yeah I would say it's pretty cool with only limited issues.
One cool thing with fregeoip is you can also download the server/script for it and run it by yourself. I ended up doing that for a few of my own projects to help with latency.
Reply
#7

What I do for geoip is run a python script that downloads geoip data then processes it into a sqlite database so no worries for latency
Reply
#8

That website is down...

Quote:
Originally Posted by Pottus
Посмотреть сообщение
What I do for geoip is run a python script that downloads geoip data then processes it into a sqlite database so no worries for latency
You could share with us
Reply
#9

Quote:
Originally Posted by PT
Посмотреть сообщение
That website is down...
It is up for me.
Reply
#10

Quote:
Originally Posted by PT
Посмотреть сообщение
You could share with us
Sure here is the python script simply run it on your servers shell I didn't write this it was downloaded but modified slightly for SA-MP.

http://pastebin.com/nYtU0Zs7

Here is a function for making a geoip query, it is done using sqlitei but can easily be re-written without it.

Код:
// Stock function for retrieving GeoIP data
stock GetGeoIP(ip, country_code[3], country[32], region_code[32], region[64], city[64], zipcode[16], lat[16], lng[16], metro[16], area[16])
{
	if (!query[0])
	{
 		strimplode(" ",
   				query,
	            sizeof(query),
	            "SELECT",
             	"`city_location`.`country_code`,",
              	"`country_blocks`.`country_name`,",
               	"`city_location`.`region_code`,",
                "`region_names`.`region_name`,",
                "`city_location`.`city_name`,",
                "`city_location`.`postal_code`,",
                "`city_location`.`latitude`,",
                "`city_location`.`longitude`,",
                "`city_location`.`metro_code`,",
                "`city_location`.`area_code`",
                "FROM `city_blocks`",
                "NATURAL JOIN `city_location`",
                "INNER JOIN  `country_blocks` ON `city_location`.`country_code` = `country_blocks`.`country_code`",
                "LEFT OUTER JOIN",
                "`region_names` ON `city_location`.`country_code` = `region_names`.`country_code`",
                "AND `city_location`.`region_code` = `region_names`.`region_code`",
                "WHERE `city_blocks`.`ip_start` <= ?",
                "ORDER BY `city_blocks`.`ip_start`",
                "DESC ",
                "LIMIT 1");
		stmt = db_prepare(GeoIP, query);
	}
	
	// Bind DB Results to DB Variables
	stmt_bind_result_field(stmt, 0, DB::TYPE_STRING, country_code, sizeof(country_code));
	stmt_bind_result_field(stmt, 1, DB::TYPE_STRING, country, sizeof(country));
	stmt_bind_result_field(stmt, 2, DB::TYPE_STRING, region_code, sizeof(region_code));
	stmt_bind_result_field(stmt, 3, DB::TYPE_STRING, region, sizeof(region));
	stmt_bind_result_field(stmt, 4, DB::TYPE_STRING, city, sizeof(city));
	stmt_bind_result_field(stmt, 5, DB::TYPE_STRING, zipcode, sizeof(zipcode));
	stmt_bind_result_field(stmt, 6, DB::TYPE_STRING, lat, sizeof(lat));
	stmt_bind_result_field(stmt, 7, DB::TYPE_STRING, lng, sizeof(lng));
	stmt_bind_result_field(stmt, 8, DB::TYPE_STRING, metro, sizeof(metro));
	stmt_bind_result_field(stmt, 9, DB::TYPE_STRING, area, sizeof(area));

	// Set the IP for the query
	stmt_bind_value(stmt, 0, DB::TYPE_UINT, ip);

	// Execute DB Query
	if (stmt_execute(stmt))
	{
		// Found a result
	    if (stmt_fetch_row(stmt)) return 1;
	}
	return 0;
}
The database uses longips so you need this function.

Код:
// Get InterIP - Slice
stock GetIntegerIP(const input[])
{
    new ip = 0, pos = -1;

    for (new i = 24; i >= 0; i -= 8) {
        ip |= strval(input[++pos]) << i;

        if (i) {
            pos = strfind(input, ".", _, pos);
        }
    }
    return ip;
}
Reply
#11

Thanks for share that ^ Jeff
Reply
#12

Good job, i love it!
Reply
#13

Forgot to change MAX_PLAYERS
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)