2012-04-20 09:42:13 +03:00
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Linq;
|
|
|
|
using System.Text;
|
|
|
|
using NLog;
|
|
|
|
using Ninject;
|
2012-04-21 11:16:15 +03:00
|
|
|
using NzbDrone.Core.Repository;
|
2012-04-20 09:42:13 +03:00
|
|
|
using NzbDrone.Core.Repository.Search;
|
|
|
|
using PetaPoco;
|
|
|
|
|
|
|
|
namespace NzbDrone.Core.Providers
|
|
|
|
{
|
|
|
|
public class SearchResultProvider
|
|
|
|
{
|
|
|
|
private readonly IDatabase _database;
|
2012-04-23 09:31:11 +03:00
|
|
|
private readonly SeriesProvider _seriesProvider;
|
|
|
|
private readonly DownloadProvider _downloadProvider;
|
2012-04-23 09:47:30 +03:00
|
|
|
private readonly EpisodeProvider _episodeProvider;
|
|
|
|
|
2012-04-20 09:42:13 +03:00
|
|
|
private static readonly Logger logger = LogManager.GetCurrentClassLogger();
|
|
|
|
|
|
|
|
[Inject]
|
2012-04-23 09:31:11 +03:00
|
|
|
public SearchResultProvider(IDatabase database, SeriesProvider seriesProvider,
|
2012-04-23 09:47:30 +03:00
|
|
|
DownloadProvider downloadProvider, EpisodeProvider episodeProvider)
|
2012-04-20 09:42:13 +03:00
|
|
|
{
|
|
|
|
_database = database;
|
2012-04-23 09:31:11 +03:00
|
|
|
_seriesProvider = seriesProvider;
|
|
|
|
_downloadProvider = downloadProvider;
|
2012-04-23 09:47:30 +03:00
|
|
|
_episodeProvider = episodeProvider;
|
2012-04-20 09:42:13 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
public SearchResultProvider()
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
public virtual void Add(SearchResult searchResult)
|
|
|
|
{
|
|
|
|
logger.Trace("Adding new search result");
|
2012-04-21 11:16:15 +03:00
|
|
|
searchResult.SuccessfulDownload = searchResult.SearchResultItems.Any(s => s.Success);
|
2012-04-20 09:42:13 +03:00
|
|
|
var id = Convert.ToInt32(_database.Insert(searchResult));
|
|
|
|
|
2012-04-21 11:16:15 +03:00
|
|
|
searchResult.SearchResultItems.ForEach(s => s.SearchResultId = id);
|
2012-04-20 09:42:13 +03:00
|
|
|
logger.Trace("Adding search result items");
|
|
|
|
_database.InsertMany(searchResult.SearchResultItems);
|
|
|
|
}
|
|
|
|
|
|
|
|
public virtual void Delete(int id)
|
|
|
|
{
|
|
|
|
logger.Trace("Deleting search result items attached to: {0}", id);
|
|
|
|
_database.Execute("DELETE FROM SearchResultItems WHERE SearchResultId = @0", id);
|
|
|
|
|
|
|
|
logger.Trace("Deleting search result: {0}", id);
|
|
|
|
_database.Delete<SearchResult>(id);
|
|
|
|
}
|
|
|
|
|
|
|
|
public virtual List<SearchResult> AllSearchResults()
|
|
|
|
{
|
2012-04-21 11:16:15 +03:00
|
|
|
var sql = @"SELECT SearchResults.Id, SearchResults.SeriesId, SearchResults.SeasonNumber,
|
|
|
|
SearchResults.EpisodeId, SearchResults.SearchTime,
|
|
|
|
Series.Title as SeriesTitle, Series.IsDaily,
|
|
|
|
Episodes.EpisodeNumber, Episodes.SeasonNumber, Episodes.Title as EpisodeTitle,
|
|
|
|
Episodes.AirDate,
|
|
|
|
Count(SearchResultItems.Id) as TotalItems,
|
2012-04-23 09:31:11 +03:00
|
|
|
SUM(CASE WHEN SearchResultItems.Success = 1 THEN 1 ELSE 0 END) as SuccessfulCount
|
2012-04-21 11:16:15 +03:00
|
|
|
FROM SearchResults
|
|
|
|
INNER JOIN Series
|
|
|
|
ON Series.SeriesId = SearchResults.SeriesId
|
|
|
|
LEFT JOIN Episodes
|
|
|
|
ON Episodes.EpisodeId = SearchResults.EpisodeId
|
|
|
|
INNER JOIN SearchResultItems
|
|
|
|
ON SearchResultItems.SearchResultId = SearchResults.Id
|
|
|
|
GROUP BY SearchResults.Id, SearchResults.SeriesId, SearchResults.SeasonNumber,
|
|
|
|
SearchResults.EpisodeId, SearchResults.SearchTime,
|
|
|
|
Series.Title, Series.IsDaily,
|
|
|
|
Episodes.EpisodeNumber, Episodes.SeasonNumber, Episodes.Title,
|
|
|
|
Episodes.AirDate";
|
|
|
|
|
|
|
|
return _database.Fetch<SearchResult>(sql);
|
2012-04-20 09:42:13 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
public virtual SearchResult GetSearchResult(int id)
|
|
|
|
{
|
2012-04-23 09:31:11 +03:00
|
|
|
var sql = @"SELECT SearchResults.Id, SearchResults.SeriesId, SearchResults.SeasonNumber,
|
|
|
|
SearchResults.EpisodeId, SearchResults.SearchTime,
|
|
|
|
Series.Title as SeriesTitle, Series.IsDaily,
|
|
|
|
Episodes.EpisodeNumber, Episodes.SeasonNumber, Episodes.Title as EpisodeTitle,
|
|
|
|
Episodes.AirDate
|
|
|
|
FROM SearchResults
|
|
|
|
INNER JOIN Series
|
|
|
|
ON Series.SeriesId = SearchResults.SeriesId
|
|
|
|
LEFT JOIN Episodes
|
|
|
|
ON Episodes.EpisodeId = SearchResults.EpisodeId
|
|
|
|
WHERE SearchResults.Id = @0";
|
|
|
|
|
|
|
|
var result = _database.Single<SearchResult>(sql, id);
|
2012-04-20 09:42:13 +03:00
|
|
|
result.SearchResultItems = _database.Fetch<SearchResultItem>("WHERE SearchResultId = @0", id);
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
2012-04-23 09:31:11 +03:00
|
|
|
|
|
|
|
public virtual void ForceDownload(int itemId)
|
|
|
|
{
|
|
|
|
var item = _database.Single<SearchResultItem>(itemId);
|
|
|
|
var searchResult = _database.Single<SearchResult>(item.SearchResultId);
|
|
|
|
var series = _seriesProvider.GetSeries(searchResult.SeriesId);
|
2012-04-23 09:47:30 +03:00
|
|
|
|
2012-04-23 09:31:11 +03:00
|
|
|
var parseResult = Parser.ParseTitle(item.ReportTitle);
|
|
|
|
parseResult.NzbUrl = item.NzbUrl;
|
|
|
|
parseResult.Series = series;
|
|
|
|
parseResult.Indexer = item.Indexer;
|
2012-04-23 09:47:30 +03:00
|
|
|
var episodes = _episodeProvider.GetEpisodesByParseResult(parseResult);
|
2012-04-23 09:31:11 +03:00
|
|
|
|
|
|
|
_downloadProvider.DownloadReport(parseResult);
|
|
|
|
}
|
2012-04-20 09:42:13 +03:00
|
|
|
}
|
|
|
|
}
|