Beispiel #1
0
        public void GetShowDataAndPost(object o, DoWorkEventArgs args)
        {
            string JSON = string.Empty;

            using (WebClient WebClient = new WebClient())
            {
                WebClient.Headers.Add("Cache-Control", "no-cache");
                if (MainLogic.WebProxy != null)
                {
                    WebClient.Proxy = MainLogic.WebProxy;
                }

                try
                {
                    JSON = WebClient.DownloadString("https://www.funimation.com/feeds/ps/videos?ut=FunimationSubscriptionUser&show_id="
                                                    + Source + "&limit=10000");
                }
                catch (WebException)
                {
                    args.Result = "Failed connect for " + Title;
                    return;
                }

                if (JSON == string.Empty)
                {
                    return;
                }
            }

            try
            {
                Episodes = JsonConvert.DeserializeObject <Episodes>(JSON);
            }
            catch (JsonException)
            {
                args.Result = "Failed JSON deserialization for " + Title;
                return;
            }

            GetDatabaseData();

            foreach (Episode Episode in Episodes.EpisodesList)
            {
                ParseFUNimationData(Episode);

                // Skip this episode if it is a result not for the current show or there is no episode number
                if (FUNimationSeriesTitle != InternalTitle || FUNimationEpisodeNumber == null)
                {
                    continue;
                }

                if (!ApplyOffsetAndCheckValidity())
                {
                    continue;
                }

                if (IsNewEpisode())
                {
                    // How does posting to reddit work?:
                    // 1 - Insert the episode into the database without PostURL so that other bots won't post in the meantime
                    // 2 - Post to reddit
                    // 3 - If the post failed remove the entry from the database. If it succeeded update PostURL with the URL
                    try
                    {
                        string InsertEpisodeQuery = @"
                                INSERT INTO Episodes VALUES (@Id, @EpisodeNumber, '')";
                        using (SQLiteCommand InsertEpisodeCommand = new SQLiteCommand(InsertEpisodeQuery, MainLogic.CurrentDB))
                        {
                            InsertEpisodeCommand.Parameters.AddWithValue("@Id", Id);
                            InsertEpisodeCommand.Parameters.AddWithValue("@EpisodeNumber", FUNimationEpisodeNumber);
                            InsertEpisodeCommand.ExecuteNonQuery();
                        }
                    }
                    catch
                    {
                        MainLogic.MainForm.Invoke(new MethodInvoker(delegate()
                        {
                            MainLogic.MainForm.ErrorListBox.Items.Insert(0, (DateTime.Now.ToString("HH:mm:ss: ") +
                                                                             "Failed insert in database for " + Title + " episode " + FUNimationEpisodeNumber));
                        }));
                        continue;
                    }

                    if (PostOnReddit())
                    {
                        try
                        {
                            string UpdateEpisodeQuery = @"
                                    UPDATE Episodes
                                    SET PostURL = @PostURL
                                    WHERE Id = @Id AND EpisodeNumber = @EpisodeNumber";
                            using (SQLiteCommand UpdateEpisodeCommand = new SQLiteCommand(UpdateEpisodeQuery, MainLogic.CurrentDB))
                            {
                                UpdateEpisodeCommand.Parameters.AddWithValue("@PostURL", PostURL);
                                UpdateEpisodeCommand.Parameters.AddWithValue("@Id", Id);
                                UpdateEpisodeCommand.Parameters.AddWithValue("@EpisodeNumber", FUNimationEpisodeNumber);
                                UpdateEpisodeCommand.ExecuteNonQuery();

                                MainLogic.MainForm.Invoke(new MethodInvoker(delegate()
                                {
                                    MainLogic.MainForm.RecentListBox.Items.Insert(0, (DateTime.Now.ToString("HH:mm:ss: ") +
                                                                                      "Successful post for " + Title + " episode " + FUNimationEpisodeNumber + " (" + PostURL + ')'));
                                }));
                            }
                        }
                        catch
                        {
                            MainLogic.MainForm.Invoke(new MethodInvoker(delegate()
                            {
                                MainLogic.MainForm.ErrorListBox.Items.Insert(0, (DateTime.Now.ToString("HH:mm:ss: ") +
                                                                                 "!ALERT! Failed update in database for " + Title + " episode " + FUNimationEpisodeNumber));
                            }));
                            continue;
                        }
                    }
                    else
                    {
                        MainLogic.MainForm.Invoke(new MethodInvoker(delegate()
                        {
                            MainLogic.MainForm.ErrorListBox.Items.Insert(0, (DateTime.Now.ToString("HH:mm:ss: ") +
                                                                             "Failed reddit post for " + Title + " episode " + FUNimationEpisodeNumber));
                        }));

                        try
                        {
                            string DeleteEpisodeQuery = @"
                                    DELETE FROM Episodes
                                    WHERE Id = @Id AND EpisodeNumber = @EpisodeNumber";
                            using (SQLiteCommand DeleteEpisodeCommand = new SQLiteCommand(DeleteEpisodeQuery, MainLogic.CurrentDB))
                            {
                                DeleteEpisodeCommand.Parameters.AddWithValue("@Id", Id);
                                DeleteEpisodeCommand.Parameters.AddWithValue("@EpisodeNumber", FUNimationEpisodeNumber);
                                DeleteEpisodeCommand.ExecuteNonQuery();
                            }
                        }
                        catch
                        {
                            MainLogic.MainForm.Invoke(new MethodInvoker(delegate()
                            {
                                MainLogic.MainForm.ErrorListBox.Items.Insert(0, (DateTime.Now.ToString("HH:mm:ss: ") +
                                                                                 "!ALERT! Failed delete in database for " + Title + " episode " + FUNimationEpisodeNumber));
                            }));
                            continue;
                        }
                    }
                }
            }
        }
