public static void UpdateInfoIfRequired(int epid) { int progid = 0; string updateExtid = null; // Test to see if the episode is marked as unavailable, and then free up the database using (SQLiteCommand command = new SQLiteCommand("select progid, extid from episodes where epid=@epid and available=0", FetchDbConn())) { command.Parameters.Add(new SQLiteParameter("@epid", epid)); using (SQLiteMonDataReader reader = new SQLiteMonDataReader(command.ExecuteReader())) { if (reader.Read()) { progid = reader.GetInt32(reader.GetOrdinal("progid")); updateExtid = reader.GetString(reader.GetOrdinal("extid")); } } } // Perform an update if the episode was marked as unavailable if (updateExtid != null) { if (Download.IsDownload(epid)) { // The episode is in the downloads list, so just mark as available using (SQLiteCommand command = new SQLiteCommand("update episodes set available=1 where epid=@epid", FetchDbConn())) { command.Parameters.Add(new SQLiteParameter("@epid", epid)); command.ExecuteNonQuery(); } } else { UpdateInfo(progid, updateExtid); } } }
private static void CheckSubscriptions() { // Fetch the current subscriptions into a list, so that the reader doesn't remain open while // checking all of the subscriptions, as this blocks writes to the database from other threads List <Subscription> subscriptions = Subscription.FetchAll(); // Work through the list of subscriptions and check for new episodes foreach (Subscription subscription in subscriptions) { List <string> episodeExtIds = null; try { episodeExtIds = Programme.GetAvailableEpisodes(subscription.Progid, false); } catch (ProviderException) { // Ignore any unhandled provider exceptions continue; } if (episodeExtIds != null) { foreach (string episodeExtId in episodeExtIds) { int?epid = null; try { epid = Episode.FetchInfo(subscription.Progid, episodeExtId); } catch (ProviderException) { // Ignore any unhandled provider exceptions continue; } if (epid == null) { continue; } try { Episode.UpdateInfoIfRequired(epid.Value); } catch (ProviderException) { // Ignore any unhandled provider exceptions continue; } if (!subscription.SingleEpisode) { Episode info = new Episode(epid.Value); if (!info.AutoDownload) { // Don't download the episode automatically, skip to the next one continue; } else if (info.Date.AddDays(14) < DateTime.Now) { // Stop checking the episodes for this subscription, this and the rest // are past the automatic download threshold break; } } if (!Download.IsDownload(epid.Value)) { Download.Add(new int[] { epid.Value }); } } } } // Wait for 10 minutes to give a pause between each check for new episodes Thread.Sleep(600000); // Queue the next subscription check. This is used instead of a loop as // it frees up a slot in the thread pool in case other actions are waiting. ThreadPool.QueueUserWorkItem(delegate { CheckSubscriptions(); }); }