Skip to content

Commit ea2c931

Browse files
authored
More robustness when downloading news (#294)
1 parent 465eddd commit ea2c931

2 files changed

Lines changed: 25 additions & 14 deletions

File tree

Backend/Source/Utils/News/PoliMi/DownloadNewsUtil.cs

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
using PoliFemoBackend.Source.Objects.Articles.News;
44
using PoliNetwork.Html.Utils;
5+
using CoreGlobals = PoliNetwork.Core.Data.GlobalVariables;
56

67
#endregion
78

@@ -11,23 +12,33 @@ public static class DownloadNewsUtil
1112
{
1213
internal static IEnumerable<ArticleNews> DownloadCurrentNews()
1314
{
14-
// Get news from the Polimi news page
15-
var docNews = HtmlNewsUtil.LoadUrl(PoliMiNewsUtil.UrlPoliMiNews);
16-
var urls = docNews
17-
?.DocumentNode.SelectNodes("//ul")
18-
.First(x => x.GetClasses().Contains("ce-menu"));
15+
try
16+
{
17+
// Get news from the Polimi news page
18+
var docNews = HtmlNewsUtil.LoadUrl(PoliMiNewsUtil.UrlPoliMiNews);
19+
var urls = docNews
20+
?.DocumentNode.SelectNodes("//ul")
21+
.First(x => x.GetClasses().Contains("ce-menu"));
1922

20-
// Get news from the Polimi home page
21-
var docPoliMi = HtmlNewsUtil.LoadUrl(PoliMiNewsUtil.UrlPoliMiHomePage);
22-
var newsPolimi = PoliMiNewsUtil.GetNewsPoliMi(docPoliMi);
23+
// Get news from the Polimi home page
24+
var docPoliMi = HtmlNewsUtil.LoadUrl(PoliMiNewsUtil.UrlPoliMiHomePage);
25+
var newsPolimi = PoliMiNewsUtil.GetNewsPoliMi(docPoliMi);
2326

24-
// Merge the two lists
25-
var newslist = MergeNewsUtil.Merge(urls?.ChildNodes, newsPolimi);
27+
// Merge the two lists
28+
var newslist = MergeNewsUtil.Merge(urls?.ChildNodes, newsPolimi);
2629

27-
// Filter & parse the news
28-
var newsobjlist = newslist.Select(ExtractNews).ToList();
30+
// Filter & parse the news
31+
var newsobjlist = newslist.Select(ExtractNews).ToList();
2932

30-
return (from item in newsobjlist where item != null select item).ToList();
33+
return (from item in newsobjlist where item != null select item).ToList();
34+
}
35+
catch (InvalidOperationException ex)
36+
{
37+
CoreGlobals.DefaultLogger.Error(
38+
"There was an error parsing the polimi page, search skipped: " + ex
39+
);
40+
return new List<ArticleNews>();
41+
}
3142
}
3243

3344
private static ArticleNews? ExtractNews(HtmlNews htmlNews)

0 commit comments

Comments
 (0)