Skip to content

Commit 29e9f16

Browse files
committed
Added repeating youtube parser
1 parent 257cdff commit 29e9f16

File tree

3 files changed

+60
-0
lines changed

3 files changed

+60
-0
lines changed

main.py

+41
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
from Config import Config
1111
from mats_counter import count_mats
12+
from youtube_parser import *
1213

1314
conf = Config('config.ini', ['telegram_token', 'destruction_timeout', 'database_filename'])
1415

@@ -28,6 +29,8 @@
2829

2930
bot_id = None
3031
last_top = None
32+
url_video_list_dima = None
33+
url_video_list_asado = None
3134

3235
#Todo:
3336
#ignore karmaspam from users
@@ -242,6 +245,40 @@ def on_msg(update, context):
242245
print(e)
243246

244247

248+
def callback_minute(context: CallbackContext):
249+
global url_video_list_dima
250+
global url_video_list_asado
251+
252+
new_video_list_dima = get_urls('https://www.youtube.com/feeds/videos.xml?channel_id=UC20M3T-H-Pv0FPOEfeQJtNQ')
253+
new_video_list_asado = get_urls('https://www.youtube.com/feeds/videos.xml?channel_id=UCfkPlh5dfjbw8hc1s-yJQdw')
254+
255+
# get new url list
256+
if url_video_list_dima is None:
257+
url_video_list_dima = new_video_list_dima
258+
return
259+
260+
if url_video_list_asado is None:
261+
url_video_list_asado = new_video_list_asado
262+
return
263+
264+
265+
# look for new videos
266+
new_videos_dima = get_new_urls(url_video_list_dima, new_video_list_dima)
267+
new_videos_asado = get_new_urls(url_video_list_asado, new_video_list_asado)
268+
269+
if len(new_videos_dima) > 0:
270+
url_video_list_dima = new_video_list_dima
271+
272+
for new_video in new_videos_dima:
273+
context.bot.send_message(chat_id='@rude_chat', text=new_video)
274+
275+
if len(new_videos_asado) > 0:
276+
url_video_list_asado = new_video_list_asado
277+
278+
for new_video in new_videos_asado:
279+
context.bot.send_message(chat_id='@rude_chat', text=new_video)
280+
281+
245282
def add_group(update, context):
246283
for member in update.message.new_chat_members:
247284
if not member.is_bot:
@@ -265,6 +302,10 @@ def main():
265302
dp.add_handler(CallbackQueryHandler(btn_clicked))
266303
dp.add_handler(MessageHandler(Filters.status_update.new_chat_members, add_group))
267304

305+
# new videos
306+
j = updater.job_queue
307+
job_minute = j.run_repeating(callback_minute, interval=60, first=0)
308+
268309
updater.start_polling()
269310
bot_id = updater.bot.id
270311
print("Bot is started.")

requirements.txt

+1
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
python-telegram-bot
2+
requests

youtube_parser.py

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import requests
2+
import re
3+
4+
def cut_url(content):
5+
text = re.findall(r'url="https:\/\/[\w.,@?^=%&:/~+#-]*', content)
6+
7+
#idk if this a good way
8+
return text[0].replace('url="', '').replace('?version=3', '').replace('/v/', '/watch?v=')
9+
10+
11+
def get_urls(url):
12+
resp = requests.get(url)
13+
urls = [cut_url(link) for link in resp.text.split('\n') if 'media:content' in link]
14+
return urls
15+
16+
17+
def get_new_urls(old_urls, new_urls):
18+
return list(set(new_urls)-set(old_urls))

0 commit comments

Comments
 (0)