- MyDownloader_demo.zip
- ffmpeg.exe
- ICSharpCode.SharpZipLib.dll
- MyDownloader.App.exe
- MyDownloader.App.exe.config
- MyDownloader.Core.dll
- MyDownloader.Extension.dll
- MyDownloader.Spider.dll
- pthreadGC2.dll
- TabStrip.dll
- MyDownloader_src.zip
- ICSharpCode.SharpZipLib.dll
- Interop.SHDocVw.dll
- MyDownloader.App
- MyDownloader.Core
- MyDownloader.Extension
- MyDownloader.IEPlugin
- MyDownloader.sln
- MyDownloader.Spider
- MyDownloader.Tests
- TabStrip.dll
|
using System;
using System.Collections.Generic;
using System.Text;
using MyDownloader.Spider.Parsers.Html;
namespace MyDownloader.Spider
{
public delegate void ResourceCompletedEventHandler(SpiderContext context, ISpiderResource resource);
public delegate void CompletedEventHandler(SpiderContext context);
public class SpiderContext
{
private bool aborted;
private string baseDirectory;
private string baseLocation;
private List<INamingRule> namingRules = new List<INamingRule>();
private List<Uri> visitedLocations = new List<Uri>();
private List<ISpiderResource> completedResources = new List<ISpiderResource>();
public SpiderContext()
{
}
public SpiderContext(string baseDirectory, string baseLocation)
{
this.baseDirectory = baseDirectory;
this.baseLocation = baseLocation;
}
#region Properties
public string BaseLocation
{
get
{
return baseLocation;
}
set
{
baseLocation = value;
}
}
public string BaseDirectory
{
get
{
return baseDirectory;
}
set
{
baseDirectory = value;
}
}
public List<INamingRule> NamingRules
{
get
{
return namingRules;
}
}
public List<Uri> VisitedLocations
{
get
{
return visitedLocations;
}
}
public List<ISpiderResource> CompletedResources
{
get
{
return completedResources;
}
}
#endregion
#region Events
public event ResourceCompletedEventHandler ResourceCompleted;
public event CompletedEventHandler Completed;
protected internal virtual void OnResourceCompleted(ISpiderResource spider)
{
if (ResourceCompleted != null)
{
ResourceCompleted(this, spider);
}
}
protected internal virtual void OnCompleted()
{
if (Completed != null)
{
Completed(this);
}
}
#endregion
private bool ShouldAdd(Uri location, ISpiderResource parentSpider)
{
#region Aborted
if (aborted)
{
return false;
}
#endregion
#region Naming rules
for (int j = 0; j < namingRules.Count; j++)
{
if (!namingRules[j].Accept(location, this, parentSpider)) return false;
}
#endregion
return true;
}
public void Abort()
{
aborted = true;
}
public void NotifyCompletion(ISpiderResource spider)
{
lock (completedResources)
{
if (!completedResources.Contains(spider))
{
completedResources.Add(spider);
}
}
OnResourceCompleted(spider);
if (completedResources.Count == VisitedLocations.Count)
{
OnCompleted();
}
}
public bool AddURIToVisit(Uri uri, ISpiderResource parentSpider)
{
if (ShouldAdd(uri, parentSpider))
{
lock (visitedLocations)
{
if (visitedLocations.Contains(uri))
{
return false;
}
visitedLocations.Add(uri);
return true;
}
}
else
{
return false;
}
}
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.