Dear all,
First and foremost, thanks for making the Wikimedia Pageviews API available; your work is highly appreciated and super useful! As a modest "thank you", I am happy to release the JavaScript client library pageviews.js for Node.js and the browser to make working with this API easy for JavaScript developers. Please find the code and all instructions at [1]. The library adds some convenience functions (getting batch pageviews and limiting the number of results) that were inspired by Dan Andreescu's Python library [2] and is Promise-based:
=== var pageviews = require('pageviews');
// Getting pageviews for a single article pageviews.getPerArticlePageviews({ article: 'Berlin', project: 'en.wikipedia', start: '20151201', end: '20151202' }).then(function(result) { console.log(JSON.stringify(result, null, 2)); }).catch(function(error) { console.log(error); });
// Getting top-n items ranked by pageviews for multiple projects pageviews.getTopPageviews({ projects: ['en.wikipedia', 'de.wikipedia'], // Plural year: '2015', month: '12', day: '01', limit: 2 // Limit to the first n results }).then(function(result) { console.log(JSON.stringify(result, null, 2)); }).catch(function(error) { console.log(error); }); ===
On a more technical note—trying to be a good citizen [3]—the client library sets an identifying User-Agent header in Node.js mode. However, trying to set the corresponding X-User-Agent (note the "X-") header from a browser context (XMLHttpRequest cannot override the browser's intrinsic User-Agent for security reasons), this fails with an error message "Request header field X-User-Agent is not allowed by Access-Control-Allow-Headers in preflight response". Maybe you could change your CORS settings and include X-User-Agent in your Access-Control-Allow-Headers?!
Hope this is useful.
Thanks, Tom
-- [1] pageviews.js: https://github.com/tomayac/pageviews.js [2] python-mwviews: https://github.com/mediawiki-utilities/python-mwviews [3] User-Agent requirement: https://wikimedia.org/api/rest_v1/?doc