3 ###############################################################################
5 # This script assumes the presence of the COVID-19 repo.
7 # It (1) reads in the article list and then (2) calls the Wikimedia API to
8 # fetch view information for each article. Output is to (3) JSON and TSV.
10 ###############################################################################
22 from csv import DictWriter
27 parser = argparse.ArgumentParser(description='Call the views API to collect Wikipedia view data.')
28 parser.add_argument('-o', '--output_folder', help='Where to save output', default="wikipedia/data", type=str)
29 parser.add_argument('-i', '--article_file', help='File listing article names', default="wikipedia/resources/enwp_wikiproject_covid19_articles.txt", type=str)
30 parser.add_argument('-d', '--query_date', help='Date if not yesterday, in YYYYMMDD format.', type=str)
31 parser.add_argument('-L', '--logging_level', help='Logging level. Options are debug, info, warning, error, critical. Default: info.', default='info', type=str),
32 parser.add_argument('-W', '--logging_destination', help='Logging destination file. (default: standard error)', type=str),
33 args = parser.parse_args()
40 outputPath = args.output_folder
41 articleFile = args.article_file
45 queryDate = args.query_date
47 yesterday = datetime.datetime.today() - datetime.timedelta(days=1)
48 queryDate = yesterday.strftime("%Y%m%d")
51 loglevel_mapping = { 'debug' : logging.DEBUG,
52 'info' : logging.INFO,
53 'warning' : logging.WARNING,
54 'error' : logging.ERROR,
55 'critical' : logging.CRITICAL }
57 if args.logging_level in loglevel_mapping:
58 loglevel = loglevel_mapping[args.logging_level]
60 print("Choose a valid log level: debug, info, warning, error, or critical")
64 if args.logging_destination:
65 logging.basicConfig(filename=args.logging_destination, filemode='a', level=loglevel)
67 logging.basicConfig(level=loglevel)
69 export_git_hash = subprocess.check_output(['git', 'rev-parse', 'HEAD']).decode().strip()
70 export_git_short_hash = subprocess.check_output(['git', 'rev-parse', '--short', 'HEAD']).decode().strip()
71 export_time = str(datetime.datetime.now())
73 logging.info(f"Starting run at {export_time}")
74 logging.info(f"Last commit: {export_git_hash}")
76 #1 Load up the list of article names
77 j_outfilename = os.path.join(outputPath, f"digobs_covid19-wikipedia-enwiki_dailyviews-{queryDate}.json")
78 t_outfilename = os.path.join(outputPath, f"digobs_covid19-wikipedia-enwiki_dailyviews-{queryDate}.tsv")
80 with open(articleFile, 'r') as infile:
81 articleList = list(infile)
83 success = 0 #for logging how many work/fail
86 #3 Save results as a JSON and TSV
87 with open(j_outfilename, 'w') as j_outfile, \
88 open(t_outfilename, 'w') as t_outfile:
90 #2 Repeatedly call the API with that list of names
92 a = a.strip("\"\n") #destringify
93 url= f"https://wikimedia.org/api/rest_v1/metrics/pageviews/per-article/en.wikipedia/all-access/all-agents/{a}/daily/{queryDate}00/{queryDate}00"
95 response = requests.get(url)
97 jd = response.json()["items"][0]
100 failure = failure + 1
101 logging.warning(f"Failure: {response.status_code} from {url}")
103 # start writing the CSV File if it doesn't exist yet
107 dw = DictWriter(t_outfile, sorted(jd.keys()), delimiter='\t')
110 logging.debug(f"printing data: {jd}")
112 # write out the line of the json file
113 print(json.dumps(jd), file=j_outfile)
115 # write out of the csv file
118 # f_Out = outputPath + "dailyviews" + queryDate + ".feather"
119 # read the json back in and make a feather file?
120 logging.debug(f"Run complete at {datetime.datetime.now()}")
121 logging.info(f"Processed {success} successful URLs and {failure} failures.")
124 if __name__ == "__main__":