import os, json, glob, logging

ARCH = "archives/"
EXP = "selection/"
sel = os.path.join(EXP, "tm-selection.js")

def find(li, url):

	d = os.path.join(ARCH, li)

	if not os.path.isdir(d):
		logging.warning("Invalid archive path: " + d)
		return None

	dir_files = [f for f in glob.glob(os.path.join(d, "*.json"))]

	for f in dir_files:
		with open(f) as fp:
			dj = json.load(fp)
			for t in dj['threads']:
				if t['url'] == url:
					return t

	return None

def construct():

	dump = {}

	with open(sel) as f:
		d = json.load(f)

	for k, v in d.items():
		dump[k] = []
		for i in v:
			m = find(i['list'], i['url'])
			if m is not None:
				m['list'] = i['list']
				dump[k].append(m)

	fout = os.path.join(EXP, "tm-selection-dump.js")

	with open(fout, 'w+', encoding='utf-8') as f:
		json.dump(dump, f, ensure_ascii=False, indent=4)

if __name__ == "__main__":

	construct()



