Skip to content

Commit

Permalink
Ember.Plugins: Implement movie image scraper.
Browse files Browse the repository at this point in the history
Signed-off-by: Andrew Fyfe <andrew@neptune-one.net>
  • Loading branch information
Andrew Fyfe committed Sep 6, 2012
1 parent f93081f commit 8fa5450
Show file tree
Hide file tree
Showing 6 changed files with 225 additions and 4 deletions.
18 changes: 18 additions & 0 deletions Ember.Plugins/Dummy/DummyPlugin.cs
Expand Up @@ -37,6 +37,8 @@ public override void InitPlugin(PluginManager manager)

manager.MovieScraper.PreMovieInfoScrape += PreMovieInfoScraperAction;
manager.MovieScraper.PostMovieInfoScrape += PostMovieInfoScraperAction;
manager.MovieScraper.PreMovieImageScrape += PreMovieImageScraperAction;
manager.MovieScraper.PostMovieImageScrape += PostMovieImageScraperAction;
}

#endregion Methods
Expand All @@ -62,6 +64,22 @@ public override void InitPlugin(PluginManager manager)
return result;
}

private MovieImageScraperActionContext PreMovieImageScraperAction(
MovieImageScraperActionContext context)
{
if (log.IsDebugEnabled)
log.Debug("PreMovieImageScraperAction[Dummy]");
return context;
}

private PluginActionResult PostMovieImageScraperAction(
PluginActionResult result)
{
if (log.IsDebugEnabled)
log.Debug("PostMovieImageScraperAction[Dummy]");
return result;
}

#endregion EventHandlers

}
Expand Down
1 change: 1 addition & 0 deletions Ember.Plugins/Ember.Plugins.csproj
Expand Up @@ -69,6 +69,7 @@
<DependentUpon>Settings.settings</DependentUpon>
</Compile>
<Compile Include="Scraper\Exceptions.cs" />
<Compile Include="Scraper\ImageScrapeActionContext.cs" />
<Compile Include="Scraper\IMovieImageScraper.cs" />
<Compile Include="Scraper\IMovieInfoScraper.cs" />
<Compile Include="Scraper\ITVImageScraper.cs" />
Expand Down
16 changes: 16 additions & 0 deletions Ember.Plugins/Events/MovieScraper.cs
Expand Up @@ -19,4 +19,20 @@ public delegate MovieInfoScraperActionContext
public delegate PluginActionResult
PostMovieInfoScraperActionHandler(PluginActionResult result);

/// <summary>
/// Delegate for PreMovieImageScraperAction.
/// </summary>
/// <param name="context">The context.</param>
/// <returns></returns>
public delegate MovieImageScraperActionContext
PreMovieImageScraperActionHandler(MovieImageScraperActionContext context);

/// <summary>
/// Delegate for PostMovieImageScraperAction.
/// </summary>
/// <param name="result">The result.</param>
/// <returns></returns>
public delegate PluginActionResult
PostMovieImageScraperActionHandler(PluginActionResult result);

}
61 changes: 60 additions & 1 deletion Ember.Plugins/Scraper/IMovieImageScraper.cs
@@ -1,9 +1,68 @@
namespace Ember.Plugins.Scraper
using EmberAPI;

