Skip to content

Commit

Permalink
Fix GetLocalsAsync (#40)
Browse files Browse the repository at this point in the history
* Fix GetLocalsAsync endpoint
* Cache GetLocalsAsync results
  • Loading branch information
kuylar authored Oct 30, 2023
1 parent 51d7bbb commit f17259b
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 25 deletions.
37 changes: 24 additions & 13 deletions InnerTube.Tests/OtherTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Text;
using System.Diagnostics;
using System.Text;

namespace InnerTube.Tests;

Expand All @@ -15,19 +16,29 @@ public void Setup()
[Test]
public async Task GetLocals()
{
Stopwatch sp = new();
StringBuilder sb = new();
InnerTubeLocals locals = await _innerTube.GetLocalsAsync();

sb.AppendLine("== LANGUAGES");
foreach ((string id, string title) in locals.Languages)
sb.AppendLine($"{RightPad($"[{id}]", 9)} {title}");

sb.AppendLine()
.AppendLine("== REGIONS");
foreach ((string id, string title) in locals.Regions)
sb.AppendLine($"{RightPad($"[{id}]", 4)} {title}");

Assert.Pass(sb.ToString());
long[] times = new long[3];

for (int i = 0; i < times.Length; i++)
{
sp.Restart();
InnerTubeLocals locals = await _innerTube.GetLocalsAsync();
sp.Stop();
times[i] = sp.ElapsedMilliseconds;

if (i != 0) continue;
sb.AppendLine("== LANGUAGES");
foreach ((string id, string title) in locals.Languages)
sb.AppendLine($"{RightPad($"[{id}]", 9)} {title}");

sb.AppendLine()
.AppendLine("== REGIONS");
foreach ((string id, string title) in locals.Regions)
sb.AppendLine($"{RightPad($"[{id}]", 4)} {title}");
}

Assert.Pass($"Times: {string.Join(", ", times)}" + "\n\n" + sb);
}

private string RightPad(string input, int length, char appendChar = ' ')
Expand Down
18 changes: 13 additions & 5 deletions InnerTube/InnerTube.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public class InnerTube
internal readonly MemoryCache PlayerCache;
internal readonly string ApiKey;
internal readonly InnerTubeAuthorization? Authorization;
private InnerTubeLocals? _cachedLocals;

/// <summary>
/// Initializes a new instance of InnerTube client.
Expand Down Expand Up @@ -440,10 +441,17 @@ public async Task<InnerTubeContinuationResponse> ContinueBrowseAsync(string cont
/// <param name="language">Language of the content</param>
/// <param name="region">Region of the content</param>
/// <returns>List of all valid languages &amp; regions</returns>
public async Task<InnerTubeLocals> GetLocalsAsync(string language = "en", string region = "US")
public async Task<InnerTubeLocals> GetLocalsAsync(bool refreshCache = false, string language = "en",
string region = "US")
{
JObject localsResponse = await MakeRequest(RequestClient.WEB, "account/account_menu", new InnerTubeRequest(),
language, region);
return new InnerTubeLocals(localsResponse);
if (refreshCache || _cachedLocals is null)
{
JObject localsResponse = await MakeRequest(RequestClient.WEB, "account/account_menu",
new InnerTubeRequest(),
language, region);
_cachedLocals = new InnerTubeLocals(localsResponse);
}

return _cachedLocals!;
}
}
}
20 changes: 13 additions & 7 deletions InnerTube/Models/InnerTubeLocals.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,19 @@ public class InnerTubeLocals

public InnerTubeLocals(JObject localsResponse)
{
JArray languagesArray = localsResponse.GetFromJsonPath<JArray>(
"actions[0].openPopupAction.popup.multiPageMenuRenderer.sections[1].multiPageMenuSectionRenderer.items[1].compactLinkRenderer.serviceEndpoint.signalServiceEndpoint.actions[0].getMultiPageMenuAction.menu.multiPageMenuRenderer.sections[0].multiPageMenuSectionRenderer.items")
!;
JArray regionsArray = localsResponse.GetFromJsonPath<JArray>(
"actions[0].openPopupAction.popup.multiPageMenuRenderer.sections[1].multiPageMenuSectionRenderer.items[3].compactLinkRenderer.serviceEndpoint.signalServiceEndpoint.actions[0].getMultiPageMenuAction.menu.multiPageMenuRenderer.sections[0].multiPageMenuSectionRenderer.items")
!;
JArray sections = localsResponse.GetFromJsonPath<JArray>(
"actions[0].openPopupAction.popup.multiPageMenuRenderer.sections[0].multiPageMenuSectionRenderer.items")!;

JArray languagesArray = sections
.First(x => x["compactLinkRenderer"]?["icon"]?["iconType"]?.ToObject<string>() == "TRANSLATE")
.GetFromJsonPath<JArray>(
"compactLinkRenderer.serviceEndpoint.signalServiceEndpoint.actions[0].getMultiPageMenuAction.menu.multiPageMenuRenderer.sections[0].multiPageMenuSectionRenderer.items")
?? new JArray();
JArray regionsArray = sections
.First(x => x["compactLinkRenderer"]?["icon"]?["iconType"]?.ToObject<string>() == "LANGUAGE")
.GetFromJsonPath<JArray>(
"compactLinkRenderer.serviceEndpoint.signalServiceEndpoint.actions[0].getMultiPageMenuAction.menu.multiPageMenuRenderer.sections[0].multiPageMenuSectionRenderer.items")
?? new JArray();

Languages = languagesArray.ToDictionary(
x => x.GetFromJsonPath<string>(
Expand All @@ -26,6 +33,5 @@ public InnerTubeLocals(JObject localsResponse)
"compactLinkRenderer.serviceEndpoint.signalServiceEndpoint.actions[0].selectCountryCommand.gl")!,
x => x.GetFromJsonPath<string>("compactLinkRenderer.title.simpleText")!
);
;
}
}

0 comments on commit f17259b

Please sign in to comment.