public static void ScrapeChannels(object args)
{
if (Interlocked.CompareExchange(ref Running, 1, 0) != 0)
return;
try
{
if (String.IsNullOrEmpty(downloadDir))
{
string dir = ConfigurationManager.AppSettings["torrentsdir"];
if (!String.IsNullOrEmpty(dir))
{
var context = args as HttpContextBase;
if (Path.IsPathRooted(dir) == false && context != null)
dir = context.Server.MapPath(dir);
if (Directory.Exists(dir))
downloadDir = dir;
else
downloadDir = Environment.CurrentDirectory;
}
}
var table = new DynamicModel("LDSTorrents", tableName: "Channels", primaryKeyField: "ChannelID");
var channels = table.All();
foreach (var channel in channels)
{
try
{
logger.InfoFormat("Scraping '{0}'...", channel.Title);
var videos = ScrapeChannel(channel);
Torrents.Save(videos.ToArray());
table.Update(new { LastUpdated = DateTime.Now }, channel.ChannelID);
}
catch (Exception ex)
{
logger.Error(String.Format("Failed to scrape channel '{0}' with the following exception: ", channel.Title), ex);
ErrorLog.GetDefault(null).Log(new Error(ex));
}
#if DEBUG
break;
#endif
}
}
catch (Exception ex)
{
logger.Error("ScrapeChannels failed with the following exception: ", ex);
ErrorLog.GetDefault(null).Log(new Error(ex));
}
Interlocked.Exchange(ref Running, 0);
}