Skip to content

Commit 13b1170

Browse files
authored
Fix news search with new website layout (#295)
* More robustness when downloading news * Adapt news search to new website * Formatting * Bump Lib_CSharp * Fix small comments
1 parent ea2c931 commit 13b1170

13 files changed

Lines changed: 76 additions & 472 deletions

File tree

Backend/Other/DB/DBPolifemo.sql

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,16 @@ create table if not exists Tags
4949
blurhash varchar(80) null
5050
);
5151

52+
create table if not exists ArticleContent
53+
(
54+
id int auto_increment
55+
primary key,
56+
title varchar(100) not null,
57+
subtitle varchar(200) null,
58+
content text not null,
59+
url varchar(500) null
60+
);
61+
5262
create table if not exists Articles
5363
(
5464
article_id int unsigned auto_increment
@@ -75,15 +85,6 @@ create table if not exists Articles
7585
foreign key (tag_id) references Tags (name)
7686
);
7787

78-
create table if not exists ArticleContent
79-
(
80-
id int auto_increment
81-
primary key,
82-
title varchar(100) not null,
83-
subtitle varchar(200) null,
84-
content text not null,
85-
url varchar(500) null
86-
);
8788

8889
create table if not exists Types
8990
(

Backend/Source/Objects/Articles/Articles.cs

Lines changed: 0 additions & 111 deletions
This file was deleted.

Backend/Source/Objects/Articles/News/ArticleContent.cs

Lines changed: 14 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
using HtmlAgilityPack;
44
using PoliFemoBackend.Source.Utils.Article;
5-
using PoliNetwork.Core.Data;
5+
using PoliFemoBackend.Source.Utils.News.PoliMi;
66
using ReverseMarkdown;
77

88
#endregion
@@ -34,53 +34,35 @@ public static ArticleContent[] LoadContentFromURL(string url)
3434
var r = new ArticleContent[2];
3535
r[0] = new ArticleContent();
3636
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();
4037

4138
for (var i = 0; i < 2; i++)
4239
{
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']");
4542
try
4643
{
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']");
5146

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();
6053

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);
6355

64-
var content = converter.Convert(
65-
ArticleContentUtil.CleanContentString(urls.InnerHtml)
66-
);
6756
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
6957
r[i].content = content.Trim();
7058
r[i].url = url;
7159

7260
if (i == 0)
7361
{
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();
8164
if (pathnode == null)
8265
break;
83-
//var b = a.SelectSingleNode("//span").InnerHtml;
8466
url = "https://polimi.it" + pathnode.GetAttributeValue("href", "");
8567
}
8668
}

Backend/Source/Objects/Articles/News/ArticleNews.cs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,12 @@ public class ArticleNews
1111
//internal News
1212
private static readonly Config config = new() { RemoveComments = true };
1313

14-
private static Converter converter = new(config);
15-
1614
public ArticleContent[] content = new ArticleContent[2];
1715

1816
public ArticleNews(string? tag, string? image)
1917
{
2018
this.tag = tag;
2119
this.image = image;
22-
internalNews = true;
2320
author_id = 1;
2421
content = new ArticleContent[2];
2522
}
@@ -33,7 +30,6 @@ public ArticleNews(
3330
double? longitude,
3431
string? blurhash,
3532
int platforms,
36-
bool? internalNews,
3733
string? tag,
3834
ArticleContent[] content
3935
)
@@ -46,7 +42,6 @@ ArticleContent[] content
4642
this.longitude = longitude;
4743
this.blurhash = blurhash;
4844
this.platforms = platforms;
49-
this.internalNews = internalNews;
5045
this.tag = tag;
5146
this.content = content;
5247
}
@@ -61,7 +56,6 @@ public ArticleNews() { }
6156
public double? longitude { get; set; }
6257
public string? blurhash { get; set; }
6358
public int? platforms { get; set; }
64-
public bool? internalNews { get; set; }
6559
public string? tag { get; set; }
6660

6761
public void AddContent(ArticleContent c)

Backend/Source/Objects/Articles/News/HtmlNews.cs

Lines changed: 0 additions & 13 deletions
This file was deleted.

Backend/Source/Objects/Articles/News/NodeFlagged.cs

Lines changed: 0 additions & 13 deletions
This file was deleted.

Backend/Source/Utils/Article/ArticleContentUtil.cs

Lines changed: 0 additions & 32 deletions
This file was deleted.

0 commit comments

Comments
 (0)