Skip to content

Commit

Permalink
added twitch provider
Browse files Browse the repository at this point in the history
  • Loading branch information
w8tcha committed May 12, 2024
1 parent b6c7870 commit aaf6370
Show file tree
Hide file tree
Showing 9 changed files with 135 additions and 44 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -106,3 +106,5 @@ Generated_Code #added for RIA/Silverlight projects
_UpgradeReport_Files/
Backup*/
UpgradeLog*.XML
/.vs
OEmbed.Core.sln.GhostDoc.xml
4 changes: 2 additions & 2 deletions Directory.Build.props
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<Project>

<PropertyGroup>
<Version>2.0.1</Version>
<AssemblyVersion>2.0.1</AssemblyVersion>
<Version>2.0.2</Version>
<AssemblyVersion>2.0.2</AssemblyVersion>
<Authors>Tatsuro Shibamura;Ingo Herbote</Authors>
<Company>Ingo Herbote</Company>
<Copyright>&#169; Tatsuro Shibamura and Ingo Herbote</Copyright>
Expand Down
9 changes: 0 additions & 9 deletions OEmbed.Core.Tests/ProviderTests/InstagramTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,6 @@ public class InstagramTests : IProviderTests
[InlineData("https://www.instagram.com/p/1XSKgBAGz-/")]
[InlineData("https://www.instagram.com/tv/CHLVnWVAF9I/")]
[InlineData("https://www.instagram.com/reel/CW0gZu2rouF/")]
[InlineData("https://www.instagr.am/dlwlrma/p/1XSKgBAGz-/")]
[InlineData("https://www.instagr.am/dlwlrma/tv/CbKjea_ARNB/")]
[InlineData("https://www.instagram.com/dlwlrma/tv/CbKjea_ARNB/")]
[InlineData("https://www.instagram.com/p/1XSKgBAGz-/?utm_source=ig_web_button_share_sheet")]
public void CanEmbedTest(string url)
{
Expand All @@ -26,9 +23,6 @@ public void CanEmbedTest(string url)
[InlineData("https://www.instagram.com/p/1XSKgBAGz-/")]
[InlineData("https://www.instagram.com/tv/CHLVnWVAF9I/")]
[InlineData("https://www.instagram.com/reel/CW0gZu2rouF/")]
[InlineData("https://www.instagr.am/dlwlrma/p/1XSKgBAGz-/")]
[InlineData("https://www.instagr.am/dlwlrma/tv/CbKjea_ARNB/")]
[InlineData("https://www.instagram.com/dlwlrma/tv/CbKjea_ARNB/")]
[InlineData("https://www.instagram.com/p/1XSKgBAGz-/?utm_source=ig_web_button_share_sheet")]
public void EmbedTest(string url)
{
Expand All @@ -47,9 +41,6 @@ public void EmbedTest(string url)
[InlineData("https://www.instagram.com/p/1XSKgBAGz-/")]
[InlineData("https://www.instagram.com/tv/CHLVnWVAF9I/")]
[InlineData("https://www.instagram.com/reel/CW0gZu2rouF/")]
[InlineData("https://www.instagr.am/dlwlrma/p/1XSKgBAGz-/")]
[InlineData("https://www.instagr.am/dlwlrma/tv/CbKjea_ARNB/")]
[InlineData("https://www.instagram.com/dlwlrma/tv/CbKjea_ARNB/")]
[InlineData("https://www.instagram.com/p/1XSKgBAGz-/?utm_source=ig_web_button_share_sheet")]
public async Task EmbedAsyncTest(string url)
{
Expand Down
47 changes: 47 additions & 0 deletions OEmbed.Core.Tests/ProviderTests/TwitchTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
namespace OEmbed.Core.Tests.ProviderTests;

public class TwitchTests : IProviderTests
{
[Theory]
[InlineData("https://www.twitch.tv/therealknossi")]
[InlineData("https://www.twitch.tv/videos/2141265767")]
[InlineData("https://www.twitch.tv/therealknossi/clip/DiligentRelatedBisonVoteYea-5mkamtYyocJOfu79")]
public void CanEmbedTest(string url)
{
var embed = new OEmbed();

var canEmbed = embed.CanEmbed(url);

Assert.True(canEmbed);
}

#if NET481
[Theory]
[InlineData("https://www.twitch.tv/therealknossi")]
[InlineData("https://www.twitch.tv/videos/2141265767")]
[InlineData("https://www.twitch.tv/therealknossi/clip/DiligentRelatedBisonVoteYea-5mkamtYyocJOfu79")]
public void EmbedTest(string url)
{
var embed = new OEmbed();

var actual = embed.Embed(url);

Assert.NotNull(actual);
}
#endif

#if NET7_0_OR_GREATER
[Theory]
[InlineData("https://www.twitch.tv/therealknossi")]
[InlineData("https://www.twitch.tv/videos/2141265767")]
[InlineData("https://www.twitch.tv/therealknossi/clip/DiligentRelatedBisonVoteYea-5mkamtYyocJOfu79")]
public async Task EmbedAsyncTest(string url)
{
var embed = new OEmbed();

var actual = await embed.EmbedAsync(url);

Assert.NotNull(actual);
}
#endif
}
2 changes: 1 addition & 1 deletion OEmbed.Core/OEmbed.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
<PackageReference Include="SonarAnalyzer.CSharp" Version="9.24.0.89429">
<PackageReference Include="SonarAnalyzer.CSharp" Version="9.25.0.90414">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
Expand Down
29 changes: 6 additions & 23 deletions OEmbed.Core/OEmbed.cs
Original file line number Diff line number Diff line change
Expand Up @@ -97,14 +97,9 @@ public Response Embed(string url)

var provider = Providers.Find(p => p.CanHandleUrl(uri));

if (provider == null)
{
return null;
}

var match = provider.MatchScheme(uri);
var match = provider?.MatchScheme(uri);

if (!match)
if (match is null || !match.Success)
{
return null;
}
Expand All @@ -114,7 +109,7 @@ public Response Embed(string url)
return (Response)Cache.Get(url);
}

return string.IsNullOrEmpty(provider.Endpoint) ? GetHtml(provider, url) : this.GetJson(provider, url);
return string.IsNullOrEmpty(provider.Endpoint) ? provider.GetHtml(provider, match, url) : this.GetJson(provider, url);
}
#endif

Expand All @@ -135,14 +130,9 @@ public async Task<Response> EmbedAsync(string url)

var provider = this.Providers.Find(p => p.CanHandleUrl(uri));

if (provider == null)
{
return null;
}
var match = provider?.MatchScheme(uri);

var match = provider.MatchScheme(uri);

if (!match)
if (match is null || !match.Success)
{
return null;
}
Expand All @@ -153,19 +143,12 @@ public async Task<Response> EmbedAsync(string url)
}

