2021-05-05 10:57:01 +00:00
|
|
|
|
using System;
|
2021-03-16 20:11:34 +00:00
|
|
|
|
using System.Diagnostics.CodeAnalysis;
|
|
|
|
|
|
|
|
|
|
namespace MediaBrowser.Common.Providers
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Parsers for provider ids.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public static class ProviderIdParsers
|
|
|
|
|
{
|
2021-03-18 10:25:58 +00:00
|
|
|
|
private const int ImdbMinNumbers = 7;
|
|
|
|
|
private const int ImdbMaxNumbers = 8;
|
2021-03-26 16:06:01 +00:00
|
|
|
|
private const string ImdbPrefix = "tt";
|
2021-03-18 10:25:58 +00:00
|
|
|
|
|
2021-03-16 20:11:34 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Parses an IMDb id from a string.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="text">The text to parse.</param>
|
|
|
|
|
/// <param name="imdbId">The parsed IMDb id.</param>
|
|
|
|
|
/// <returns>True if parsing was successful, false otherwise.</returns>
|
2021-08-04 12:40:09 +00:00
|
|
|
|
public static bool TryFindImdbId(ReadOnlySpan<char> text, out ReadOnlySpan<char> imdbId)
|
2021-03-16 20:11:34 +00:00
|
|
|
|
{
|
2022-04-15 17:27:38 +00:00
|
|
|
|
// IMDb id is at least 9 chars (tt + 7 numbers)
|
2021-03-18 19:52:56 +00:00
|
|
|
|
while (text.Length >= 2 + ImdbMinNumbers)
|
2021-03-16 20:11:34 +00:00
|
|
|
|
{
|
2021-03-29 08:35:29 +00:00
|
|
|
|
var ttPos = text.IndexOf(ImdbPrefix);
|
2021-03-16 20:11:34 +00:00
|
|
|
|
if (ttPos == -1)
|
|
|
|
|
{
|
|
|
|
|
imdbId = default;
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-26 16:06:01 +00:00
|
|
|
|
text = text.Slice(ttPos);
|
|
|
|
|
var i = 2;
|
|
|
|
|
var limit = Math.Min(text.Length, ImdbMaxNumbers + 2);
|
|
|
|
|
for (; i < limit; i++)
|
2021-03-16 20:11:34 +00:00
|
|
|
|
{
|
2021-03-18 19:52:56 +00:00
|
|
|
|
var c = text[i];
|
2021-03-19 11:34:21 +00:00
|
|
|
|
if (!IsDigit(c))
|
2021-03-16 20:11:34 +00:00
|
|
|
|
{
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-15 17:27:38 +00:00
|
|
|
|
// Skip if more than 8 digits + 2 chars for tt
|
2021-03-26 16:06:01 +00:00
|
|
|
|
if (i <= ImdbMaxNumbers + 2 && i >= ImdbMinNumbers + 2)
|
2021-03-16 20:11:34 +00:00
|
|
|
|
{
|
2021-03-26 16:06:01 +00:00
|
|
|
|
imdbId = text.Slice(0, i);
|
2021-03-16 20:11:34 +00:00
|
|
|
|
return true;
|
|
|
|
|
}
|
2021-03-18 10:25:58 +00:00
|
|
|
|
|
2021-03-18 19:52:56 +00:00
|
|
|
|
text = text.Slice(i);
|
2021-03-16 20:11:34 +00:00
|
|
|
|
}
|
2021-03-18 10:25:58 +00:00
|
|
|
|
|
|
|
|
|
imdbId = default;
|
|
|
|
|
return false;
|
2021-03-16 20:11:34 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Parses an TMDb id from a movie url.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="text">The text with the url to parse.</param>
|
|
|
|
|
/// <param name="tmdbId">The parsed TMDb id.</param>
|
|
|
|
|
/// <returns>True if parsing was successful, false otherwise.</returns>
|
2021-08-04 12:40:09 +00:00
|
|
|
|
public static bool TryFindTmdbMovieId(ReadOnlySpan<char> text, out ReadOnlySpan<char> tmdbId)
|
2021-03-26 16:06:01 +00:00
|
|
|
|
=> TryFindProviderId(text, "themoviedb.org/movie/", out tmdbId);
|
2021-03-16 20:11:34 +00:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Parses an TMDb id from a series url.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="text">The text with the url to parse.</param>
|
|
|
|
|
/// <param name="tmdbId">The parsed TMDb id.</param>
|
|
|
|
|
/// <returns>True if parsing was successful, false otherwise.</returns>
|
2021-08-04 12:40:09 +00:00
|
|
|
|
public static bool TryFindTmdbSeriesId(ReadOnlySpan<char> text, out ReadOnlySpan<char> tmdbId)
|
2021-03-26 16:06:01 +00:00
|
|
|
|
=> TryFindProviderId(text, "themoviedb.org/tv/", out tmdbId);
|
2021-03-16 20:11:34 +00:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Parses an TVDb id from a url.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="text">The text with the url to parse.</param>
|
|
|
|
|
/// <param name="tvdbId">The parsed TVDb id.</param>
|
|
|
|
|
/// <returns>True if parsing was successful, false otherwise.</returns>
|
2021-08-04 12:40:09 +00:00
|
|
|
|
public static bool TryFindTvdbId(ReadOnlySpan<char> text, out ReadOnlySpan<char> tvdbId)
|
2021-03-26 16:06:01 +00:00
|
|
|
|
=> TryFindProviderId(text, "thetvdb.com/?tab=series&id=", out tvdbId);
|
2021-03-16 20:11:34 +00:00
|
|
|
|
|
2021-03-26 16:06:01 +00:00
|
|
|
|
private static bool TryFindProviderId(ReadOnlySpan<char> text, ReadOnlySpan<char> searchString, [NotNullWhen(true)] out ReadOnlySpan<char> providerId)
|
2021-03-16 20:11:34 +00:00
|
|
|
|
{
|
2021-03-18 19:52:56 +00:00
|
|
|
|
var searchPos = text.IndexOf(searchString);
|
2021-03-18 10:25:58 +00:00
|
|
|
|
if (searchPos == -1)
|
2021-03-16 20:11:34 +00:00
|
|
|
|
{
|
2021-03-18 10:25:58 +00:00
|
|
|
|
providerId = default;
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2021-03-16 20:11:34 +00:00
|
|
|
|
|
2021-03-18 19:52:56 +00:00
|
|
|
|
text = text.Slice(searchPos + searchString.Length);
|
2021-03-16 20:11:34 +00:00
|
|
|
|
|
2021-03-18 10:25:58 +00:00
|
|
|
|
int i = 0;
|
2021-03-18 19:52:56 +00:00
|
|
|
|
for (; i < text.Length; i++)
|
2021-03-18 10:25:58 +00:00
|
|
|
|
{
|
2021-03-18 19:52:56 +00:00
|
|
|
|
var c = text[i];
|
2021-03-16 20:11:34 +00:00
|
|
|
|
|
2021-03-19 11:34:21 +00:00
|
|
|
|
if (!IsDigit(c))
|
2021-03-16 20:11:34 +00:00
|
|
|
|
{
|
2021-03-18 10:25:58 +00:00
|
|
|
|
break;
|
2021-03-16 20:11:34 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2021-03-18 10:25:58 +00:00
|
|
|
|
|
|
|
|
|
if (i >= 1)
|
|
|
|
|
{
|
2021-03-26 16:06:01 +00:00
|
|
|
|
providerId = text.Slice(0, i);
|
2021-03-18 10:25:58 +00:00
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
providerId = default;
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2021-03-19 11:34:21 +00:00
|
|
|
|
|
|
|
|
|
private static bool IsDigit(char c)
|
|
|
|
|
{
|
|
|
|
|
return c >= '0' && c <= '9';
|
|
|
|
|
}
|
2021-03-16 20:11:34 +00:00
|
|
|
|
}
|
|
|
|
|
}
|