-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
executable file
·66 lines (56 loc) · 2.02 KB
/
main.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
#!/usr/bin/env python3
import pickle
import requests
import json
from urllib import request
from urllib.error import HTTPError
from json import loads
import urllib.request
WEBHOOK_URL = 'YOUR_WEBHOOK_URL' # Discord webhook url
PATH = 'YOUR_PATH' #Path where you stock video id for doesnt spam discord
PLAYLISTID= 'PLAYLIST_ID' # Youtube playlist ID
TOKEN = 'YOUTUBE API TOKEN' #TOKEN API
try:
id = pickle.load(open("{}/lastvideo".format(PATH), "rb"))
except (OSError, IOError) as e:
foo = 3
pickle.dump(foo, open("{}/lastvideo".format(PATH), "wb"))
API_ENDPOINT = 'https://www.googleapis.com/youtube/v3/playlistItems?part=snippet&playlistId={0}&key={1}&maxResults=1'.format(PLAYLISTID,TOKEN)
#api call here
r = requests.get(url = API_ENDPOINT)
TITLE = loads(r.text)['items'][0]['snippet']['title'] # title video
VIDEO = loads(r.text)['items'][0]['snippet']['resourceId']['videoId'] # id video
URL = "https://i.ytimg.com/vi/{}/mqdefault.jpg".format(VIDEO) # thumbnails
DATE = loads(r.text)['items'][0]['snippet']['publishedAt'] # date
# La payload
payload = {
'username':"bot username",
'content': "content message",
'avatar_url':"bot avatar",
'embeds': [
{
'title': TITLE,
'description': 'description',
'url': 'https://www.youtube.com/watch?v={}'.format(VIDEO),
"color": 16711680,
'author': {'name': 'author'},
'timestamp': DATE,
"image": {"url": URL},
},
]
}
# header parameters
headers = {
'Content-Type': 'application/json',
'user-agent': 'Mozilla/5.0 (X11; U; Linux i686) Gecko/20071127 Firefox/2.0.0.11'
}
# request discord
req = request.Request(url=WEBHOOK_URL,
data=json.dumps(payload).encode('utf-8'),
headers=headers,
method='POST')
if VIDEO:
if VIDEO != id :
response = request.urlopen(req)
with open('{}/lastvideo'.format(PATH), 'wb') as f:
pickle.dump(VIDEO, f)