forked from shibayan/OEmbedSharp
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
135 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }; | ||
} | ||
} |