How to get description and image of first result from Wikipedia API - json

This is my first post on the community and im not a native english speaker so please pardon me for my bad english and for any mistakes i might do in posting this.
Im creating an application(NodeJS) that will search for a planet name on Wikipedia and return the first result description and image in JSON format.
My Requirements are:
Must be in JSON Format;
Must be done with only ONE Api Call;
Of course i searched google and Stackoverflow for a solution , before posting.
By following the Wikipedia API DOC (https://www.mediawiki.org/wiki/API:Opensearch) && (https://www.mediawiki.org/wiki/API:Main_page)
I tried this query:
https://en.wikipedia.org/w/api.php?action=opensearch&search=planet%20mars&limit=1&namespace=0&format=json
This only gives me The title and the link for the article
If i try the same query but in xml format:
https://en.wikipedia.org/w/api.php?action=opensearch&search=planet%20mars&limit=1&namespace=0&format=xml
As you can see by changing format to xml , it works , i can get the image tag ! But my application won't accept xml format(for security reason) !
How can i get the same result , but in JSON FORMAT ?
Is there any other way of successfully fetching the description and image of a search result from wikipedia ?

I found a solution , so i will answer my own question maybe it might help someone one day
the api call i must use is: https://en.wikipedia.org/w/api.php?format=json&action=query&prop=extracts|pageimages&exintro&explaintext&generator=search&gsrsearch=intitle:planet%20mars&gsrlimit=1&redirects=1

Related

Incomplete Json - Behance API

I'm working with the Behance API to build a plugin. I got the API key and built the URL to get my project list data in JSON format.
The weird thing is that, the JSON is not complete at all, there are many project missing, 14 missing to be specific.
Does someone have any idea?
Thanks in advance.
Found the solution here: https://help.behance.net/hc/en-us/community/posts/202357274-Number-of-Behance-API-request-results-limited-to-25-
It seems that by default the number of items is limited to 25. To get more you need to paginate adding a query get after the URL.
ex: http://www.behance.net/v2/users/gokhanaltinigne/projects?api_key=##&per_page=25&page=2

JSON API filter included resources

The question is about JSON API specification and how properly do a request
(I'm using ruby on rails and the json api resources gem but that's a general question anyway, I know how to implement it, I just want to follow the rules of JSON API at: http://jsonapi.org/format/)
Situation 1:
I want to get all shelves
I want to include all books that are on those shelves
The get I'm supposed to use in this case is:
www.library.com/shelves?include=books
Situation 2:
I want to get all books but only books that are marked as unread
The get I'm supposed to use is:
www.library.com/books?filter[unread]=true
What would be correct way of designing request for all shelves with included unread books?
Can't figure this one out
www.library.com/shelves?include=books&filter[books.unread]=true ?
www.library.com/shelves?include=unread_books ? <- would have to specify another resource, books that are unread
www.library.com/shelves?filter[books.unread]=true ?
What's the most correct way of doing this?
EDIT
After speaking with my tech lead and a few other programmers, the first options is favoured the most in such cases
I would bet on the first one:
www.library.com/shelves?include=books&filter[books.unread]=true
JSON API currently does not support filtering includes, but this doesn't mean you have to be strict on the definition (check https://github.com/cerebris/jsonapi-resources/issues/314)
I would go with the same approach as brian:
www.library.com/shelves?include=books&filter[books.unread]=true
I just wanted to give some more background to the answer.

Is it possible to get Reddit subcomment json?

I'm looking at the Reddit API and I'm not finding any information on how to pull subcomment JSON.
I want to get a comment and all of its children.
http://www.reddit.com/r/subreddit/comments/commentid/anytext.json will get any particular post posted to Reddit (if you put in the subreddit and the id of any particular article), however using this same method to try to get a comment won't work.
Is there anyway to get this data? I'm doing this in Javascript, but this is an endpoint issue above all else, so that doesn't really matter.
Add .json to the comment permalink, eg https://www.reddit.com/r/test/comments/3azr6z/doge/cshhtsi.json .

How do I get the right values out of this JSON?

I've got a really stupid question but I can't seem to find any answer whatsoever. I have this JSON from an online weather API, the JSON looks like this:
{"coord":{"lon":4.4,"lat":51.22},"sys":{"type":1,"id":4839,"message":0.1065,"country":"Belgium","sunrise":1414218231,"sunset":1414254541},"weather":[{"id":701,"main":"Mist","description":"mist","icon":"50d"},{"id":310,"main":"Drizzle","description":"light intensity drizzle rain","icon":"09d"}],"base":"cmc stations","main":{"temp":286.3,"pressure":1019,"humidity":87,"temp_min":285.15,"temp_max":287.15},"wind":{"speed":3.1,"deg":210},"clouds":{"all":75},"dt":1414232888,"id":2803138,"name":"Antwerp","cod":200}
I want to use this JSON data in a datawarehouse tool Pentaho. I'm stuck though on how to get the right values.
Say I want to retrieve "country" for example, which is located inside "sys". How would I go about retrieving it using the dollar sign notation? I tried $.sys.country but that didn't work. I'm not sure how to translate this JSON structure to dollar sign notation.
Really hope someone can help, thanks in advance!
After some more surfing and googling I found the solution. I wasn't aware, but the specific data warehousing tool I use (Pentaho) uses JSONPath (http://goessner.net/articles/JsonPath/ - it's the official JSONPath website but Chrome gives me a Malware warning for some reason...) for getting data out of JSON. The syntax I used in my original question $.sys.country was right al along, there was just a problem in my Pentaho workflow (the REST step for getting the JSON was run more than once due to a number I didn't change). So now everything's working and I can get the values out of the JSON!
Links I found that solved my problem:
http://woodlouse.blogspot.fr/2013/02/how-to-create-pentaho-report-using-rest.html
http://jsonpath.curiousconcept.com/ (very handy JSONPath Expression
Tester I found)
Hope this can help other people with the same problem too!

Is it possible to parse a Google+ (Google Plus) profile page?

If you view the source of a Google+ profile page, it appears rather complex. It seems most of the data is kept in a huge JSON-like objects. However, they don't seem to be really JSON, since they don't get recognized when I try to decode them. I am hoping the format is more clear to other people here. How would you go about parsing it? It seems it would fairly trivial, if you know where to start.
Here is a sample profile, for example: http://plus.google.com/104560124403688998123
Here's a PHP API I'm working on. It can download and parse the data for a profile page and people's public relationships.
https://github.com/jmstriegel/php.googleplusapi
The JSON piece is a bit mangled. To generate valid JSON, you basically have to remove the first 5 characters that prevent XSRF attacks and then add in all the nulls that have been removed. Here's the code specific to handling parsing the weird Google Plus JSON responses:
https://github.com/jmstriegel/php.googleplusapi/blob/master/lib/GooglePlus/GoogleUtil.php
Call GoogleUtil::FetchGoogleJSON( $url ) and you'll get back a giant array that you can then pull data from. Using this, it should be trivial to make a proxy service to translate stuff into valid json(p) for you to use in your own apps.
I don't have access to Google+ yet, so I'll just answer the general question - that is, how to parse JSON.
JSON is just JavaScript, so parsing it is as simple as evaluating the script. To do this, use the eval() JavaScript function.
var obj = eval('{"JSON":"goes here"}');
Another option is to leverage a console tool. Popular modern browsers pretty much all have them. I recommend Firebug for Firefox in particular.
Using Firefox, log into Google+, then open the Firebug console. You can use the console's dir() command to create a browseable representation of the data. Ex:
console.dir(eval('{"JSON":"goes here"}'));
Sorry I can't be more specific about how to get a handle on Google+'s JSON in particular; without access to the service, this is about the best I can do blind. Good luck!
Thanks to Jason for the excellent php class which reads a profile page into an array.
I've used this class as a base and then parsed it, based upon Russell Beattie's python code from the original appspot rss feed application.
Code here
A few notes:
I use this to merge G+ and WP feeds, hence writing posts into an intermediate array ($items).
I have a convention of creating a pseudo title in Google Plus posts, by emboldening a line and adding two newlines before writing the post. The function getTitle strips this out as a better formatted title in my website and getSummary produces the rest of the post with duplicating the title.
It's made up of a number of parts, an object describing your picasa images, one describing the fields on your profile, one describing your friends.
Most of the long numbers are the internal IDs of people, posts and photos. For instance, my ID is 105249724614922381234. Other than that, it could be parsed if you needed to.