private static string GetTorrents(Notification notification, Settings settings) { string text = string.Empty; var torrentGetter = new LostFilmTorrentGetter(); try { List <TorrentDescription> torrents = torrentGetter.GetEpisodeTorrents(notification.Episode, settings.SiteLogin, settings.SitePassword); if (torrents.Count != 0) { text += " (" + torrents.Select(t => t.Quality) .Aggregate(string.Empty, (s, s1) => s + " " + string.Format(DownloadCommand.DownloadCommandFormat, notification.Id, s1)) + ")"; } } catch (Exception e) { Logger.Error(e, "SendEpisodesNotifications: An error occured while retrieving torrents for {notification.Episode.Show.Title} - {notification.Episode.Title}"); text += " (Не удалось получить список торрентов. Возможно указан неверный логин/пароль)"; } text += "\n"; return(text); }
public override void Execute() { using (AppDbContext db = new AppDbContext()) { Program.Logger.Debug($"{GetType().Name}: Retrieving notification with id: {NotificationId}"); var notification = db.GetNotificationById(NotificationId); if (notification == null) { Program.Logger.Debug($"{GetType().Name}: Notification with specified Id was not found"); Status = false; return; } Program.Logger.Debug($"{GetType().Name}: Retrieving settings of {notification.Subscription.User}"); var settings = db.GetSettingsByUser(notification.Subscription.User); if (settings == null) { Program.Logger.Debug($"{GetType().Name}: User's settings were not found"); Status = false; return; } ITorrentGetter torrentGetter = new LostFilmTorrentGetter(); List <TorrentDescription> torrents = null; Program.Logger.Debug($"{GetType().Name}: Retrieving torrents for {notification.Episode.Show} - {notification.Episode.Title}"); try { torrents = torrentGetter.GetEpisodeTorrents(notification.Episode, settings.SiteLogin, settings.SitePassword); } catch (Exception e) { Program.Logger.Error(e, $"An error occured while retrieving torrents for {notification.Episode.Show.Title} - {notification.Episode.Title}"); TelegramApi.SendMessage(Message.From, "(Не удалось получить список торрентов. Возможно указан неверный логин/пароль)"); Status = false; return; } Program.Logger.Debug($"{GetType().Name}: Number of torrents: {torrents?.Count() ?? 0}"); TorrentDescription torrent = null; if (torrents != null && torrents.Count() != 0) { Program.Logger.Debug($"{GetType().Name}: Retrieving torrent with required quality ({Quality})"); torrent = torrents.FirstOrDefault(t => t.Quality == Quality); } if (torrent == null) { Program.Logger.Debug($"{GetType().Name}: Torrent with required quality was not found"); Status = false; return; } Program.Logger.Debug($"{GetType().Name}: Creating new download task"); db.DownloadTasks.Add(new DownloadTask() { Episode = notification.Episode, User = notification.Subscription.User, TorrentUrl = torrent.TorrentUri.ToString() }); Program.Logger.Debug($"{GetType().Name}: Saving changes to database"); db.SaveChanges(); Status = true; } }
public void SendEpisodesNotifications() { using (AppDbContext db = new AppDbContext()) { Logger.Trace("SendEpisodesNotifications: Retrieving new notifications"); List<Notification> notifications; try { notifications = db.Notifications .Where(n => !n.Notified) .ToList(); } catch (Exception e) { Logger.Error(e, "SendEpisodesNotifications: An error occurred while retrieving new notifications"); return; } if (notifications.Count == 0) { return; } Dictionary<User, List<Notification>> notificationDictionary = notifications.Aggregate( new Dictionary<User, List<Notification>>(), (d, notification) => { if (d.ContainsKey(notification.Subscription.User)) { d[notification.Subscription.User].Add(notification); } else { d.Add(notification.Subscription.User, new List<Notification>() {notification}); } return d; } ); Logger.Debug("SendEpisodesNotifications: Sending new notifications"); foreach (var userNotifications in notificationDictionary) { string text = string.Empty; foreach (Notification notification in userNotifications.Value) { text += notification.Subscription.Show.Title + " - " + notification.Episode.Title; Settings settings = db.GetSettingsByUser(userNotifications.Key); if (settings != null) { ITorrentGetter torrentGetter = new LostFilmTorrentGetter(); try { List<TorrentDescription> torrents = torrentGetter.GetEpisodeTorrents(notification.Episode, settings.SiteLogin, settings.SitePassword); if (torrents.Count != 0) { text += " (" + torrents.Select(t => t.Quality) .Aggregate(string.Empty, (s, s1) => s + " " + string.Format(DownloadCommand.DownloadCommandFormat, notification.Id, s1)) + ")"; } } catch (Exception e) { Logger.Error(e, $"SendEpisodesNotifications: An error occured while retrieving torrents for {notification.Episode.Show.Title} - {notification.Episode.Title}"); text += "(Не удалось получить список торрентов. Возможно указан неверный логин/пароль)"; } text += "\n"; } } try { TelegramApi.SendMessage(userNotifications.Key.TelegramUserId, text); } catch (Exception e) { Logger.Error(e, "SendEpisodesNotifications: An error occurred while sending new notifications"); } } Logger.Info($"SendEpisodesNotifications: {notificationDictionary.Count} new " + $"{((notificationDictionary.Count == 1) ? "notification was sent" : "notifications were sent")}"); Logger.Debug("SendEpisodesNotifications: Marking new notifications as notified"); try { notifications.ForEach( notification => { db.Notifications.First(n => n.Id == notification.Id).Notified = true; } ); } catch (Exception e) { Logger.Error(e, "SendEpisodesNotifications: An error occurred while marking new notifications as notified"); } Logger.Trace("SendEpisodesNotifications: Saving changes to database"); try { db.SaveChanges(); } catch (Exception e) { Logger.Error(e, "SendEpisodesNotifications: An error occurred while saving changes to database"); } } }
public override void Execute() { using (AppDbContext db = new AppDbContext()) { Program.Logger.Debug($"{GetType().Name}: Retrieving notification with id: {NotificationId}"); Notification notification = null; try { notification = db.GetNotificationById(NotificationId); } catch (Exception e) { Program.Logger.Error(e, $"{GetType().Name}: An error occurred while retrieving notification"); } if (notification == null) { Program.Logger.Debug($"{GetType().Name}: Notification with specified Id was not found"); Status = false; return; } Program.Logger.Debug($"{GetType().Name}: Retrieving settings of {notification.Subscription.User}"); Settings settings = null; try { settings = db.GetSettingsByUser(notification.Subscription.User); } catch (Exception e) { Program.Logger.Error(e, "An error occurred while retrieving user's settings"); } if (settings == null) { Program.Logger.Debug($"{GetType().Name}: User's settings were not found"); Status = false; return; } ITorrentGetter torrentGetter = new LostFilmTorrentGetter(); List<TorrentDescription> torrents = null; Program.Logger.Debug($"{GetType().Name}: Retrieving torrents for {notification.Episode.Show} - {notification.Episode.Title}"); try { torrents = torrentGetter.GetEpisodeTorrents(notification.Episode, settings.SiteLogin, settings.SitePassword); } catch (Exception e) { Program.Logger.Error(e, $"An error occured while retrieving torrents for {notification.Episode.Show.Title} - {notification.Episode.Title}"); TelegramApi.SendMessage(Message.From, "(Не удалось получить список торрентов. Возможно указан неверный логин/пароль)"); Status = false; return; } Program.Logger.Debug($"{GetType().Name}: Number of torrents: {torrents?.Count() ?? 0}"); TorrentDescription torrent = null; if (torrents != null && torrents.Count() != 0) { Program.Logger.Debug($"{GetType().Name}: Retrieving torrent with required quality ({Quality})"); torrent = torrents.FirstOrDefault(t => t.Quality == Quality); } if (torrent == null) { Program.Logger.Debug($"{GetType().Name}: Torrent with required quality was not found"); Status = false; return; } Program.Logger.Debug($"{GetType().Name}: Creating new download task"); db.DownloadTasks.Add(new DownloadTask() { Episode = notification.Episode, User = notification.Subscription.User, TorrentUrl = torrent.TorrentUri.ToString() }); Program.Logger.Debug($"{GetType().Name}: Saving changes to database"); try { db.SaveChanges(); } catch (Exception e) { throw new Exception($"{GetType().Name}: An error occurred while saving changes to database", e); } Status = true; } }
private static string GetTorrents(Notification notification, Settings settings) { string text = string.Empty; var torrentGetter = new LostFilmTorrentGetter(); try { List<TorrentDescription> torrents = torrentGetter.GetEpisodeTorrents(notification.Episode, settings.SiteLogin, settings.SitePassword); if (torrents.Count != 0) { text += " (" + torrents.Select(t => t.Quality) .Aggregate(string.Empty, (s, s1) => s + " " + string.Format(DownloadCommand.DownloadCommandFormat, notification.Id, s1)) + ")"; } } catch (Exception e) { Logger.Error(e, "SendEpisodesNotifications: An error occured while retrieving torrents for {notification.Episode.Show.Title} - {notification.Episode.Title}"); text += " (Не удалось получить список торрентов. Возможно указан неверный логин/пароль)"; } text += "\n"; return text; }