forked from danefairbanks/TwitchClipManager
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
366 lines (342 loc) · 14.8 KB
/
Program.cs
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
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net.Http;
using System.Threading;
namespace ClipManager
{
class ClipInfo
{
public string id { get; set; }
public int view_count { get; set; }
public string creator_name { get; set; }
public string created_at { get; set; }
public string title { get; set; }
}
class Program
{
static string TwitchClientID = "kimne78kx3ncx6brgo4mv6wki5h1ko";
static string TwitchToken;
static string UserId;
static string Login;
static string Cursor;
static string StartDateTime;
static string EndDateTime;
static bool Download = false;
static bool Delete = false;
static string RootPath = Environment.CurrentDirectory;
static void Main(string[] args)
{
Console.WriteLine($"--== TwitchClipDownloader ==--");
LoadConfig();
GetUserID();
var folder = Path.Combine(RootPath, "downloads");
if (!Directory.Exists(folder))
Directory.CreateDirectory(folder);
var clips = GetClips();
while (clips.Count > 0)
{
foreach (var clip in clips)
{
if (DateTime.Parse(clip.created_at) >=
DateTime.Parse(StartDateTime) &&
DateTime.Parse(clip.created_at) <=
DateTime.Parse(EndDateTime))
{
var fileName = SanitizeFile($"{clip.created_at} - {clip.title} - {clip.creator_name} - {clip.id}.mp4");
var savePath = Path.Combine(folder, fileName);
try
{
if (Download)
{
Console.WriteLine($"Downloading {clip.id} - {clip.title} by {clip.creator_name}");
var sourceUrl = GetClipUri(clip.id);
DownloadClip(sourceUrl, savePath);
}
else
{
Console.WriteLine($"Found {fileName}");
}
}
catch (Exception ex)
{
File.AppendAllText(Path.Combine(RootPath, "error.log"), $"{clip.id} download failed: {ex.Message}");
}
}
}
// TODO: this delete removes batch of returns, need to adjust to individually delete on date range filter match
if (Delete)
{
Console.WriteLine($"Deleting {string.Join(',', clips.Select(c => c.id))}");
DeleteClips(clips.Select(c => c.id).ToList());
}
UpdateCursor();
if (!Delete && Cursor == null)
break;
clips = GetClips();
}
Console.WriteLine($"--== FIN ==--");
}
static void LoadConfig()
{
var configPath = Path.Combine(RootPath, "appsettings.json");
bool resume = false;
if (File.Exists(configPath))
{
Console.WriteLine("Session found resume? (y or n):");
var resumeResp = Console.ReadLine();
resume = resumeResp.ToLower().StartsWith('y');
if (resume)
{
var fs = File.OpenRead(configPath);
var fsr = new StreamReader(fs);
var config = JObject.Parse(fsr.ReadToEnd());
Cursor = config["cursor"]?.ToString();
TwitchToken = config["twitchtoken"]?.ToString();
StartDateTime = config["startdatetime"]?.ToString();
EndDateTime = config["enddatetime"]?.ToString();
Download = config["download"]?.ToObject<bool>() == true;
Delete = config["delete"]?.ToObject<bool>() == true;
}
}
if (!resume)
{
if (File.Exists(configPath))
File.Delete(configPath);
GetConfig();
}
}
static void GetConfig()
{
Console.WriteLine("Paste in auth token:");
TwitchToken = Console.ReadLine().Trim();
Console.WriteLine("Start Date ('yyyy-MM-ddThh:mm:ss' or blank for very first):");
var startDateTimeResp = Console.ReadLine();
if (startDateTimeResp == "") StartDateTime = DateTime.UnixEpoch.ToString();
else StartDateTime = startDateTimeResp;
Console.WriteLine("End Date ('yyyy-MM-ddThh:mm:ss' or blank for very last):");
var endDateTimeResp = Console.ReadLine();
if (endDateTimeResp == "") EndDateTime = DateTime.UtcNow.ToString();
else EndDateTime = endDateTimeResp;
Console.WriteLine("Download (y or n):");
var downloadResp = Console.ReadLine();
Download = downloadResp.ToLower().StartsWith('y');
Console.WriteLine("Delete (y or n):");
var deleteResp = Console.ReadLine();
Delete = deleteResp.ToLower().StartsWith('y');
if (Delete)
{
Console.WriteLine("Are you REALLY sure you want to Delete (non-reversible!) (y or n):");
var deleteRespConfirm = Console.ReadLine();
Delete = deleteRespConfirm.ToLower().StartsWith('y');
}
var configPath = Path.Combine(RootPath, "appsettings.json");
var config = new JObject()
{
["twitchtoken"] = TwitchToken,
["startdatetime"] = StartDateTime,
["enddatetime"] = EndDateTime,
["download"] = Download,
["delete"] = Delete
};
var fsw = File.OpenWrite(configPath);
var sw = new StreamWriter(fsw);
sw.Write(config.ToString());
sw.Close();
}
static void UpdateCursor()
{
var configPath = Path.Combine(RootPath, "appsettings.json");
JObject config = new JObject();
if (File.Exists(configPath))
{
var fsr = File.OpenRead(configPath);
var sr = new StreamReader(fsr);
config = JObject.Parse(sr.ReadToEnd());
fsr.Close();
File.Delete(configPath);
}
config["cursor"] = Cursor;
var fsw = File.OpenWrite(configPath);
var sw = new StreamWriter(fsw);
sw.Write(config.ToString());
sw.Close();
}
static void GetUserID()
{
try
{
var http = new HttpClient();
http.DefaultRequestHeaders.Add("Client-ID", TwitchClientID);
http.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", TwitchToken);
var res = http.GetStringAsync($"https://api.twitch.tv/helix/users").GetAwaiter().GetResult();
var jtok = JToken.Parse(res);
UserId = jtok["data"][0]["id"].ToString();
Login = jtok["data"][0]["login"].ToString();
}
catch (Exception ex)
{
Console.WriteLine($"{TwitchClientID} get user id failed: {ex.Message}");
System.Environment.Exit(1);
}
}
static IList<ClipInfo> GetClips()
{
var gql = new JArray()
{
new JObject()
{
["extensions"] = new JObject()
{
["persistedQuery"] = new JObject()
{
["version"] = 1,
["sha256Hash"] = "b300f79444fdcf2a1a76c101f466c8c9d7bee49b643a4d7878310a4e03944232"
}
},
["operationName"] = "ClipsManagerTable_User",
["variables"] = new JObject()
{
["login"] = Login,
["limit"] = 5,
["criteria"] = new JObject()
{
["sort"] = "VIEWS_DESC",
["period"] = "ALL_TIME",
["broadcasterID"] = UserId
}
}
}
};
if (!Delete && !string.IsNullOrWhiteSpace(Cursor))
{
gql[0]["variables"]["cursor"] = Cursor;
}
var content = gql.ToString(Newtonsoft.Json.Formatting.None);
var ghttp = new HttpClient();
ghttp.DefaultRequestHeaders.Add("Client-ID", TwitchClientID);
ghttp.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("OAuth", TwitchToken);
// TODO: need to change this to official "helix" api as using this gql way may have consequences
var res = ghttp.PostAsync("https://gql.twitch.tv/gql", new StringContent(content)).GetAwaiter().GetResult().Content.ReadAsStringAsync().GetAwaiter().GetResult();
var jtok = JArray.Parse(res);
var retVal = new List<ClipInfo>();
bool hasNextPage = jtok[0]["data"]["user"]["clips"]["pageInfo"]["hasNextPage"]?.ToObject<bool>() == true;
foreach (dynamic e in jtok[0]["data"]["user"]["clips"]["edges"])
{
dynamic node = e.node;
string creator = node.broadcaster.login;
try
{
creator = node.curator.login;
}
catch
{
}
retVal.Add(new ClipInfo
{
id = node.slug,
title = node.title,
creator_name = creator,
created_at = DateTime.Parse(Convert.ToString(node.createdAt)).ToString("yyyy-MM-ddThh:mm:ss"),
view_count = node.viewCount
});
if (!Delete && e.cursor != null && hasNextPage)
{
Cursor = e.cursor;
}
}
if (!Delete && !hasNextPage) Cursor = null;
return retVal;
}
static string GetClipUri(string clipId)
{
var gql = new JArray();
gql.Add(new JObject()
{
["extensions"] = new JObject()
{
["persistedQuery"] = new JObject()
{
["version"] = 1,
["sha256Hash"] = "9bfcc0177bffc730bd5a5a89005869d2773480cf1738c592143b5173634b7d15"
}
},
["operationName"] = "VideoAccessToken_Clip",
["variables"] = new JObject()
{
["slug"] = clipId
}
});
var content = gql.ToString(Newtonsoft.Json.Formatting.None);
var ghttp = new HttpClient();
ghttp.DefaultRequestHeaders.Add("Client-ID", TwitchClientID);
var res = ghttp.PostAsync("https://gql.twitch.tv/gql", new StringContent(content)).GetAwaiter().GetResult().Content.ReadAsStringAsync().GetAwaiter().GetResult();
var jtok = JArray.Parse(res);
string sourceURL = jtok[0]["data"]["clip"]["videoQualities"][0]["sourceURL"].ToString();
// FIXME: 2021-06-01 - temp workaround Twitch changed API, but older environment still works, just replace CDN
string sourceURLWorkaround = sourceURL.Replace("https://production.assets.clips.twitchcdn.net", "https://clips-media-assets2.twitch.tv");
return sourceURLWorkaround;
}
static void DeleteClips(IList<string> clips)
{
var gql = new JArray();
gql.Add(new JObject()
{
["extensions"] = new JObject()
{
["persistedQuery"] = new JObject()
{
["version"] = 1,
["sha256Hash"] = "df142a7eec57c5260d274b92abddb0bd1229dc538341434c90367cf1f22d71c4"
}
},
["operationName"] = "Clips_DeleteClips",
["variables"] = new JObject()
{
["input"] = new JObject()
{
["slugs"] = new JArray(clips.ToArray())
}
}
});
var content = gql.ToString(Newtonsoft.Json.Formatting.None);
var ghttp = new HttpClient();
ghttp.DefaultRequestHeaders.Add("Client-ID", TwitchClientID);
ghttp.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("OAuth", TwitchToken);
while (true)
{
var res = ghttp.PostAsync("https://gql.twitch.tv/gql", new StringContent(content)).GetAwaiter().GetResult().Content.ReadAsStringAsync().GetAwaiter().GetResult();
var jtok = JArray.Parse(res);
if (!res.Contains("error"))
break;
else
Thread.Sleep(10000);
}
}
static void DownloadClip(string sourceUrl, string savePath)
{
Console.WriteLine($"--== DownloadClip ==--");
Console.WriteLine(sourceUrl);
try {
var http = new HttpClient();
var stream = http.GetStreamAsync(sourceUrl).GetAwaiter().GetResult(); // failing here (404 return code)
if (File.Exists(savePath))
File.Delete(savePath);
var fs = new FileStream(savePath, FileMode.CreateNew);
stream.CopyTo(fs);
fs.Close();
}
catch (HttpRequestException hre)
{
Console.WriteLine(hre.Message);
}
}
static string SanitizeFile(string origFileName)
{
var invalids = Path.GetInvalidFileNameChars();
return string.Join("_", origFileName.Split(invalids, StringSplitOptions.RemoveEmptyEntries)).TrimEnd('.');
}
}
}