NATION

PASSWORD

NationStates API (nationdata/regiondata)

Bug reports, general help, ideas for improvements, and questions about how things are meant to work.

Advertisement

Remove ads

User avatar
Scoochi2
Envoy
 
Posts: 207
Founded: Antiquity
Ex-Nation

Postby Scoochi2 » Wed Apr 20, 2016 8:48 am

The Blaatschapen wrote:
[violet] wrote:Blaat, could you perhaps edit this post, since it's linked from the API Examples doc.


I will TG scoochi2 about this, rather than editing their post.

Fixed: script now sets a user agent and as Trotterdam suggested will terminate if you leave it at the default. :clap:
Last edited by Scoochi2 on Wed Apr 20, 2016 10:10 am, edited 2 times in total.
The problem with a UDP joke is that you have no idea if people got it.

User avatar
[violet]
Executive Director
 
Posts: 16205
Founded: Antiquity

Postby [violet] » Wed Apr 20, 2016 7:02 pm

The Blaatschapen wrote:Not likely to work (code does not say anything about a User Agent) :
Perl: Script by Unibot II

That works, because it has a default UserAgent, but is non-compliant for the same reason.

User avatar
Sandaoguo
Diplomat
 
Posts: 541
Founded: Apr 07, 2013
Left-Leaning College State

Postby Sandaoguo » Wed Apr 20, 2016 7:49 pm

Code: Select all
<?php
   // Set up UserAgent
   $options = array('http' => array('user_agent' => 'Your NS Username/Site URL/Email'));
   $context = stream_context_create($options);

   // Grab API response, kill if error
   $response = file_get_contents('http://www.nationstates.net/cgi-bin/api.cgi?nation=testlandia', false, $context) or exit('Could not grab API response');

   // Convert to SimpleXML object
   $xml = simplexml_load_string($response);

   // Grab wanted variables

   $population = $xml->POPULATION;
   $civil_rights = $xml->FREEDOMSCORES->CIVILRIGHTS;

   echo 'Population: ' . $population . '<br>Civil Rights: ' . $civil_rights;
?>


Note: This method of changing the stream context doesn't work for all PHP functions. If in doubt, just use this, which should work every time:

Code: Select all
<?php

ini_set('user_agent', 'Your NS Username/Site URL/Email')

code.......
?>


Also, it's possible to set an automatic user-agent with your server URL:
Code: Select all
<?php
   // Set up UserAgent
   $options = array('http' => array('user_agent' => $_SERVER['SERVER_NAME']));
   $context = stream_context_create($options);


Code: Select all
<?php
ini_set('user_agent', $_SERVER['SERVER_NAME']);
Last edited by Sandaoguo on Thu Apr 21, 2016 9:32 am, edited 3 times in total.

User avatar
[violet]
Executive Director
 
Posts: 16205
Founded: Antiquity

Postby [violet] » Sun May 01, 2016 4:57 pm

Added tgcanrecruit and tgcancampaign to the Nation API. These return 0 if the nation is currently blocking all Recruitment / Campaign telegrams, otherwise 1.

This allows API and manual recruiters to better target their messages.

User avatar
Leppikania
Minister
 
Posts: 2332
Founded: Apr 13, 2015
Left-wing Utopia

Postby Leppikania » Sun May 01, 2016 5:15 pm

[violet] wrote:Added tgcanrecruit and tgcancampaign to the Nation API. These return 0 if the nation is currently blocking all Recruitment / Campaign telegrams, otherwise 1.

This allows API and manual recruiters to better target their messages.

Sounds good, although can there be a shard for blocking external telegrams?
INTP, -4.25 Economic Left/Right, -4.1 Social Libertarian/Authoritarian, tastes like chicken.
I do use NS stats, thank you very much.
Funny Quotes
Pie charts for industries
Request an Embassy

User avatar
The Kaboomlandian Foreign Territory
Lobbyist
 
Posts: 15
Founded: Sep 09, 2015
Ex-Nation

Postby The Kaboomlandian Foreign Territory » Mon May 02, 2016 7:52 pm

I requested an API key 179 days ago, intending to use it for recruitment purposes, but never got around to using it. Is it possible to check if it still exists?

EDIT: Actually, never mind. I've instead filed a GHR for a small, passworded region I own, and will use that client key instead if my request is approved.
Last edited by The Kaboomlandian Foreign Territory on Mon May 02, 2016 7:58 pm, edited 2 times in total.
Supreme Chancellor of Pharos

I keep this nation solely for administrative purposes of Pharos. All TGs should go to Kaboomlandia.

