This is my function I made and keep in mind I'm still trying to perfect my craft at coding but I'm in a pinch. The bot properly sends the live notification when a streamer is live but when the next check happens it says they are offline when they are indeed still live and I can't figure out what I'm doing wrong anyone have any ideas to what I'm doing wrong?
async def check_youtube_live_status(): global current_youtube_status
youtube_channel_ids = ['UCqX-Censored', 'UCbt-Censored', 'UCaL1aZGD-Censored']
excluded_video_ids = ['RYmySdF-Censored', '7-J3f-Censored']
try:
for channel_id in youtube_channel_ids:
# Fetch channel title and icon using YouTube Data API
channel_info_url = f"https://www.googleapis.com/youtube/v3/channels?part=snippet&id={channel_id}&key={YOUTUBE_API_KEY}"
channel_info_response = requests.get(channel_info_url)
if channel_info_response.status_code == 200:
channel_info_data = channel_info_response.json()
if 'items' in channel_info_data and len(channel_info_data['items']) > 0:
channel_title = channel_info_data['items'][0]['snippet']['title']
channel_icon = channel_info_data['items'][0]['snippet']['thumbnails']['default']['url']
else:
channel_title = channel_id # Use channel ID as title if name not found
channel_icon = None
else:
channel_title = channel_id # Use channel ID as title in case of API error
channel_icon = None
url = f"https://www.youtube.com/channel/{channel_id}/live"
user_agent = UserAgent()
headers = {'User-Agent': user_agent.random}
response = requests.get(url, headers=headers)
if response.status_code == 200:
soup = BeautifulSoup(response.content, 'html.parser')
canonical_link = soup.find('link', {'rel': 'canonical'})
if canonical_link and 'href' in canonical_link.attrs:
canonical_url = canonical_link['href']
is_live = '/watch?v=' in canonical_url and all(video_id not in canonical_url for video_id in excluded_video_ids)
# Check if status changed from offline to live
if current_youtube_status.get(channel_id) != is_live:
current_youtube_status[channel_id] = is_live
channel = bot.get_channel(1187979891988254781) # Replace with your target Discord channel ID
if is_live:
if channel:
embed = discord.Embed(
title=f" {channel_title} is currently LIVE on YouTube!",
color=0x00FF00 # You can change the color as per your preference
)
if channel_icon:
embed.set_thumbnail(url=channel_icon) # Set channel icon as thumbnail
embed.add_field(
name="Watch here",
value=f"[Click Here]({canonical_url}) to watch on YouTube!",
inline=False
)
await channel.send(embed=embed)
else:
if channel:
embed = discord.Embed(
title=f" {channel_title} is currently offline on YouTube.",
color=0xFF0000 # You can change the color as per your preference
)
await channel.send(embed=embed)
else:
print("No canonical link found.")
else:
print("Failed to fetch the channel live page.")
except Exception as e:
print(f'Error checking YouTube live status: {e}')
So I tried looking for different solutions like networking issues or even API issues it doesn't seem to be the cause or reason than I thought of maybe its because its catching the live and not live status not right I've tried using another method of checking status but nothing I've tried hasn't worked.