| | 1 | | using System; |
| | 2 | | using System.Diagnostics.CodeAnalysis; |
| | 3 | |
|
| | 4 | | namespace MediaBrowser.Common.Providers |
| | 5 | | { |
| | 6 | | /// <summary> |
| | 7 | | /// Parsers for provider ids. |
| | 8 | | /// </summary> |
| | 9 | | public static class ProviderIdParsers |
| | 10 | | { |
| | 11 | | private const int ImdbMinNumbers = 7; |
| | 12 | | private const int ImdbMaxNumbers = 8; |
| | 13 | | private const string ImdbPrefix = "tt"; |
| | 14 | |
|
| | 15 | | /// <summary> |
| | 16 | | /// Parses an IMDb id from a string. |
| | 17 | | /// </summary> |
| | 18 | | /// <param name="text">The text to parse.</param> |
| | 19 | | /// <param name="imdbId">The parsed IMDb id.</param> |
| | 20 | | /// <returns>True if parsing was successful, false otherwise.</returns> |
| | 21 | | public static bool TryFindImdbId(ReadOnlySpan<char> text, out ReadOnlySpan<char> imdbId) |
| | 22 | | { |
| | 23 | | // IMDb id is at least 9 chars (tt + 7 numbers) |
| 34 | 24 | | while (text.Length >= 2 + ImdbMinNumbers) |
| | 25 | | { |
| 26 | 26 | | var ttPos = text.IndexOf(ImdbPrefix); |
| 26 | 27 | | if (ttPos == -1) |
| | 28 | | { |
| 3 | 29 | | imdbId = default; |
| 3 | 30 | | return false; |
| | 31 | | } |
| | 32 | |
|
| 23 | 33 | | text = text.Slice(ttPos); |
| 23 | 34 | | var i = 2; |
| 23 | 35 | | var limit = Math.Min(text.Length, ImdbMaxNumbers + 2); |
| 215 | 36 | | for (; i < limit; i++) |
| | 37 | | { |
| 109 | 38 | | var c = text[i]; |
| 109 | 39 | | if (!IsDigit(c)) |
| | 40 | | { |
| | 41 | | break; |
| | 42 | | } |
| | 43 | | } |
| | 44 | |
|
| | 45 | | // Skip if more than 8 digits + 2 chars for tt |
| 23 | 46 | | if (i <= ImdbMaxNumbers + 2 && i >= ImdbMinNumbers + 2) |
| | 47 | | { |
| 12 | 48 | | imdbId = text.Slice(0, i); |
| 12 | 49 | | return true; |
| | 50 | | } |
| | 51 | |
|
| 11 | 52 | | text = text.Slice(i); |
| | 53 | | } |
| | 54 | |
|
| 8 | 55 | | imdbId = default; |
| 8 | 56 | | return false; |
| | 57 | | } |
| | 58 | |
|
| | 59 | | /// <summary> |
| | 60 | | /// Parses an TMDb id from a movie url. |
| | 61 | | /// </summary> |
| | 62 | | /// <param name="text">The text with the url to parse.</param> |
| | 63 | | /// <param name="tmdbId">The parsed TMDb id.</param> |
| | 64 | | /// <returns>True if parsing was successful, false otherwise.</returns> |
| | 65 | | public static bool TryFindTmdbMovieId(ReadOnlySpan<char> text, out ReadOnlySpan<char> tmdbId) |
| 10 | 66 | | => TryFindProviderId(text, "themoviedb.org/movie/", out tmdbId); |
| | 67 | |
|
| | 68 | | /// <summary> |
| | 69 | | /// Parses an TMDb id from a series url. |
| | 70 | | /// </summary> |
| | 71 | | /// <param name="text">The text with the url to parse.</param> |
| | 72 | | /// <param name="tmdbId">The parsed TMDb id.</param> |
| | 73 | | /// <returns>True if parsing was successful, false otherwise.</returns> |
| | 74 | | public static bool TryFindTmdbSeriesId(ReadOnlySpan<char> text, out ReadOnlySpan<char> tmdbId) |
| 6 | 75 | | => TryFindProviderId(text, "themoviedb.org/tv/", out tmdbId); |
| | 76 | |
|
| | 77 | | /// <summary> |
| | 78 | | /// Parses an TVDb id from a url. |
| | 79 | | /// </summary> |
| | 80 | | /// <param name="text">The text with the url to parse.</param> |
| | 81 | | /// <param name="tvdbId">The parsed TVDb id.</param> |
| | 82 | | /// <returns>True if parsing was successful, false otherwise.</returns> |
| | 83 | | public static bool TryFindTvdbId(ReadOnlySpan<char> text, out ReadOnlySpan<char> tvdbId) |
| 6 | 84 | | => TryFindProviderId(text, "thetvdb.com/?tab=series&id=", out tvdbId); |
| | 85 | |
|
| | 86 | | private static bool TryFindProviderId(ReadOnlySpan<char> text, ReadOnlySpan<char> searchString, [NotNullWhen(tru |
| | 87 | | { |
| 22 | 88 | | var searchPos = text.IndexOf(searchString); |
| 22 | 89 | | if (searchPos == -1) |
| | 90 | | { |
| 10 | 91 | | providerId = default; |
| 10 | 92 | | return false; |
| | 93 | | } |
| | 94 | |
|
| 12 | 95 | | text = text.Slice(searchPos + searchString.Length); |
| | 96 | |
|
| 12 | 97 | | int i = 0; |
| 106 | 98 | | for (; i < text.Length; i++) |
| | 99 | | { |
| 55 | 100 | | var c = text[i]; |
| | 101 | |
|
| 55 | 102 | | if (!IsDigit(c)) |
| | 103 | | { |
| | 104 | | break; |
| | 105 | | } |
| | 106 | | } |
| | 107 | |
|
| 12 | 108 | | if (i >= 1) |
| | 109 | | { |
| 9 | 110 | | providerId = text.Slice(0, i); |
| 9 | 111 | | return true; |
| | 112 | | } |
| | 113 | |
|
| 3 | 114 | | providerId = default; |
| 3 | 115 | | return false; |
| | 116 | | } |
| | 117 | |
|
| | 118 | | private static bool IsDigit(char c) |
| | 119 | | { |
| 164 | 120 | | return c >= '0' && c <= '9'; |
| | 121 | | } |
| | 122 | | } |
| | 123 | | } |