forked from dunderrrrrr/youtube-telegram-bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathyt-telegram-bot.py
43 lines (35 loc) · 1.06 KB
/
yt-telegram-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
import os
import requests
from settings import pid, apikey, apiurl, watchurl
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
from db import vids, engine
from tbot import channel_post
Session = sessionmaker(bind=engine)
session = Session()
def vidindb():
vids = []
for vid in session.query(vids):
vids.append(vid.v_id)
return(vids)
def dbinsert(title, id, date):
vid = vids(title, id, date)
session.add(vid)
session.commit()
def get_videos(url):
resp = requests.get(url)
data = resp.json()
return(data['items'])
def telegram_notify(content, v_date):
channel_post(content, v_date)
if __name__ == '__main__':
videos = get_videos(apiurl)
vidsindb = vidindb()
for video in videos:
v_title = video['snippet']['title']
v_id = video['snippet']['resourceId']['videoId']
v_date = video['snippet']['publishedAt']
if v_id not in vidsindb:
dbinsert(v_title, v_id, v_date)
content = watchurl + v_id
telegram_notify(content, v_date)