-
Notifications
You must be signed in to change notification settings - Fork 0
/
ryck.py
executable file
·426 lines (332 loc) · 14.9 KB
/
ryck.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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
#!/usr/bin/env python3
################################################################################
###### # # ##### # #
# # # # # # # #
# # # # # # #
##### ##### ###### # # ### ##### #####
# # # # # #
# # # # # # #
# # # ##### # #
################################################################################
#
# ryck.py
# https://github.com/johanbluecreek/ryck
#
__version__ = "0.1.3"
#
################################################################################
################################################################################
################################################################################
### # # ###### ####### ###### ####### #####
# ## ## # # # # # # # # #
# # # # # # # # # # # # #
# # # # ###### # # ###### # #####
# # # # # # # # # #
# # # # # # # # # # #
### # # # ####### # # # #####
################################################################################
import requests
import json
import copy
import random
import subprocess
import sys
import os
import base64
from datetime import date
import time
from shutil import copyfile
import argparse as ap
################################################################################
####### # # # # ##### ####### ### ####### # # #####
# # # ## # # # # # # # ## # # #
# # # # # # # # # # # # # # #
##### # # # # # # # # # # # # # #####
# # # # # # # # # # # # # # #
# # # # ## # # # # # # # ## # #
# ##### # # ##### # ### ####### # # #####
################################################################################
def get_clientID():
client_id = 'YTFoZGY2Zm4xYmNpdW0wZHUxcXJ1aGx1OGd4N2I0'
client_id = base64.b64decode(client_id.encode()).decode()
return client_id
def get_headers():
client_id = get_clientID()
headers = {'Client-ID': client_id}
return headers
def construct_link(lang, game, limit):
base_link = 'https://api.twitch.tv/kraken/streams'
# start adding options
call_link = base_link + '?'
# language
# XXX: There is also a 'broadcaster_language', but 'language' seems accurate enough
call_link += 'language=' + lang + '&'
# game
call_link += 'game=' + game + '&'
# limit
call_link += 'limit=' + limit + '&'
# end the link
call_link += 'stream_type=live'
return call_link
def create_input(work_dir):
input_file = work_dir + "/input.conf"
if os.path.isfile(input_file):
backup_file = input_file + "-" + date.today().isoformat() + "-" + str(int(time.time()))
print("Ryck: Creating backup of old `input.conf`: %s" % backup_file)
copyfile(input_file, backup_file)
with open(input_file, 'w') as f:
f.write('> quit 0\n')
f.write('q quit 4\n')
f.write('R run "/bin/bash" "-c" "echo \\\"${path}\\\" >> ~/.ryck/remember"\n')
f.write('i show-text "${title}"\n')
f.write('Ctrl+o run "/bin/bash" "-c" "xdg-open \\\"${path}\\\""\n')
f.write('X quit 8')
def known_games():
games = {'',
'Anna',
'Casino',
'Counter-Strike: Global Offensive',
'Crossout',
'Dead by Daylight',
'Destiny 2',
'Dota 2',
'Escape From Tarkov',
'FIFA 18',
'Fallout: New Vegas',
'Fortnite',
'Frostpunk',
'God of War',
'Grand Theft Auto V',
'Hand Simulator',
'Hearthstone',
'IRL',
'Jikkyou Powerful Pro Yakyuu 2018',
'League of Legends',
'Magic: The Gathering',
'Murderous Pursuits',
'Music',
'Overwatch',
"PLAYERUNKNOWN'S BATTLEGROUNDS",
'Pillars of Eternity',
'Poker',
'Rust',
"Sherlock Holmes: The Devil's Daughter",
'StarCraft II',
'Terraria',
'Total War Saga: Thrones of Britannia',
'World of Tanks',
'World of Warcraft'
}
print("Ryck: These are a bunch of 'game's that have been used.")
print(" There are more than these, and you have to escape characters yourself for now.")
print(" Example: 'Pillars of Eternity' yeilds correct game if entered as 'pillars+of+eternity'.\n")
print(games)
def known_langs():
#XXX: there is no way of doing this properly. Russians appear to select broadcaster_language in the opposite way as swedish streamers do. Whatever you do, you will miss streams.
langs = {'English': 'en', 'Swedish': 'sv'}
print("Ryck: You can try which every you want, but these are the ones that has been known to work:\n")
print(langs)
################################################################################
# # ##### ####### # ##### #######
# # # # # # # # # #
# # # # # # # #
# # ##### ##### # # # #### #####
# # # # ####### # # #
# # # # # # # # # #
##### ##### ####### # # ##### #######
################################################################################
# # ###### # # #### ##### # #
## ## # ## ## # # # # # #
# ## # ##### # ## # # # # # #
# # # # # # # ##### #
# # # # # # # # # #
# # ###### # # #### # # #
def play_remember(work_dir, input_file, mpv_args):
remember_file = work_dir + "/remember"
memorised_streams = []
if play_memory:
if os.path.isfile(remember_file):
with open(remember_file, 'r') as f:
lines = f.readlines()
for line in lines:
memorised_streams.append(line)
for stream in memorised_streams:
p = subprocess.Popen(
[
'mpv',
stream,
'--input-conf=%s' % input_file,
'--title=\"%s\"' % stream
] + mpv_args
, shell=False)
p.communicate()
if p.returncode == 4:
sys.exit()
print("Ryck: All memorised links consumed. Exiting.")
sys.exit()
else:
print("Ryck: No `remember` file found. Exiting.")
sys.exit()
##### ###### ###### ## # # # #####
# # # # # # # # # #
# # ##### ##### # # # # # #
# # # # ###### # # # #
# # # # # # # # # #
##### ###### # # # #### ###### #
def play_default(work_dir, input_file, lang, game, maximum, minimum, exclude, only, sorting, mpv_args):
# Hard coded limit of what the twitch-api accepts
limit = 100
# Headers to auth with twitch-api
headers = get_headers()
# Create a list of the excluded streams
exclusion_file = work_dir + "/exclude"
excluded_streams = []
if os.path.isfile(exclusion_file):
with open(exclusion_file, 'r') as f:
lines = f.readlines()
for line in lines:
excluded_streams.append(line.strip('\n'))
# Fetch all the streams
streams = []
if maximum <= limit and maximum > 0:
# For a small number of streams, just get them all at once
call_link = construct_link(lang, game, str(maximum))
r = requests.get(call_link, headers=headers)
data = r.json()
streams += copy.copy(data['streams'])
elif maximum > limit:
# If larger than limit we have to make several calls
call_link = construct_link(lang, game, str(limit))
r = requests.get(call_link, headers=headers)
data = r.json()
streams += copy.copy(data['streams'])
while len(streams) <= maximum and len(data['streams']) == limit and data['streams'][-1]['viewers'] > minimum:
r = requests.get(data['_links']['next'], headers=headers)
data = r.json()
streams += copy.copy(data['streams'][:maximum-len(streams)])
else:
# Else, just fetch them all
call_link = construct_link(lang, game, str(limit))
r = requests.get(call_link, headers=headers)
data = r.json()
streams += copy.copy(data['streams'])
while len(data['streams']) == limit and data['streams'][-1]['viewers'] > minimum:
r = requests.get(data['_links']['next'], headers=headers)
data = r.json()
streams += copy.copy(data['streams'])
# Remove those with too low viewer count
if minimum > 0:
new_streams = []
for stream in streams:
if stream['viewers'] >= minimum:
new_streams.append(stream)
streams = new_streams
# Removed those containing excluded words
if not exclude == ['']:
new_streams = []
for stream in streams:
if not any(map(lambda x: x in stream['channel']['status'].lower(), exclude)):
new_streams.append(stream)
streams = new_streams
if not only == ['']:
new_streams = []
for stream in streams:
if all(map(lambda x: x in stream['channel']['status'].lower(), only)):
new_streams.append(stream)
streams = new_streams
# Then we proceed to sort the streams (popularity/viewers is default sorting)
if sorting == 'random':
random.shuffle(streams)
# Then we play
left = len(streams)
for stream in streams:
print("Ryck: Streams left: ", left)
left -= 1
if stream['channel']['url'] in excluded_streams:
print("Ryck: Stream has been excluded. Moving on.")
else:
stream_name = stream['channel']['name']
stream_status = stream['channel']['status']
stream_link = stream['channel']['url']
print('')
print('Ryck now playing: ' + stream_name)
print(' ' + stream_status)
print('')
p = subprocess.Popen(
[
'mpv',
stream_link,
'--input-conf=%s' % input_file,
'--title=\"%s\"' % stream_status
] + mpv_args
, shell=False)
p.communicate()
if p.returncode == 4:
sys.exit()
if p.returncode == 8:
print("Ryck: Excluding steam.")
with open(exclusion_file, 'a') as f:
f.write(stream_link + '\n')
################################################################################
# # # ### # #
## ## # # # ## #
# # # # # # # # # #
# # # # # # # # #
# # ####### # # # #
# # # # # # ##
# # # # ### # #
################################################################################
if __name__ == '__main__':
### Resolve arguments ###
parser = ap.ArgumentParser(usage='%(prog)s [options] [-- [mpv]]', description='Stream twitch streams instead of browsing them.')
# Optional arguemnts
parser.add_argument('--game', metavar='GAME', type=str, default='irl', help='Change the "game"-type twitch streams should be fetched from.')
parser.add_argument('--lang', metavar='LANGUAGE', type=str, default='en', help='Change the language the stream should be in.')
parser.add_argument('--max', metavar='MAX', type=int, default=0, help='Set how many streams should be fetched (0 or lower means all).')
parser.add_argument('--min', metavar='MIN', type=int, default=0, help='Set the minimal amount of views the streamer should have.')
parser.add_argument('--exclude', metavar='EXC', type=str, default="", help='Comma separated list of words to exclude from streamers "status".')
parser.add_argument('--only', metavar='ONLY', type=str, default="", help='Comma separated list of words that a streamer must have in "status".')
parser.add_argument('--sort', metavar='SORT', type=str, default='random', help='Sort streams to be played after "random" or anything else (which uses twitch default popularity sorting).')
parser.add_argument('--gen-input', action='store_true', help='Ryck will generate a new input.conf and backup the old.')
parser.add_argument('--print-game', action='store_true', help='Prints known game options.')
parser.add_argument('--print-lang', action='store_true', help='Prints known language options.')
parser.add_argument('--play-mem', action='store_true', help='Ryck will play the streams saved by user (`R` (`shift+r`)), available in `~/.ryck/remember`')
parser.add_argument('--version', action='version', version='%(prog)s {}'.format(__version__), help="Prints version number and exits.")
# Positional arguments
parser.add_argument('mpv', nargs=ap.REMAINDER, help='Arguments to pass to `mpv`.')
# Pass them to variables
args = parser.parse_args()
game = args.game
lang = args.lang
maximum = args.max
minimum = args.min
exclude = args.exclude.split(",")
only = args.only.split(",")
sorting = args.sort
if any(map(lambda x: "?" in x or "&" in x, [game, lang])):
print("Ryck: Are you trying to manipulate the twitch-api call? Stop that!")
sys.exit()
gen_input = args.gen_input
print_game = args.print_game
print_lang = args.print_lang
play_memory = args.play_mem
mpv_args = args.mpv[1:]
### Build a working env for ryck ###
work_dir = os.environ['HOME'] + "/.ryck"
if not os.path.isdir(work_dir):
os.mkdir(work_dir)
### Generate input file if non exist ###
input_file = work_dir + "/input.conf"
if not os.path.isfile(input_file):
create_input(work_dir)
### Start actually doing something ###
if gen_input:
create_input(work_dir)
if print_game:
known_games()
if print_lang:
known_langs()
if play_memory:
play_remember(work_dir, input_file, mpv_args)
if not play_memory and not gen_input and not print_game and not print_lang:
play_default(work_dir, input_file, lang, game, maximum, minimum, exclude, only, sorting, mpv_args)