Skip to content

Commit

Permalink
Cleanup (#1131)
Browse files Browse the repository at this point in the history
* Fully qualify Translations namespace

* Remove unused methods

* Add missing access modifiers

* Suppress naming issues

* Fix bad string key names in japanese

* Fix ArgumentException args

* Use type keyword

* Remove unused imports

* Lower finalization retry timeout to 5 seconds

* Delete `__typename`s

* Delete unused classes

* Remove redundant qualifiers

* Remove redundant variable

* Rename test constants
  • Loading branch information
ScrubN authored Jul 2, 2024
1 parent 564752a commit b2ffb6d
Show file tree
Hide file tree
Showing 24 changed files with 46 additions and 198 deletions.
30 changes: 15 additions & 15 deletions TwitchDownloaderCore.Tests/ToolTests/M3U8Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public class M3U8Tests
[InlineData(true, "ru-RU")]
public void CorrectlyParsesTwitchM3U8OfTransportStreams(bool useStream, string culture)
{
const string ExampleM3U8Twitch =
const string EXAMPLE_M3U8_TWITCH =
"#EXTM3U" +
"\n#EXT-X-VERSION:3" +
"\n#EXT-X-TARGETDURATION:10" +
Expand All @@ -37,13 +37,13 @@ public void CorrectlyParsesTwitchM3U8OfTransportStreams(bool useStream, string c
M3U8 m3u8;
if (useStream)
{
var bytes = Encoding.Unicode.GetBytes(ExampleM3U8Twitch);
var bytes = Encoding.Unicode.GetBytes(EXAMPLE_M3U8_TWITCH);
using var ms = new MemoryStream(bytes);
m3u8 = M3U8.Parse(ms, Encoding.Unicode);
}
else
{
m3u8 = M3U8.Parse(ExampleM3U8Twitch);
m3u8 = M3U8.Parse(EXAMPLE_M3U8_TWITCH);
}

CultureInfo.CurrentCulture = oldCulture;
Expand Down Expand Up @@ -73,7 +73,7 @@ public void CorrectlyParsesTwitchM3U8OfTransportStreams(bool useStream, string c
[InlineData(true, "ru-RU")]
public void CorrectlyParsesTwitchM3U8OfLiveStreams(bool useStream, string culture)
{
const string ExampleM3U8Twitch =
const string EXAMPLE_M3U8_TWITCH =
"#EXTM3U" +
"\n#EXT-X-VERSION:3" +
"\n#EXT-X-TARGETDURATION:5" +
Expand Down Expand Up @@ -129,13 +129,13 @@ public void CorrectlyParsesTwitchM3U8OfLiveStreams(bool useStream, string cultur
M3U8 m3u8;
if (useStream)
{
var bytes = Encoding.Unicode.GetBytes(ExampleM3U8Twitch);
var bytes = Encoding.Unicode.GetBytes(EXAMPLE_M3U8_TWITCH);
using var ms = new MemoryStream(bytes);
m3u8 = M3U8.Parse(ms, Encoding.Unicode);
}
else
{
m3u8 = M3U8.Parse(ExampleM3U8Twitch);
m3u8 = M3U8.Parse(EXAMPLE_M3U8_TWITCH);
}

CultureInfo.CurrentCulture = oldCulture;
Expand Down Expand Up @@ -167,7 +167,7 @@ public void CorrectlyParsesTwitchM3U8OfLiveStreams(bool useStream, string cultur
[InlineData(true, "ru-RU")]
public void CorrectlyParsesTwitchM3U8OfPlaylists(bool useStream, string culture)
{
const string ExampleM3U8Twitch =
const string EXAMPLE_M3U8_TWITCH =
"#EXTM3U" +
"\n#EXT-X-TWITCH-INFO:ORIGIN=\"s3\",B=\"false\",REGION=\"NA\",USER-IP=\"255.255.255.255\",SERVING-ID=\"123abc456def789ghi012jkl345mno67\",CLUSTER=\"cloudfront_vod\",USER-COUNTRY=\"US\",MANIFEST-CLUSTER=\"cloudfront_vod\"" +
"\n#EXT-X-MEDIA:TYPE=VIDEO,GROUP-ID=\"chunked\",NAME=\"1080p60\",AUTOSELECT=NO,DEFAULT=NO" +
Expand Down Expand Up @@ -217,13 +217,13 @@ public void CorrectlyParsesTwitchM3U8OfPlaylists(bool useStream, string culture)
M3U8 m3u8;
if (useStream)
{
var bytes = Encoding.Unicode.GetBytes(ExampleM3U8Twitch);
var bytes = Encoding.Unicode.GetBytes(EXAMPLE_M3U8_TWITCH);
using var ms = new MemoryStream(bytes);
m3u8 = M3U8.Parse(ms, Encoding.Unicode);
}
else
{
m3u8 = M3U8.Parse(ExampleM3U8Twitch);
m3u8 = M3U8.Parse(EXAMPLE_M3U8_TWITCH);
}

CultureInfo.CurrentCulture = oldCulture;
Expand Down Expand Up @@ -280,7 +280,7 @@ public void CorrectlyParsesTwitchM3U8StreamInfo(string streamInfoString, int ban
[InlineData(true, "ru-RU")]
public void CorrectlyParsesKickM3U8OfTransportStreams(bool useStream, string culture)
{
const string ExampleM3U8Kick =
const string EXAMPLE_M3U8_KICK =
"#EXTM3U" +
"\n#EXT-X-VERSION:4" +
"\n#EXT-X-MEDIA-SEQUENCE:0" +
Expand Down Expand Up @@ -343,13 +343,13 @@ public void CorrectlyParsesKickM3U8OfTransportStreams(bool useStream, string cul
M3U8 m3u8;
if (useStream)
{
var bytes = Encoding.Unicode.GetBytes(ExampleM3U8Kick);
var bytes = Encoding.Unicode.GetBytes(EXAMPLE_M3U8_KICK);
using var ms = new MemoryStream(bytes);
m3u8 = M3U8.Parse(ms, Encoding.Unicode);
}
else
{
m3u8 = M3U8.Parse(ExampleM3U8Kick);
m3u8 = M3U8.Parse(EXAMPLE_M3U8_KICK);
}

CultureInfo.CurrentCulture = oldCulture;
Expand Down Expand Up @@ -378,7 +378,7 @@ public void CorrectlyParsesKickM3U8OfTransportStreams(bool useStream, string cul
[InlineData(true, "ru-RU")]
public void CorrectlyParsesKickM3U8OfPlaylists(bool useStream, string culture)
{
const string ExampleM3U8Kick =
const string EXAMPLE_M3U8_KICK =
"#EXTM3U" +
"\n#EXT-X-SESSION-DATA:DATA-ID=\"net.live-video.content.id\",VALUE=\"AbC123dEf456\"" +
"\n#EXT-X-SESSION-DATA:DATA-ID=\"net.live-video.customer.id\",VALUE=\"123456789012\"" +
Expand Down Expand Up @@ -423,13 +423,13 @@ public void CorrectlyParsesKickM3U8OfPlaylists(bool useStream, string culture)
M3U8 m3u8;
if (useStream)
{
var bytes = Encoding.Unicode.GetBytes(ExampleM3U8Kick);
var bytes = Encoding.Unicode.GetBytes(EXAMPLE_M3U8_KICK);
using var ms = new MemoryStream(bytes);
m3u8 = M3U8.Parse(ms, Encoding.Unicode);
}
else
{
m3u8 = M3U8.Parse(ExampleM3U8Kick);
m3u8 = M3U8.Parse(EXAMPLE_M3U8_KICK);
}

CultureInfo.CurrentCulture = oldCulture;
Expand Down
2 changes: 1 addition & 1 deletion TwitchDownloaderCore/Extensions/M3U8Extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public static M3U8.Stream GetStreamOfQuality(this M3U8 m3u8, string qualityStrin
{
if (m3u8.Streams.Length == 0)
{
throw new ArgumentException(nameof(m3u8), "M3U8 does not contain any streams.");
throw new ArgumentException("M3U8 does not contain any streams.", nameof(m3u8));
}

if (TryGetKeywordStream(m3u8, qualityString, out var keywordStream))
Expand Down
4 changes: 2 additions & 2 deletions TwitchDownloaderCore/Tools/M3U8Parse.cs
Original file line number Diff line number Diff line change
Expand Up @@ -283,9 +283,9 @@ public static ExtByteRange Parse(ReadOnlySpan<char> text)

public partial record ExtMediaInfo
{
public static Stream.ExtMediaInfo Parse(ReadOnlySpan<char> text)
public static ExtMediaInfo Parse(ReadOnlySpan<char> text)
{
var mediaInfo = new Stream.ExtMediaInfo();
var mediaInfo = new ExtMediaInfo();

if (text.StartsWith(MEDIA_INFO_KEY))
text = text[13..];
Expand Down
4 changes: 1 addition & 3 deletions TwitchDownloaderCore/TsMerger.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using System.Collections.Generic;
using System.Collections.Generic;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
Expand Down Expand Up @@ -135,7 +134,6 @@ private static async Task<bool> VerifyVideoPart(string filePath)
private async Task CombineVideoParts(IReadOnlyCollection<string> fileList, FileStream outputStream, CancellationToken cancellationToken)
{
DriveInfo outputDrive = DriveHelper.GetOutputDrive(mergeOptions.OutputFile);
string outputFile = mergeOptions.OutputFile;

int partCount = fileList.Count;
int doneCount = 0;
Expand Down
17 changes: 4 additions & 13 deletions TwitchDownloaderCore/TwitchHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ public static async Task<string> GetVideoPlaylist(long videoId, string token, st
return await response.Content.ReadAsStringAsync();
}

static bool IsAuthException(Exception ex)
private static bool IsAuthException(Exception ex)
{
while (ex != null)
{
Expand Down Expand Up @@ -566,7 +566,7 @@ public static async Task<List<TwitchEmote>> GetEmotes(List<Comment> comments, st

var nameList = comments.Where(comment => comment.message.user_badges != null)
.SelectMany(comment => comment.message.user_badges)
.Where(badge => !String.IsNullOrWhiteSpace(badge._id))
.Where(badge => !string.IsNullOrWhiteSpace(badge._id))
.Where(badge => globalBadges.ContainsKey(badge._id) || subBadges.ContainsKey(badge._id))
.Select(badge => badge._id).Distinct();

Expand Down Expand Up @@ -1011,15 +1011,6 @@ where DateTime.UtcNow.Ticks - directoryInfo.LastWriteTimeUtc.Ticks > TimeSpan.Ti
: $"{wasDeleted} old video caches were deleted, {toDelete.Length - wasDeleted} could not be deleted.");
}

public static int TimestampToSeconds(string input)
{
// Gets total seconds from timestamp in the format of 0h0m0s
input = input.Replace('h', ':').Replace('m', ':').Replace("s", "");
TimeSpan returnSpan = TimeSpan.Parse(input);

return (int)returnSpan.TotalSeconds;
}

public static async Task<string> GetStreamerName(int id)
{
try
Expand All @@ -1036,7 +1027,7 @@ public static async Task<GqlUserInfoResponse> GetUserInfo(IEnumerable<string> id
{
RequestUri = new Uri("https://gql.twitch.tv/gql"),
Method = HttpMethod.Post,
Content = new StringContent("{\"query\":\"query{users(ids:[" + String.Join(",", idList.Select(x => "\\\"" + x + "\\\"").ToArray()) + "]){id,login,createdAt,updatedAt,description,profileImageURL(width:300)}}\",\"variables\":{}}", Encoding.UTF8, "application/json")
Content = new StringContent("{\"query\":\"query{users(ids:[" + string.Join(",", idList.Select(x => "\\\"" + x + "\\\"").ToArray()) + "]){id,login,createdAt,updatedAt,description,profileImageURL(width:300)}}\",\"variables\":{}}", Encoding.UTF8, "application/json")
};
request.Headers.Add("Client-ID", "kimne78kx3ncx6brgo4mv6wki5h1ko");
using var response = await httpClient.SendAsync(request, HttpCompletionOption.ResponseHeadersRead);
Expand Down Expand Up @@ -1086,7 +1077,7 @@ public static async Task<byte[]> GetImage(DirectoryInfo cacheDir, string imageUr
}
catch (Exception e) when (e is IOException or SecurityException)
{
// File being written to by parallel process? Maybe. Can just fallback to HTTP request.
// File being written to by parallel process? Maybe. Can just fall back to HTTP request.
logger.LogVerbose($"Failed to read from or delete {file.Name}: {e.Message}");
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections.Generic;

namespace TwitchDownloaderCore.TwitchObjects.Api
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections.Generic;

namespace TwitchDownloaderCore.TwitchObjects.Api
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections.Generic;

namespace TwitchDownloaderCore.TwitchObjects.Api
{
Expand Down
66 changes: 0 additions & 66 deletions TwitchDownloaderCore/TwitchObjects/GifEmote.cs

This file was deleted.

6 changes: 1 addition & 5 deletions TwitchDownloaderCore/TwitchObjects/Gql/GqlBadgeResponse.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.Generic;
using System.Text.Json.Serialization;
using System.Threading.Tasks;

namespace TwitchDownloaderCore.TwitchObjects.Gql
{
Expand Down
4 changes: 1 addition & 3 deletions TwitchDownloaderCore/TwitchObjects/Gql/GqlCheerResponse.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using System;
using System.Collections.Generic;
using System.Text;
using System.Collections.Generic;

namespace TwitchDownloaderCore.TwitchObjects.Gql
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ public class ClipToken
public string id { get; set; }
public PlaybackAccessToken playbackAccessToken { get; set; }
public VideoQuality[] videoQualities { get; set; }
public string __typename { get; set; }
}

public class ClipTokenData
Expand All @@ -17,7 +16,6 @@ public class PlaybackAccessToken
{
public string signature { get; set; }
public string value { get; set; }
public string __typename { get; set; }
}

public class GqlClipTokenResponse
Expand All @@ -31,6 +29,5 @@ public class VideoQuality
public double frameRate { get; set; }
public string quality { get; set; }
public string sourceURL { get; set; }
public string __typename { get; set; }
}
}
Loading

0 comments on commit b2ffb6d

Please sign in to comment.