22using System . Collections . Generic ;
33using System . Globalization ;
44using System . Linq ;
5+ using System . Text . RegularExpressions ;
56
67namespace Kavita . Common . Helpers ;
78#nullable enable
@@ -14,11 +15,10 @@ public sealed record HardcoverUrlSlug(string Slug, bool IsStandAlone);
1415/// <summary>
1516/// Handles all things parsing of External Ids (weblinks, not set checks, anilist:X)
1617/// </summary>
17- public static class ExternalIdParser
18+ public static partial class ExternalIdParser
1819{
1920 private const string AniListWeblinkWebsite = "https://anilist.co/manga/" ;
2021 private const string MalWeblinkWebsite = "https://myanimelist.net/manga/" ;
21- private const string GoogleBooksWeblinkWebsite = "https://books.google.com/books?id=" ;
2222 private const string MangaDexWeblinkWebsite = "https://mangadex.org/title/" ;
2323 private const string AniListStaffWebsite = "https://anilist.co/staff/" ;
2424 private const string AniListCharacterWebsite = "https://anilist.co/character/" ;
@@ -53,7 +53,6 @@ public static class ExternalIdParser
5353 {
5454 { AniListWeblinkWebsite , 0 } ,
5555 { MalWeblinkWebsite , 0 } ,
56- { GoogleBooksWeblinkWebsite , 0 } ,
5756 { MangaDexWeblinkWebsite , 0 } ,
5857 { AniListStaffWebsite , 0 } ,
5958 { AniListCharacterWebsite , 0 } ,
@@ -97,19 +96,14 @@ public static int GetAniListStaffId(string? url)
9796 return ExtractId < int ? > ( url , AniListStaffWebsite ) ?? 0 ;
9897 }
9998
100- public static string ? GetGoogleBooksId ( string ? weblinks )
101- {
102- return ExtractId < string ? > ( weblinks , GoogleBooksWeblinkWebsite ) ;
103- }
104-
10599 public static string ? GetMangaDexId ( string ? weblinks )
106100 {
107101 return ExtractId < string ? > ( weblinks , MangaDexWeblinkWebsite ) ;
108102 }
109103
110104 public static int GetMangaBakaId ( string ? weblinks )
111105 {
112- return ExtractId < int ? > ( weblinks , MangaBakaWebsite ) ?? 0 ;
106+ return ExtractId < int ? > ( weblinks , MangaBakaWebsite ) ?? ExtractId < int ? > ( weblinks , MangaBakaWebsite , 1 ) ?? 0 ;
113107 }
114108
115109 #region Header-based Parsing
@@ -200,19 +194,26 @@ public static string GetHardcoverStaffId(string? url)
200194 /// </summary>
201195 /// <param name="webLinks"></param>
202196 /// <param name="website"></param>
197+ /// <param name="indexOverride"></param>
203198 /// <returns></returns>
204- private static T ? ExtractId < T > ( string ? webLinks , string website )
199+ private static T ? ExtractId < T > ( string ? webLinks , string website , int ? indexOverride = null )
205200 {
206201 if ( string . IsNullOrEmpty ( webLinks ) ) return default ;
207202
208- var index = WeblinkExtractionMap [ website ] ;
203+ var index = indexOverride ?? WeblinkExtractionMap [ website ] ;
209204 foreach ( var webLink in webLinks . Split ( ',' , StringSplitOptions . RemoveEmptyEntries | StringSplitOptions . TrimEntries ) )
210205 {
211206 if ( ! webLink . StartsWith ( website ) ) continue ;
212207
213208 var tokens = webLink . Split ( website ) [ 1 ] . Split ( '/' ) ;
214209 var value = tokens [ index ] ;
215210
211+ // Clean any query params
212+ if ( QueryParamsRegex ( ) . IsMatch ( value ) )
213+ {
214+ value = value . Split ( '?' ) [ 0 ] ;
215+ }
216+
216217 if ( typeof ( T ) == typeof ( int ? ) )
217218 {
218219 if ( int . TryParse ( value , CultureInfo . InvariantCulture , out var intValue ) ) return ( T ) ( object ) intValue ;
@@ -264,4 +265,7 @@ public static string GetHardcoverStaffId(string? url)
264265
265266 throw new ArgumentException ( "Unsupported ID type. Supported types are int, long, and string." , nameof ( id ) ) ;
266267 }
268+
269+ [ GeneratedRegex ( ".*?\\ D+=.*" ) ]
270+ private static partial Regex QueryParamsRegex ( ) ;
267271}
0 commit comments