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 ###############################################################################
26 parser = argparse.ArgumentParser(description='Call the views API repeatedly.')
27 parser.add_argument('-o', '--output_folder', help='Where to save output', default="../data/", type=str)
28 parser.add_argument('-i', '--article_file', help='File listing article names', default="../resources/articles.txt", type=str)
29 parser.add_argument('-d', '--query_date', help='Date if not yesterday, in YYYYMMDD format please.', type=str)
30 args = parser.parse_args()
39 outputPath = args.output_folder
40 articleFile = args.article_file
43 queryDate = args.query_date
45 yesterday = datetime.datetime.today() - datetime.timedelta(days=1)
46 queryDate = yesterday.strftime("%Y%m%d")
48 queryDate = queryDate + "00" #requires specifying hours
52 #1 Load up the list of article names
54 with open(articleFile, 'r') as infileHandle:
55 #theInfile = csv.reader(infileHandle, quotechar='"')
56 theInfile = csv.reader(infileHandle)
57 next(theInfile) #skip header
58 for currentLine in theInfile:
59 articleList.append(currentLine)
61 j_Out = outputPath + "dailyviews" + queryDate + ".json"
62 t_Out = outputPath + "dailyviews" + queryDate + ".tsv"
66 i = 0 #iterator to deal with end of file
68 #2 Repeatedly call the API with that list of names
73 url= "https://wikimedia.org/api/rest_v1/metrics/pageviews/per-article/en.wikipedia/all-access/all-agents/"
74 url= url + a + "/daily/" + queryDate + "/" + queryDate #for now, single date at a time
75 response = requests.get(url)
77 jd = json.loads(response.content)
78 j.append(jd["items"][0])
81 #3 Save results as a JSON and TSV
83 #all data in j now, make json file
84 with open(j_Out, 'w') as j_outfile:
85 json.dump(j, j_outfile, indent=2)
87 with open(t_Out, 'w') as t_outfile:
88 dw = csv.DictWriter(t_outfile, sorted(j[0].keys()), delimiter='\t')
93 f_Out = outputPath + "dailyviews" + queryDate + ".feather"
94 #read the json back in and make a feather file?
97 if __name__ == "__main__":