#!/usr/bin/env python

import glob
import os
import re
from bs4 import BeautifulSoup

with open("/".join([os.environ["HOME"],".novelrc"])) as f:
	s = f.readlines()

def file2url(fn):
	yy = os.path.basename(fn)
	yy = re.sub(r'^h\.','https://',yy)
	yy = re.sub(r'-','/',yy)
	yy = re.sub(r'episodes.+$','',yy)
	yy = re.sub(r'/1/$','/',yy)
	return(yy)

s.sort()
s99 = {}
for x in s:
	y = re.sub(r'[\r\n\t]','',x)
	s = y.split("/")
	#print(s)
	if(s[2] == "ncode.syosetu.com"):
		#nv = "".join([s[0],"//",s[2],"/",s[3],"/"])
		nv = "".join([s[2],"-",s[3]])
	else:
		#nv = "".join([s[0],"//",s[2],"/",s[3],"/",s[4],"/"])
		nv = "".join([s[2],"-",s[3],"-",s[4]])
	s99[nv] = "on"

for x in s99:
	#print(x)
	#print("#..データを探しています。")
	ddir = "*"+x+"*"
	ddir = "/".join([os.environ["HOME"],".noveldir",ddir])
	#print(ddir)
	for f in glob.glob(ddir):
		ff = os.path.basename(f)
		#print(ff)
		print(file2url(f),flush=True,end=": ")
		data = ""
		with open(f) as f2:
			data = "".join(f2.readlines())
		soup = BeautifulSoup(data, "html.parser")
		print(soup.select("title")[0].text)
		break
	#break
