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:
JavaScript
x
34
34
1
async def newvideo(): #система оповещений ютуб
2
with open("youtubeuser.json", "r") as f:
3
data=json.load(f)
4
5
print(" All systems online. Searching new vids...")
6
7
for youtube_channel in data:
8
9
channel = f"https://www.youtube.com/channel/{youtube_channel}"
10
11
html = requests.get(channel+"/videos").text
12
13
try:
14
latest_video_url = f"https://www.youtube.com/watch?v=" + re.search('(?<="videoId":").*?(?=")', html).group() #!Разобраться в смысле этого кода
15
16
except:
17
continue
18
#это типо как if else, только круче!
19
20
if not str(data[youtube_channel]["latest_video_url"]) == latest_video_url:
21
22
data[str [youtube_channel]['latest_video_url'] == latest_video_url
23
24
with open("youtubeuser.json", "w") as f:
25
json.dump(data, f)
26
27
discord_channel_id = data[str(youtube_channel)]['notifying_discord_channel']
28
discord_channel = bot.get_channel(int(discord_channel_id))
29
30
msg = f"Хэй! На канале {data[str(youtube_channel)]['channel_name']} происходит какой-то движ! Наши системы уловили что-то интересное. Бегом смотреть! n"
31
"{latest_video_url}"
32
33
await discord_channel.send(msg)```
34
Advertisement
Answer
The actual error is on the line before the with
statement:
JavaScript
1
2
1
data[str [youtube_channel]['latest_video_url'] == latest_video_url
2
This has unbalanced brackets, and generally doesn’t make any sense. Fix that error and the with
statement will work just fine.