import requests, bs4
import re
import unicodedata
import os

PARENT_LINK ='http://www.livesinabox.com/friends/'


def get_elems(res):
	scripts_page = bs4.BeautifulSoup(res.text, "lxml")
	elems = scripts_page.select('li')
	return elems

def get_file_name(elem):
	episode_name = elem.getText()
	colon_index = episode_name.index(":")
	
	season_episode = episode_name[len("Episode "):colon_index].strip()
	try:
		season = int(season_episode[:-2])
		episode = int(season_episode[-2:])
		filename = f'S{season:02d}E{episode:02d}'
	except ValueError:
		return
	return filename

def get_episode_link(elem):
	link_regex = re.compile(r'(href=\".*\")>')
	m = link_regex.search(str(elem))
	x = m.group().index('">')
	episode_link = PARENT_LINK+m.group()[6:x]
	return episode_link

def get_episode_script(episode_link):
	episode_page = requests.get(episode_link)
	episode_soup = bs4.BeautifulSoup(episode_page.text, "lxml")
	episode_script = episode_soup.select('p')
	return episode_script

if not os.path.isdir('scripts'):
	os.mkdir('scripts')
final_file = os.path.join('scripts', 'texts.txt')

def write_episode_script(episode_script, final_file):
	f = open(final_file, 'a+')
	for line in episode_script:
		try:
			pattern = re.compile(r'Written by\: .*|\{.*|Transcribed by\: .*')
			if not(bool(pattern.match(line.getText()))):
				f.write(line.getText().replace('\n',' ')+'\n')
		except UnicodeEncodeError:
			x = unicodedata.normalize('NFKD', line.getText()+'\n').encode('ascii','ignore')
			f.write(x.decode("utf-8").strip())
	f.close()

num_seasons = 10

for i in range(num_seasons):
	res = requests.get(f'{PARENT_LINK}season{i+1}.shtml')
	elems = get_elems(res)
	for elem in elems:
		filename = get_file_name(elem)
		
		episode_link = get_episode_link(elem)
		episode_script = get_episode_script(episode_link)
			
		try:
			write_episode_script(episode_script, final_file)  # There is an episode without any number after season 7 episode 23. Hence this error-handling
			print(f'{filename} Downloaded!')
		except TypeError:
			continue