Beispiel #2
0
        public void GetShowDataAndPost(object o, DoWorkEventArgs args)
        {
            string JSON = string.Empty;
            using (WebClient WebClient = new WebClient())
            {
                WebClient.Headers.Add("Cache-Control", "no-cache");
                if (MainLogic.WebProxy != null)
                    WebClient.Proxy = MainLogic.WebProxy;

                try
                {
                    JSON = WebClient.DownloadString("https://www.funimation.com/feeds/ps/videos?ut=FunimationSubscriptionUser&show_id="
                    + Source + "&limit=10000");
                }
                catch (WebException)
                {
                    args.Result = "Failed connect for " + Title;
                    return;
                }

                if (JSON == string.Empty)
                    return;
            }

            try
            {
                Episodes = JsonConvert.DeserializeObject<Episodes>(JSON);
            }
            catch (JsonException)
            {
                args.Result = "Failed JSON deserialization for " + Title;
                return;
            }

            GetDatabaseData();

            foreach (Episode Episode in Episodes.EpisodesList)
            {
                ParseFUNimationData(Episode);

                // Skip this episode if it is a result not for the current show or there is no episode number
                if (FUNimationSeriesTitle != InternalTitle || FUNimationEpisodeNumber == null)
                    continue;

                if (!ApplyOffsetAndCheckValidity())
                    continue;

                if (IsNewEpisode())
                {
                    // How does posting to reddit work?:
                    // 1 - Insert the episode into the database without PostURL so that other bots won't post in the meantime
                    // 2 - Post to reddit
                    // 3 - If the post failed remove the entry from the database. If it succeeded update PostURL with the URL
                    try
                    {
                        string InsertEpisodeQuery = @"
                                INSERT INTO Episodes VALUES (@Id, @EpisodeNumber, '')";
                        using (SQLiteCommand InsertEpisodeCommand = new SQLiteCommand(InsertEpisodeQuery, MainLogic.CurrentDB))
                        {
                            InsertEpisodeCommand.Parameters.AddWithValue("@Id", Id);
                            InsertEpisodeCommand.Parameters.AddWithValue("@EpisodeNumber", FUNimationEpisodeNumber);
                            InsertEpisodeCommand.ExecuteNonQuery();
                        }
                    }
                    catch
                    {
                        MainLogic.MainForm.Invoke(new MethodInvoker(delegate ()
                        {
                            MainLogic.MainForm.ErrorListBox.Items.Insert(0, (DateTime.Now.ToString("HH:mm:ss: ") +
                                "Failed insert in database for " + Title + " episode " + FUNimationEpisodeNumber));
                        }));
                        continue;
                    }

                    if (PostOnReddit())
                    {
                        try
                        {
                            string UpdateEpisodeQuery = @"
                                    UPDATE Episodes
                                    SET PostURL = @PostURL
                                    WHERE Id = @Id AND EpisodeNumber = @EpisodeNumber";
                            using (SQLiteCommand UpdateEpisodeCommand = new SQLiteCommand(UpdateEpisodeQuery, MainLogic.CurrentDB))
                            {
                                UpdateEpisodeCommand.Parameters.AddWithValue("@PostURL", PostURL);
                                UpdateEpisodeCommand.Parameters.AddWithValue("@Id", Id);
                                UpdateEpisodeCommand.Parameters.AddWithValue("@EpisodeNumber", FUNimationEpisodeNumber);
                                UpdateEpisodeCommand.ExecuteNonQuery();

                                MainLogic.MainForm.Invoke(new MethodInvoker(delegate ()
                                {
                                    MainLogic.MainForm.RecentListBox.Items.Insert(0, (DateTime.Now.ToString("HH:mm:ss: ") +
                                        "Successful post for " + Title + " episode " + FUNimationEpisodeNumber + " (" + PostURL + ')'));
                                }));
                            }
                        }
                        catch
                        {
                            MainLogic.MainForm.Invoke(new MethodInvoker(delegate ()
                            {
                                MainLogic.MainForm.ErrorListBox.Items.Insert(0, (DateTime.Now.ToString("HH:mm:ss: ") +
                                    "!ALERT! Failed update in database for " + Title + " episode " + FUNimationEpisodeNumber));
                            }));
                            continue;
                        }
                    }
                    else
                    {
                        MainLogic.MainForm.Invoke(new MethodInvoker(delegate ()
                        {
                            MainLogic.MainForm.ErrorListBox.Items.Insert(0, (DateTime.Now.ToString("HH:mm:ss: ") +
                                "Failed reddit post for " + Title + " episode " + FUNimationEpisodeNumber));
                        }));

                        try
                        {
                            string DeleteEpisodeQuery = @"
                                    DELETE FROM Episodes
                                    WHERE Id = @Id AND EpisodeNumber = @EpisodeNumber";
                            using (SQLiteCommand DeleteEpisodeCommand = new SQLiteCommand(DeleteEpisodeQuery, MainLogic.CurrentDB))
                            {
                                DeleteEpisodeCommand.Parameters.AddWithValue("@Id", Id);
                                DeleteEpisodeCommand.Parameters.AddWithValue("@EpisodeNumber", FUNimationEpisodeNumber);
                                DeleteEpisodeCommand.ExecuteNonQuery();
                            }
                        }
                        catch
                        {
                            MainLogic.MainForm.Invoke(new MethodInvoker(delegate ()
                            {
                                MainLogic.MainForm.ErrorListBox.Items.Insert(0, (DateTime.Now.ToString("HH:mm:ss: ") +
                                    "!ALERT! Failed delete in database for " + Title + " episode " + FUNimationEpisodeNumber));
                            }));
                            continue;
                        }
                    }
                }
            }
        }