-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathyoutubeplay3.py
More file actions
95 lines (76 loc) · 2.87 KB
/
youtubeplay3.py
File metadata and controls
95 lines (76 loc) · 2.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
import streamlink
import subprocess
import time
import os
from selenium import webdriver
from bs4 import BeautifulSoup
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.action_chains import ActionChains
import yt_dlp
# Configuring Chrome options
chrome_options = Options()
chrome_options.add_argument("--headless")
chrome_options.add_argument("--disable-gpu")
# Instanciando o driver do Chrome
driver = webdriver.Chrome(options=chrome_options)
# URL da página desejada
url_youtube = "https://www.youtube.com/channel/UCYfdidRxbB8Qhf0Nx7ioOYw"
# Abrir a página desejada
driver.get(url_youtube)
# Aguardar alguns segundos para carregar todo o conteúdo da página
time.sleep(5)
# Scroll to the bottom of the page using ActionChains
while True:
try:
# Find the last video on the page
last_video = driver.find_element_by_xpath("//a[@id='video-title'][last()]")
# Scroll to the last video
actions = ActionChains(driver)
actions.move_to_element(last_video).perform()
time.sleep(1)
except:
break
# Get the page source again after scrolling to the bottom
html_content = driver.page_source
time.sleep(5)
# Find the links and titles of the videos found
try:
soup = BeautifulSoup(html_content, "html.parser")
videos = soup.find_all("a", id="video-title", class_="yt-simple-endpoint style-scope ytd-grid-video-renderer")
links = ["https://www.youtube.com" + video.get("href") for video in videos]
titles = [video.get("title") for video in videos]
except Exception as e:
print(f"Erro: {e}")
finally:
# Close the driver
driver.quit()
# Instalando streamlink
subprocess.run(['pip', 'install', '--user', '--upgrade', 'yt dlp'])
subprocess.run(['pip', 'install', 'pytube'])
time.sleep(5)
from pytube import YouTube
# Define as opções para o youtube-dl
ydl_opts = {
'format': 'best', # Obtém a melhor qualidade
'write_all_thumbnails': False, # Não faz download das thumbnails
'skip_download': True, # Não faz download do vídeo
}
# Get the playlist and write to file
try:
with open('./YOUTUBEPLAY3.m3u', 'w', encoding='utf-8') as f:
f.write("#EXTM3U\n")
for i, link in enumerate(links):
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
info = ydl.extract_info(link, download=False)
if 'url' not in info:
print(f"Erro ao gravar informações do vídeo {link}: 'url'")
continue
url = info['url']
thumbnail_url = info['thumbnail']
description = info.get('description', '')[:10]
title = info.get('title', '')
f.write(f"#EXTINF:-1 group-title=\"YOUTUBE3\" tvg-logo=\"{thumbnail_url}\",{title} - {description}...\n")
f.write(f"{url}\n")
f.write("\n")
except Exception as e:
print(f"Erro ao criar o arquivo .m3u8: {e}")