Last active
November 2, 2017 15:29
-
-
Save staeff/9100e2881c323fff21d0 to your computer and use it in GitHub Desktop.
Wikimedia API calls
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<html> | |
<head> | |
</head> | |
<body> | |
<h2>Jpgs</h2> | |
<a href='https://de.wikipedia.org/wiki/Wikipedia:Hauptseite#/media/File:Fl%C3%BCchtlinge_in_Braunau_am_Inn_01.JPG' class='bootstrap_oembed'> | |
https://de.wikipedia.org/wiki/Wikipedia:Hauptseite#/media/File:Fl%C3%BCchtlinge_in_Braunau_am_Inn_01.JPG</a> | |
<a href="https://de.wikipedia.org/wiki/Wikipedia:Hauptseite#/media/File:Viscountess_Amberley_cropped.jpg" class="bootstrap_oembed">https://de.wikipedia.org/wiki/Wikipedia:Hauptseite#/media/File:Viscountess_Amberley_cropped.jpg</a> | |
<a href="https://de.wikipedia.org/wiki/Wikipedia:Hauptseite#/media/File:British_commander_and_Indian_crew_encounter_elephant_near_Meiktila_2.jpg" class="bootstrap_oembed">https://de.wikipedia.org/wiki/Wikipedia:Hauptseite#/media/File:British_commander_and_Indian_crew_encounter_elephant_near_Meiktila_2.jpg</a> | |
<h2>Png</h2> | |
<a href="https://fr.wikipedia.org/wiki/Assistant_familial#/media/File:La_profession_en_pratique_juillet_2015.png" class="bootstrap_oembed">https://fr.wikipedia.org/wiki/Assistant_familial#/media/File:La_profession_en_pratique_juillet_2015.png</a> | |
<h2>svg</h2> | |
<a href="https://de.wikipedia.org/wiki/Wikipedia:Hauptseite#/media/File:Flag_of_WHO.svg" class="bootstrap_oembed">https://de.wikipedia.org/wiki/Wikipedia:Hauptseite#/media/File:Flag_of_WHO.svg</a> | |
<script type="text/javascript" src="//code.jquery.com/jquery-1.12.0.min.js"></script> | |
<script type="text/javascript" src="example1.js"></script> | |
</body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"batchcomplete": "", | |
"query": { | |
"normalized": [{ | |
"from": "File:WTC_Transportation_Hub_2-01-16.jpg", | |
"to": "File:WTC Transportation Hub 2-01-16.jpg" | |
}], | |
"pages": { | |
"46741890": { | |
"pageid": 46741890, | |
"ns": 6, | |
"title": "File:WTC Transportation Hub 2-01-16.jpg", | |
"imagerepository": "local", | |
"imageinfo": [{ | |
"thumburl": "https://upload.wikimedia.org/wikipedia/commons/thumb/2/21/WTC_Transportation_Hub_2-01-16.jpg/450px-WTC_Transportation_Hub_2-01-16.jpg", | |
"thumbwidth": 450, | |
"thumbheight": 304, | |
"url": "https://upload.wikimedia.org/wikipedia/commons/2/21/WTC_Transportation_Hub_2-01-16.jpg", | |
"descriptionurl": "https://commons.wikimedia.org/wiki/File:WTC_Transportation_Hub_2-01-16.jpg", | |
"descriptionshorturl": "https://commons.wikimedia.org/w/index.php?curid=46741890", | |
"extmetadata": { | |
"DateTime": { | |
"value": "2016-02-02 15:10:48", | |
"source": "mediawiki-metadata", | |
"hidden": "" | |
}, | |
"ObjectName": { | |
"value": "WTC Transportation Hub 2-01-16", | |
"source": "mediawiki-metadata", | |
"hidden": "" | |
}, | |
"CommonsMetadataExtension": { | |
"value": 1.2, | |
"source": "extension", | |
"hidden": "" | |
}, | |
"Categories": { | |
"value": "Self-published work|WTC Transportation Hub|World Trade Center site in 2016", | |
"source": "commons-categories", | |
"hidden": "" | |
}, | |
"Assessments": { | |
"value": "", | |
"source": "commons-categories", | |
"hidden": "" | |
}, | |
"ImageDescription": { | |
"value": "World Trade Center Transportation Hub in New York, New York, USA", | |
"source": "commons-desc-page" | |
}, | |
"DateTimeOriginal": { | |
"value": "2016-02-02", | |
"source": "commons-desc-page" | |
}, | |
"Credit": { | |
"value": "<span class=\"int-own-work\" lang=\"en\">Own work</span>", | |
"source": "commons-desc-page", | |
"hidden": "" | |
}, | |
"Artist": { | |
"value": "<a href=\"//commons.wikimedia.org/w/index.php?title=User:JJBers&action=edit&redlink=1\" class=\"new\" title=\"User:JJBers (page does not exist)\">JJBers</a>", | |
"source": "commons-desc-page" | |
}, | |
"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": "" | |
}, | |
"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": "", | |
"source": "commons-desc-page", | |
"hidden": "" | |
}, | |
"License": { | |
"value": "cc-by-sa-4.0", | |
"source": "commons-templates", | |
"hidden": "" | |
} | |
} | |
}] | |
} | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function getData(url) { | |
return $.ajax({ | |
url: url, | |
type: 'GET', | |
headers: { 'Api-User-Agent': 'Freitag website/1.0' }, | |
dataType: 'jsonp' | |
}); | |
} | |
var $oembed_link = $('.bootstrap_oembed'); | |
var url = $oembed_link.attr('href'); | |
var wikimedia = 'https://commons.wikimedia.org/w/api.php'; | |
$('a.bootstrap_oembed').each(function() { | |
var $this = $(this), | |
url = $this.attr('href'); | |
if (/https:\/\/[^\/]*\.wikipedia\.org\/.*media\/File:/.test(url)) { | |
var filename = /File:.*?$/i.exec(url); | |
var options = '?action=query&format=json&prop=imageinfo&' + | |
'iilimit=1&iiprop=url|extmetadata&iiurlwidth=445&' + | |
'titles=' + filename; | |
var api_call = wikimedia + options; | |
getData(api_call).done(function(data){ | |
var pages = data.query.pages, | |
pagekey; | |
// we need the supposedly only key which is not predictable | |
for (var key in pages) { | |
// hasOwnProperty protects against inherited props && | |
// the key contains just numbers | |
if(pages.hasOwnProperty(key) && /[0-9]/.test(key)){ | |
pagekey = key; | |
} | |
} | |
var imageinfo = pages[pagekey].imageinfo[0], | |
thumburl = imageinfo.thumburl, | |
desc_url = imageinfo.descriptionurl, | |
license = imageinfo.extmetadata.LicenseShortName.value, | |
artist = imageinfo.extmetadata.Artist.value, | |
desc = imageinfo.extmetadata.ObjectName.value, | |
output = '<img src="' + thumburl + '" alt="' + desc + | |
'" title="' + desc + '"/>' + '<p class="credit"> ' + | |
'<a href="'+ desc_url +'">'+ desc + '</a> von '+ | |
artist +' ('+ license + ')</p>'; | |
$this.replaceWith(output); | |
}); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment