i’ve come across this error. Found on the Internet based on discord.py code to create YouTube alerts on the server. When starting the bot, the system encounters a syntax error and points to the “with” operator. Previously, this operator has already been used in the code and did not give errors. Please help those who know. Screenshot: https://imgur.com/L3yroea Code:
async def newvideo(): #система оповещений ютуб
with open("youtubeuser.json", "r") as f:
data=json.load(f)
print(" All systems online. Searching new vids...")
for youtube_channel in data:
channel = f"https://www.youtube.com/channel/{youtube_channel}"
html = requests.get(channel+"/videos").text
try:
latest_video_url = f"https://www.youtube.com/watch?v=" + re.search('(?<="videoId":").*?(?=")', html).group() #!Разобраться в смысле этого кода
except:
continue
#это типо как if else, только круче!
if not str(data[youtube_channel]["latest_video_url"]) == latest_video_url:
data[str [youtube_channel]['latest_video_url'] == latest_video_url
with open("youtubeuser.json", "w") as f:
json.dump(data, f)
discord_channel_id = data[str(youtube_channel)]['notifying_discord_channel']
discord_channel = bot.get_channel(int(discord_channel_id))
msg = f"Хэй! На канале {data[str(youtube_channel)]['channel_name']} происходит какой-то движ! Наши системы уловили что-то интересное. Бегом смотреть! n"
"{latest_video_url}"
await discord_channel.send(msg)```
Advertisement
Answer
The actual error is on the line before the with statement:
data[str [youtube_channel]['latest_video_url'] == latest_video_url
This has unbalanced brackets, and generally doesn’t make any sense. Fix that error and the with statement will work just fine.