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_views/data", type=str)
29 parser.add_argument('-i', '--article_file', help='File listing article names', default="wikipedia_views/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")
50 queryDate = queryDate + "00" #requires specifying hours
53 loglevel_mapping = { 'debug' : logging.DEBUG,
54 'info' : logging.INFO,
55 'warning' : logging.WARNING,
56 'error' : logging.ERROR,
57 'critical' : logging.CRITICAL }
59 if args.logging_level in loglevel_mapping:
60 loglevel = loglevel_mapping[args.logging_level]
62 print("Choose a valid log level: debug, info, warning, error, or critical")
66 if args.logging_destination:
67 logging.basicConfig(filename=args.logging_destination, filemode='a', level=loglevel)
69 logging.basicConfig(level=loglevel)
71 export_git_hash = subprocess.check_output(['git', 'rev-parse', 'HEAD']).decode().strip()
72 export_git_short_hash = subprocess.check_output(['git', 'rev-parse', '--short', 'HEAD']).decode().strip()
73 export_time = str(datetime.datetime.now())
75 logging.info(f"Starting run at {export_time}")
76 logging.info(f"Last commit: {export_git_hash}")
78 #1 Load up the list of article names
79 j_outfilename = os.path.join(outputPath, f"digobs_covid19-wikipedia-enwiki_dailyviews-{queryDate}.json")
80 t_outfilename = os.path.join(outputPath, f"digobs_covid19-wikipedia-enwiki_dailyviews-{queryDate}.tsv")
82 with open(articleFile, 'r') as infile:
83 articleList = list(infile)
85 success = 0 #for logging how many work/fail
88 #3 Save results as a JSON and TSV
89 with open(j_outfilename, 'w') as j_outfile, \
90 open(t_outfilename, 'w') as t_outfile:
92 #2 Repeatedly call the API with that list of names
94 a = a.strip("\"\n") #destringify
95 url= f"https://wikimedia.org/api/rest_v1/metrics/pageviews/per-article/en.wikipedia/all-access/all-agents/{a}/daily/{queryDate}/{queryDate}"
97 response = requests.get(url)
99 jd = response.json()["items"][0]
100 success = success + 1
102 failure = failure + 1
103 logging.warning(f"Failure: {response.status_code} from {url}")
105 # start writing the CSV File if it doesn't exist yet
109 dw = DictWriter(t_outfile, sorted(jd.keys()), delimiter='\t')
112 logging.debug(f"printing data: {jd}")
114 # write out the line of the json file
115 print(json.dumps(jd), file=j_outfile)
117 # write out of the csv file
120 # f_Out = outputPath + "dailyviews" + queryDate + ".feather"
121 # read the json back in and make a feather file?
122 logging.debug(f"Run complete at {datetime.datetime.now()}")
123 logging.info(f"Processed {success} successful URLs and {failure} failures.")
126 if __name__ == "__main__":