Skip to content
This repository has been archived by the owner on Jul 30, 2022. It is now read-only.

Commit

Permalink
More general error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
imaginary-upside committed Sep 16, 2019
1 parent f355dfc commit c4f5ba8
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 31 deletions.
4 changes: 2 additions & 2 deletions JellyfinJav/Providers/Asianscreens/AsianscreensApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,14 @@ public async Task loadActress(string id)
public DateTime? getBirthdate()
{
var rx = new Regex("<B>DOB:.*\n.*>(.*)<\\/FONT>", RegexOptions.Compiled);
var match = rx.Match(html)?.Groups[1].Value;
var match = rx.Match(html)?.Groups[1].Value.Trim();

if ("n/a" == match || String.IsNullOrEmpty(match))
{
return null;
}

return DateTime.Parse(match.Trim());
return DateTime.Parse(match);
}

public string getCover()
Expand Down
27 changes: 16 additions & 11 deletions JellyfinJav/Providers/R18/R18Api.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,23 @@ class R18Api
private string html;

public string id;
public bool successful = false;

public R18Api()
{
this.httpClient = new HttpClient();
}

public async Task findVideo(string code)
public async Task<bool> findVideo(string code)
{
id = await getProductId(code);
if (String.IsNullOrEmpty(id))
{
return false;
}

await loadVideo(id);

return true;
}

public async Task loadVideo(string id)
Expand Down Expand Up @@ -93,26 +99,25 @@ public IEnumerable<string> getCategories()
var rx = new Regex("<dd itemprop=\"dateCreated\">(.*\n.*)<br>", RegexOptions.Compiled);
var match = rx.Match(html);

var date = match?.Groups[1].Value.Trim();
if (String.IsNullOrEmpty(date))
{
return null;
}

// r18.com uses non-standard abreviations which c# doesn't natively support.
var culture = new CultureInfo("en-US");
culture.DateTimeFormat.AbbreviatedMonthNames = new string[]
{
"Jan.", "Feb.", "Mar.", "Apr.", "May", "June", "July", "Aug.", "Sept.", "Oct.", "Nov.", "Dec.", ""
};

try
{
return DateTime.Parse(match?.Groups[1].Value.Trim(), culture);
}
catch
{
return null;
}
return DateTime.Parse(date, culture);
}

public string getStudio()
{
var rx = new Regex("<a .* itemprop=\"name\">\\s*(.*)<\\/a>", RegexOptions.Compiled);
var rx = new Regex("<a .* itemprop=\"name\">\\s*(.*)\\n?<\\/a>", RegexOptions.Compiled);
var match = rx.Match(html);

return match?.Groups[1].Value.Trim();
Expand Down
23 changes: 13 additions & 10 deletions JellyfinJav/Providers/R18/R18ImageProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,41 +8,44 @@
using MediaBrowser.Controller.Providers;
using MediaBrowser.Model.Entities;
using MediaBrowser.Model.Providers;
using Microsoft.Extensions.Logging;

namespace JellyfinJav.Providers.R18
{
public class R18ImageProvider : IRemoteImageProvider
{
private readonly IHttpClient httpClient;
private readonly ILogger logger;

public string Name => "R18";

public R18ImageProvider(IHttpClient httpClient)
public R18ImageProvider(IHttpClient httpClient, ILogger logger)
{
this.httpClient = httpClient;
this.logger = logger;
}

public Task<IEnumerable<RemoteImageInfo>> GetImages(BaseItem item, CancellationToken cancelToken)
{
var result = new List<RemoteImageInfo>();
logger.LogInformation("[JellyfinJav] Finding images for: " + item.Name);

var id = item.GetProviderId("R18");
if (string.IsNullOrEmpty(id))
{
return Task.FromResult<IEnumerable<RemoteImageInfo>>(result);
return Task.FromResult<IEnumerable<RemoteImageInfo>>(new RemoteImageInfo[] { });
}


// probably should be downloading the full size image, and then cropping the front cover
var primaryImage = String.Format("https://pics.r18.com/digital/video/{0}/{0}ps.jpg", id);
result.Add(new RemoteImageInfo
return Task.FromResult<IEnumerable<RemoteImageInfo>>(new RemoteImageInfo[]
{
ProviderName = Name,
Type = ImageType.Primary,
Url = primaryImage
new RemoteImageInfo
{
ProviderName = Name,
Type = ImageType.Primary,
Url = primaryImage
}
});

return Task.FromResult<IEnumerable<RemoteImageInfo>>(result);
}

public Task<HttpResponseInfo> GetImageResponse(string url, CancellationToken cancelToken)
Expand Down
30 changes: 22 additions & 8 deletions JellyfinJav/Providers/R18/R18Provider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,47 +9,61 @@
using MediaBrowser.Model.Entities;
using MediaBrowser.Model.Providers;
using MediaBrowser.Controller.Configuration;
using Microsoft.Extensions.Logging;

namespace JellyfinJav.Providers.R18
{
public class R18Provider : IRemoteMetadataProvider<Movie, MovieInfo>
{
private readonly IServerConfigurationManager configManager;
private readonly IHttpClient httpClient;
private readonly ILogger logger;

public string Name => "R18";

public R18Provider(IServerConfigurationManager configManager,
IHttpClient httpClient)
IHttpClient httpClient,
ILogger logger)
{
this.configManager = configManager;
this.httpClient = httpClient;
this.logger = logger;
}

public async Task<MetadataResult<Movie>> GetMetadata(MovieInfo info,
CancellationToken cancelToken)
{
logger.LogInformation("[JellyfinJav] Scanning: " + info.Name);

var result = new MetadataResult<Movie>();

var r18Client = new R18Api();

if (info.ProviderIds.ContainsKey("R18"))
{
await r18Client.loadVideo(info.ProviderIds["R18"]);
}
else
{
await r18Client.findVideo(info.Name);
if (!await r18Client.findVideo(info.Name))
{
return result;
}
}

result.Item = new Movie();
result.Item = new Movie
{
OriginalTitle = info.Name,
Name = r18Client.getTitle(),
PremiereDate = r18Client.getReleaseDate()
};
result.Item.ProviderIds.Add("R18", r18Client.id);
result.Item.OriginalTitle = info.Name;
result.Item.Name = r18Client.getTitle();
result.Item.PremiereDate = r18Client.getReleaseDate();
result.Item.Studios = new string[] { r18Client.getStudio() };
result.HasMetadata = true;

if (!String.IsNullOrEmpty(r18Client.getStudio()))
{
result.Item.AddStudio(r18Client.getStudio());
}

foreach (string category in r18Client.getCategories())
{
result.Item.AddGenre(category);
Expand Down

0 comments on commit c4f5ba8

Please sign in to comment.