return string.IsNullOrEmpty(provider.Endpoint)
? GetHtml(provider, url)
? provider.GetHtml(provider, match, url)
: await this.GetJsonAsync(provider, url);
}

#endif

private static Response GetHtml(Provider provider, string url)
{
var response = new Response { Html = provider.Html.Replace("{url}", url), Type = ResponseType.Rich };

return response;
}

#if NET481
/// <summary>
/// Get the oEmbed JSON
Expand Down
22 changes: 19 additions & 3 deletions OEmbed.Core/Provider.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
namespace OEmbed.Core;
using System.Linq;

namespace OEmbed.Core;

/// <summary>
/// Class Provider.
Expand Down Expand Up @@ -50,9 +52,23 @@ public virtual bool CanHandleUrl(Uri uri)
/// </summary>
/// <param name="uri">The URI.</param>
/// <returns>bool.</returns>
public virtual bool MatchScheme(Uri uri)
public virtual Match MatchScheme(Uri uri)
{
return this.Matches.Exists(match => match.Match(uri.PathAndQuery).Success);
return this.Matches.Select(match => match.Match(uri.PathAndQuery)).FirstOrDefault(check => check.Success);
}

/// <summary>
/// Gets the embed HTML for the Provider
/// </summary>
/// <param name="provider">The provider.</param>
/// <param name="match">The match.</param>
/// <param name="url">The URL.</param>
/// <returns>OEmbed.Core.Response.</returns>
public virtual Response GetHtml(Provider provider, Match match, string url)
{
var response = new Response { Html = provider.Html.Replace("{url}", url), Type = ResponseType.Rich };

return response;
}

