ProviderIdParsers.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. #nullable enable
  2. using System;
  3. using System.Diagnostics.CodeAnalysis;
  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. /// <summary>
  14. /// Parses an IMDb id from a string.
  15. /// </summary>
  16. /// <param name="text">The text to parse.</param>
  17. /// <param name="imdbId">The parsed IMDb id.</param>
  18. /// <returns>True if parsing was successful, false otherwise.</returns>
  19. public static bool TryParseImdbId(string text, [NotNullWhen(true)] out string? imdbId)
  20. {
  21. var span = text.AsSpan();
  22. var tt = "tt".AsSpan();
  23. // imdb id is at least 9 chars (tt + 7 numbers)
  24. while (span.Length >= 2 + ImdbMinNumbers)
  25. {
  26. var ttPos = span.IndexOf(tt);
  27. if (ttPos == -1)
  28. {
  29. imdbId = default;
  30. return false;
  31. }
  32. span = span.Slice(ttPos + tt.Length);
  33. var i = 0;
  34. for (; i < Math.Min(span.Length, ImdbMaxNumbers); i++)
  35. {
  36. var c = span[i];
  37. if (!IsDigit(c))
  38. {
  39. break;
  40. }
  41. }
  42. // skip if more than 8 digits
  43. if (i <= ImdbMaxNumbers && i >= ImdbMinNumbers)
  44. {
  45. imdbId = string.Concat(tt, span.Slice(0, i));
  46. return true;
  47. }
  48. span = span.Slice(i);
  49. }
  50. imdbId = default;
  51. return false;
  52. }
  53. /// <summary>
  54. /// Parses an TMDb id from a movie url.
  55. /// </summary>
  56. /// <param name="text">The text with the url to parse.</param>
  57. /// <param name="tmdbId">The parsed TMDb id.</param>
  58. /// <returns>True if parsing was successful, false otherwise.</returns>
  59. public static bool TryParseTmdbMovieId(string text, [NotNullWhen(true)] out string? tmdbId)
  60. => TryParseProviderId(text, "themoviedb.org/movie/", out tmdbId);
  61. /// <summary>
  62. /// Parses an TMDb id from a series url.
  63. /// </summary>
  64. /// <param name="text">The text with the url to parse.</param>
  65. /// <param name="tmdbId">The parsed TMDb id.</param>
  66. /// <returns>True if parsing was successful, false otherwise.</returns>
  67. public static bool TryParseTmdbSeriesId(string text, [NotNullWhen(true)] out string? tmdbId)
  68. => TryParseProviderId(text, "themoviedb.org/tv/", out tmdbId);
  69. /// <summary>
  70. /// Parses an TVDb id from a url.
  71. /// </summary>
  72. /// <param name="text">The text with the url to parse.</param>
  73. /// <param name="tvdbId">The parsed TVDb id.</param>
  74. /// <returns>True if parsing was successful, false otherwise.</returns>
  75. public static bool TryParseTvdbId(string text, [NotNullWhen(true)] out string? tvdbId)
  76. => TryParseProviderId(text, "thetvdb.com/?tab=series&id=", out tvdbId);
  77. private static bool TryParseProviderId(string text, string searchString, [NotNullWhen(true)] out string? providerId)
  78. {
  79. var span = text.AsSpan();
  80. var searchSpan = searchString.AsSpan();
  81. var searchPos = span.IndexOf(searchSpan);
  82. if (searchPos == -1)
  83. {
  84. providerId = default;
  85. return false;
  86. }
  87. span = span.Slice(searchPos + searchSpan.Length);
  88. int i = 0;
  89. for (; i < span.Length; i++)
  90. {
  91. var c = span[i];
  92. if (!IsDigit(c))
  93. {
  94. break;
  95. }
  96. }
  97. if (i >= 1)
  98. {
  99. providerId = span.Slice(0, i).ToString();
  100. return true;
  101. }
  102. providerId = default;
  103. return false;
  104. }
  105. private static bool IsDigit(char c)
  106. {
  107. return c >= '0' && c <= '9';
  108. }
  109. }
  110. }