Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hotfix clip qualities again #900

Merged
merged 1 commit into from
Nov 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions TwitchDownloaderCore/ClipDownloader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -86,33 +86,34 @@ void DownloadProgressHandler(StreamCopyProgress streamProgress)
private async Task<string> GetDownloadUrl()
{
var listLinks = await TwitchHelper.GetClipLinks(downloadOptions.Id);
var clip = listLinks[0].data.clip;

if (listLinks[0].data.clip.playbackAccessToken is null)
if (clip.playbackAccessToken is null)
{
throw new NullReferenceException("Invalid Clip, deleted possibly?");
}

if (listLinks[0].data.clip.videoQualities is null || listLinks[0].data.clip.videoQualities.Count == 0)
if (clip.videoQualities is null || clip.videoQualities.Length == 0)
{
throw new NullReferenceException("Clip has no video qualities, deleted possibly?");
}

string downloadUrl = "";

foreach (var quality in listLinks[0].data.clip.videoQualities)
foreach (var quality in clip.videoQualities)
{
if (quality.quality + "p" + (quality.frameRate.ToString() == "30" ? "" : quality.frameRate.ToString()) == downloadOptions.Quality)
if (quality.quality + "p" + (Math.Round(quality.frameRate) == 30 ? "" : Math.Round(quality.frameRate).ToString("F0")) == downloadOptions.Quality)
{
downloadUrl = quality.sourceURL;
}
}

if (downloadUrl == "")
{
downloadUrl = listLinks[0].data.clip.videoQualities.First().sourceURL;
downloadUrl = clip.videoQualities.First().sourceURL;
}

return downloadUrl + "?sig=" + listLinks[0].data.clip.playbackAccessToken.signature + "&token=" + HttpUtility.UrlEncode(listLinks[0].data.clip.playbackAccessToken.value);
return downloadUrl + "?sig=" + clip.playbackAccessToken.signature + "&token=" + HttpUtility.UrlEncode(clip.playbackAccessToken.value);
}

private static async Task DownloadFileTaskAsync(string url, string destinationFile, int throttleKib, IProgress<StreamCopyProgress> progress, CancellationToken cancellationToken)
Expand Down
32 changes: 32 additions & 0 deletions TwitchDownloaderCore/Tools/ClipQualityComparer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
using System;
using System.Collections.Generic;
using TwitchDownloaderCore.TwitchObjects.Gql;

namespace TwitchDownloaderCore.Tools
{
public class ClipQualityComparer : IComparer<VideoQuality>
{
public int Compare(VideoQuality x, VideoQuality y)
{
if (x is null)
{
if (y is null) return 0;
return -1;
}

if (y is null) return 1;

if (int.TryParse(x.quality, out var xQuality) | int.TryParse(y.quality, out var yQuality))
{
if (xQuality < yQuality) return 1;
if (xQuality > yQuality) return -1;

if (x.frameRate < y.frameRate) return 1;
if (x.frameRate > y.frameRate) return -1;
return 0;
}

return Math.Clamp(string.Compare(x.quality, y.quality, StringComparison.Ordinal), -1, 1) * -1;
}
}
}
2 changes: 1 addition & 1 deletion TwitchDownloaderCore/Tools/CommentTools.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public int Compare(Comment x, Comment y)
return -1;
}

if (y is null) return -1;
if (y is null) return 1;

// In the off chance that it causes problems with old chats, we will first compare offsets before comparing creation dates.
var xOffset = x.content_offset_seconds;
Expand Down
8 changes: 6 additions & 2 deletions TwitchDownloaderCore/TwitchHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
using System.Threading;
using System.Threading.Tasks;
using TwitchDownloaderCore.Chat;
using TwitchDownloaderCore.Tools;
using TwitchDownloaderCore.TwitchObjects;
using TwitchDownloaderCore.TwitchObjects.Api;
using TwitchDownloaderCore.TwitchObjects.Gql;
Expand Down Expand Up @@ -80,7 +81,7 @@ public static async Task<GqlClipResponse> GetClipInfo(object clipId)
return await response.Content.ReadFromJsonAsync<GqlClipResponse>();
}

public static async Task<List<GqlClipTokenResponse>> GetClipLinks(string clipId)
public static async Task<GqlClipTokenResponse[]> GetClipLinks(string clipId)
{
var request = new HttpRequestMessage()
{
Expand All @@ -91,7 +92,10 @@ public static async Task<List<GqlClipTokenResponse>> GetClipLinks(string clipId)
request.Headers.Add("Client-ID", "kimne78kx3ncx6brgo4mv6wki5h1ko");
using var response = await httpClient.SendAsync(request, HttpCompletionOption.ResponseHeadersRead);
response.EnsureSuccessStatusCode();
return await response.Content.ReadFromJsonAsync<List<GqlClipTokenResponse>>();

var gqlClipTokenResponses = await response.Content.ReadFromJsonAsync<GqlClipTokenResponse[]>();
Array.Sort(gqlClipTokenResponses[0].data.clip.videoQualities, new ClipQualityComparer());
return gqlClipTokenResponses;
}

public static async Task<GqlVideoSearchResponse> GetGqlVideos(string channelName, string cursor = "", int limit = 50)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
using System.Collections.Generic;

namespace TwitchDownloaderCore.TwitchObjects.Gql
namespace TwitchDownloaderCore.TwitchObjects.Gql
{
public class ClipToken
{
public string id { get; set; }
public PlaybackAccessToken playbackAccessToken { get; set; }
public List<VideoQuality> videoQualities { get; set; }
public VideoQuality[] videoQualities { get; set; }
public string __typename { get; set; }
}

Expand Down
2 changes: 1 addition & 1 deletion TwitchDownloaderWPF/PageClipDownload.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ private async Task GetClipInfo()
btnGetInfo.IsEnabled = false;
comboQuality.Items.Clear();
Task<GqlClipResponse> taskClipInfo = TwitchHelper.GetClipInfo(clipId);
Task<List<GqlClipTokenResponse>> taskLinks = TwitchHelper.GetClipLinks(clipId);
Task<GqlClipTokenResponse[]> taskLinks = TwitchHelper.GetClipLinks(clipId);
await Task.WhenAll(taskClipInfo, taskLinks);

GqlClipResponse clipData = taskClipInfo.Result;
Expand Down
Loading