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 a (3) JSON, TSV, and
12 ###############################################################################
15 #1 Load up the list of article names
17 #2 Repeatedly call the API with that list of names
19 #3 Save results as a TSV
33 parser = argparse.ArgumentParser(description='Call the views API repeatedly.')
34 parser.add_argument('-o', '--output_folder', help='Where to save output', default="../data/", type=str)
35 parser.add_argument('-i', '--article_file', help='File listing article names', default="../resources/articles.txt", type=str)
36 parser.add_argument('-d', '--query_date', help='Date if not yesterday, in YYYYMMDD format please.', type=str)
37 args = parser.parse_args()
46 outputPath = args.output_folder
47 articleFile = args.article_file
50 queryDate = args.query_date
52 yesterday = datetime.datetime.today() - datetime.timedelta(days=1)
53 queryDate = yesterday.strftime("%Y%m%d")
55 queryDate = queryDate + "00" #requires specifying hours
59 with open(articleFile, 'r') as infileHandle:
60 #theInfile = csv.reader(infileHandle, quotechar='"')
61 theInfile = csv.reader(infileHandle)
62 next(theInfile) #skip header
63 for currentLine in theInfile:
64 articleList.append(currentLine)
66 j_Out = outputPath + "dailyviews" + queryDate + ".json"
67 with open(j_Out, 'w') as outfile:
70 i = 0 #iterator to deal with end of file
75 url= "https://wikimedia.org/api/rest_v1/metrics/pageviews/per-article/en.wikipedia/all-access/all-agents/"
76 url= url + a + "/daily/" + queryDate + "/" + queryDate #for now, single date at a time
79 response = requests.get(url)
83 j=json.loads(response.content)
84 with open(j_Out, 'a') as j_outfile:
85 json.dump(j, j_outfile)
86 if i < len(articleList):
87 j_outfile.write(",\n")
92 #with open(outputPath + "dailyviews" + queryDate + ".tsv", 'a') as t_outfile:
93 # dw = csv.DictWriter(t_outfile, sorted(j[0].keys()), delimiter='\t')
100 with open(j_Out, 'a') as j_outfile:
103 #read the json back in and make a feather file?
107 if __name__ == "__main__":