User avatar
Greater Tern
Envoy
 
Posts: 331
Founded: Mar 04, 2010
Civil Rights Lovefest

Postby Greater Tern » Tue May 03, 2016 9:48 pm

Hey [violet], are there any plans to make the 'censusranks' shard (for region/world) official? I do agree with Trotterdam that the TITLE, DESC, REGIONRANK and RANK field could be removed. The score for each nation might be useful, though.
Last edited by Greater Tern on Tue May 03, 2016 10:02 pm, edited 1 time in total.

User avatar
Flanderlion
Minister
 
Posts: 2226
Founded: Nov 25, 2013
Psychotic Dictatorship

Postby Flanderlion » Wed May 04, 2016 1:52 am

Greater Tern wrote:Hey [violet], are there any plans to make the 'censusranks' shard (for region/world) official? I do agree with Trotterdam that the TITLE, DESC, REGIONRANK and RANK field could be removed. The score for each nation might be useful, though.

I was writing something the other day and got stuck on how to see the census ranks of each nation without having to check each nation individually. I tried a few combinations with the API and gave up, thinking that it was already in the API and I just hadn't figured out the combination. If it isn't already there, as I see above, would there be an option for this part: https://www.nationstates.net/cgi-bin/ap ... nks&scale= to have the actual score as well as rank of each nation? Thanks.
As always, I'm representing myself.
Information
Wishlist

User avatar
Greater Tern
Envoy
 
Posts: 331
Founded: Mar 04, 2010
Civil Rights Lovefest

Postby Greater Tern » Wed May 04, 2016 9:14 am

Flanderlion wrote:I was writing something the other day and got stuck on how to see the census ranks of each nation without having to check each nation individually. I tried a few combinations with the API and gave up, thinking that it was already in the API and I just hadn't figured out the combination. If it isn't already there, as I see above, would there be an option for this part: https://www.nationstates.net/cgi-bin/ap ... nks&scale= to have the actual score as well as rank of each nation? Thanks.

The way it's implemented right now, you can assume that the rank starts at 1 from the top and you can count the number of items on the list afterwards. You can also specify a starting point with the start parameter, like so: https://www.nationstates.net/cgi-bin/ap ... s&start=99

Actually [violet], for the current implementation, if you tell the query to start at x, the first nation it returns starts at x+1 instead, which could be confusing. Would it be possible to get a list starting at x as well? Thanks.

User avatar
Trotterdam
Postmaster-General
 
Posts: 10541
Founded: Jan 12, 2012
Left-Leaning College State

Postby Trotterdam » Wed May 04, 2016 9:47 am

Greater Tern wrote:Would it be possible to get a list starting at x as well?
Well, you do that by subtracting one before making the request.

It might be more intuitive if the field were named something like "after" rather than "start", though.

User avatar
Greater Tern
Envoy
 
Posts: 331
Founded: Mar 04, 2010
Civil Rights Lovefest

Postby Greater Tern » Wed May 04, 2016 9:53 am

Trotterdam wrote:Well, you do that by subtracting one before making the request.

It might be more intuitive if the field were named something like "after" rather than "start", though.

That's what I meant, sorry. It would be more intuitive if the list returned started at x, or if the parameter was named 'after' instead.

User avatar
[violet]
Executive Director
 
Posts: 16205
Founded: Antiquity

Postby [violet] » Thu May 05, 2016 10:40 pm

As suggested by posts above:
  • World/Region API: censusranks "start" parameter now acts like you'd expect, i.e. "start=1" starts at rank #1.
  • World/Region API: Removed "title"/"desc" data from censusranks output
  • Region API: Removed "regionrank" data from censusranks output
  • World API: Added "censustitle" and "censusdesc"
This is now deemed official and added to the API documentation.

Flanderlion wrote:score as well as rank of each nation?

It's on the ToDo list!

User avatar
Flanderlion
Minister
 
Posts: 2226
Founded: Nov 25, 2013
Psychotic Dictatorship

Postby Flanderlion » Fri May 06, 2016 7:43 pm

[violet] wrote:As suggested by posts above:
  • World/Region API: censusranks "start" parameter now acts like you'd expect, i.e. "start=1" starts at rank #1.
  • World/Region API: Removed "title"/"desc" data from censusranks output
  • Region API: Removed "regionrank" data from censusranks output
  • World API: Added "censustitle" and "censusdesc"
This is now deemed official and added to the API documentation.

Flanderlion wrote:score as well as rank of each nation?

It's on the ToDo list!

