upgrade
This commit is contained in:
parent
e2321915e0
commit
7552ac50c9
29
main.py
29
main.py
|
@ -4,10 +4,7 @@ import feedparser
|
||||||
import yt_dlp as youtube_dl
|
import yt_dlp as youtube_dl
|
||||||
from config import DISCORD_BOT_TOKEN, DISCORD_CHANNEL_ID, YOUTUBE_CHANNEL_IDS
|
from config import DISCORD_BOT_TOKEN, DISCORD_CHANNEL_ID, YOUTUBE_CHANNEL_IDS
|
||||||
import os
|
import os
|
||||||
|
import asyncio
|
||||||
import logging
|
|
||||||
|
|
||||||
logging.basicConfig(level=logging.INFO)
|
|
||||||
|
|
||||||
intents = discord.Intents.default()
|
intents = discord.Intents.default()
|
||||||
intents.messages = True
|
intents.messages = True
|
||||||
|
@ -18,11 +15,6 @@ bot = commands.Bot(command_prefix='!', intents=intents)
|
||||||
download_dir = 'downloads'
|
download_dir = 'downloads'
|
||||||
os.makedirs(download_dir, exist_ok=True)
|
os.makedirs(download_dir, exist_ok=True)
|
||||||
|
|
||||||
ydl_opts = {
|
|
||||||
'format': 'best',
|
|
||||||
'outtmpl': f'{download_dir}/%(title)s [%(id)s].%(ext)s',
|
|
||||||
}
|
|
||||||
|
|
||||||
downloaded_videos_file = 'downloaded_videos.txt'
|
downloaded_videos_file = 'downloaded_videos.txt'
|
||||||
|
|
||||||
def get_downloaded_videos():
|
def get_downloaded_videos():
|
||||||
|
@ -43,17 +35,31 @@ def get_latest_video_url(channel_id):
|
||||||
|
|
||||||
@tasks.loop(minutes=10)
|
@tasks.loop(minutes=10)
|
||||||
async def check_new_videos():
|
async def check_new_videos():
|
||||||
logging.info("Checking new videos...")
|
|
||||||
downloaded_videos = get_downloaded_videos()
|
downloaded_videos = get_downloaded_videos()
|
||||||
for channel_id in YOUTUBE_CHANNEL_IDS:
|
for channel_id in YOUTUBE_CHANNEL_IDS:
|
||||||
logging.info(f"Checking channel: {channel_id}")
|
print(f"Checking new videos for channel: {channel_id}")
|
||||||
|
try:
|
||||||
video_url, video_id = get_latest_video_url(channel_id)
|
video_url, video_id = get_latest_video_url(channel_id)
|
||||||
if video_url and video_id not in downloaded_videos:
|
if video_url and video_id not in downloaded_videos:
|
||||||
|
channel_download_dir = os.path.join(download_dir, channel_id)
|
||||||
|
os.makedirs(channel_download_dir, exist_ok=True)
|
||||||
|
|
||||||
|
ydl_opts = {
|
||||||
|
'format': 'best',
|
||||||
|
'outtmpl': f'{channel_download_dir}/%(title)s [%(id)s].%(ext)s',
|
||||||
|
}
|
||||||
|
|
||||||
with youtube_dl.YoutubeDL(ydl_opts) as ydl:
|
with youtube_dl.YoutubeDL(ydl_opts) as ydl:
|
||||||
ydl.download([video_url])
|
ydl.download([video_url])
|
||||||
add_downloaded_video(video_id)
|
add_downloaded_video(video_id)
|
||||||
channel = bot.get_channel(int(DISCORD_CHANNEL_ID))
|
channel = bot.get_channel(int(DISCORD_CHANNEL_ID))
|
||||||
await channel.send(f"@everyone New tard video dropped and has been archived: {video_url}")
|
await channel.send(f"@everyone New tard video dropped and has been archived: {video_url}")
|
||||||
|
print(f"Downloaded and notified for channel: {channel_id}")
|
||||||
|
else:
|
||||||
|
print(f"No new videos or already downloaded for channel: {channel_id}")
|
||||||
|
except Exception as e:
|
||||||
|
print(f"Error processing channel {channel_id}: {e}")
|
||||||
|
await asyncio.sleep(5)
|
||||||
|
|
||||||
@bot.event
|
@bot.event
|
||||||
async def on_ready():
|
async def on_ready():
|
||||||
|
@ -61,3 +67,4 @@ async def on_ready():
|
||||||
check_new_videos.start()
|
check_new_videos.start()
|
||||||
|
|
||||||
bot.run(DISCORD_BOT_TOKEN)
|
bot.run(DISCORD_BOT_TOKEN)
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue