-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtwitch-rec.py
288 lines (252 loc) · 11.2 KB
/
twitch-rec.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
#!/usr/bin/env python3
import datetime
import getopt
import http.client
import json
import logging
import os
import requests
import subprocess
import sys
import textwrap
import time
import urllib
class Colorlog():
logging.basicConfig(
format='[\033[1;32m%(asctime)s\033[1;0m] [%(levelname)s] %(message)s', level=logging.INFO)
logging.addLevelName(
logging.ERROR, "\033[1;31m%s\033[1;0m" % logging.getLevelName(logging.ERROR))
logging.addLevelName(
logging.WARNING, "\033[1;33m%s\033[1;0m" % logging.getLevelName(logging.WARNING))
logging.addLevelName(
logging.INFO, "\033[1;32m%s\033[1;0m" % logging.getLevelName(logging.INFO))
class Config():
def __init__(self):
self.json_streamer = ""
self.json_quality = ""
self.json_tw_client_id = ""
self.json_tw_oauth_token = ""
self.json_po_token = ""
self.json_po_user = ""
self.cli_streamer = ""
self.cli_quality = ""
self.cli_tw_client_id = ""
self.cli_tw_oauth_token = ""
self.cli_po_token = ""
self.cli_po_user = ""
def write_json(self, data, jsonfile):
with open(jsonfile, "w+") as self.cf:
json.dump(data, self.cf, indent=4, separators=(',', ': '))
def read_json(self):
with open(config_file, "r") as self.cf:
return json.load(self.cf)
def cli(self):
# Create config if it doesn't exist
if not os.path.isfile(config_file):
self.create_config("", "", "", "", "", "")
# Load current config
self.data = self.read_json()
self.json_streamer = self.data["rec"]["streamer"]
self.json_quality = self.data["rec"]["quality"]
self.json_tw_client_id = self.data["twitch"]["client_id"]
self.json_tw_oauth_token = self.data["twitch"]["oauth"]
self.json_po_token = self.data["pushover"]["token"]
self.json_po_user = self.data["pushover"]["user"]
# Read cli args
self.opts = getopt.getopt(sys.argv[1:], 'hs:q:c:o:t:u:', [
'streamer=', 'quality='])[0]
for self.opt, self.arg in self.opts:
if self.opt in ('-h', '--help'):
print(textwrap.dedent("""
Usage: python3 twitch_rec.py [OPTION] [ARG]
-h, --help Shows instructions about this script
-s, --streamer Specifies the twitch streamer to record
-q, --quality Specifies the quality of the livestream
-c, --client-id Specifies the twitch client id. Create one here: https://dev.twitch.tv/dashboard/apps
-o, --oauth Specifies the twitch oauth token. Create one with "streamlink --twitch-oauth-authenticate"
-t, --pu-token Specifies the pushover app token. Create one at pushover.net
-u, --pu-user Specifies the pushover user key. Create one at pushover.net
Running the script without cli arguments will ask you for all the settings and will create a json config file for you.
If you already have one, it will read the configs from there.
"""))
sys.exit(0)
elif self.opt in ('-s', '--streamer'):
self.cli_streamer = self.arg
elif self.opt in ('-q', '--quality'):
self.cli_quality = self.arg
elif self.opt in ('-c', '--client-id'):
self.cli_tw_client_id = self.arg
elif self.opt in ('-o', '--oauth'):
self.cli_tw_oauth_token = self.arg
elif self.opt in ('-t', '--pu-token'):
self.cli_po_token = self.arg
elif self.opt in ('-u', '--pu-user'):
self.cli_po_user = self.arg
# Set streamer
if not self.cli_streamer:
if not self.json_streamer:
self.streamer = input(
"Which streamer do you want to record?: ")
else:
self.streamer = self.json_streamer
else:
self.streamer = self.cli_streamer
# Set quality
if not self.cli_quality:
if not self.json_quality:
self.quality = input(
"In which quality do you want to record? (If you dont know, just type \"best\"): ")
else:
self.quality = self.json_quality
else:
self.quality = self.cli_quality
# Set twitch client id
if not self.cli_tw_client_id:
if not self.json_tw_client_id:
self.tw_client_id = input("What is your twitch client id?: ")
else:
self.tw_client_id = self.json_tw_client_id
else:
self.tw_client_id = self.cli_tw_client_id
# Set twitch oauth token
if not self.cli_tw_oauth_token:
if not self.json_tw_oauth_token:
self.tw_oauth_token = input(
"What is your twitch oauth token?: ")
else:
self.tw_oauth_token = self.json_tw_oauth_token
else:
self.tw_oauth_token = self.cli_tw_oauth_token
# Set pushover token
if not self.cli_po_token:
if not self.json_po_token:
self.po_token = input("What is your pushover token?: ")
else:
self.po_token = self.json_po_token
else:
self.po_token = self.cli_po_token
# Set pushover user
if not self.cli_po_user:
if not self.json_po_user:
self.po_user = input("What is your pushover user?: ")
else:
self.po_user = self.json_po_user
else:
self.po_user = self.cli_po_user
return self.streamer, self.quality, self.tw_client_id, self.tw_oauth_token, self.po_token, self.po_user
def create_config(self, streamer, quality, tw_client_id, tw_oauth_token, po_token, po_user):
data = {
"rec": {
"streamer": streamer,
"quality": quality
},
"twitch": {
"client_id": tw_client_id,
"oauth": tw_oauth_token
},
"pushover": {
"token": po_token,
"user": po_user
}}
self.write_json(data, config_file)
class TwitchRecorder:
def __init__(self):
self.refresh = 15.0
self.root_path = os.path.dirname(os.path.realpath(__file__))
# Read final config
self.data = Config().read_json()
self.streamer = self.data["rec"]["streamer"]
self.quality = self.data["rec"]["quality"]
self.tw_client_id = self.data["twitch"]["client_id"]
self.tw_oauth_token = self.data["twitch"]["oauth"]
self.po_token = self.data["pushover"]["token"]
self.po_user = self.data["pushover"]["user"]
def run(self):
# Set recording path
self.rec_path = os.path.join(self.root_path, "recs")
if(os.path.isdir(self.rec_path) is False):
os.makedirs(self.rec_path)
# Check interval
if(self.refresh < 15):
logging.warning(
"Interval should not be lower than 15 seconds.")
logging.warning("Interval will be set to 15 seconds.")
self.refresh = 15
# Start loop
logging.info("Checking for " + self.streamer + " every " + str(self.refresh) +
" seconds. Recording in " + self.quality + " quality.")
self.loopcheck()
def check_user(self):
# Check if user online, 0: online, 1: offline, 2: not found, 3: error
self.url = "https://api.twitch.tv/helix/streams?user_login=" + self.streamer
self.info = None
self.status = 3
try:
self.r = requests.get(
self.url, headers={"Client-ID": self.tw_client_id}, timeout=15)
self.r.raise_for_status()
self.info = self.r.json()
try:
if self.info["data"][0]["type"] == "live":
self.status = 0
except IndexError:
self.status = 1
except requests.exceptions.RequestException as e:
if e.response:
if e.response.reason == "Not Found" or e.response.reason == "Unprocessable Entity":
self.status = 2
return self.status
def loopcheck(self):
while True:
# Status messages
status = self.check_user()
if status is 3:
logging.error("Unexpected error. Will try again in 5 minutes.")
time.sleep(300)
elif status is 2:
logging.error("streamer not found. Invalid streamer or typo.")
time.sleep(self.refresh)
elif status is 1:
logging.info(self.streamer + " currently offline, checking again in " +
str(self.refresh) + " seconds.")
time.sleep(self.refresh)
elif status is 0:
logging.info(self.streamer + " is live. Recording ...")
self.filename = self.streamer + "_" + \
datetime.datetime.now().strftime("%Y-%m-%d_%Hh%Mm%Ss") + ".ts"
# File name
self.rec_filename = os.path.join(self.rec_path, self.filename)
# Start streamlink process
subprocess.call(["streamlink", "--twitch-oauth-token",
self.tw_oauth_token,
"twitch.tv/" + self.streamer,
self.quality, "-o", self.rec_filename])
# Send Pushover
if self.po_token and self.po_user:
logging.info("Sending pushover message ...")
conn = http.client.HTTPSConnection("api.pushover.net:443")
conn.request("POST", "/1/messages.json",
urllib.parse.urlencode({"token": self.po_token,
"user": self.po_user, "html": "1",
"message": "Recorded: " + self.filename, }),
{"Content-type": "application/x-www-form-urlencoded"})
conn.getresponse()
# Wait for twitch api to refresh
logging.info(
"Waiting 5 minutes for the twitch api to refresh ... (yes, the api is that slow)")
time.sleep(300)
logging.info("Going back to checking ...")
if __name__ == "__main__":
try:
# Define where the config file is located
config_file = os.path.join(os.path.dirname(
os.path.abspath(__file__)), "config.json")
# Update config with cli args and ask user for missing configs
streamer, quality, tw_client_id, tw_oauth_token, po_token, po_user = Config().cli()
# Write that config to a file
Config().create_config(streamer, quality,
tw_client_id, tw_oauth_token, po_token, po_user)
# Start the recoding process
TwitchRecorder().run()
except KeyboardInterrupt:
logging.error("Quit by user.")