Thank you, and looking forward to it.
As always, I'm representing myself.
Information
Wishlist

User avatar
Flanderlion
Minister
 
Posts: 2226
Founded: Nov 25, 2013
Psychotic Dictatorship

Postby Flanderlion » Mon May 09, 2016 7:15 pm

Sorry for the double post:

http://www.nationstates.net/cgi-bin/api ... s;scale=65
Is the influence there (as in the score bit) the average influence level? Also is it calculated as I request it, or once daily, or once when the census cycles through?
As always, I'm representing myself.
Information
Wishlist

User avatar
[violet]
Executive Director
 
Posts: 16205
Founded: Antiquity

Postby [violet] » Mon May 09, 2016 8:10 pm

Average (mean) influence, yes, calculated once per day based on the nations present in the region at the time.

User avatar
Glen-Rhodes
Powerbroker
 
Posts: 9027
Founded: Jun 25, 2008
Ex-Nation

Re: NationStates API (nationdata/regiondata)

Postby Glen-Rhodes » Wed May 11, 2016 1:34 pm

Would there be any way to add influence to the daily nation dumps? Currently, there are only two ways to get the influence stats of all nations in a region: page scrape the comparison page, or use thousands of API calls (in the case of TSP).


Sent from my iPhone using Tapatalk

User avatar
[violet]
Executive Director
 
Posts: 16205
Founded: Antiquity

Postby [violet] » Wed May 11, 2016 5:32 pm

[violet] wrote:
Flanderlion wrote:score as well as rank of each nation?

It's on the ToDo list!

Now implemented: the 'censusranks' shard returns score data as well as rank.

User avatar
[violet]
Executive Director
 
Posts: 16205
Founded: Antiquity

Postby [violet] » Wed May 11, 2016 5:36 pm

Glen-Rhodes wrote:Would there be any way to add influence to the daily nation dumps? Currently, there are only two ways to get the influence stats of all nations in a region: page scrape the comparison page, or use thousands of API calls (in the case of TSP).

It's a little easier now: https://www.nationstates.net/cgi-bin/ap ... 65;start=0

User avatar
Trotterdam
Postmaster-General
 
Posts: 10541
Founded: Jan 12, 2012
Left-Leaning College State

Postby Trotterdam » Wed May 11, 2016 8:08 pm

[violet] wrote:
[violet] wrote:It's on the ToDo list!
Now implemented: the 'censusranks' shard returns score data as well as rank.
Could you make it accept a &mode=score+rank+prank (or whatever subset I actually care about) parameter like the normal census shard does?

User avatar
Flanderlion
Minister
 
Posts: 2226
Founded: Nov 25, 2013
Psychotic Dictatorship

Postby Flanderlion » Wed May 11, 2016 9:06 pm

[violet] wrote:
[violet] wrote:It's on the ToDo list!

Now implemented: the 'censusranks' shard returns score data as well as rank.

Thanks a metric ton. If I have a spare few days I'll try and update Unibot's Perl script (if he doesn't want to pop in and fix it himself, he's still active).
As always, I'm representing myself.
Information
Wishlist

User avatar
King Nephmir II
Chargé d'Affaires
 
Posts: 400
Founded: Jun 04, 2015
Benevolent Dictatorship

Postby King Nephmir II » Sun May 15, 2016 2:26 pm

After many days of research and learning VBA (okay, maybe not days, but still a lot of hours :P), I finally figured out how to send API requests with a User Agent header attached in Excel.

