mirror of
https://github.com/Readarr/Readarr
synced 2026-01-04 14:43:42 +01:00
New: Ensure covers up to date with Goodreads
This commit is contained in:
parent
709a945531
commit
9100191ad0
4 changed files with 43 additions and 10 deletions
|
|
@ -202,8 +202,6 @@ protected virtual void AddRequestHeaders(HttpWebRequest webRequest, HttpHeader h
|
|||
case "If-Modified-Since":
|
||||
webRequest.IfModifiedSince = HttpHeader.ParseDateTime(header.Value);
|
||||
break;
|
||||
case "Range":
|
||||
throw new NotImplementedException();
|
||||
case "Referer":
|
||||
webRequest.Referer = header.Value;
|
||||
break;
|
||||
|
|
|
|||
|
|
@ -45,7 +45,7 @@ public void Setup()
|
|||
.Build();
|
||||
|
||||
_httpResponse = new HttpResponse(null, new HttpHeader(), "");
|
||||
Mocker.GetMock<IHttpClient>().Setup(c => c.Head(It.IsAny<HttpRequest>())).Returns(_httpResponse);
|
||||
Mocker.GetMock<IHttpClient>().Setup(c => c.Get(It.IsAny<HttpRequest>())).Returns(_httpResponse);
|
||||
}
|
||||
|
||||
[TestCase(".png")]
|
||||
|
|
|
|||
|
|
@ -38,7 +38,7 @@ public bool AlreadyExists(DateTime? serverModifiedDate, long? serverContentLengt
|
|||
return lastModifiedLocal.Value.ToUniversalTime() == serverModifiedDate.Value.ToUniversalTime();
|
||||
}
|
||||
|
||||
return true;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -29,8 +29,7 @@ public class MediaCoverService :
|
|||
IHandleAsync<BookDeletedEvent>,
|
||||
IMapCoversToLocal
|
||||
{
|
||||
private const double HTTP_RATE_LIMIT = 0;
|
||||
private const string USER_AGENT = "Dalvik/2.1.0 (Linux; U; Android 10; Mi A2 Build/QKQ1.190910.002)";
|
||||
private const string USER_AGENT = "Dalvik/2.1.0 (Linux; U; Android 10; SM-G975U Build/QP1A.190711.020)";
|
||||
|
||||
private readonly IImageResizer _resizer;
|
||||
private readonly IBookService _bookService;
|
||||
|
|
@ -127,11 +126,13 @@ private void EnsureArtistCovers(Author author)
|
|||
|
||||
try
|
||||
{
|
||||
alreadyExists = _coverExistsSpecification.AlreadyExists(null, null, fileName);
|
||||
var serverFileHeaders = GetServerHeaders(cover.Url);
|
||||
|
||||
alreadyExists = _coverExistsSpecification.AlreadyExists(serverFileHeaders.LastModified, GetContentLength(serverFileHeaders), fileName);
|
||||
|
||||
if (!alreadyExists)
|
||||
{
|
||||
DownloadCover(author, cover, DateTime.Now);
|
||||
DownloadCover(author, cover, serverFileHeaders.LastModified ?? DateTime.Now);
|
||||
}
|
||||
}
|
||||
catch (WebException e)
|
||||
|
|
@ -169,11 +170,13 @@ public void EnsureAlbumCovers(Book book)
|
|||
var alreadyExists = false;
|
||||
try
|
||||
{
|
||||
alreadyExists = _coverExistsSpecification.AlreadyExists(null, null, fileName);
|
||||
var serverFileHeaders = GetServerHeaders(cover.Url);
|
||||
|
||||
alreadyExists = _coverExistsSpecification.AlreadyExists(serverFileHeaders.LastModified, GetContentLength(serverFileHeaders), fileName);
|
||||
|
||||
if (!alreadyExists)
|
||||
{
|
||||
DownloadAlbumCover(book, cover, DateTime.Now);
|
||||
DownloadAlbumCover(book, cover, serverFileHeaders.LastModified ?? DateTime.Now);
|
||||
}
|
||||
}
|
||||
catch (WebException e)
|
||||
|
|
@ -269,6 +272,38 @@ private int[] GetDefaultHeights(MediaCoverTypes coverType)
|
|||
}
|
||||
}
|
||||
|
||||
private HttpHeader GetServerHeaders(string url)
|
||||
{
|
||||
// Goodreads doesn't allow a HEAD, so request a zero byte range instead
|
||||
var request = new HttpRequest(url)
|
||||
{
|
||||
AllowAutoRedirect = true,
|
||||
};
|
||||
|
||||
request.Headers.Add("Range", "bytes=0-0");
|
||||
request.Headers.Add("User-Agent", USER_AGENT);
|
||||
|
||||
return _httpClient.Get(request).Headers;
|
||||
}
|
||||
|
||||
private long? GetContentLength(HttpHeader headers)
|
||||
{
|
||||
var range = headers.Get("content-range");
|
||||
|
||||
if (range == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
var split = range.Split('/');
|
||||
if (split.Length == 2 && long.TryParse(split[1], out long length))
|
||||
{
|
||||
return length;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public void HandleAsync(AuthorRefreshCompleteEvent message)
|
||||
{
|
||||
EnsureArtistCovers(message.Author);
|
||||
|
|
|
|||
Loading…
Reference in a new issue