Comparing version 1.0.0 to 1.0.1
{ | ||
"name": "pageviews", | ||
"version": "1.0.0", | ||
"version": "1.0.1", | ||
"description": "A lightweight JavaScript client library for the Wikimedia Pageviews API for Wikipedia and various of its sister projects for Node.js and the browser.", | ||
@@ -5,0 +5,0 @@ "main": "pageviews.js", |
@@ -140,7 +140,7 @@ /** | ||
// Required: month | ||
if ((!params.month) || (!/^(?:0[1-9]|1[012])$/.test(params.month))) { | ||
if ((!params.month) || (!/^(?:0?[1-9]|1[012])$/.test(params.month))) { | ||
return new Error('Required parameter "month" missing or invalid.'); | ||
} | ||
// Required: day | ||
if ((!params.day) || (!/^(?:0[1-9]|[12][0-9]|3[01])$/.test(params.day))) { | ||
if ((!params.day) || (!/^(?:0?[1-9]|[12][0-9]|3[01])$/.test(params.day))) { | ||
return new Error('Required parameter "day" missing or invalid.'); | ||
@@ -323,4 +323,6 @@ } | ||
var year = params.year; | ||
var month = params.month; | ||
var day = params.day; | ||
var month = typeof params.month === 'number' && params.month < 10 ? | ||
'0' + params.month : params.month; | ||
var day = typeof params.day === 'number' && params.day < 10 ? | ||
'0' + params.day : params.day; | ||
var limit = params.limit || false; | ||
@@ -327,0 +329,0 @@ // Optional params |
@@ -17,2 +17,2 @@ /** | ||
*/ | ||
var request,USER_AGENT="pageviews.js",environment="undefined"==typeof window?"node":"browser";if("node"===environment){request=require("request");var package=require("./package.json");USER_AGENT="pageviews.js–v"+package.version+" ("+package.repository.url+")"}else request=function(e,r){var t=new XMLHttpRequest;t.addEventListener("load",function(){return r(null,{statusCode:this.status},this.responseText)}),t.addEventListener("error",function(e){return r(e)}),t.open("GET",e.url),t.send()};var pageviews=function(){var e="https://wikimedia.org/api/rest_v1",r={"default":"all-access",allowed:["all-access","desktop","mobile-web","mobile-app"]},t={"default":"all-agents",allowed:["all-agents","user","spider","bot"]},a={"default":"hourly",allowed:["daily","hourly","monthly"]},i={"default":"daily",allowed:["daily"]},n=function(e,n){if(!e)return new Error("Required parameters missing.");if(!e.project&&!e.projects)return"getAggregatedPageviews"===n||"getTopPageviews"===n?new Error('Required parameter "project" or "projects" missing.'):new Error('Required parameter "project" missing.');if(e.project&&-1===e.project.indexOf("."))return new Error('Required parameter "project" invalid.');if(("getAggregatedPageviews"===n||"getTopPageviews"===n)&&e.projects&&(!Array.isArray(e.projects)||!e.projects.length||e.projects.filter(function(e){return-1===e.indexOf(".")}).length))return new Error('Required parameter "projects" invalid.');if("getPerArticlePageviews"===n){if(!e.article&&!e.articles)return new Error('Required parameter "article" or "articles" missing.');if(e.articles&&(!Array.isArray(e.articles)||!e.articles.length))return new Error('Required parameter "articles" invalid.')}if("getPerArticlePageviews"===n){if(!e.start||!/^(?:19|20)\d\d[- /.]?(?:0[1-9]|1[012])[- /.]?(?:0[1-9]|[12][0-9]|3[01])$/.test(e.start))return new Error('Required parameter "start" missing or invalid.');if(!e.end||!/^(19|20)\d\d[- /.]?(0[1-9]|1[012])[- /.]?(0[1-9]|[12][0-9]|3[01])$/.test(e.end))return new Error('Required parameter "end" missing or invalid.')}else if("getAggregatedPageviews"===n){if(!e.start||!/^(?:19|20)\d\d[- /.]?(?:0[1-9]|1[012])[- /.]?(?:0[1-9]|[12][0-9]|3[01])[- /.]?(?:[012][0-9])$/.test(e.start))return new Error('Required parameter "start" missing or invalid.');if(!e.end||!/^(19|20)\d\d[- /.]?(0[1-9]|1[012])[- /.]?(0[1-9]|[12][0-9]|3[01])[- /.]?(?:[012][0-9])$/.test(e.end))return new Error('Required parameter "end" missing or invalid.')}if("getTopPageviews"===n){if(!e.year||!/^(?:19|20)\d\d$/.test(e.year))return new Error('Required parameter "year" missing or invalid.');if(!e.month||!/^(?:0[1-9]|1[012])$/.test(e.month))return new Error('Required parameter "month" missing or invalid.');if(!e.day||!/^(?:0[1-9]|[12][0-9]|3[01])$/.test(e.day))return new Error('Required parameter "day" missing or invalid.');if(e.limit&&!/^\d+$/.test(e.limit)&&0<e.limit&&e.limit<=1e3)return new Error('Invalid optional parameter "limit".')}if(e.access&&-1===r.allowed.indexOf(e.access))return new Error('Invalid optional parameter "access".');if(e.agent&&-1===t.allowed.indexOf(e.agent))return new Error('Invalid optional parameter "agent".');if(e.granularity)if("getAggregatedPageviews"===n){if(-1===a.allowed.indexOf(e.granularity))return new Error('Invalid optional parameter "granularity".')}else if("getPerArticlePageviews"===n&&-1===i.allowed.indexOf(e.granularity))return new Error('Invalid optional parameter "granularity".');return e},s=function(e,r,t){var a;if(e||200!==r.statusCode){if(e)return e;if(404===r.statusCode)try{return a=JSON.parse(t),new Error(a.detail)}catch(i){return new Error(i)}return new Error("Status code "+r.statusCode)}try{a=JSON.parse(t)}catch(i){return new Error(i)}return a},o=function(a){return new Promise(function(l,u){if(a=n(a,"getPerArticlePageviews"),a.stack)return u(a);if(a.articles){var d=[];return a.articles.map(function(e,r){var t=a;delete t.articles,t.article=e,d[r]=o(t)}),l(Promise.all(d))}var c=a.project,g=encodeURIComponent(a.article.replace(/\s/g,"_")),p=a.start,f=a.end,v=a.access?a.access:r["default"],m=a.agent?a.agent:t["default"],w=a.granularity?a.granularity:i["default"],E={url:e+"/metrics/pageviews/per-article/"+c+"/"+v+"/"+m+"/"+g+"/"+w+"/"+p+"/"+f,headers:{"User-Agent":USER_AGENT}};request(E,function(e,r,t){var a=s(e,r,t);return a.stack?u(a):l(a)})})},l=function(i){return new Promise(function(o,u){if(i=n(i,"getAggregatedPageviews"),i.stack)return u(i);if(i.projects){var d=[];return i.projects.map(function(e,r){var t=i;delete t.projects,t.project=e,d[r]=l(t)}),o(Promise.all(d))}var c=i.project,g=i.start,p=i.end,f=i.access?i.access:r["default"],v=i.agent?i.agent:t["default"],m=i.granularity?i.granularity:a["default"],w={url:e+"/metrics/pageviews/aggregate/"+c+"/"+f+"/"+v+"/"+m+"/"+g+"/"+p,headers:{"User-Agent":USER_AGENT}};request(w,function(e,r,t){var a=s(e,r,t);return a.stack?u(a):o(a)})})},u=function(t){return new Promise(function(a,i){if(t=n(t,"getTopPageviews"),t.stack)return i(t);if(t.projects){var o=[];return t.projects.map(function(e,r){var a=t;delete a.projects,a.project=e,o[r]=u(a)}),a(Promise.all(o))}var l=t.project,d=t.year,c=t.month,g=t.day,p=t.limit||!1,f=t.access?t.access:r["default"],v={url:e+"/metrics/pageviews/top/"+l+"/"+f+"/"+d+"/"+c+"/"+g,headers:{"User-Agent":USER_AGENT}};request(v,function(e,r,t){var n=s(e,r,t);return n.stack?i(n):(p&&(n.items[0].articles=n.items[0].articles.slice(0,p)),a(n))})})},d=function(){return new Promise(function(r,t){var a={url:e+"/metrics/pageviews/",headers:{"User-Agent":USER_AGENT}};request(a,function(e,a,i){var n=s(e,a,i);return n.stack?t(n):r(n)})})};return{getPageviewsDimensions:d,getPerArticlePageviews:o,getAggregatedPageviews:l,getTopPageviews:u}}();"node"===environment&&(module.exports=pageviews); | ||
var request,USER_AGENT="pageviews.js",environment="undefined"==typeof window?"node":"browser";if("node"===environment){request=require("request");var package=require("./package.json");USER_AGENT="pageviews.js–v"+package.version+" ("+package.repository.url+")"}else request=function(e,r){var t=new XMLHttpRequest;t.addEventListener("load",function(){return r(null,{statusCode:this.status},this.responseText)}),t.addEventListener("error",function(e){return r(e)}),t.open("GET",e.url),t.send()};var pageviews=function(){var e="https://wikimedia.org/api/rest_v1",r={"default":"all-access",allowed:["all-access","desktop","mobile-web","mobile-app"]},t={"default":"all-agents",allowed:["all-agents","user","spider","bot"]},a={"default":"hourly",allowed:["daily","hourly","monthly"]},i={"default":"daily",allowed:["daily"]},n=function(e,n){if(!e)return new Error("Required parameters missing.");if(!e.project&&!e.projects)return"getAggregatedPageviews"===n||"getTopPageviews"===n?new Error('Required parameter "project" or "projects" missing.'):new Error('Required parameter "project" missing.');if(e.project&&-1===e.project.indexOf("."))return new Error('Required parameter "project" invalid.');if(("getAggregatedPageviews"===n||"getTopPageviews"===n)&&e.projects&&(!Array.isArray(e.projects)||!e.projects.length||e.projects.filter(function(e){return-1===e.indexOf(".")}).length))return new Error('Required parameter "projects" invalid.');if("getPerArticlePageviews"===n){if(!e.article&&!e.articles)return new Error('Required parameter "article" or "articles" missing.');if(e.articles&&(!Array.isArray(e.articles)||!e.articles.length))return new Error('Required parameter "articles" invalid.')}if("getPerArticlePageviews"===n){if(!e.start||!/^(?:19|20)\d\d[- \/.]?(?:0[1-9]|1[012])[- \/.]?(?:0[1-9]|[12][0-9]|3[01])$/.test(e.start))return new Error('Required parameter "start" missing or invalid.');if(!e.end||!/^(19|20)\d\d[- \/.]?(0[1-9]|1[012])[- \/.]?(0[1-9]|[12][0-9]|3[01])$/.test(e.end))return new Error('Required parameter "end" missing or invalid.')}else if("getAggregatedPageviews"===n){if(!e.start||!/^(?:19|20)\d\d[- \/.]?(?:0[1-9]|1[012])[- \/.]?(?:0[1-9]|[12][0-9]|3[01])[- \/.]?(?:[012][0-9])$/.test(e.start))return new Error('Required parameter "start" missing or invalid.');if(!e.end||!/^(19|20)\d\d[- \/.]?(0[1-9]|1[012])[- \/.]?(0[1-9]|[12][0-9]|3[01])[- \/.]?(?:[012][0-9])$/.test(e.end))return new Error('Required parameter "end" missing or invalid.')}if("getTopPageviews"===n){if(!e.year||!/^(?:19|20)\d\d$/.test(e.year))return new Error('Required parameter "year" missing or invalid.');if(!e.month||!/^(?:0?[1-9]|1[012])$/.test(e.month))return new Error('Required parameter "month" missing or invalid.');if(!e.day||!/^(?:0?[1-9]|[12][0-9]|3[01])$/.test(e.day))return new Error('Required parameter "day" missing or invalid.');if(e.limit&&!/^\d+$/.test(e.limit)&&0<e.limit&&e.limit<=1e3)return new Error('Invalid optional parameter "limit".')}if(e.access&&-1===r.allowed.indexOf(e.access))return new Error('Invalid optional parameter "access".');if(e.agent&&-1===t.allowed.indexOf(e.agent))return new Error('Invalid optional parameter "agent".');if(e.granularity)if("getAggregatedPageviews"===n){if(-1===a.allowed.indexOf(e.granularity))return new Error('Invalid optional parameter "granularity".')}else if("getPerArticlePageviews"===n&&-1===i.allowed.indexOf(e.granularity))return new Error('Invalid optional parameter "granularity".');return e},s=function(e,r,t){var a;if(e||200!==r.statusCode){if(e)return e;if(404===r.statusCode)try{return a=JSON.parse(t),new Error(a.detail)}catch(i){return new Error(i)}return new Error("Status code "+r.statusCode)}try{a=JSON.parse(t)}catch(i){return new Error(i)}return a},o=function(a){return new Promise(function(u,l){if(a=n(a,"getPerArticlePageviews"),a.stack)return l(a);if(a.articles){var d=[];return a.articles.map(function(e,r){var t=a;delete t.articles,t.article=e,d[r]=o(t)}),u(Promise.all(d))}var c=a.project,g=encodeURIComponent(a.article.replace(/\s/g,"_")),p=a.start,f=a.end,m=a.access?a.access:r["default"],v=a.agent?a.agent:t["default"],w=a.granularity?a.granularity:i["default"],E={url:e+"/metrics/pageviews/per-article/"+c+"/"+m+"/"+v+"/"+g+"/"+w+"/"+p+"/"+f,headers:{"User-Agent":USER_AGENT}};request(E,function(e,r,t){var a=s(e,r,t);return a.stack?l(a):u(a)})})},u=function(i){return new Promise(function(o,l){if(i=n(i,"getAggregatedPageviews"),i.stack)return l(i);if(i.projects){var d=[];return i.projects.map(function(e,r){var t=i;delete t.projects,t.project=e,d[r]=u(t)}),o(Promise.all(d))}var c=i.project,g=i.start,p=i.end,f=i.access?i.access:r["default"],m=i.agent?i.agent:t["default"],v=i.granularity?i.granularity:a["default"],w={url:e+"/metrics/pageviews/aggregate/"+c+"/"+f+"/"+m+"/"+v+"/"+g+"/"+p,headers:{"User-Agent":USER_AGENT}};request(w,function(e,r,t){var a=s(e,r,t);return a.stack?l(a):o(a)})})},l=function(t){return new Promise(function(a,i){if(t=n(t,"getTopPageviews"),t.stack)return i(t);if(t.projects){var o=[];return t.projects.map(function(e,r){var a=t;delete a.projects,a.project=e,o[r]=l(a)}),a(Promise.all(o))}var u=t.project,d=t.year,c="number"==typeof t.month&&t.month<10?"0"+t.month:t.month,g="number"==typeof t.day&&t.day<10?"0"+t.day:t.day,p=t.limit||!1,f=t.access?t.access:r["default"],m={url:e+"/metrics/pageviews/top/"+u+"/"+f+"/"+d+"/"+c+"/"+g,headers:{"User-Agent":USER_AGENT}};request(m,function(e,r,t){var n=s(e,r,t);return n.stack?i(n):(p&&(n.items[0].articles=n.items[0].articles.slice(0,p)),a(n))})})},d=function(){return new Promise(function(r,t){var a={url:e+"/metrics/pageviews/",headers:{"User-Agent":USER_AGENT}};request(a,function(e,a,i){var n=s(e,a,i);return n.stack?t(n):r(n)})})};return{getPageviewsDimensions:d,getPerArticlePageviews:o,getAggregatedPageviews:u,getTopPageviews:l}}();"node"===environment&&(module.exports=pageviews); |
@@ -75,1 +75,25 @@ var pageviews = typeof window === 'undefined' ? | ||
}); | ||
pageviews.getTopPageviews({ | ||
project: 'en.wikipedia', | ||
year: 2015, | ||
month: 12, | ||
day: 1, | ||
limit: 2 | ||
}).then(function(result) { | ||
console.log(JSON.stringify(result, null, 2)); | ||
}).catch(function(error) { | ||
console.log(error); | ||
}); | ||
pageviews.getTopPageviews({ | ||
project: 'en.wikipedia', | ||
year: 2015, | ||
month: 9, | ||
day: 30, | ||
limit: 2 | ||
}).then(function(result) { | ||
console.log(JSON.stringify(result, null, 2)); | ||
}).catch(function(error) { | ||
console.log(error); | ||
}); |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
39730
487