-
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathpageviews.min.js
17 lines (16 loc) · 9.78 KB
/
pageviews.min.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
/**
* @license
* Copyright 2017 Thomas Steiner (@tomayac). All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
var request,packageJson,USER_AGENT="pageviews.js",environment="undefined"==typeof window?"node":"browser";"node"===environment?(request=require("request"),packageJson=require("./package.json"),USER_AGENT="pageviews.js-v"+packageJson.version+" ("+packageJson.repository.url+")"):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(){function d(e,r){function t(e){return e<10?"0"+e:e.toString()}if(!e)return new Error("Required parameters missing.");if(!e.project&&!e.projects)return"getAggregatedPageviews"===r||"getTopPageviews"===r||"getTopPageviewsByCountry"===r||"getAggregatedLegacyPagecounts"===r?new Error('Required parameter "project" or "projects" missing.'):new Error('Required parameter "project" missing.');if(e.project&&"all-projects"!==e.project&&"wikidata"!==e.project&&-1===e.project.indexOf("."))return new Error('Required parameter "project" invalid.');if(("getAggregatedPageviews"===r||"getAggregatedLegacyPagecounts"===r||"getTopPageviews"===r||"getTopPageviewsByCountry"===r)&&e.projects&&"all-projects"!==e.projects&&(!Array.isArray(e.projects)||!e.projects.length||e.projects.filter(function(e){return-1===e.indexOf(".")&&"all-projects"!==e&&"wikidata"!==e}).length))return new Error('Required parameter "projects" invalid.');if("getPerArticlePageviews"===r){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"===r||"getUniqueDevices"===r){if(!e.start)return new Error('Required parameter "start" missing.');if(e.start="object"==typeof e.start?e.start.getUTCFullYear()+t(e.start.getUTCMonth()+1)+t(e.start.getUTCDate()):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" invalid.');if(!e.end)return new Error('Required parameter "end" missing.');if(e.end="object"==typeof e.end?e.end.getUTCFullYear()+t(e.end.getUTCMonth()+1)+t(e.end.getUTCDate()):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" invalid.')}else if("getAggregatedPageviews"===r||"getAggregatedLegacyPagecounts"===r){if(!e.start)return new Error('Required parameter "start" missing.');if(e.start="object"==typeof e.start?e.start.getUTCFullYear()+t(e.start.getUTCMonth()+1)+(t(e.start.getUTCDate())+t(e.start.getUTCHours())):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)return new Error('Required parameter "end" missing.');if(e.end="object"==typeof e.end?e.end.getUTCFullYear()+t(e.end.getUTCMonth()+1)+t(e.end.getUTCDate())+t(e.end.getUTCHours()):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("getTopPageviewsByCountry"===r){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.access&&-1===f.allowed.indexOf(e.access))return new Error('Invalid optional parameter "access".')}if("getTopPageviews"===r){if(e.date&&(e.date="object"==typeof e.date?e.date:new Date(e.date.substr(0,4)+"-"+e.date.substr(4,2)+"-"+e.date.substr(6,2)),e.year=e.date.getUTCFullYear(),e.month=t(e.date.getUTCMonth()+1),e.day=t(e.date.getUTCDate())),!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]|all-days)$/.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===f.allowed.indexOf(e.access))return new Error('Invalid optional parameter "access".');if(e.accessSite&&-1===l.allowed.indexOf(e.accessSite))return new Error('Invalid optional parameter "accessSite".');if(e.agent&&-1===m.allowed.indexOf(e.agent))return new Error('Invalid optional parameter "agent".');if(e.granularity)if("getAggregatedPageviews"===r||"getAggregatedLegacyPagecounts"===r){if(-1===c.allowed.indexOf(e.granularity))return new Error('Invalid optional parameter "granularity".')}else if("getPerArticlePageviews"===r){if(-1===w.allowed.indexOf(e.granularity))return new Error('Invalid optional parameter "granularity".')}else if("getUniqueDevices"===r&&-1===u.allowed.indexOf(e.granularity))return new Error('Invalid optional parameter "granularity".');return e}function g(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||a.title)}catch(e){return new Error(e)}return new Error("Status code "+r.statusCode)}try{a=JSON.parse(t)}catch(e){return new Error(e)}return a}var p="https://wikimedia.org/api/rest_v1",f={default:"all-access",allowed:["all-access","desktop","mobile-web","mobile-app"]},l={default:"all-sites",allowed:["all-sites","desktop-site","mobile-site","all-access"]},m={default:"all-agents",allowed:["all-agents","user","spider","bot"]},c={default:"hourly",allowed:["daily","hourly","monthly"]},w={default:"daily",allowed:["daily","monthly"]},u={default:"daily",allowed:["daily","monthly"]},v=function(c){return new Promise(function(a,n){if((c=d(c,"getPerArticlePageviews")).stack)return n(c);if(c.articles){var i=[];return c.articles.map(function(e,r){var t=c;delete t.articles,t.article=e,i[r]=v(t)}),a(Promise.all(i))}var e=c.project,r=encodeURIComponent(c.article.replace(/\s/g,"_")),t=c.start,s=c.end,o=c.access||f.default,u=c.agent||m.default,l=c.granularity||w.default;request({url:p+"/metrics/pageviews/per-article/"+e+"/"+o+"/"+u+"/"+r+"/"+l+"/"+t+"/"+s,headers:{"User-Agent":USER_AGENT}},function(e,r,t){t=g(e,r,t);return(t.stack?n:a)(t)})})},y=function(l){return new Promise(function(a,n){if((l=d(l,"getAggregatedPageviews")).stack)return n(l);if("all-projects"===l.projects&&(l.projects=null,l.project="all-projects"),l.projects){var i=[];return l.projects.map(function(e,r){var t=l;delete t.projects,t.project=e,i[r]=y(t)}),a(Promise.all(i))}var e=l.project,r=l.start,t=l.end,s=l.access||f.default,o=l.agent||m.default,u=l.granularity||c.default;request({url:p+"/metrics/pageviews/aggregate/"+e+"/"+s+"/"+o+"/"+u+"/"+r+"/"+t,headers:{"User-Agent":USER_AGENT}},function(e,r,t){t=g(e,r,t);return(t.stack?n:a)(t)})})},j=function(u){return new Promise(function(a,n){if((u=d(u,"getAggregatedLegacyPagecounts")).stack)return n(u);if("all-projects"===u.projects&&(u.projects=null,u.project="all-projects"),u.projects){var i=[];return u.projects.map(function(e,r){var t=u;delete t.projects,t.project=e,i[r]=j(t)}),a(Promise.all(i))}var e=u.project,r=u.start,t=u.end,s=u.accessSite||l.default,o=u.granularity||c.default;request({url:p+"/metrics/legacy/pagecounts/aggregate/"+e+"/"+s+"/"+o+"/"+r+"/"+t,headers:{"User-Agent":USER_AGENT}},function(e,r,t){t=g(e,r,t);return(t.stack?n:a)(t)})})},E=function(l){return new Promise(function(a,n){if((l=d(l,"getTopPageviews")).stack)return n(l);if(l.projects){var i=[];return l.projects.map(function(e,r){var t=l;delete t.projects,t.project=e,i[r]=E(t)}),a(Promise.all(i))}var e=l.project,r=l.year,t="number"==typeof l.month&&l.month<10?"0"+l.month:l.month,s="number"==typeof l.day&&l.day<10?"0"+l.day:l.day,o=l.limit||!1,u=l.access||f.default;request({url:p+"/metrics/pageviews/top/"+e+"/"+u+"/"+r+"/"+t+"/"+s,headers:{"User-Agent":USER_AGENT}},function(e,r,t){t=g(e,r,t);return t.stack?n(t):(o&&(t.items[0].articles=t.items[0].articles.slice(0,o)),a(t))})})},P=function(o){return new Promise(function(a,n){if((o=d(o,"getTopPageviewsByCountry")).stack)return n(o);if(o.projects){var i=[];return o.projects.map(function(e,r){var t=o;delete t.projects,t.project=e,i[r]=P(t)}),a(Promise.all(i))}var e=o.project,r=o.year,t="number"==typeof o.month&&o.month<10?"0"+o.month:o.month,s=o.access||f.default;request({url:p+"/metrics/pageviews/top-by-country/"+e+"/"+s+"/"+r+"/"+t,headers:{"User-Agent":USER_AGENT}},function(e,r,t){t=g(e,r,t);return(t.stack?n:a)(t)})})};return{getPageviewsDimensions:function(){return new Promise(function(a,n){request({url:p+"/metrics/pageviews/",headers:{"User-Agent":USER_AGENT}},function(e,r,t){t=g(e,r,t);return(t.stack?n:a)(t)})})},getPerArticlePageviews:v,getAggregatedPageviews:y,getAggregatedLegacyPagecounts:j,getTopPageviews:E,getTopPageviewsByCountry:P,getUniqueDevices:function(o){return new Promise(function(a,n){if((o=d(o,"getUniqueDevices")).stack)return n(o);var e=o.project,r=o.start,t=o.end,i=o.accessSite||l.default,s=o.granularity||u.default;request({url:p+"/metrics/unique-devices/"+e+"/"+i+"/"+s+"/"+r+"/"+t,headers:{"User-Agent":USER_AGENT}},function(e,r,t){t=g(e,r,t);return(t.stack?n:a)(t)})})}}}();"node"===environment&&(module.exports=pageviews);