示例#1
0
        static async Task<List<Topic>> FetchTopics(SubCategory subCategory)
        {
            var url = subCategory.Url.Replace("liste_sujet-1", $"liste_sujet-{Loc.SubCategory.TopicsPage}");
            
            var html = await HttpClientHelper.Get(url);

            if (string.IsNullOrEmpty(html)) return null;

            var htmlDoc = new HtmlDocument();
            htmlDoc.LoadHtml(html);

            var topicNodes = htmlDoc.DocumentNode.Descendants("tr").Where(x => x.GetAttributeValue("class", "").Contains("sujet ligne_booleen")).ToArray();

            if (topicNodes == null) return null;
            var topics = new List<Topic>();
            foreach (var topicNode in topicNodes)
            {
                var topicName = topicNode.Descendants("a").FirstOrDefault(x => x.GetAttributeValue("class", "") == "cCatTopic").InnerText;
                var topicAuthor = topicNode.Descendants("td").FirstOrDefault(x => x.GetAttributeValue("class", "").Contains("sujetCase6")).InnerText.Trim();

                var topicPageNode = topicNode.Descendants("td").FirstOrDefault(x => x.GetAttributeValue("class", "") == "sujetCase4").InnerText;
                var topicPage = 1;
                if (!string.IsNullOrEmpty(topicPageNode))
                {
                    int.TryParse(topicPageNode, out topicPage);
                    if (topicPage == 0)
                        topicPage = 1;
                }
                
                var sujetCase3Node = topicNode.Descendants("td").FirstOrDefault(x => x.GetAttributeValue("class", "") == "sujetCase3");

                var topicUrl = sujetCase3Node.Descendants("a").FirstOrDefault(x => x.GetAttributeValue("href", "").StartsWith("/hfr/")).GetAttributeValue("href","");

                var topicIsStickyNodes = sujetCase3Node.Descendants("img");
                var topicIsSticky = topicIsStickyNodes.FirstOrDefault(x => x.GetAttributeValue("src", "").Contains("flechesticky"));
                var topicIsClosed = topicIsStickyNodes.FirstOrDefault(x=>x.GetAttributeValue("alt", "") == "closed");
                

                var topic = new Topic();
                topic.TopicName = topicName;
                topic.TopicAuthor = topicAuthor;
                topic.TopicIsSticky = topicIsSticky != null;
                topic.TopicIsClosed = topicIsClosed != null;
                topic.TopicNbPage = topicPage;
                topic.TopicDrapURI = topicUrl;

                topics.Add(topic);
            }

            return topics;
        }
示例#2
0
 private void CurrentTopic_TopicReadyToBeDisplayed(Topic topic)
 {
     TopicWebView.NavigationCompleted += TopicWebViewOnNavigationCompleted;
     TopicWebView.Navigate(Strings.TopicPageCacheUri);
 }
示例#3
0
 public static async Task GetPosts(Topic currentTopic)
 {
     Debug.WriteLine("Fetching Posts");
     await Fetch(currentTopic);
     Debug.WriteLine("Updating UI with new Posts list");
 }
