Skip to content

Instantly share code, notes, and snippets.

@staeff
Last active February 6, 2017 21:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save staeff/7c2df514fbd8bdff5bec to your computer and use it in GitHub Desktop.
Save staeff/7c2df514fbd8bdff5bec to your computer and use it in GitHub Desktop.
Get and process (oembed) data from flickr and wikimedia
  • Flickr
  • Wikimedia
// Simple script to get data from an API with jQuery
$.ajax({url: 'https://publish.twitter.com/oembed',
data: {url: 'https://twitter.com/derfreitag/status/828355522424365060'},
type: "GET",
dataType: "jsonp",
}).done(function( data ) {
console.log(data);
});
// Need jQuery as external library to run
// Call to flickr endpoint
function getFlickrData(url) {
return $.ajax({
url: url,
type: 'GET',
dataType: 'jsonp'
});
}
var allowed_licenses = ["4", "5", "6", "7", "8", "9", "10"],
flickr = 'https://www.flickr.com/services/oembed.json',
photo_url = "https://www.flickr.com/photos/salvatore_gerace/17229803302";
url = flickr + "?url=" + photo_url + '&jsoncallback=?';
getFlickrData(url).done(function(result) {
console.log(result);
console.log(result.license_id + ": " + result.license);
console.log($.inArray(result.license_id, allowed_licenses));
}).fail(function(err) {
alert( "error: " + err);
});
var result = {"continue":{"iistart":"2015-02-19T08:55:25Z","continue":"||"},"query":{"normalized":[{"from":"File:L\u00fcdinghausen,_Flugplatz_Borkenberge,_PKW_auf_dem_Parkplatz_--_2014_--_0079.jpg","to":"File:L\u00fcdinghausen, Flugplatz Borkenberge, PKW auf dem Parkplatz -- 2014 -- 0079.jpg"}],"pages":{"38473256":{"pageid":38473256,"ns":6,"title":"File:L\u00fcdinghausen, Flugplatz Borkenberge, PKW auf dem Parkplatz -- 2014 -- 0079.jpg","imagerepository":"local","imageinfo":[{"thumburl":"https://upload.wikimedia.org/wikipedia/commons/thumb/e/e1/L%C3%BCdinghausen%2C_Flugplatz_Borkenberge%2C_PKW_auf_dem_Parkplatz_--_2014_--_0079.jpg/450px-L%C3%BCdinghausen%2C_Flugplatz_Borkenberge%2C_PKW_auf_dem_Parkplatz_--_2014_--_0079.jpg","thumbwidth":450,"thumbheight":391,"url":"https://upload.wikimedia.org/wikipedia/commons/e/e1/L%C3%BCdinghausen%2C_Flugplatz_Borkenberge%2C_PKW_auf_dem_Parkplatz_--_2014_--_0079.jpg","descriptionurl":"https://commons.wikimedia.org/wiki/File:L%C3%BCdinghausen,_Flugplatz_Borkenberge,_PKW_auf_dem_Parkplatz_--_2014_--_0079.jpg","descriptionshorturl":"https://commons.wikimedia.org/w/index.php?curid=38473256","extmetadata":{"DateTime":{"value":"2016-02-04 17:00:33","source":"mediawiki-metadata","hidden":""},"ObjectName":{"value":"L\u00fcdinghausen, Flugplatz Borkenberge, PKW auf dem Parkplatz -- 2014 -- 0079","source":"mediawiki-metadata","hidden":""},"CommonsMetadataExtension":{"value":1.2,"source":"extension","hidden":""},"Categories":{"value":"2014 in L\u00fcdinghausen|8 (number)|BMW 501 / 502|BMW roundel logos|Black automobiles|Changes of images by XRay of the recent weeks|Exposure time 1/400 sec|F-number f/9|Featured pictures of L\u00fcdinghausen|Featured pictures of automobiles|Flugplatz Borkenberge|ISO speed rating 100|Images by XRay (flat list)|July 2014 in Germany|Lens focal length 61 mm|Media with locations|Objects with inscriptions in German needing translation|Photographs by Dietmar Rabich/Featured pictures|Photographs by Dietmar Rabich/L\u00fcdinghausen|Photographs by Dietmar Rabich/Quality images|Photographs by Dietmar Rabich/Quality images of Germany|Photographs taken on 2014-07-20|Recent changes of images by XRay by third parties|Reflections on structures in Germany|Self-published work|Sunlight reflections|Taken with Canon EF-S 15-85mm F3.5-5.6 IS USM|Taken with Canon EOS 600D|Uploaded with Vicu\u00f1aUploader|Vehicles in L\u00fcdinghausen|With trademark","source":"commons-categories","hidden":""},"Assessments":{"value":"featured|potd|quality","source":"commons-categories","hidden":""},"GPSLatitude":{"value":"51.779888","source":"commons-desc-page","hidden":""},"GPSLongitude":{"value":"7.280600","source":"commons-desc-page","hidden":""},"GPSMapDatum":{"value":"WGS-84","source":"commons-desc-page","hidden":""},"ImageDescription":{"value":"BMW 502 V8 in the parking lot of the Borkenberge Airfield, <a href=\"//en.wikipedia.org/wiki/L%C3%BCdinghausen\" class=\"extiw\" title=\"en:L\u00fcdinghausen\">L\u00fcdinghausen</a>, <a href=\"//en.wikipedia.org/wiki/North_Rhine-Westphalia\" class=\"extiw\" title=\"en:North Rhine-Westphalia\">North Rhine-Westphalia</a>, Germany","source":"commons-desc-page"},"DateTimeOriginal":{"value":"2014-07-20 14:07","source":"commons-desc-page"},"Credit":{"value":"<span class=\"int-own-work\" lang=\"en\">Own work</span>","source":"commons-desc-page","hidden":""},"Artist":{"value":"<b>\u00a9 <a href=\"//commons.wikimedia.org/wiki/User:XRay\" title=\"User:XRay\">Dietmar Rabich</a></b>, <small><a rel=\"nofollow\" class=\"external text\" href=\"http://www.rabich.de/\">rabich.de</a></small>","source":"commons-desc-page"},"Permission":{"value":"<div style=\"clear:both; border:1px solid #999999; margin:1em 0.8em; padding:0.5em 0.8em; background-color: #ffffff; font-size: 90%;\">\n<div class=\"floatright\"><a href=\"//commons.wikimedia.org/wiki/File:HSSection_sign.svg\" class=\"image\"><img alt=\"HSSection sign.svg\" src=\"https://upload.wikimedia.org/wikipedia/commons/thumb/1/18/HSSection_sign.svg/100px-HSSection_sign.svg.png\" width=\"100\" height=\"100\" srcset=\"https://upload.wikimedia.org/wikipedia/commons/thumb/1/18/HSSection_sign.svg/150px-HSSection_sign.svg.png 1.5x, https://upload.wikimedia.org/wikipedia/commons/thumb/1/18/HSSection_sign.svg/200px-HSSection_sign.svg.png 2x\" data-file-width=\"156\" data-file-height=\"156\"></a></div>\n<div><b>I. Licence</b></div>\n<div style=\"margin-top:0.5em;\">The author of this picture, Dietmar Rabich, published this file under the Creative Commons Licence <i>Attribution-ShareAlike 4.0</i> (see also <a rel=\"nofollow\" class=\"external text\" href=\"https://creativecommons.org/licenses/by-sa/4.0/deed.en\">human-readable summary</a>; other licences and more on this see section <a href=\"//commons.wikimedia.org/wiki/File:L%C3%BCdinghausen,_Flugplatz_Borkenberge,_PKW_auf_dem_Parkplatz_--_2014_--_0079.jpg#ImgLic\" title=\"File:L\u00fcdinghausen, Flugplatz Borkenberge, PKW auf dem Parkplatz -- 2014 -- 0079.jpg\">Licensing</a>). The image can be used freely under the license, as long as the author is mentioned for example in the following form in the caption or for printed products in the image credits: <b>\u00a9 Dietmar Rabich, <a rel=\"nofollow\" class=\"external text\" href=\"http://www.rabich.de/\">rabich.de</a>, CC BY-SA 4.0, Wikimedia Commons</b>. Please read the full license requirements carefully before using this image.</div>\n<div style=\"margin-top:0.5em;\">It is also necessary to provide a copy of the license or at least its full Internet address (<a rel=\"nofollow\" class=\"external free\" href=\"http://creativecommons.org/licenses/by-sa/4.0/legalcode\">http://creativecommons.org/licenses/by-sa/4.0/legalcode</a>). When used in online media in addition to the aforementioned copyright and license specifying a reference (link) to the original image - so this page with descriptions and license conditions - would be nice. For more information or examples of use can be found at <a href=\"//commons.wikimedia.org/wiki/Commons:Reusing_content_outside_Wikimedia\" title=\"Commons:Reusing content outside Wikimedia\">re-use (with example file)</a>. </div>\n<div style=\"margin-top:0.5em;\">If you need another license to use, so please contact the author <b>before</b> using.</div>\n<div style=\"margin-top:0.5em;\"><b>II. Fairness</b></div>\n<div style=\"margin-top:0.5em;\">From printed materials and other tangible products that use that work, the user is asked to send a copy to the author. The mailing address can be requested via the contact options.</div>\n<div style=\"margin-top:0.5em;\">Please note that the author had quite considerable effort to draw up this file. So it can only be fair to comply with the conditions of <a href=\"//en.wikipedia.org/wiki/Free_license\" class=\"extiw\" title=\"en:Free license\">free licenses</a> listed here. In the case of a breach of the license conditions, the rights granted hereunder will expire automatically.</div>\n<div style=\"margin-top:0.5em; clear: both;\"><b>III. Disclaimer</b></div>\n<div style=\"margin-top:0.5em;\">The author does not adopt third parties' rights to own. It is possible that third parties are to be considered for example in case of trademark, design or personality rights. You are responsible to check and respect these rights in the publication in the respective medium or country.</div>\n<div style=\"margin-top:0.5em;\">The author is not responsible for the accuracy of the information contained herein (description, title, etc.). Likewise, the author assumes no liability for further use.</div>\n<div style=\"margin-top:0.5em;\">Please note the <a href=\"//commons.wikimedia.org/wiki/Commons:General_disclaimer\" title=\"Commons:General disclaimer\">general disclaimer</a> and please read <a href=\"//commons.wikimedia.org/wiki/Commons:Reusing_content_outside_Wikimedia\" title=\"Commons:Reusing content outside Wikimedia\">reusing content outside Wikimedia</a> too.</div>\n<div style=\"margin-top:0.5em;\"><b>IV. Contact</b></div>\n<div style=\"margin-top:0.5em;\">\n<b><a href=\"//commons.wikimedia.org/wiki/File:Nuvola_apps_korn.png\" class=\"image\"><img alt=\"Nuvola apps korn.png\" src=\"https://upload.wikimedia.org/wikipedia/commons/thumb/c/c3/Nuvola_apps_korn.png/18px-Nuvola_apps_korn.png\" width=\"18\" height=\"18\" srcset=\"https://upload.wikimedia.org/wikipedia/commons/thumb/c/c3/Nuvola_apps_korn.png/27px-Nuvola_apps_korn.png 1.5x, https://upload.wikimedia.org/wikipedia/commons/thumb/c/c3/Nuvola_apps_korn.png/36px-Nuvola_apps_korn.png 2x\" data-file-width=\"128\" data-file-height=\"128\"></a> <a href=\"//de.wikipedia.org/wiki/Spezial:E-Mail/XRay\" class=\"extiw\" title=\"de:Spezial:E-Mail/XRay\">E-mail to the author</a></b> or the <a href=\"//commons.wikimedia.org/wiki/User_talk:XRay\" title=\"User talk:XRay\">author's talk page</a> <small>or the <a rel=\"nofollow\" class=\"external text\" href=\"http://www.rabich.de/\">author's website</a></small>\n</div>\n</div>","source":"commons-desc-page","hidden":""},"LicenseShortName":{"value":"CC BY-SA 4.0","source":"commons-desc-page","hidden":""},"UsageTerms":{"value":"Creative Commons Attribution-Share Alike 4.0","source":"commons-desc-page","hidden":""},"AttributionRequired":{"value":"true","source":"commons-desc-page","hidden":""},"Attribution":{"value":"","source":"commons-desc-page","hidden":""},"LicenseUrl":{"value":"http://creativecommons.org/licenses/by-sa/4.0","source":"commons-desc-page","hidden":""},"Copyrighted":{"value":"True","source":"commons-desc-page","hidden":""},"Restrictions":{"value":"trademarked","source":"commons-desc-page","hidden":""},"License":{"value":"cc-by-sa-4.0","source":"commons-templates","hidden":""}}}]}}}};
var pages = result.query.pages;
// need to access first and only key
for (var key in pages) {
pagekey = key;
}
var imageinfo = pages[pagekey].imageinfo[0];
var thumburl = imageinfo.thumburl;
var license = imageinfo.extmetadata.LicenseShortName.value;
function getData(url) {
return $.ajax({
url: url,
type: 'GET',
headers: { 'Api-User-Agent': 'Freitag website/1.0' },
dataType: 'jsonp'
});
}
// Diese URL bauen wir
// https://commons.wikimedia.org/w/api.php?action=query&format=json&prop=imageinfo&iilimit=1&iiprop=url|extmetadata&iiurlwidth=450&titles=File:L%C3%BCdinghausen,_Flugplatz_Borkenberge,_PKW_auf_dem_Parkplatz_--_2014_--_0079.jpg
var endpoint = 'https://commons.wikimedia.org/w/api.php';
var source = 'https://commons.wikimedia.org/wiki/Main_Page#/media/File:L%C3%BCdinghausen,_Flugplatz_Borkenberge,_PKW_auf_dem_Parkplatz_--_2014_--_0079.jpg';
var file = /File:.*?jpg/.exec(source);
var query = "action=query&";
query += "format=json&";
query += "prop=imageinfo&";
query += "iilimit=1&";
query += "iiprop=url|extmetadata&";
query += "iiurlwidth=450&";
query += "titles=" + file;
var url = endpoint + "?" + query;
console.log(url);
getData(url).done(function(data){
console.log(JSON.stringify(data));
}).fail(function(err){console.log(err);});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment