Protoball:Count Game Tab Sources.py

From Protoball
Jump to navigation Jump to search
def count_region_sources(region_text):
	regions = region_text.split("\n")
	from urllib import request
	from bs4 import BeautifulSoup
	import re
	ref_item = re.compile("\(\d+\)")
	total_sources = 0
	for region in regions:
		r = request.urlopen("http://protoball.org/Game_Tab:" + region.replace(" ", "_"))
		t = r.read()
		soup = BeautifulSoup(t)
		tables = soup.select("table.nice")
		rows = []
		for table in tables:
			rows.extend(table.select("tr")[1:])
		sources = 0
		for row in rows:
			source_cell = row.find_all("td")[-1]
			text = source_cell.get_text()
			sources += len(ref_item.findall(text))
		print(region, ": ", sources)
		total_sources += sources
	print("Total Sources", ": ", total_sources)