-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcomment_bot.py
330 lines (259 loc) · 13.1 KB
/
comment_bot.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
import os
import json
import random
import logging
import asyncio
import psycopg2
import datetime
import googleapiclient.discovery
from telegram import Bot
from dotenv import load_dotenv
from googleapiclient.errors import HttpError
from google.oauth2.credentials import Credentials
from google.auth.transport.requests import Request
from google_auth_oauthlib.flow import InstalledAppFlow
# ✅ Логове за дебъгване
logging.basicConfig(level=logging.INFO, format="%(asctime)s - %(levelname)s - %(message)s")
logger = logging.getLogger(__name__)
# ✅ Зареждаме променливите от .env файла
load_dotenv()
DATABASE_URL = os.getenv("DATABASE_URL")
GOOGLE_CREDENTIALS = os.getenv("GOOGLE_CREDENTIALS")
REFRESH_TOKEN = os.getenv("YOUTUBE_REFRESH_TOKEN")
load_dotenv()
TELEGRAM_TOKEN = os.getenv("TELEGRAM_TOKEN")
load_dotenv()
TELEGRAM_CHAT_ID = os.getenv("TELEGRAM_CHAT_ID")
if not DATABASE_URL:
raise ValueError("❌ Грешка: DATABASE_URL не е зададен!")
if not GOOGLE_CREDENTIALS:
raise ValueError("❌ Грешка: GOOGLE_CREDENTIALS не е зададен!")
if not TELEGRAM_TOKEN:
raise ValueError("❌ Грешка: TELEGRAM_TOKEN не е зададен!")
if not TELEGRAM_CHAT_ID:
raise ValueError("❌ Грешка: TELEGRAM_CHAT_ID не е зададен!")
SCOPES = ["https://www.googleapis.com/auth/youtube.force-ssl"]
async def send_telegram_summary(commented_videos):
"""📩 Изпраща обобщение на потребителя в Telegram след коментиране на видеа."""
if not TELEGRAM_CHAT_ID or not TELEGRAM_TOKEN:
logger.warning("⚠️ TELEGRAM_CHAT_ID или TELEGRAM_TOKEN не са зададени! Пропускаме известието.")
return
try:
bot = Bot(token=TELEGRAM_TOKEN) # ✅ Поправено, използваме TELEGRAM_TOKEN!
message = "📢 **Дневен отчет за коментари**\n\n"
message += f"📅 Дата: {datetime.datetime.now().strftime('%Y-%m-%d')}\n"
message += f"💬 Общо коментирани видеа: {len(commented_videos)}\n\n"
for index, (video_url, comment_text) in enumerate(commented_videos, start=1):
message += (
f"🎬 **Видео {index}:** [{video_url}]({video_url})\n"
f"💬 **Коментар:** {comment_text}\n"
f"────────────────────────\n"
)
# ✅ Изпращаме съобщението към потребителя в Telegram
await bot.send_message(chat_id=TELEGRAM_CHAT_ID, text=message, parse_mode="Markdown",
disable_web_page_preview=True)
logger.info("📩 Изпратено известие в Telegram!")
except Exception as e:
logger.error(f"❌ Грешка при изпращане на известие в Telegram: {e}")
def get_authenticated_service():
"""Свързване с YouTube API чрез OAuth 2.0 (без нужда от ръчно влизане)"""
creds = None
credentials_json = json.loads(GOOGLE_CREDENTIALS)
if REFRESH_TOKEN:
creds = Credentials.from_authorized_user_info({
"token": None,
"refresh_token": REFRESH_TOKEN,
"token_uri": "https://oauth2.googleapis.com/token",
"client_id": credentials_json["installed"]["client_id"],
"client_secret": credentials_json["installed"]["client_secret"]
})
creds.refresh(Request())
else:
flow = InstalledAppFlow.from_client_config(credentials_json, SCOPES)
creds = flow.run_console()
logger.info("🔑 Нов OAuth токен генериран. Запази refresh_token за бъдеща употреба!")
return googleapiclient.discovery.build("youtube", "v3", credentials=creds)
# ✅ Свързваме се с YouTube API чрез OAuth
youtube = get_authenticated_service()
def connect_db():
"""Свързване към PostgreSQL"""
return psycopg2.connect(DATABASE_URL, sslmode='require')
def fetch_latest_video_for_channel(channel_url):
"""Взема най-новото видео от даден YouTube канал (channel_url е YouTube Channel ID)"""
try:
logger.info(f"🔍 Извличаме последното видео от канал: {channel_url}...")
if not channel_url.startswith("UC"):
logger.error(f"❌ Грешен Channel ID: {channel_url}. Очакваме ID да започва с 'UC'.")
return None, None
request = youtube.search().list(
part="id",
channelId=channel_url, # Подаваме YouTube Channel ID
order="date",
maxResults=1
)
response = request.execute()
logger.info(f"📩 Отговор от YouTube API: {response}")
if "items" in response and len(response["items"]) > 0:
video_data = response["items"][0]
if "videoId" in video_data["id"]:
video_id = video_data["id"]["videoId"]
video_url = f"https://www.youtube.com/watch?v={video_id}"
logger.info(f"✅ Намерено видео: {video_url}")
return video_id, video_url
else:
logger.warning("⚠️ Няма videoId в отговора.")
return None, None
else:
logger.warning(f"⚠️ Няма намерени видеа за канал {channel_url}.")
return None, None
except Exception as e:
logger.error(f"❌ Грешка при извличане на видео за канал {channel_url}: {e}")
return None, None
def add_video_to_db(video_id, video_url, channel_id, user_id):
"""Добавя ново видео в базата, ако още не съществува"""
conn = connect_db()
cursor = conn.cursor()
cursor.execute("SELECT COUNT(*) FROM videos WHERE video_id = %s", (video_id,))
exists = cursor.fetchone()[0]
if exists == 0:
cursor.execute("""
INSERT INTO videos (channel_id, video_url, video_id, user_id)
VALUES (%s, %s, %s, %s)
""", (channel_id, video_url, video_id, user_id))
conn.commit()
logger.info(f"✅ Видео добавено в базата: {video_url}")
return True # ✅ Видео е ново
cursor.close()
conn.close()
return False # 🚫 Видео вече съществува
def get_channels_from_db():
"""Взима всички канали и потребителите им от базата"""
conn = connect_db()
cursor = conn.cursor()
cursor.execute("SELECT channel_url, user_id FROM channels") # ✅ Взимаме само нужните колони
channels = cursor.fetchall()
cursor.close()
conn.close()
return [(row[0], row[1]) for row in channels] # ✅ Уверяваме се, че връщаме само два елемента на ред
def get_latest_videos():
"""Взема новите видеа от базата, които още не са коментирани"""
conn = connect_db()
cursor = conn.cursor()
cursor.execute("""
SELECT videos.video_id, videos.video_url, videos.channel_id, channels.user_id
FROM videos
JOIN channels ON videos.channel_id = channels.id
WHERE videos.video_id NOT IN (SELECT video_id FROM posted_comments)
""")
videos = cursor.fetchall()
cursor.close()
conn.close()
return videos
def has_already_commented(video_id, user_id):
"""Проверява дали вече сме коментирали дадено видео"""
conn = connect_db()
cursor = conn.cursor()
cursor.execute("SELECT COUNT(*) FROM posted_comments WHERE video_id = %s AND user_id = %s", (video_id, user_id))
count = cursor.fetchone()[0]
cursor.close()
conn.close()
return count > 0
def post_comment(youtube, video_id, comment_text, user_id):
"""Публикува коментар в YouTube и го запазва в базата."""
try:
request = youtube.commentThreads().insert(
part="snippet",
body={
"snippet": {
"videoId": video_id,
"topLevelComment": {
"snippet": {
"textOriginal": comment_text
}
}
}
}
)
request.execute()
# ✅ Взимаме заглавието на видеото и името на канала
video_title, channel_name = get_video_details(video_id)
# ✅ Запазваме коментара в базата
save_posted_comment(video_id, user_id, comment_text, video_title, channel_name)
return True # ✅ Успешно публикуване
except HttpError as e:
error_message = e.content.decode("utf-8") if hasattr(e, "content") else str(e)
logger.error(f"❌ Грешка при публикуване: {error_message}")
return False
def get_video_details(video_id):
"""Взима заглавието на видеото от YouTube API"""
try:
request = youtube.videos().list(
part="snippet",
id=video_id
)
response = request.execute()
if "items" in response and len(response["items"]) > 0:
video_title = response["items"][0]["snippet"]["title"]
channel_name = response["items"][0]["snippet"]["channelTitle"]
return video_title, channel_name
else:
return "Неизвестно заглавие", "Неизвестен канал"
except Exception as e:
logger.error(f"❌ Грешка при взимане на заглавието: {e}")
return "Грешка при взимане на заглавие", "Грешка при взимане на канал"
def save_posted_comment(video_id, user_id, comment_text, video_title, channel_name):
"""Запазва коментара в `posted_comments`, за да не се публикува отново."""
conn = connect_db()
cursor = conn.cursor()
cursor.execute("""
INSERT INTO posted_comments (video_id, user_id, comment_text, video_title, channel_name, commented_at)
VALUES (%s, %s, %s, %s, %s, NOW())
""", (video_id, user_id, comment_text, video_title, channel_name))
conn.commit()
cursor.close()
conn.close()
logger.info(f"💾 Запазен коментар в базата за видео {video_title} ({video_id})")
def get_channel_id_from_db(channel_url):
"""Връща channel_id за даден channel_url"""
conn = connect_db()
cursor = conn.cursor()
cursor.execute("SELECT id FROM channels WHERE channel_url = %s", (channel_url,))
result = cursor.fetchone()
cursor.close()
conn.close()
return result[0] if result else None
def run_comment_bot():
"""Основна логика на бота - проверява нови видеа, коментира ги и изпраща отчет в Telegram"""
channels = get_channels_from_db()
commented_videos = [] # ✅ Събира коментираните видеа за дневното известие
for channel_url, user_id in channels:
logger.info(f"🔍 Проверяваме за нови видеа в канал {channel_url}...")
channel_id = get_channel_id_from_db(channel_url)
if not channel_id:
logger.warning(f"⚠️ Пропускаме {channel_url}, защото няма съответстващ channel_id.")
continue
video_id, video_url = fetch_latest_video_for_channel(channel_url)
if video_id:
is_new_video = add_video_to_db(video_id, video_url, channel_id, user_id)
if is_new_video:
comments = [
"Страхотно видео! 🔥",
"Браво, много добро съдържание! 👌",
"Този контент е супер полезен! 🚀",
"Топ! 🔥",
"👌👌👌",
"🔥🔥🔥",
"cool! 🚀",
"Продължавай в същия дух! 🙌",
" 🙌 🙌 🙌 ",
" Благодаря! 👌",
]
comment_text = random.choice(comments)
if post_comment(youtube, video_id, comment_text, user_id):
logger.info(f"✅ Коментар публикуван: {comment_text} на {video_url}")
commented_videos.append((video_url, comment_text)) # ✅ Записваме видео за известието
# ✅ Ако има коментирани видеа, изпращаме съобщение
if commented_videos:
asyncio.run(send_telegram_summary(commented_videos))
if __name__ == "__main__":
run_comment_bot()