#!/usr/bin/env python3
import re
import os
from bs4 import BeautifulSoup
with open('titles', 'r') as f:
html = f.read()
soup = BeautifulSoup(html)
titles = {}
for episode in soup.find_all('tr', attrs={'class': "vevent"}):
num = episode.find('td').text
title = episode.find('td', attrs={'class': 'summary'}).text
title = title.replace('"', '')
titles[int(num)] = [num.zfill(2), title]
reg = re.compile('.*S\d+E(\d+).*')
for i in os.listdir(os.getcwd()):
match = reg.match(i)
if match is not None:
episode = int(match.group(1))
path = os.path.join(os.getcwd(), i)
num, title = titles[episode]
ext = os.path.splitext(path)[-1]
filename = '{} - {}{}'.format(num, title, ext)
new_path = os.path.join(os.getcwd(), filename)
print(num, title)
os.rename(path, new_path)