4
4
using System . Linq ;
5
5
using System . Net ;
6
6
using System . Net . Http ;
7
- using System . Net . Http . Json ;
8
7
using System . Text ;
9
8
using System . Text . RegularExpressions ;
10
9
using System . Threading ;
11
10
using System . Threading . Tasks ;
12
- using Jellyfin . Extensions . Json ;
13
11
using MediaBrowser . Common . Net ;
14
12
using MediaBrowser . Controller . Entities ;
15
13
using MediaBrowser . Controller . Providers ;
@@ -22,7 +20,7 @@ namespace Jellyfin.Plugin.Bookshelf.Providers.GoogleBooks
22
20
/// <summary>
23
21
/// Google books provider.
24
22
/// </summary>
25
- public class GoogleBooksProvider : IRemoteMetadataProvider < Book , BookInfo >
23
+ public class GoogleBooksProvider : BaseGoogleBooksProvider , IRemoteMetadataProvider < Book , BookInfo >
26
24
{
27
25
// convert these characters to whitespace for better matching
28
26
// there are two dashes with different char codes
@@ -69,6 +67,7 @@ public class GoogleBooksProvider : IRemoteMetadataProvider<Book, BookInfo>
69
67
public GoogleBooksProvider (
70
68
ILogger < GoogleBooksProvider > logger ,
71
69
IHttpClientFactory httpClientFactory )
70
+ : base ( logger , httpClientFactory )
72
71
{
73
72
_httpClientFactory = httpClientFactory ;
74
73
_logger = logger ;
@@ -81,38 +80,59 @@ public GoogleBooksProvider(
81
80
public async Task < IEnumerable < RemoteSearchResult > > GetSearchResults ( BookInfo searchInfo , CancellationToken cancellationToken )
82
81
{
83
82
cancellationToken . ThrowIfCancellationRequested ( ) ;
84
- var list = new List < RemoteSearchResult > ( ) ;
85
83
86
- var searchResults = await GetSearchResultsInternal ( searchInfo , cancellationToken ) . ConfigureAwait ( false ) ;
87
- if ( searchResults is null )
84
+ Func < BookResult , RemoteSearchResult > getSearchResultFromBook = ( BookResult info ) =>
88
85
{
89
- return Enumerable . Empty < RemoteSearchResult > ( ) ;
90
- }
86
+ var remoteSearchResult = new RemoteSearchResult ( ) ;
91
87
92
- foreach ( var result in searchResults . Items )
93
- {
94
- if ( result . VolumeInfo is null )
88
+ remoteSearchResult . SetProviderId ( GoogleBooksConstants . ProviderId , info . Id ) ;
89
+ remoteSearchResult . SearchProviderName = GoogleBooksConstants . ProviderName ;
90
+ remoteSearchResult . Name = info . VolumeInfo ? . Title ;
91
+ remoteSearchResult . Overview = WebUtility . HtmlDecode ( info . VolumeInfo ? . Description ) ;
92
+ remoteSearchResult . ProductionYear = GetYearFromPublishedDate ( info . VolumeInfo ? . PublishedDate ) ;
93
+
94
+ if ( info . VolumeInfo ? . ImageLinks ? . Thumbnail != null )
95
95
{
96
- continue ;
96
+ remoteSearchResult . ImageUrl = info . VolumeInfo . ImageLinks . Thumbnail ;
97
97
}
98
98
99
- var remoteSearchResult = new RemoteSearchResult ( ) ;
99
+ return remoteSearchResult ;
100
+ } ;
100
101
101
- remoteSearchResult . SetProviderId ( GoogleBooksConstants . ProviderId , result . Id ) ;
102
- remoteSearchResult . SearchProviderName = GoogleBooksConstants . ProviderName ;
103
- remoteSearchResult . Name = result . VolumeInfo . Title ;
104
- remoteSearchResult . Overview = WebUtility . HtmlDecode ( result . VolumeInfo . Description ) ;
105
- remoteSearchResult . ProductionYear = GetYearFromPublishedDate ( result . VolumeInfo . PublishedDate ) ;
102
+ var googleBookId = searchInfo . GetProviderId ( GoogleBooksConstants . ProviderId ) ;
106
103
107
- if ( result . VolumeInfo . ImageLinks ? . Thumbnail != null )
104
+ if ( ! string . IsNullOrWhiteSpace ( googleBookId ) )
105
+ {
106
+ var bookData = await FetchBookData ( googleBookId , cancellationToken ) . ConfigureAwait ( false ) ;
107
+
108
+ if ( bookData == null || bookData . VolumeInfo == null )
108
109
{
109
- remoteSearchResult . ImageUrl = result . VolumeInfo . ImageLinks . Thumbnail ;
110
+ return Enumerable . Empty < RemoteSearchResult > ( ) ;
110
111
}
111
112
112
- list . Add ( remoteSearchResult ) ;
113
+ return new [ ] { getSearchResultFromBook ( bookData ) } ;
113
114
}
115
+ else
116
+ {
117
+ var searchResults = await GetSearchResultsInternal ( searchInfo , cancellationToken ) . ConfigureAwait ( false ) ;
118
+ if ( searchResults is null )
119
+ {
120
+ return Enumerable . Empty < RemoteSearchResult > ( ) ;
121
+ }
122
+
123
+ var list = new List < RemoteSearchResult > ( ) ;
124
+ foreach ( var result in searchResults . Items )
125
+ {
126
+ if ( result . VolumeInfo is null )
127
+ {
128
+ continue ;
129
+ }
114
130
115
- return list ;
131
+ list . Add ( getSearchResultFromBook ( result ) ) ;
132
+ }
133
+
134
+ return list ;
135
+ }
116
136
}
117
137
118
138
/// <inheritdoc />
@@ -173,11 +193,7 @@ public async Task<HttpResponseMessage> GetImageResponse(string url, Cancellation
173
193
var searchString = GetSearchString ( item ) ;
174
194
var url = string . Format ( CultureInfo . InvariantCulture , GoogleApiUrls . SearchUrl , WebUtility . UrlEncode ( searchString ) , 0 , 20 ) ;
175
195
176
- var httpClient = _httpClientFactory . CreateClient ( NamedClient . Default ) ;
177
-
178
- using var response = await httpClient . GetAsync ( url , cancellationToken ) . ConfigureAwait ( false ) ;
179
-
180
- return await response . Content . ReadFromJsonAsync < SearchResult > ( JsonDefaults . Options , cancellationToken ) . ConfigureAwait ( false ) ;
196
+ return await GetResultFromAPI < SearchResult > ( url , cancellationToken ) . ConfigureAwait ( false ) ;
181
197
}
182
198
183
199
private async Task < string ? > FetchBookId ( BookInfo item , CancellationToken cancellationToken )
@@ -240,19 +256,6 @@ public async Task<HttpResponseMessage> GetImageResponse(string url, Cancellation
240
256
return bookReleaseYear ;
241
257
}
242
258
243
- private async Task < BookResult ? > FetchBookData ( string googleBookId , CancellationToken cancellationToken )
244
- {
245
- cancellationToken . ThrowIfCancellationRequested ( ) ;
246
-
247
- var url = string . Format ( CultureInfo . InvariantCulture , GoogleApiUrls . DetailsUrl , googleBookId ) ;
248
-
249
- var httpClient = _httpClientFactory . CreateClient ( NamedClient . Default ) ;
250
-
251
- using var response = await httpClient . GetAsync ( url , cancellationToken ) . ConfigureAwait ( false ) ;
252
-
253
- return await response . Content . ReadFromJsonAsync < BookResult > ( JsonDefaults . Options , cancellationToken ) . ConfigureAwait ( false ) ;
254
- }
255
-
256
259
private Book ? ProcessBookData ( BookResult bookResult , CancellationToken cancellationToken )
257
260
{
258
261
if ( bookResult . VolumeInfo is null )
0 commit comments