/// <summary>
Expand Down
6 changes: 0 additions & 6 deletions OEmbed.Core/Providers/CodePenProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,7 @@

/// <summary>
/// Class CodePenProvider.
/// Implements the <see cref="OEmbed.Core.Provider" />
/// Implements the <see cref="System.IEquatable{OEmbed.Core.Provider}" />
/// Implements the <see cref="System.IEquatable{OEmbed.Core.Providers.CodePenProvider}" />
/// </summary>
/// <seealso cref="OEmbed.Core.Provider" />
/// <seealso cref="System.IEquatable{OEmbed.Core.Provider}" />
/// <seealso cref="System.IEquatable{OEmbed.Core.Providers.CodePenProvider}" />
public record CodePenProvider : Provider
{
/// <summary>
Expand Down
58 changes: 58 additions & 0 deletions OEmbed.Core/Providers/TwitchProvider.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
namespace OEmbed.Core.Providers;

/// <summary>
/// Class TwitchProvider.
/// </summary>
public record TwitchProvider : Provider
{
/// <summary>
/// Initializes a new instance of the <see cref="TwitchProvider"/> class.
/// </summary>
public TwitchProvider()
{
this.Name = "Twitch";
this.Hosts = ["twitch.tv", "www.twitch.tv", "go.twitch.tv"];

this.AddMatches(
"/(?!videos|clip)(?<channel>([a-zA-Z0-9]+))?",
"/(?:videos)/(?<videoId>[0-9]+)?",
@"/(?:[\w_\-]+/)?(?:clip)/(?<clipId>(.*?))?");

this.Html = """
<iframe src="{url}"></iframe>
""";
}

/// <summary>
/// Gets the embed HTML for the Provider
/// </summary>
/// <param name="provider">The provider.</param>
/// <param name="match">The match.</param>
/// <param name="url">The URL.</param>
/// <returns>OEmbed.Core.Response.</returns>
public override Response GetHtml(Provider provider, Match match, string url)
{
if (!string.IsNullOrEmpty(match.Groups["videoId"].Value))
{
return new Response { Html = $"""
<iframe src="https://player.twitch.tv/?video={match.Groups["videoId"].Value}&parent=twitch.tv&autoplay=false" height="720" width="1280" allowfullscreen></iframe>
""", Type = ResponseType.Rich };
}

if (!string.IsNullOrEmpty(match.Groups["clipId"].Value))
{
return new Response { Html = $"""
<iframe src="https://player.twitch.tv/?collection={match.Groups["clipId"].Value}&parent=twitch.tv" height="720" width="1280" allowfullscreen></iframe>
""", Type = ResponseType.Rich };
}

if (!string.IsNullOrEmpty(match.Groups["channel"].Value))
{
return new Response { Html = $"""
<iframe src="https://player.twitch.tv/?channel={match.Groups["channel"].Value}&parent=twitch.tv&muted=true" height="720" width="1280" allowfullscreen></iframe>
""", Type = ResponseType.Rich };
}

return new Response { Html = provider.Html.Replace("{url}", url), Type = ResponseType.Rich };
}
}

0 comments on commit aaf6370

Please sign in to comment.