aboutsummaryrefslogtreecommitdiff
path: root/backend/hist_data/gen_pop_data.py
blob: 8eaa142269276b386bbee7797261947543c60b98 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#!/usr/bin/python3

"""
Adds Wikipedia page view info to the database as popularity values.
"""

import os, sqlite3

PAGEVIEWS_DB = os.path.join('enwiki', 'pageview_data.db')
DB_FILE = 'data.db'

def genData(pageviewsDb: str, dbFile: str) -> None:
	dbCon = sqlite3.connect(dbFile)
	dbCur = dbCon.cursor()
	#
	print('Getting event data')
	titleToId: dict[str, int] = {}
	for eventId, title in dbCur.execute('SELECT id, title FROM events'):
		titleToId[title] = eventId
	#
	print('Getting view counts')
	pdbCon = sqlite3.connect(pageviewsDb)
	pdbCur = pdbCon.cursor()
	titleToViews: dict[str, int] = {}
	iterNum = 0
	for title, views in pdbCur.execute('SELECT title, views from views'):
		iterNum += 1
		if iterNum % 1e6 == 0:
			print(f'At iteration {iterNum}')
		#
		if title not in titleToId:
			continue
		titleToViews[title] = views
	pdbCon.close()
	#
	print(f'Result: {len(titleToViews)} out of {len(titleToId)}')
	dbCur.execute('CREATE TABLE pop (id INT PRIMARY KEY, pop INT)')
	dbCur.execute('CREATE INDEX pop_idx ON pop(pop)')
	for title, views in titleToViews.items():
		dbCur.execute('INSERT INTO pop VALUES (?, ?)', (titleToId[title], views))
	#
	dbCon.commit()
	dbCon.close()

if __name__ == '__main__':
	import argparse
	parser = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter)
	args = parser.parse_args()
	#
	genData(PAGEVIEWS_DB, DB_FILE)