import time
from selenium import webdriver
from selenium.webdriver.common.keys import Keys



class YoutubeSearchScraper():
	SEARCH_URL = 'https://www.youtube.com/results'
	WAIT_LOAD = 3 # Time to wait for more content to load

	def __init__(self, drivername="firefox"):
		drivers = {'firefox': webdriver.Firefox, 'chrome': webdriver.Chrome, 'opera': webdriver.Opera,
					'ie': webdriver.Ie, 'android': webdriver.Android, 'edge': webdriver.Edge }
		driver = drivers.get(drivername, drivers.get('firefox'))
		self.driver = driver()

	def extract(self, match_list):
		results = []
		for i in  match_list:
			url = i.get_attribute('href')
			title = i.text
			if url and title:
				if 'watch?' in url:
					result = {'url': url, 'title': title}
					results.append(result)
		return results

	def parse_top_results(self, search_term):
		"""
			Returns a list of dictionaries containing the url and titles of top videos with the
			keys 'url'  and 'title' respectively.
		"""
		param = '?search_query=' + search_term
		self.driver.get(self.SEARCH_URL+param)
		results = self.extract(self.driver.find_elements_by_class_name('yt-simple-endpoint'))
		return results

	def parse_all_results(self, search_term):
		"""
			Yields dictionaries containing the url and titles of videos with the keys 
			'url'  and 'title' respectively.
		"""
		param = '?search_query=' + search_term
		driver = self.driver
		driver.get(self.SEARCH_URL+param)
		get_scroll_height_script = "return (document.getElementsByTagName('html')[0]).scrollHeight"
		end = Keys.END
		found_class = 0
		classes = self.driver.find_elements_by_class_name('yt-simple-endpoint')
		found_class = len(classes)
		yield from self.extract(classes)
		page = self.driver.find_element_by_tag_name('html')

		while True:
			old_height = driver.execute_script(get_scroll_height_script)
			print(old_height)
			page.send_keys(end)
			time.sleep(self.WAIT_LOAD)
			new_height = driver.execute_script(get_scroll_height_script)
			print(new_height)
			if old_height == new_height:
				break

			classes = self.driver.find_elements_by_class_name('yt-simple-endpoint')[found_class:]
			found_class += len(classes)
				yield from self.extract(classes)
