-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
63 lines (50 loc) · 1.98 KB
/
app.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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import re
from time import sleep
from fs import get_config
from youtube import youtube
from datetime import datetime
from telegram import telegram_send_message
SCOPES = ["https://www.googleapis.com/auth/youtube.readonly"]
CONFIG = get_config('config/config.json')
CREDENTIAL_FILE_NAME = f"config/{CONFIG['youtube_api_service_account_credentials_file_name']}"
TELEGRAM_API_KEY = CONFIG['telegram_api_key']
TELEGRAM_CHANNEL_ID = CONFIG['channel_id']
VIDEO_CATEGORY_ID = CONFIG['videoCategoryId']
REGION_CODE = CONFIG['regionCode']
MAX_RESULTS = CONFIG['maxResults']
SLEEP_TIMEOUT = CONFIG['sleep_timeout']
def get_new_videos():
videos_dict = youtube(SCOPES, CREDENTIAL_FILE_NAME,
VIDEO_CATEGORY_ID, REGION_CODE, MAX_RESULTS)
return videos_dict['items']
def format_message(videos_list):
videos = []
for video in videos_list:
title = re.sub(r'[^a-zA-Z0-9 \n\.]', '', video['snippet']['title'])
views = format(int(video['statistics']['viewCount']), ",")
likes = format(int(video['statistics']['likeCount']), ",")
dislikes = format(int(video['statistics']['dislikeCount']), ",")
url = f"https://youtube.com/watch?v={video['id']}"
videos.append(
f"\n\n*{title}*\n\n*Views: {views}*\n*Likes: {likes}*\n*Dislikes: {dislikes}*\n\n[Watch on YouTube.com]({url})")
return videos
def send_videos():
while True:
try:
videos = get_new_videos()
if len(videos) > 1:
video_formated = format_message(videos)
for video in video_formated:
telegram_send_message(
TELEGRAM_API_KEY, TELEGRAM_CHANNEL_ID, video)
sleep(SLEEP_TIMEOUT)
break
except Exception:
sleep(SLEEP_TIMEOUT)
def main():
send_videos()
while True:
if datetime.strftime(datetime.utcnow(), '%H:%M:%S') == '04:00:00':
send_videos()
if __name__ == "__main__":
main()