-
Notifications
You must be signed in to change notification settings - Fork 1
/
utils.py
195 lines (178 loc) · 6.1 KB
/
utils.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
# Import general libraries
import sys
import json
import signal
import string
import requests
# global variable which sets if we should terminate
terminated_requested = False
def signal_handler(sig, frame):
global terminated_requested
terminated_requested = True
print('terminate requested!!!!!')
def setup_signal_handle():
signal.signal(signal.SIGINT, signal_handler)
def webvtt_time_string(seconds):
minutes = seconds / 60
seconds = seconds % 60
hours = int(minutes / 60)
minutes = int(minutes % 60)
return '%i:%02i:%06.3f' % (hours, minutes, seconds)
def get_valid_filename(filename):
valid_chars = "-_%s%s" % (string.ascii_letters, string.digits)
filename = filename.lower().replace(' ', '_')
return ''.join(c for c in filename if c in valid_chars)
def get_vod_moments(void_id):
# get response
try:
gql_response = get_vod_graphql_info(void_id)
gql_obj = json.loads(gql_response)
moments = []
for moment in gql_obj["data"]["video"]["moments"]["edges"]:
data = {
"duration": int(moment["node"]["durationMilliseconds"] / 1000.0),
"offset": int(moment["node"]["positionMilliseconds"] / 1000.0),
}
if "details" in moment["node"] and "game" in moment["node"]["details"]:
data["id"] = moment["node"]["details"]["game"]["id"]
data["name"] = moment["node"]["details"]["game"]["displayName"]
else:
data["id"] = "-1"
data["name"] = "Unknown"
if "type" in moment["node"]:
data["type"] = moment["node"]["type"]
moments.append(data)
return moments
except Exception as e:
print(e)
return []
def get_vod_moments_from_twitcharchive_string(data):
# get response
try:
gql_obj = json.loads(data)
moments = []
for moment in gql_obj:
data = {
"duration": int(moment["node"]["durationMilliseconds"] / 1000.0),
"offset": int(moment["node"]["positionMilliseconds"] / 1000.0),
}
if "details" in moment["node"] and "game" in moment["node"]["details"]:
data["id"] = moment["node"]["details"]["game"]["id"]
data["name"] = moment["node"]["details"]["game"]["displayName"]
else:
data["id"] = "-1"
data["name"] = "Unknown"
if "type" in moment["node"]:
data["type"] = moment["node"]["type"]
moments.append(data)
return moments
except Exception as e:
print(e)
return []
def get_vod_graphql_info(vod_id):
# seems to just be a default client id
# https://dev.twitch.tv/docs/authentication
client_id = "kimne78kx3ncx6brgo4mv6wki5h1ko"
# auth = "xxxxxx"
# formulate the graphql query format
# https://graphiql-online.com/graphiql
# https://api.twitch.tv/gql
query = '''
query Query($videoId: ID) {
video(id: $videoId) {
moments(momentRequestType: VIDEO_CHAPTER_MARKERS, types: GAME_CHANGE) {
pageInfo {
hasNextPage
}
edges {
node {
details {
... on GameChangeMomentDetails {
game {
id
displayName
name
}
}
}
positionMilliseconds
durationMilliseconds
type
}
}
}
}
}
'''
variables = {'videoId': vod_id}
url = 'https://gql.twitch.tv/gql'
response = requests.post(
url,
json={'query': query, 'variables': variables},
# headers={"Client-ID": client_id, "Authorization": "OAuth "+auth}
headers={"Client-ID": client_id}
)
return response.text
def get_clip_data(clip_id):
# get response
try:
gql_response = get_clip_graphql_info(clip_id)
gql_obj = json.loads(gql_response)
if gql_obj["data"]["clip"]["videoOffsetSeconds"] == None:
print("\t- clip's VOD was deleted, unable to find offset...")
return {
"vod_id": -1,
"offset": -1,
"duration": gql_obj["data"]["clip"]["durationSeconds"],
}
# print(gql_obj)
return {
"vod_id": gql_obj["data"]["clip"]["video"]["id"],
"offset": gql_obj["data"]["clip"]["videoOffsetSeconds"],
"duration": gql_obj["data"]["clip"]["durationSeconds"],
}
except Exception as e:
# print(e)
return {
"vod_id": -1,
"offset": -1,
"duration": -1,
}
def get_clip_graphql_info(clip_id):
# seems to just be a default client id
# https://dev.twitch.tv/docs/authentication
# desktop: kimne78kx3ncx6brgo4mv6wki5h1ko
# mobile: kd1unb4b3q4t58fwlpcbzcbnm76a8fp
client_id = "kd1unb4b3q4t58fwlpcbzcbnm76a8fp"
# auth = "xxxxxx"
# formulate the graphql query format
# https://graphiql-online.com/graphiql
# https://api.twitch.tv/gql
query = '''
query Query($clip_id: ID!) {
clip(slug: $clip_id) {
videoOffsetSeconds
viewCount
durationSeconds
video {
id
}
}
}
'''
variables = {'clip_id': clip_id}
url = 'https://gql.twitch.tv/gql'
response = requests.post(
url,
json={'query': query, 'variables': variables},
# headers={"Client-ID": client_id, "Authorization": "OAuth "+auth}
headers={"Client-ID": client_id}
)
return response.text
def send_pushover_message(auth, text):
if auth["pushover_enable"]:
payload = {"message": text, "user": auth["pushover_user_key"], "token": auth["pushover_app_key"] }
resp = requests.post('https://api.pushover.net/1/messages.json', data=payload, headers={'User-Agent': 'Python'})
if not resp.ok:
print("[error]: bad response from pushover: ")
print(resp)