> ## Documentation Index
> Fetch the complete documentation index at: https://calendar-effects.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# etl/pageviews_download.py

> Download daily Wikipedia page views (real users only, no bots) for each article of an in-scope retailer in config/wiki_articles.csv, over the study period, and save them to raw/wiki/<article>.json.

`etl/pageviews_download.py` · chapter [Building a row](https://calendar-effects.mintlify.site/tour/03-building-a-row.md), file 3 · 43 lines · [open in the repository](https://github.sfu.ca/aba395/Calendar-Effects/blob/main/etl/pageviews_download.py)

## What it does

Download daily Wikipedia page views (real users only, no bots) for each article
of an in-scope retailer in config/wiki\_articles.csv, over the study period, and save
them to raw/wiki/\<article>.json.

Run from anywhere:   python3 etl/pageviews\_download.py

Some companies' articles were renamed during the study period. Wikipedia counts views
under the title the reader asked for, so after a rename the old title's views
move to the new title. config/wiki\_articles.csv lists BOTH titles for those
companies, and etl/pageviews\_clean.py adds them together per ticker and day.

Articles already in raw/wiki/ are skipped, so re-running only fetches new ones.

## What is inside

* **`HEADERS`** · [line 23](https://github.sfu.ca/aba395/Calendar-Effects/blob/main/etl/pageviews_download.py#L23)
* **`OUT_DIR`** · [line 24](https://github.sfu.ca/aba395/Calendar-Effects/blob/main/etl/pageviews_download.py#L24)

## Connections

**Uses:** [`src/common/contracts.py`](https://calendar-effects.mintlify.site/reference/src/common/contracts), [`src/common/settings.py`](https://calendar-effects.mintlify.site/reference/src/common/settings), [`src/common/study.py`](https://calendar-effects.mintlify.site/reference/src/common/study)

## Source

```python title="etl/pageviews_download.py" lines expandable theme={null}
"""Download daily Wikipedia page views (real users only, no bots) for each article
of an in-scope retailer in config/wiki_articles.csv, over the study period, and save
them to raw/wiki/<article>.json.

Run from anywhere:   python3 etl/pageviews_download.py

Some companies' articles were renamed during the study period. Wikipedia counts views
under the title the reader asked for, so after a rename the old title's views
move to the new title. config/wiki_articles.csv lists BOTH titles for those
companies, and etl/pageviews_clean.py adds them together per ticker and day.

Articles already in raw/wiki/ are skipped, so re-running only fetches new ones.
"""
import json, time
from urllib.parse import quote

import requests

from common.contracts import load_wiki_articles
from common.settings import RAW_DIR
from common.study import STUDY_END, STUDY_START

HEADERS = {'User-Agent': 'CMPT732-student-project (ska510@sfu.ca)'}
OUT_DIR = RAW_DIR / 'wiki'

articles = [row['article'] for row in load_wiki_articles(in_scope_only=True)]

OUT_DIR.mkdir(parents=True, exist_ok=True)
for a in articles:
    path = OUT_DIR / (a + '.json')
    if path.exists():
        print(a, 'already downloaded, skipping')
        continue
    url = ('https://wikimedia.org/api/rest_v1/metrics/pageviews/per-article/en.wikipedia/all-access/user/'
           + quote(a, safe='') + '/daily/'
           + STUDY_START.strftime('%Y%m%d') + '/' + STUDY_END.strftime('%Y%m%d'))
    r = requests.get(url, headers=HEADERS, timeout=30)
    print(a, r.status_code)
    r.raise_for_status()
    with open(path, 'w') as f:
        for item in r.json()['items']:
            f.write(json.dumps(item) + '\n')
    time.sleep(0.5)
```


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.