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 ###############################################################################
19 from requests import Request
20 from csv import DictWriter
26 parser = argparse.ArgumentParser(description='Call the views API to collect Wikipedia revision data.')
27 parser.add_argument('-o', '--output_folder', help='Where to save output', default="wikipedia/data", type=str)
28 parser.add_argument('-i', '--article_file', help='File listing article names', default="wikipedia/resources/enwp_wikiproject_covid19_articles.txt", type=str)
29 parser.add_argument('-d', '--query_date', help='Date if not yesterday, in YYYYMMDD format.', type=str)
30 parser.add_argument('-L', '--logging_level', help='Logging level. Options are debug, info, warning, error, critical. Default: info.', default='info', type=str),
31 parser.add_argument('-W', '--logging_destination', help='Logging destination file. (default: standard error)', type=str),
32 args = parser.parse_args()
38 output_path = args.output_folder
39 article_filename = args.article_file
42 query_date = args.query_date
44 yesterday = datetime.datetime.today() - datetime.timedelta(days=1)
45 query_date = yesterday.strftime("%Y%m%d")
48 loglevel_mapping = { 'debug' : logging.DEBUG,
49 'info' : logging.INFO,
50 'warning' : logging.WARNING,
51 'error' : logging.ERROR,
52 'critical' : logging.CRITICAL }
54 if args.logging_level in loglevel_mapping:
55 loglevel = loglevel_mapping[args.logging_level]
57 print("Choose a valid log level: debug, info, warning, error, or critical")
61 if args.logging_destination:
62 logging.basicConfig(filename=args.logging_destination, filemode='a', level=loglevel)
64 logging.basicConfig(level=loglevel)
66 export_git_hash = subprocess.check_output(['git', 'rev-parse', 'HEAD']).decode().strip()
67 export_git_short_hash = subprocess.check_output(['git', 'rev-parse', '--short', 'HEAD']).decode().strip()
68 export_time = str(datetime.datetime.now())
70 logging.info(f"Starting run at {export_time}")
71 logging.info(f"Last commit: {export_git_hash}")
73 json_output_filename = os.path.join(output_path, f"digobs_covid19-wikipedia-enwiki_revisions-{query_date}.json")
74 tsv_output_filename = os.path.join(output_path, f"digobs_covid19-wikipedia-enwiki_revisions-{query_date}.tsv")
76 api_session = api.Session("https://en.wikipedia.org/w/api.php")
78 # list of properties from the API we want to gather (basically all of
79 # them supported by mediawik-utilities)
81 rv_props = {'revid' : 'ids',
82 'timestamp' : 'timestamp',
87 'contentmodel' : 'contentmodel',
89 'comment' : 'comment',
90 'content' : 'content' }
92 exclude_from_tsv = ['tags', 'comment', 'content']
94 # load the list of articles
95 with open(article_filename, 'r') as infile:
96 article_list = [art.strip() for art in list(infile)]
98 def get_revisions_for_page(title):
99 return api_session.revisions.query(properties=rv_props.values(),
103 tsv_fields = ['title', 'pageid', 'namespace']
104 tsv_fields = tsv_fields + list(rv_props.keys())
106 # drop fields that we identified for exclusion
107 tsv_fields = [e for e in tsv_fields if e not in exclude_from_tsv]
109 # add special export fields
110 tsv_fields = tsv_fields + ['url', 'export_timestamp', 'export_commit']
112 export_info = { 'git_commit' : export_git_hash,
113 'timestamp' : export_time }
115 with open(json_output_filename, 'w') as json_output, \
116 open(tsv_output_filename, 'w') as tsv_output:
118 tsv_writer = DictWriter(tsv_output, fieldnames=tsv_fields, delimiter="\t")
119 tsv_writer.writeheader()
121 for article in article_list:
122 logging.info(f"pulling revisiosn for: {article}")
123 for rev in get_revisions_for_page(article):
124 logging.debug(f"processing raw revision: {rev}")
126 # add export metadata
127 rev['exported'] = export_info
129 # save the json version of the code
130 print(json.dumps(rev), file=json_output)
132 # handle missing data
133 if "sha1" not in rev:
136 # add page title information
137 rev['title'] = rev['page']['title']
138 rev['pageid'] = rev['page']['pageid']
139 rev['namespace'] = rev['page']['ns']
142 rev['url'] = Request('GET', 'https://en.wikipedia.org/w/index.php',
143 params={'title' : rev['title'].replace(" ", "_"),
144 'oldid' : rev['revid']}).prepare().url
146 rev['export_timestamp'] = export_time
147 rev['export_commit'] = export_git_short_hash
149 tsv_writer.writerow({k: rev[k] for k in tsv_fields})
152 if __name__ == "__main__":