File tree Expand file tree Collapse file tree 1 file changed +20
-10
lines changed Expand file tree Collapse file tree 1 file changed +20
-10
lines changed Original file line number Diff line number Diff line change 11import csv
22import io
3- import urllib
3+ import logging
4+ import urllib .parse
45import zipfile
56
67from requests import Session
@@ -10,12 +11,21 @@ def get_number_of_visitors(language: str, requests: Session) -> int:
1011 params = urllib .parse .urlencode (
1112 {'filters' : f'[["contains","event:page",["/{ language } /"]]]' , 'period' : 'all' }
1213 )
13- r = requests .get (
14- f'https://plausible.io/docs.python.org/export?{ params } ' , timeout = 20
15- )
16- with (
17- zipfile .ZipFile (io .BytesIO (r .content ), 'r' ) as z ,
18- z .open ('visitors.csv' ) as csv_file ,
19- ):
20- csv_reader = csv .DictReader (io .TextIOWrapper (csv_file ))
21- return sum (int (row ['visitors' ]) for row in csv_reader )
14+ for _ in range (2 ):
15+ r = requests .get (
16+ f'https://plausible.io/docs.python.org/export?{ params } ' , timeout = 40
17+ )
18+ try :
19+ with (
20+ zipfile .ZipFile (io .BytesIO (r .content ), 'r' ) as z ,
21+ z .open ('visitors.csv' ) as csv_file ,
22+ ):
23+ csv_reader = csv .DictReader (io .TextIOWrapper (csv_file ))
24+ return sum (int (row ['visitors' ]) for row in csv_reader )
25+ except zipfile .BadZipFile as bad_zip_file :
26+ logging .exception (
27+ f'Plausible responded with broken archive for { language } . Retrying.'
28+ )
29+ exception = bad_zip_file
30+ continue
31+ raise exception
You can’t perform that action at this time.
0 commit comments