-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtwitch-get-video-attribute.py
executable file
·128 lines (103 loc) · 3.6 KB
/
twitch-get-video-attribute.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
#!/usr/bin/env python3
import os
from argparse import ArgumentParser
from sys import stdin, stderr, stdout
from twitch.constants import Twitch
from util.auth_header_provider import AuthHeaderProvider
from util.contents import Contents
def main():
try:
args = parse_args()
user_id = cmdline_or_stdin(args.user_id)
search_string = cmdline_or_stdin(args.search_string)
attribute_names = args.name
videos = matching_videos_of(user_id, search_string, attribute_names)
if args.list_attributes:
list_attributes_of(videos[0])
else:
print_attributes(videos, attribute_names)
except ValueError as error:
stderr.write(str(error) + os.linesep)
exit(1)
def cmdline_or_stdin(arg):
return arg if arg else stdin.readline().strip()
def parse_args():
parser = ArgumentParser(
description="Search channel's past broadcast by name and any other attribute provided by the '-n' option"
)
parser.add_argument('user_id', nargs='?')
parser.add_argument('search_string', nargs='?')
parser.add_argument(
'-n',
'--name',
metavar='NAME',
help="""
print video attribute by it\'s NAME.
Specify multiple times for a list. (defaults to "id, title")
""",
action='append',
default=[]
)
parser.add_argument(
'-l',
'--list-attributes',
help='show the list of attribute names',
action='store_true',
default=False
)
args = parser.parse_args()
args.name = args.name if len(args.name) != 0 else ['id', 'title']
return args
def matching_videos_of(user_id, search_string, attribute_names):
token = search_string.strip().lower()
attributes = set(attribute_names)
def matches(video):
return True in [
token in video[attribute].strip().lower()
for attribute in
attributes
]
videos = videos_of(user_id)
matched_videos = list(filter(matches, videos))
if len(matched_videos) == 0:
raise ValueError('No matching videos found!')
return matched_videos
def videos_of(user_id):
auth_header = AuthHeaderProvider.authenticate()
def fetch_videos(cursor):
params = {'user_id': user_id, 'first': 100}
if cursor:
params['after'] = cursor
response = Contents.json(
Twitch.videos_url,
params=params,
headers=auth_header,
onerror=lambda _: raise_error('Failed to get the videos list!')
)
video_entries = response['data']
pagination = response['pagination']
cursor = pagination['cursor'] if 'cursor' in pagination else None
return video_entries, cursor
videos, next_page = fetch_videos(None)
while len(videos) > 0:
for video in videos:
yield video
if next_page is not None:
videos, next_page = fetch_videos(next_page)
else:
videos = []
def raise_error(message):
raise ValueError(message)
def list_attributes_of(video):
for attribute in sorted(video.keys()):
stdout.write(attribute + os.linesep)
def print_attributes(videos, attribute_names):
for video in videos:
attribute_values = map(lambda a: get_attribute(video, a), attribute_names)
stdout.write('|'.join(attribute_values) + os.linesep)
def get_attribute(video, attribute_name):
if attribute_name not in video:
raise ValueError('Video has no attribute "{}"'.format(attribute_name))
return video[attribute_name]
if __name__ == '__main__':
main()