Skip to content

Commit e1c9eaf

Browse files
committed
Revert "Remove visitors tracking (#69)"
This reverts commit db81660.
1 parent db81660 commit e1c9eaf

File tree

4 files changed

+47
-1
lines changed

4 files changed

+47
-1
lines changed

generate.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
# "gitpython",
55
# "potodo",
66
# "jinja2",
7+
# "requests",
78
# "docutils",
89
# ]
910
# ///
@@ -24,6 +25,7 @@
2425

2526
import contribute
2627
import build_status
28+
from visitors import get_number_of_visitors
2729
from completion import branches_from_devguide, get_completion, TranslatorsData
2830
from repositories import get_languages_and_repos, Language
2931

@@ -68,10 +70,12 @@ def get_project_data(
6870
built = language.code in languages_built
6971
if repo:
7072
completion, translators_data, branch, change = get_completion(clones_dir, repo)
73+
visitors_num = get_number_of_visitors(language.code, http) if built else 0
7174
else:
7275
completion = 0.0
7376
translators_data = TranslatorsData(0, False)
7477
change = 0.0
78+
visitors_num = 0
7579
branch = None
7680
return LanguageProjectData(
7781
language,
@@ -80,6 +84,7 @@ def get_project_data(
8084
completion,
8185
change,
8286
translators_data,
87+
visitors_num,
8388
built,
8489
in_switcher=languages_built.get(language.code),
8590
uses_platform=language.code in contribute.pulling_from_transifex,
@@ -95,6 +100,7 @@ class LanguageProjectData:
95100
completion: float
96101
change: float
97102
translators: TranslatorsData
103+
visitors: int
98104
built: bool
99105
in_switcher: bool | None
100106
uses_platform: bool

style.css

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ th {
3939
.progress-bar.low + .progress-bar-outer-label {
4040
display: inline-block;
4141
}
42-
td[data-label="translators"] {
42+
td[data-label="visitors"], td[data-label="translators"] {
4343
text-align: right;
4444
}
4545
td[data-label="completion"] {

template.html.jinja

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
<tr>
1313
<th>language</th>
1414
<th>switcher</th>
15+
<th>visitors*</th>
1516
<th>translators</th>
1617
<th>completion</th>
1718
</tr>
@@ -29,6 +30,15 @@
2930
<a href="https://docs.python.org/{{ project.language.code }}/" target="_blank">✗</a>
3031
{% endif %}
3132
</td>
33+
<td data-label="visitors">
34+
{% if project.built %}
35+
<a href="https://plausible.io/docs.python.org?filters=((contains,page,(/{{ project.language.code }}/)))" target="_blank">
36+
{{ '{:,}'.format(project.visitors) }}
37+
</a>
38+
{% else %}
39+
{{ '{:,}'.format(project.visitors) }}
40+
{% endif %}
41+
</td>
3242
<td data-label="translators">
3343
{% if project.translators.link %}<a href="{{ project.translators.link }}" target="_blank">{% endif %}
3444
{{ project.translators.number }}
@@ -42,6 +52,7 @@
4252
{% endfor %}
4353
</tbody>
4454
</table>
55+
<p>* sum of <a href="https://plausible.io/data-policy#how-we-count-unique-users-without-cookies" target="_blank">daily unique visitors</a> since 8 June 2024</p>
4556
<p>For more information about translations, see the <a href="https://devguide.python.org/documentation/translating/" target="_blank">Python Developer’s Guide</a>.</p>
4657
<p>Last updated at {{ generation_time.strftime('%A, %-d %B %Y, %-H:%M:%S %Z') }} (in {{ duration // 60 }}:{{ "{:02}".format(duration % 60) }} minutes).</p>
4758
</body>

visitors.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import csv
2+
import io
3+
import urllib.parse
4+
import zipfile
5+
from logging import info
6+
7+
from urllib3 import PoolManager, Retry
8+
9+
10+
def get_number_of_visitors(language: str, http: PoolManager) -> int:
11+
params = urllib.parse.urlencode(
12+
{'filters': f'[["contains","event:page",["/{language}/"]]]', 'period': 'all'}
13+
)
14+
response = http.request(
15+
'GET',
16+
f'https://plausible.io/docs.python.org/export?{params}',
17+
retries=Retry(status_forcelist=(404, 500, 502)),
18+
)
19+
info(f'visitors {response.status=} ({language=})')
20+
with (
21+
zipfile.ZipFile(io.BytesIO(response.data), 'r') as z,
22+
z.open('visitors.csv') as csv_file,
23+
):
24+
csv_reader = csv.DictReader(io.TextIOWrapper(csv_file))
25+
return sum(int(row['visitors']) for row in csv_reader)
26+
27+
28+
if __name__ == '__main__':
29+
print(get_number_of_visitors('pl', PoolManager()))

0 commit comments

Comments
 (0)