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)
56 next(theInfile) #skip header
57 for currentLine in theInfile:
58 articleList.append(currentLine)
60 j_Out = outputPath + "dailyviews" + queryDate + ".json"
61 t_Out = outputPath + "dailyviews" + queryDate + ".tsv"
65 i = 0 #iterator to deal with end of file
67 #2 Repeatedly call the API with that list of names
72 url= "https://wikimedia.org/api/rest_v1/metrics/pageviews/per-article/en.wikipedia/all-access/all-agents/"
73 url= url + a + "/daily/" + queryDate + "/" + queryDate #for now, single date at a time
74 response = requests.get(url)
76 jd = json.loads(response.content)
77 j.append(jd["items"][0])
80 #3 Save results as a JSON and TSV
82 #all data in j now, make json file
83 with open(j_Out, 'w') as j_outfile:
84 json.dump(j, j_outfile, indent=2)
86 with open(t_Out, 'w') as t_outfile:
87 dw = csv.DictWriter(t_outfile, sorted(j[0].keys()), delimiter='\t')
92 f_Out = outputPath + "dailyviews" + queryDate + ".feather"
93 #read the json back in and make a feather file?
96 if __name__ == "__main__":