namespace Ember.Plugins.Scraper
{
/// <summary>
/// Defines a movie image scraper.
/// </summary>
public interface IMovieImageScraper
{

/// <summary>
/// Scrapes the movie posters.
/// </summary>
/// <param name="context">The context.</param>
/// <returns></returns>
PluginActionResult ScrapeMovieImage(MovieImageScraperActionContext context);

}

/// <summary>
/// Context for a movie image scraper action.
/// </summary>
public class MovieImageScraperActionContext
: ImageScrapeActionContext
{

#region Fields

private Structures.DBMovie dbMovie;

#endregion Fields


#region Properties

/// <summary>
/// Gets the movie.
/// </summary>
public Structures.DBMovie DBMovie
{
get { return dbMovie; }
}

#endregion Properties


#region Constructor

/// <summary>
/// Initializes a new instance of the <see cref="MovieImageScraperActionContext"/> class.
/// </summary>
/// <param name="dbMovie">The movie.</param>
/// <param name="scrapeType">The type of scrape to perform.</param>
/// <param name="askIfMultipleResults">if set to <c>true</c> ask the user to select a movie if multiple results are found.</param>
public MovieImageScraperActionContext(
Structures.DBMovie dbMovie,
ImageScrapeType imageScrapeType,
ScrapeType scrapeType,
bool askIfMultipleResults)
: base(imageScrapeType, scrapeType, askIfMultipleResults)
{
this.dbMovie = dbMovie;
}

#endregion Constructor

}
}
72 changes: 72 additions & 0 deletions Ember.Plugins/Scraper/ImageScrapeActionContext.cs
@@ -0,0 +1,72 @@
namespace Ember.Plugins.Scraper
{

/// <summary>
/// The type of image scrape to perform.
/// </summary>
public enum ImageScrapeType
{
/// <summary>
/// Scrape a poster.
/// </summary>
Poster,

/// <summary>
/// Scrape fanart.
/// </summary>
Fanart,
}


/// <summary>
/// Context for image scraper actions.
/// </summary>
public class ImageScrapeActionContext
: ScraperActionContext
{

#region Fields

private ImageScrapeType imageScrapeType;

#endregion Fields


#region Properties

/// <summary>
/// Gets the type of image scrape to perform.
/// </summary>
/// <value>
/// The type of image scrape to perform.
/// </value>
public ImageScrapeType ImageScrapeType
{
get { return imageScrapeType; }
}

#endregion Properties


#region Constructor

/// <summary>
/// Initializes a new instance of the <see cref="ImageScrapeActionContext"/> class.
/// </summary>
/// <param name="imageScrapeType">Type of the image scrape.</param>
/// <param name="scrapeType">Type of the scrape.</param>
/// <param name="askIfMultipleResults">if set to <c>true</c> [ask if multiple results].</param>
public ImageScrapeActionContext(
ImageScrapeType imageScrapeType,
ScrapeType scrapeType,
bool askIfMultipleResults)
: base(scrapeType, askIfMultipleResults)
{
this.imageScrapeType = imageScrapeType;
}

#endregion Constructor

}

}
61 changes: 58 additions & 3 deletions Ember.Plugins/Scraper/MovieScraperManager.cs
Expand Up @@ -12,16 +12,31 @@ public class MovieScraperManager
: IDisposable, IMovieInfoScraper, IMovieImageScraper
{

#region Events

/// <summary>
/// Occurs before a movie is scraped.
/// Occurs before movie information is scraped.
/// </summary>
public event Events.PreMovieInfoScraperActionHandler PreMovieInfoScrape;

/// <summary>
/// Occurs after a movie is scraped.
/// Occurs after movie information is scraped.
/// </summary>
public event Events.PostMovieInfoScraperActionHandler PostMovieInfoScrape;

/// <summary>
/// Occurs before movie poster is scraped.
/// </summary>
public event Events.PreMovieImageScraperActionHandler PreMovieImageScrape;

/// <summary>
/// Occurs after movie poster is scraped.
/// </summary>
public event Events.PostMovieImageScraperActionHandler PostMovieImageScrape;

#endregion Events


#region Fields

private static readonly log4net.ILog log = log4net.LogManager.GetLogger(
Expand Down Expand Up @@ -62,7 +77,7 @@ public PluginActionResult ScrapeMovieInfo(MovieInfoScraperActionContext context)
PluginActionResult result = null;

foreach (IMovieInfoScraper plugin in manager.Plugins
.Where(p => p.Enabled && p is IMovieInfoScraper)
.Where(p => p.Enabled && p.Plugin is IMovieInfoScraper)
.OrderBy(p => p.Order)
.Select(p => p.Plugin))
{
Expand All @@ -82,6 +97,46 @@ public PluginActionResult ScrapeMovieInfo(MovieInfoScraperActionContext context)
#endregion IMovieInfoScraper


#region IMovieImageScraper

/// <summary>
/// Scrapes the movie posters.
/// </summary>
/// <param name="context">The context.</param>
/// <returns></returns>
public PluginActionResult ScrapeMovieImage(MovieImageScraperActionContext context)
{
if (context == null)
throw new ArgumentNullException("context");
if (manager.Plugins.Count == 0)
return new PluginActionResult();

if (PreMovieImageScrape != null)
context = PreMovieImageScrape(context);

PluginActionResult result = null;

foreach (IMovieImageScraper plugin in manager.Plugins
.Where(p => p.Enabled && p.Plugin is IMovieImageScraper)
.OrderBy(p => p.Order)
.Select(p => p.Plugin))
{
result = plugin.ScrapeMovieImage(context);
if (result != null && result.BreakChain) break;
}

if (result == null)
result = new PluginActionResult();

if (PostMovieImageScrape != null)
result = PostMovieImageScrape(result);

return result;
}

#endregion IMovieImageScraper


#region IDisposable

#region Fields
Expand Down

0 comments on commit 8fa5450

Please sign in to comment.