from PyQt5.QtCore import QUrl
from PyQt5.QtWebEngineWidgets import QWebEngineView, QWebEngineProfile, QWebEnginePage, QWebEngineSettings
from PyQt5.QtWidgets import QApplication
from selenium import webdriver
from bs4 import BeautifulSoup as bs
import time

keyword = input("what>")
url = 'https://www.youtube.com/results?search_query={}'.format(keyword)

# 옵션 생성
options = webdriver.ChromeOptions()
# 창 숨기는 옵션 추가
options.add_argument("headless")

driver = webdriver.Chrome('C:\chromedriver_win32\chromedriver.exe', options=options)
driver.get(url)
soup = bs(driver.page_source, 'html.parser')
driver.close()

video_url = soup.select('a#video-title')
view = soup.select('a#video-title')

url_list = []

for i in video_url:
    url_list.append('{}{}'.format('https://www.youtube.com', i.get('href')))


for j in range(len(url_list)):
    url_list[j] = url_list[j].replace("https://www.youtube.com/watch?v=", "")

if __name__ == '__main__':
    import sys
    app = QApplication(sys.argv)
    webview = QWebEngineView()
    profile = QWebEngineProfile("my_profile", webview)
    profile.defaultProfile().setPersistentCookiesPolicy(QWebEngineProfile.ForcePersistentCookies)
    webpage = QWebEnginePage(profile, webview)
    webpage.settings().setAttribute(QWebEngineSettings.PlaybackRequiresUserGesture, False)
    webview.setPage(webpage)
    webview.load(QUrl("https://www.youtube.com/embed/" + url_list[0] + "?autoplay=1"))
    webview.show()
    sys.exit(app.exec_())