|
2 | 2 |
|
3 | 3 | using HtmlAgilityPack; |
4 | 4 | using PoliFemoBackend.Source.Utils.Article; |
5 | | -using PoliNetwork.Core.Data; |
| 5 | +using PoliFemoBackend.Source.Utils.News.PoliMi; |
6 | 6 | using ReverseMarkdown; |
7 | 7 |
|
8 | 8 | #endregion |
@@ -34,53 +34,35 @@ public static ArticleContent[] LoadContentFromURL(string url) |
34 | 34 | var r = new ArticleContent[2]; |
35 | 35 | r[0] = new ArticleContent(); |
36 | 36 | r[1] = new ArticleContent(); |
37 | | - if (url.Contains("landingpages")) |
38 | | - return r; // If this is a landing page, return an empty array as this is not a news article |
39 | | - var web = new HtmlWeb(); |
40 | 37 |
|
41 | 38 | for (var i = 0; i < 2; i++) |
42 | 39 | { |
43 | | - var doc = web.Load(url); |
44 | | - var urls1 = doc.DocumentNode.SelectNodes("//div"); |
| 40 | + var doc = HtmlPageUtil.LoadUrl(url); |
| 41 | + var article = doc.DocumentNode.SelectSingleNode("//div[@class='article']"); |
45 | 42 | try |
46 | 43 | { |
47 | | - // Try to get the news-single-item class, if none are found, try to get the content id |
48 | | - var urls = urls1.FirstOrDefault(x => |
49 | | - x.GetAttributeValue("class", "") == "news-single-item" |
50 | | - ); |
| 44 | + // Get the html article text |
| 45 | + var htmlContent = article.SelectSingleNode("//div[@itemprop='articleBody']"); |
51 | 46 |
|
52 | | - // If empty, try to get the "content" id |
53 | | - if (urls == null) |
54 | | - { |
55 | | - GlobalVariables.DefaultLogger.Info( |
56 | | - "No news-single-item class found, trying to get the content element" |
57 | | - ); |
58 | | - urls = urls1.First(x => x.GetAttributeValue("id", "") == "content"); |
59 | | - } |
| 47 | + r[i].title = article |
| 48 | + .SelectSingleNode("//h1[@itemprop='headline']") |
| 49 | + .InnerText.Trim(); |
| 50 | + r[i].subtitle = article |
| 51 | + .SelectSingleNode("//div[@itemprop='description']") |
| 52 | + .InnerText.Trim(); |
60 | 53 |
|
61 | | - r[i].title = urls.SelectSingleNode("//h1").InnerHtml.Trim(); |
62 | | - r[i].subtitle = urls.SelectSingleNode("//h2").InnerHtml.Trim(); |
| 54 | + var content = converter.Convert(htmlContent.InnerHtml); |
63 | 55 |
|
64 | | - var content = converter.Convert( |
65 | | - ArticleContentUtil.CleanContentString(urls.InnerHtml) |
66 | | - ); |
67 | 56 | content = content.Replace("](/", "](https://www.polimi.it/"); // Replace relative PoliMi links with absolute ones |
68 | | - content = content.Replace("\r\n\r\n* * *", ""); // Remove the final horizontal line |
69 | 57 | r[i].content = content.Trim(); |
70 | 58 | r[i].url = url; |
71 | 59 |
|
72 | 60 | if (i == 0) |
73 | 61 | { |
74 | | - var pathnode = doc |
75 | | - .DocumentNode.Descendants("li") |
76 | | - .FirstOrDefault(li => li.GetAttributeValue("id", "") == "lienglish") |
77 | | - ?.Descendants("span") |
78 | | - .First() |
79 | | - .Descendants("a") |
80 | | - .FirstOrDefault(); |
| 62 | + //Get the second li element (the link to the english version, if available) |
| 63 | + var pathnode = doc.DocumentNode.SelectNodes("//li//a").Skip(1).FirstOrDefault(); |
81 | 64 | if (pathnode == null) |
82 | 65 | break; |
83 | | - //var b = a.SelectSingleNode("//span").InnerHtml; |
84 | 66 | url = "https://polimi.it" + pathnode.GetAttributeValue("href", ""); |
85 | 67 | } |
86 | 68 | } |
|
0 commit comments