mirror of https://github.com/Jackett/Jackett
Add imdbid validation
This commit is contained in:
parent
0c02767d8f
commit
c2e3d0e225
|
@ -1,6 +1,7 @@
|
|||
using AutoMapper;
|
||||
using Jackett.Models;
|
||||
using Jackett.Services;
|
||||
using Jackett.Utils;
|
||||
using NLog;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
@ -12,7 +13,8 @@ using System.Text;
|
|||
using System.Threading.Tasks;
|
||||
using System.Web;
|
||||
using System.Web.Http;
|
||||
|
||||
using System.Xml.Linq;
|
||||
|
||||
namespace Jackett.Controllers
|
||||
{
|
||||
[AllowAnonymous]
|
||||
|
@ -32,6 +34,24 @@ namespace Jackett.Controllers
|
|||
cacheService = c;
|
||||
}
|
||||
|
||||
public HttpResponseMessage GetErrorXML(int code, string description)
|
||||
{
|
||||
var xdoc = new XDocument(
|
||||
new XDeclaration("1.0", "UTF-8", null),
|
||||
new XElement("error",
|
||||
new XAttribute("code", code.ToString()),
|
||||
new XAttribute("description", description)
|
||||
)
|
||||
);
|
||||
|
||||
var xml = xdoc.Declaration.ToString() + Environment.NewLine + xdoc.ToString();
|
||||
|
||||
return new HttpResponseMessage()
|
||||
{
|
||||
Content = new StringContent(xml, Encoding.UTF8, "application/xml")
|
||||
};
|
||||
}
|
||||
|
||||
[HttpGet]
|
||||
public async Task<HttpResponseMessage> Call(string indexerID)
|
||||
{
|
||||
|
@ -62,6 +82,27 @@ namespace Jackett.Controllers
|
|||
{
|
||||
logger.Warn(string.Format("Rejected a request to {0} which is unconfigured.", indexer.DisplayName));
|
||||
return Request.CreateResponse(HttpStatusCode.Forbidden, "This indexer is not configured.");
|
||||
}
|
||||
|
||||
if (torznabQuery.ImdbID != null)
|
||||
{
|
||||
if (torznabQuery.QueryType != "movie")
|
||||
{
|
||||
logger.Warn(string.Format("A non movie request with an imdbid was made from {0}.", Request.GetOwinContext().Request.RemoteIpAddress));
|
||||
return GetErrorXML(201, "Incorrect parameter: only movie-search supports the imdbid parameter");
|
||||
}
|
||||
|
||||
if (torznabQuery.SearchTerm != null)
|
||||
{
|
||||
logger.Warn(string.Format("A movie-search request from {0} was made contining q and imdbid.", Request.GetOwinContext().Request.RemoteIpAddress));
|
||||
return GetErrorXML(201, "Incorrect parameter: please specify either imdbid or q");
|
||||
}
|
||||
|
||||
if (ParseUtil.GetImdbID(torznabQuery.ImdbID) == null)
|
||||
{
|
||||
logger.Warn(string.Format("A movie-search request from {0} was made with an invalid imdbid.", Request.GetOwinContext().Request.RemoteIpAddress));
|
||||
return GetErrorXML(201, "Incorrect parameter: invalid imdbid format");
|
||||
}
|
||||
}
|
||||
|
||||
var releases = await indexer.PerformQuery(torznabQuery);
|
||||
|
|
|
@ -9,6 +9,7 @@ namespace Jackett.Utils
|
|||
new Regex(
|
||||
@"(?<![\uD800-\uDBFF])[\uDC00-\uDFFF]|[\uD800-\uDBFF](?![\uDC00-\uDFFF])|[\x00-\x08\x0B\x0C\x0E-\x1F\x7F-\x9F\uFEFF\uFFFE\uFFFF]",
|
||||
RegexOptions.Compiled);
|
||||
private static readonly Regex ImdbId = new Regex(@"^tt(\d{7})$",RegexOptions.Compiled);
|
||||
|
||||
public static string NormalizeSpace(string s)
|
||||
{
|
||||
|
@ -72,5 +73,14 @@ namespace Jackett.Utils
|
|||
{
|
||||
return long.TryParse(NormalizeNumber(str), NumberStyles.Any, CultureInfo.InvariantCulture, out result);
|
||||
}
|
||||
|
||||
public static int? GetImdbID(string imdbstr)
|
||||
{
|
||||
var match = ImdbId.Match(imdbstr);
|
||||
if (!match.Success)
|
||||
return null;
|
||||
|
||||
return CoerceInt(match.Groups[1].Value);
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue