-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
89 lines (74 loc) · 2.68 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#!/usr/bin/env python3
import pickle
import requests
import json
import time
import urllib.request
from urllib import request
from urllib.error import HTTPError
from json import loads
WEBHOOK_URL = 'YOUR_WEBHOOK_URL' # Discord webhook url
PATH = 'YOUR_PATH' #Path where you stock stream id for doesnt spam discord
try:
id = pickle.load(open("{}/twitch".format(PATH), "rb"))
except (OSError, IOError) as e:
foo = 3
pickle.dump(foo, open("{}/twitch".format(PATH), "wb"))
API_ENDPOINT = 'https://api.twitch.tv/helix/streams?user_login=<YOUR TWITCH USERNAME>'
#Create app https://dev.twitch.tv/console/apps
Client_ID = 'CLIENT_ID'
#Get twitch token https://id.twitch.tv/oauth2/authorize?client_id=CLIENT_APP_ID&redirect_uri=URI_APP&response_type=token
TOKEN = 'Bearer YOUR_TOKEN'
#data to be sent to api
head = {
"Authorization": TOKEN,
'Client-ID' : Client_ID
}
#api call here
r = requests.get(url = API_ENDPOINT, headers = head)
GAME_ENDPOINT = 'https://api.twitch.tv/helix/games?id={0}'.format(loads(r.text)['data'][0]['game_id'])
g = requests.get(url = GAME_ENDPOINT, headers = head)
# La payload
payload = {
'username':"Botname",
'content': "message",
'avatar_url':"avatar url",
'embeds': [
{
'title': loads(r.text)['data'][0]['title'], # Le titre de la carte
'description': 'description', # Le corps de la carte
'url': 'https://www.twitch.tv/mraurontv', # Si vous voulez faire un lien
"color": 6570404,
'author': {'name': 'author'}, # Pourquoi pas mettre des auteurs ?
'timestamp': loads(r.text)['data'][0]['started_at'],
"image": {"url": 'https://static-cdn.jtvnw.net/previews-ttv/live_user_{}-1280x720.jpg?rnd={}'.format(loads(r.text)['data'][0]['user_login'],int(time.time()))},
"fields": [
{
"name": "Game",
"value": loads(g.text)['data'][0]['name'],
"inline": True,
},
{
"name": "Viewers",
"value": loads(r.text)["data"][0]['viewer_count'],
"inline" : True,
}
]
},
]
}
# Les paramètres d'en-tête de la requête
headers = {
'Content-Type': 'application/json',
'user-agent': 'Mozilla/5.0 (X11; U; Linux i686) Gecko/20071127 Firefox/2.0.0.11'
}
# Enfin on construit notre requête
req = request.Request(url=WEBHOOK_URL,
data=json.dumps(payload).encode('utf-8'),
headers=headers,
method='POST')
if loads(r.text)['data'][0]['user_id']:
if loads(r.text)['data'][0]['id'] != id:
response = request.urlopen(req)
with open('{}/twitch'.format(PATH), 'wb') as f:
pickle.dump(loads(r.text)['data'][0]['id'], f)