-
Notifications
You must be signed in to change notification settings - Fork 0
/
youtubebot.py
36 lines (32 loc) · 1.51 KB
/
youtubebot.py
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
import os
import googleapiclient.discovery
import googleapiclient.errors
import time
api_key = ''
channel_id = ''
def get_latest_video_id(api_key, channel_id):
youtube = googleapiclient.discovery.build('youtube', 'v3', developerKey=api_key)
request = youtube.search().list(part='id', channelId=channel_id, maxResults=1, order='date')
response = request.execute()
video_id = response['items'][0]['id']['videoId']
return video_id
def check_for_new_video(api_key, channel_id):
latest_video_id = get_latest_video_id(api_key, channel_id)
if not os.path.isfile('latest_video_id.txt'):
with open('latest_video_id.txt', 'w') as file:
file.write(latest_video_id)
return False
with open('latest_video_id.txt', 'r') as file:
stored_video_id = file.read()
if latest_video_id != stored_video_id:
with open('latest_video_id.txt', 'w') as file:
file.write(latest_video_id)
return True
return False
while True:
if check_for_new_video(api_key, channel_id):
latest_video_id = get_latest_video_id(api_key, channel_id)
video_url = f'https://www.youtube.com/watch?v={latest_video_id}'
print(f'Новое видео: {video_url}')
# Здесь вы можете вставить код для отправки ссылки куда-либо (например, через email или мессенджер)
time.sleep(15) # Проверка каждую минуту