Exemplo n.º 1
0
 private void Get_FB_RSS_Completed(object sender, DownloadStringCompletedEventArgs e)
 {
     try
     {
         if (e.Error == null)
         {
             var RSS_Data = from RSS in XElement.Parse(e.Result).Descendants("item")
                            select new RSSClass
             {
                 Title   = RSS.Element("title").Value,
                 PubDate = RSS.Element("pubDate").Value,
                 //Content = RSS.Element("description").Value,
                 //Content = HttpUtility.HtmlDecode(Regex.Replace(RSS.Element("description").Value.ToString(), "<[^>]+>", string.Empty).Replace("\r", "&#10;").Replace("\n", "&#10;")),
                 //Content = HttpUtility.HtmlDecode(Regex.Replace(RSS.Element("description").Value.ToString(), "<[^>]+>", string.Empty).Replace("\r", "&#xD;").Replace("\n", "&#xD;")),
                 //Content = HttpUtility.HtmlDecode(Regex.Replace(RSS.Element("description").Value.ToString(), "<[^>]+>", string.Empty).Replace("\r", "&#x0a;").Replace("\n", "&#x0a;")),
                 //Content = HttpUtility.HtmlDecode(Regex.Replace(RSS.Element("description").Value.ToString(), "<[^>]+>", string.Empty).Replace("br", "&#xD;").Replace("\n", "&#x0a;")),
                 Content = HttpUtility.HtmlDecode(RSS.Element("description").Value.ToString().Replace("<br />", "&#xD;")),
                 Author  = RSS.Element("author").Value,
                 Link    = RSS.Element("link").Value.ToString().Replace("www", "m")
             };
             TextBlock_Error.Visibility = Visibility.Collapsed;
             ListBox_RSS.ItemsSource    = RSS_Data;
         }
         else
         {
             TextBlock_Error.Text = "Erreur lors du téléchargement des données: " + e.Error.ToString();
         }
     }
     catch (Exception ex)
     {
         MessageBox.Show("Une erreur est survenue.");
         FlurryWP7SDK.Api.LogError(DateTime.Now.Hour.ToString("00") + "h" + DateTime.Now.Minute.ToString("00") + ": About Get_FB_RSS_Completed: " + ex.Message.ToString(), ex);
         FlurryWP7SDK.Api.LogError(ex.StackTrace.ToString(), ex);
     }
 }
Exemplo n.º 2
0
        public void GetShowDataAndPost(object o, DoWorkEventArgs args)
        {
            string XML = string.Empty;

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

                try
                {
                    XML = WebClient.DownloadString(API_URL + "api2/seriesdetail/" + Source);
                }
                catch (WebException)
                {
                    args.Result = "Failed connect for " + Title;
                    return;
                }

                // This occurs when retrieving something that is not in your region.
                if (XML == "FAILED" || string.IsNullOrEmpty(XML))
                {
                    return;
                }
            }

            GetDatabaseData();

            XElement RSS;

            try
            {
                RSS = XElement.Parse(XML);
            }
            catch (System.Xml.XmlException)
            {
                args.Result = "Failed XML parse for " + Title;
                return;
            }

            DAISUKISeriesTitle = RSS.Element("abstruct").Element("title").Value;
            List <XElement> Episodes = new List <XElement>();

            // We need to iterate over every movieset because depending on the region you're in
            // episodes may be in a different movieset. There's also cases where episodes are split across
            // moviesets. Special episodes always have their own movieset.
            IEnumerable <XElement> Moviesets = RSS.Elements("movieset");

            foreach (XElement Movieset in Moviesets)
            {
                IEnumerable <XElement> SubEpisodes = Movieset.Element("items").Elements("item");
                foreach (XElement SubEpisode in SubEpisodes)
                {
                    Episodes.Add(SubEpisode);
                }
            }

            // Sort the episodes. Episodes that can't be parsed to a decimal are put in front of the list
            decimal dummy;

            Episodes = Episodes.OrderBy(e => (decimal.TryParse(e.Element("chapter").Value, out dummy) ? decimal.Parse(e.Element("chapter").Value) : 0)).ToList();

            foreach (XElement XElement in Episodes)
            {
                // Skip this show if the results are not for the current show
                if (DAISUKISeriesTitle != InternalTitle)
                {
                    return;
                }

                ParseDAISUKIData(XElement);

                if (DAISUKIEpisodeNumber == null || !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", DAISUKIEpisodeNumber);
                            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 " + DAISUKIEpisodeNumber));
                        }));
                        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", DAISUKIEpisodeNumber);
                                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 " + DAISUKIEpisodeNumber + " (" + 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 " + DAISUKIEpisodeNumber));
                            }));
                            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 " + DAISUKIEpisodeNumber));
                        }));

                        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", DAISUKIEpisodeNumber);
                                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 " + DAISUKIEpisodeNumber));
                            }));
                            continue;
                        }
                    }
                }
                else
                {
                    continue;
                }
            }
        }