PK'YƒH-Àˆ²²pageviewapi/client.py"""Client to wikimedia pageview api. API doc: https://wikitech.wikimedia.org/wiki/Analytics/AQS/Pageview_API Supported endpoints: - per-article - top - aggregate """ from attrdict import AttrDict import requests __version__ = "0.2.3" # User-agent PROJECT_URL = "https://github.com/Commonists/pageview-api" UA = "Python pageview-api client v{version} <{url}>" USER_AGENT = { 'User-Agent': UA.format(url=PROJECT_URL, version=__version__) } API_BASE_URL = "https://wikimedia.org/api/rest_v1/metrics/pageviews" # Per article PA_ENDPOINT = "per-article" PA_ARGS = "{project}/{access}/{agent}/{page}/{granularity}/{start}/{end}" # Top TOP_ENDPOINT = "top" TOP_ARGS = "{project}/{access}/{year}/{month}/{day}" # aggregate AG_ENDPOINT = "aggregate" AG_ARGS = "{project}/{access}/{agent}/{granularity}/{start}/{end}" def per_article(project, page, start, end, access='all-access', agent='all-agents', granularity='daily'): """Per article API. >>> import pageviewapi >>> pageview.per_article('en.wikipedia', 'Paris', '20151106', '20151120') will requests views for Paris article between 2015-11-06 and 2015-11-20 """ args = PA_ARGS.format(project=project, page=page, start=start, end=end, access=access, agent=agent, granularity=granularity) return __api__(PA_ENDPOINT, args) def top(project, year, month, day, access='all-access'): """Top 1000 most visited articles from project on a given date. >>> import pageviewapi >>> views = pageviewapi.top('fr.wikipedia', 2015, 11, 14) >>> views['items'][0]['articles'][0] {u'article': u'Wikip\xe9dia:Accueil_principal', u'rank': 1, u'views': 1600547} """ args = TOP_ARGS.format(project=project, access=access, year=year, month=month, day=day) return __api__(TOP_ENDPOINT, args) def aggregate(project, start, end, access='all-access', agent='all-agents', granularity='daily'): """Aggregate API. >>> import pageviewapi >>> pageviewapi.aggregate('fr.wikipedia', '2015100100', '2015103100') """ args = AG_ARGS.format(project=project, start=start, end=end, access=access, agent=agent, granularity=granularity) return __api__(AG_ENDPOINT, args) def __api__(end_point, args, api_url=API_BASE_URL): """Calling API.""" url = "/".join([api_url, end_point, args]) return AttrDict(requests.get(url, headers=USER_AGENT).json()) class APIValues(object): @classmethod def allvalues(cls): """All values in the enum class. Returns: All upper cased value from APIValues. """ return sorted([vars(cls)[variable] for variable in dir(cls) if variable.isupper()]) class Access(APIValues): """Access values allows to filter by access. If you want to filter by mobile use: Mobile application: Access.MOBILE_APP Mobile web: Access.MOBILE_WEB Desktop: Access.DESKTOP Default: Access.ALL_ACCESS """ ALL_ACCESS = "all-access" DESKTOP = "desktop" MOBILE_APP = "mobile-app" MOBILE_WEB = "mobile-web" class Agent(APIValues): """Agent values allows to filter by kind of user-agent. All: Agent.ALL_AGENTS User: Agent.USER Bot: Agent.BOT Spider: Agent.SPIDER """ ALL_AGENTS = "all-agents" USER = "user" BOT = "bot" SPIDER = "spider" PKµZƒHw­Vapageviewapi/__init__.py"""Python client for wikimedia pageview api.""" from pageviewapi.client import per_article from pageviewapi.client import top from pageviewapi.client import aggregate from pageviewapi.client import Access from pageviewapi.client import Agent from pageviewapi.client import __version__ PKãZƒH,¯,ZZpageviewapi/period.py"""Helper functions on period.""" import datetime import pageviewapi.client def sum_last(project, page, last=30, agent='all-agents', access='all-access'): """Page views during last days.""" views = pageviewapi.client.per_article(project, page, __days_ago__(last), __today__(), access=access, agent=agent) return sum([daily['views'] for daily in views['items']]) def avg_last(project, page, last=30, agent='all-agents', access='all-access'): """Page views during last days.""" views = pageviewapi.client.per_article(project, page, __days_ago__(last), __today__(), access=access, agent=agent) return __avg__([daily['views'] for daily in views['items']]) def __today__(): """Date of the day as YYYYmmdd format.""" return datetime.date.today().strftime('%Y%m%d') def __days_ago__(days): """Days ago as YYYYmmdd format.""" today = datetime.date.today() delta = datetime.timedelta(days=days) ago = today - delta return ago.strftime('%Y%m%d') def __avg__(numericlist): """Basic average function.""" return sum(numericlist) / float(len(numericlist)) PK¶]ƒHÚ„ uu+pageviewapi-0.2.3.dist-info/DESCRIPTION.rst# pageview-api [![Build Status](https://travis-ci.org/Commonists/pageview-api.svg?branch=master)](https://travis-ci.org/Commonists/pageview-api) [![Code Health](https://landscape.io/github/Commonists/pageview-api/master/landscape.svg?style=flat)](https://landscape.io/github/Commonists/pageview-api/master) [![License](http://img.shields.io/badge/license-MIT-orange.svg?style=flat)](http://opensource.org/licenses/MIT) Wikimedia Pageview API client Installation ------------ In order to install system wide on system using sudo you can use: ```sh pip install git+https://github.com/Commonists/pageview-api.git ``` Examples -------- Number of view on English Wikipedia of article Paris from November 6th to November 20th 2015 ```python import pageviewapi pageviewapi.per_article('en.wikipedia', 'Paris', '20151106', '20151120', access='all-access', agent='all-agents', granularity='daily') ``` Aggregation: Get a daily pageview count timeseries of all projects for the month of October 2015 ```python import pageviewapi pageviewapi.aggregate('fr.wikipedia', '2015100100', '2015103100', access='all-access', agent='all-agents', granularity='daily') ``` Most viewed articles on French Wikipedia on November 14th, 2015 ```python import pageviewapi pageviewapi.top('fr.wikipedia', 2015, 11, 14, access='all-access') ``` Sum (resp. average) of view during last 30 days ```python import pageviewapi.period pageviewapi.period.sum_last('fr.wikipedia', 'Paris', last=30, access='all-access', agent='all-agents') pageviewapi.period.avg_last('fr.wikipedia', 'Paris', last=30) ``` PK¶]ƒHä 0Çàà)pageviewapi-0.2.3.dist-info/metadata.json{"classifiers": ["Development Status :: 4 - Beta", "Environment :: Console", "Intended Audience :: Developers", "License :: OSI Approved :: MIT License", "Operating System :: OS Independent", "Programming Language :: Python", "Topic :: Utilities"], "extensions": {"python.details": {"contacts": [{"email": "ps.huard@gmail.com", "name": "Commonists", "role": "author"}], "document_names": {"description": "DESCRIPTION.rst"}, "project_urls": {"Home": "http://github.com/Commonists/pageviewapi"}}}, "extras": [], "generator": "bdist_wheel (0.26.0)", "license": "MIT", "metadata_version": "2.0", "name": "pageviewapi", "run_requires": [{"requires": ["attrdict", "requests"]}], "summary": "Wikimedia Pageview API client", "version": "0.2.3"}PKµ]ƒHP'Q )pageviewapi-0.2.3.dist-info/top_level.txtpageviewapi PK¶]ƒHŒ''\\!pageviewapi-0.2.3.dist-info/WHEELWheel-Version: 1.0 Generator: bdist_wheel (0.26.0) Root-Is-Purelib: true Tag: py2-none-any PK¶]ƒHWjYű±$pageviewapi-0.2.3.dist-info/METADATAMetadata-Version: 2.0 Name: pageviewapi Version: 0.2.3 Summary: Wikimedia Pageview API client Home-page: http://github.com/Commonists/pageviewapi Author: Commonists Author-email: ps.huard@gmail.com License: MIT Platform: UNKNOWN Classifier: Development Status :: 4 - Beta Classifier: Environment :: Console Classifier: Intended Audience :: Developers Classifier: License :: OSI Approved :: MIT License Classifier: Operating System :: OS Independent Classifier: Programming Language :: Python Classifier: Topic :: Utilities Requires-Dist: attrdict Requires-Dist: requests # pageview-api [![Build Status](https://travis-ci.org/Commonists/pageview-api.svg?branch=master)](https://travis-ci.org/Commonists/pageview-api) [![Code Health](https://landscape.io/github/Commonists/pageview-api/master/landscape.svg?style=flat)](https://landscape.io/github/Commonists/pageview-api/master) [![License](http://img.shields.io/badge/license-MIT-orange.svg?style=flat)](http://opensource.org/licenses/MIT) Wikimedia Pageview API client Installation ------------ In order to install system wide on system using sudo you can use: ```sh pip install git+https://github.com/Commonists/pageview-api.git ``` Examples -------- Number of view on English Wikipedia of article Paris from November 6th to November 20th 2015 ```python import pageviewapi pageviewapi.per_article('en.wikipedia', 'Paris', '20151106', '20151120', access='all-access', agent='all-agents', granularity='daily') ``` Aggregation: Get a daily pageview count timeseries of all projects for the month of October 2015 ```python import pageviewapi pageviewapi.aggregate('fr.wikipedia', '2015100100', '2015103100', access='all-access', agent='all-agents', granularity='daily') ``` Most viewed articles on French Wikipedia on November 14th, 2015 ```python import pageviewapi pageviewapi.top('fr.wikipedia', 2015, 11, 14, access='all-access') ``` Sum (resp. average) of view during last 30 days ```python import pageviewapi.period pageviewapi.period.sum_last('fr.wikipedia', 'Paris', last=30, access='all-access', agent='all-agents') pageviewapi.period.avg_last('fr.wikipedia', 'Paris', last=30) ``` PK¶]ƒH—? óó"pageviewapi-0.2.3.dist-info/RECORDpageviewapi/__init__.py,sha256=Eh0othuwW8E6Bl_Aunl6flAzqoTV1H1cREZGimJuCHw,287 pageviewapi/client.py,sha256=hSZSnU5mOXxEnMbQOy-9cwdGzD0NGvYjizo9_JkS9bY,3762 pageviewapi/period.py,sha256=LncQyxN5daliA-pqHOGbrDHkw1vNGuKT1tigxBqIteM,1370 pageviewapi-0.2.3.dist-info/DESCRIPTION.rst,sha256=ryVmOLlLrHHKRZI4g87sNIRjzZAu6BRvfoXNCo5604A,1653 pageviewapi-0.2.3.dist-info/METADATA,sha256=WJtvX7kmwAy394Tx29aHaBtiuxVD7NTYJPSE1KZwMJ4,2225 pageviewapi-0.2.3.dist-info/RECORD,, pageviewapi-0.2.3.dist-info/WHEEL,sha256=JTb7YztR8fkPg6aSjc571Q4eiVHCwmUDlX8PhuuqIIE,92 pageviewapi-0.2.3.dist-info/metadata.json,sha256=Y7LXU5QlRIj9OVfifrKJnDFDe4YR7vM2FYXhWsP2Fhk,736 pageviewapi-0.2.3.dist-info/top_level.txt,sha256=mdokV4yU7N63l3Le90rlvpW_8chdEwQF7F2KkWP3858,12 PK'YƒH-Àˆ²²pageviewapi/client.pyPKµZƒHw­Vaåpageviewapi/__init__.pyPKãZƒH,¯,ZZ9pageviewapi/period.pyPK¶]ƒHÚ„ uu+Æpageviewapi-0.2.3.dist-info/DESCRIPTION.rstPK¶]ƒHä 0Çàà)„pageviewapi-0.2.3.dist-info/metadata.jsonPKµ]ƒHP'Q )«pageviewapi-0.2.3.dist-info/top_level.txtPK¶]ƒHŒ''\\!þpageviewapi-0.2.3.dist-info/WHEELPK¶]ƒHWjYű±$™ pageviewapi-0.2.3.dist-info/METADATAPK¶]ƒH—? óó"Œ)pageviewapi-0.2.3.dist-info/RECORDPK ÿ,