示例#4
0
        public static async Task Fetch(Topic currentTopic)
        {
            await ThreadUI.Invoke(() =>
            {
                Loc.Topic.IsTopicLoading = true;
            });

            var html = await HttpClientHelper.Get(currentTopic.TopicDrapURI);
            if (string.IsNullOrEmpty(html)) return;

            await ThreadUI.Invoke(() =>
            {
                Loc.Topic.CurrentTopic.Html = html;
            });

            var htmlDoc = new HtmlDocument();
            htmlDoc.LoadHtml(html);

            var postNodes =
                htmlDoc.DocumentNode.Descendants("table")
                    .Where(x => x.GetAttributeValue("class", "") == "messagetable")
                    .ToArray();

            if (postNodes == null || !postNodes.Any()) return;

            string TempHTMLMessagesList = "";
            string TempHTMLMessage = "";
            string TempHTMLTopic = "";

            string BodyTemplate = "";
            string MessageTemplate = "";

            // This is absolutely quick and dirty code :o
            Assembly asm = typeof(App).GetTypeInfo().Assembly;

            using (Stream stream = asm.GetManifestResourceStream(HFRRessources.Tpl_Topic))
            using (StreamReader reader = new StreamReader(stream))
            {
                BodyTemplate = reader.ReadToEnd();
            }

            using (Stream stream = asm.GetManifestResourceStream(HFRRessources.Tpl_Message))
            using (StreamReader reader = new StreamReader(stream))
            {
                MessageTemplate = reader.ReadToEnd();
            }

            var i = 0;
            foreach (var postNode in postNodes)
            {
                TempHTMLMessage = MessageTemplate;

                // Id de la réponse
                var reponseId =
                    postNode.Descendants("a")
                        .FirstOrDefault(x => x.GetAttributeValue("href", "").StartsWith("#t"))
                        .GetAttributeValue("href", "")
                        .Replace("#t", "");
                
                // Pseudo
                var pseudo = postNode.Descendants("b").FirstOrDefault(x => x.GetAttributeValue("class", "") == "s2").InnerText.CleanFromWeb();

                // Mood
                var mood = postNode.Descendants("span").FirstOrDefault(x=>x.GetAttributeValue("class","") == "MoodStatus")?.InnerText.CleanFromWeb();

                // Img
                var avatarUri = "ms-appx-web:///Assets/HTML/UI/rsz_no_avatar.png";
                var avatarClass = "no_avatar";
                var divAvatarNode = postNode.Descendants("div").FirstOrDefault(x=>x.GetAttributeValue("class", "") == "avatar_center");
                if (divAvatarNode != null && divAvatarNode.ChildNodes.Any())
                {
                    var imgAvatarNode = divAvatarNode.FirstChild;
                    var uri = imgAvatarNode.GetAttributeValue("src", "");
                    if (!string.IsNullOrEmpty(uri))
                    {
                        avatarUri = uri;
                        avatarClass = "";
                    }
                }

                // Date
                var date = postNode.Descendants("div").FirstOrDefault(x=>x.GetAttributeValue("class","") == "toolbar").InnerText.CleanFromWeb();
                date = date.Replace("Posté le ", "");

                // Content
                var content = postNode.Descendants("div").FirstOrDefault(x => x.GetAttributeValue("id", "").Contains("para")).InnerHtml;
                int lastPostText = content.IndexOf("<div style=\"clear: both;\"> </div>", StringComparison.Ordinal);
                if (lastPostText == -1)
                {
                    lastPostText = content.Length;
                }

                content = content.Substring(0, lastPostText);
                content = content.CleanFromWeb();

                TempHTMLMessage = TempHTMLMessage.Replace("%%ID%%", i.ToString());
                TempHTMLMessage = TempHTMLMessage.Replace("%%POSTID%%", reponseId);

                if (pseudo == "Modération")
                {
                    TempHTMLMessage = TempHTMLMessage.Replace("%%moderation%%", "moderation");
                }
                else
                {
                    TempHTMLMessage = TempHTMLMessage.Replace("%%moderation%%", "");
                }

                TempHTMLMessage = TempHTMLMessage.Replace("%%no_avatar_class%%", avatarClass);

                if (Loc.Settings.SquareAvatarStylePreferred)
                {
                    TempHTMLMessage = TempHTMLMessage.Replace("%%round_avatar_class%%", "");
                }
                else
                {
                    TempHTMLMessage = TempHTMLMessage.Replace("%%round_avatar_class%%", "round");
                }

                TempHTMLMessage = TempHTMLMessage.Replace("%%AUTEUR_AVATAR%%", avatarUri);
                TempHTMLMessage = TempHTMLMessage.Replace("%%AUTEUR_PSEUDO%%", pseudo);
                TempHTMLMessage = TempHTMLMessage.Replace("%%MESSAGE_DATE%%", date);

                TempHTMLMessage = TempHTMLMessage.Replace("%%MESSAGE_CONTENT%%", content);

                TempHTMLMessagesList += TempHTMLMessage;
                i++;
            }

            // Get URL of the new post form
            var url = htmlDoc.DocumentNode.Descendants("form").FirstOrDefault(x => x.GetAttributeValue("id", "") == "repondre_form").GetAttributeValue("action", "");
            currentTopic.TopicNewPostUriForm = WebUtility.HtmlDecode(url);

            TempHTMLTopic = BodyTemplate.Replace("%%MESSAGES%%", TempHTMLMessagesList);

            await ThreadUI.Invoke(() =>
            {
                SolidColorBrush color = (SolidColorBrush)App.Current.Resources["SystemControlHighlightAltListAccentLowBrush"];
                var colorString = $"{color.Color.R.ToString()}, {color.Color.G.ToString()}, {color.Color.B.ToString()}";

                TempHTMLTopic = TempHTMLTopic.Replace("%%ACCENTCOLOR%%", colorString);
                //await FileIO.WriteTextAsync(cssFile, css);
            });


            // Create/Open WebSite-Cache folder
            var subfolder = await ApplicationData.Current.LocalFolder.CreateFolderAsync(Strings.WebSiteCacheFolderName, CreationCollisionOption.OpenIfExists);
            var file = await subfolder.CreateFileAsync($"{Strings.WebSiteCacheFileName}", CreationCollisionOption.ReplaceExisting);
            await FileIO.WriteTextAsync(file, TempHTMLTopic);

            await ThreadUI.Invoke(() =>
            {
                Loc.Topic.UpdateTopicWebView(currentTopic);

                Loc.Topic.IsTopicLoading = false;
            });
        }
示例#5
0
 public void UpdateTopicWebView(Topic topic)
 {
     TopicReadyToBeDisplayed?.Invoke(topic);
     Task.Run(async () => await DrapFetcher.GetDraps());
 }