It works for every shard that I need (well, so far, at least, as I haven't touched the newer shards yet), except I seem to be having trouble with one API call in particular, that being the region data.

The following is copied directly from my tool:

=IFERROR(IF(OR(Calculator!J8="[Nation]", Calculator!J8=""), "", WEBSERVICE("http://www.nationstates.net/cgi-bin/api.cgi?nation=" & Calculator!J8 & "&q=region+wa+influence+censusscore-65+censusscore-66+endorsements")), "")


The above cell works, in that it generates data for whatever nation is inputted on cell J8 on the "Calculator" page. Gathering the world and WA data needed also works just fine. The one below, however, seems to be problematic, despite being the exact same formula as the nation call above:

=IFERROR(IF(OR(Calculator!D8="[Region]", Calculator!D8=""), "", WEBSERVICE("http://www.nationstates.net/cgi-bin/api.cgi?region=" & Calculator!D8 & "&q=numnations+nations+power")), "")


This does work when the region is entered into the URL directly, however this is not feasible due to the nature of the workbook.

=WEBSERVICE("http://www.nationstates.net/cgi-bin/api.cgi?region=auralia&q=numnations+nations+power")


All formulas worked correctly before the change to the API took place (the one where sending a user agent is technically required in order to obtain data), so I know the sheet is supposed to work in its current format.

The VBA module I'm using is this (with my own user agent in place of the example one), just two simple lines:
Code: Select all
Sub Agent ()
XMLHTTP.setRequestHeader "Content-Type", "text/xml"
XMLHTTP.setRequestHeader "User-Agent", "Mozilla/5.0 (Windows NT 6.1; rv:25.0) Gecko/20100101 Firefox/25.0"
End Sub


This problem is either with NationStates or Excel, or I am simply missing something blatantly obvious. Though since it worked before the change, and it's working for the nation, world, and WA calls, it's likely that it could be NS side, hence why I'm posting it here. If it turns out to not be NS-related, to anyone else with Excel knowledge, some help would be appreciated.

Thanks!
Last edited by King Nephmir II on Sun May 15, 2016 2:26 pm, edited 1 time in total.

User avatar
[violet]
Executive Director
 
Posts: 16205
Founded: Antiquity

Postby [violet] » Sun May 15, 2016 5:31 pm

I would guess it's an error in the spreadsheet, since the URL works when you try it manually. There are some lines in our log fetching http://www.nationstates.net/cgi-bin/api ... ions+power with the UserAgent "Excel/16.0", which I'm guessing is you, all of which worked with no errors.

I think though that WEBSERVICE() is entirely separate to whatever VBA code you have, so if you set a UserAgent in VBA but then make calls from WEBSERVICE(), it will ignore that. Which is probably why I'm seeing "Excel/16.0".

User avatar
Unibot III
Negotiator
 
Posts: 7113
Founded: Mar 11, 2011
Democratic Socialists

Postby Unibot III » Sun May 15, 2016 6:28 pm

Flanderlion, I've deleted literally everything I knew about Perl from my head to make some space for some more constitutional law nonsense, so feel free to take the reins on updating the script. Cheers.

*waves to [violet] cheerfully too*
[violet] wrote:I mean this in the best possible way,
but Unibot is not a typical NS player.
Milograd wrote:You're a caring, resolute lunatic
with the best of intentions.
Org. Join Date: 25-05-2008 | Former Delegate of TRR

Factbook // Collected works // Gameplay Alignment Test //
9 GA Res., 14 SC Res. // Headlines from Unibot // WASC HQ: A Guide

▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬
✯ Duty is Eternal, Justice is Imminent: UDL

User avatar
King Nephmir II
Chargé d'Affaires
 
Posts: 400
Founded: Jun 04, 2015
Benevolent Dictatorship

Postby King Nephmir II » Sun May 15, 2016 7:43 pm

[violet] wrote:I would guess it's an error in the spreadsheet, since the URL works when you try it manually. There are some lines in our log fetching http://www.nationstates.net/cgi-bin/api ... ions+power with the UserAgent "Excel/16.0", which I'm guessing is you, all of which worked with no errors.

I think though that WEBSERVICE() is entirely separate to whatever VBA code you have, so if you set a UserAgent in VBA but then make calls from WEBSERVICE(), it will ignore that. Which is probably why I'm seeing "Excel/16.0".

Hmm... I've tried everything I can think of, but it just doesn't work...

Without those two lines in the VBA, I am unable to make any API calls using webservice, but that is interesting how it ignored it. Is "Excel/16.0" a specific enough user agent? There may not be a way to change it.

User avatar
Glen-Rhodes
Powerbroker
 
Posts: 9027
Founded: Jun 25, 2008
Ex-Nation

Postby Glen-Rhodes » Mon May 16, 2016 5:51 pm

Got rate-blocked by the API (which was expected). The user-facing page for that has a small error:

Your IP address xxxx has sent more than 50 requests in 30 seconds. Access to the API has been blocked for the next 2343321006009 minutes. Please do not send bursts of traffic!


That would be about 4,458,373 years. :p

PreviousNext

Advertisement

Remove ads

Return to Technical

Who is online

Users browsing this forum: Bisofeyr, Draavida, Dunkirlothesia, East Chimore, Gesaria, Gloon Ald, Madigon, North American Imperial State, Republic of Libriano, Romanum et Britannia Minor, Russo-Austria, Sniper Slayers, Team Prattle, The Southern Dependencies, Three Galaxies, Umidus, United Calanworie, Uvolla, Yuspuize

Advertisement

Remove ads