@@ -31,9 +31,34 @@ private class NewsStorage
3131 public string Category { get ; set ; }
3232 public bool Parsed { get ; set ; } = false ;
3333
34- public NewsStorage ( string newsUrl )
34+ public NewsStorage ( string bodyContent )
3535 {
36- NewsUrl = newsUrl ;
36+ ParseJson ( bodyContent ) ;
37+ }
38+
39+ public void ParseJson ( string bodyContent )
40+ {
41+ if ( bodyContent . Contains ( "slug" ) )
42+ {
43+ Match titleMatch = Regex . Match ( bodyContent , "(?<=\" title\" :\" )(.*?)(?=\" )" ) ;
44+ Match urlMatch = Regex . Match ( bodyContent , "(?<=\" slug\" :\" )(.*?)(?=\" )" ) ;
45+ Match imageMatch = Regex . Match ( bodyContent , "(?<=\" imageUrl\" :\" )(.*?)(?=\" )" ) ;
46+
47+ if ( titleMatch . Success && titleMatch . Groups [ 1 ] . Success )
48+ {
49+ Title = titleMatch . Groups [ 1 ] . Value ;
50+ }
51+
52+ if ( urlMatch . Success && urlMatch . Groups [ 1 ] . Success )
53+ {
54+ NewsUrl = ( "https://www.rocketleague.com/en/news/" + urlMatch . Groups [ 1 ] . Value ) ;
55+ }
56+
57+ if ( imageMatch . Success && imageMatch . Groups [ 1 ] . Success )
58+ {
59+ ThumbnailUrl = imageMatch . Groups [ 1 ] . Value ;
60+ }
61+ }
3762 }
3863 }
3964
@@ -354,16 +379,20 @@ private async Task<NewsStorage> ParseLink(NewsStorage newsStorage)
354379
355380 if ( ! string . IsNullOrEmpty ( pageBody ) )
356381 {
357- Match titleMatch = Regex . Match ( pageBody , "\" headline\" : \" (.*)\" " ) ;
382+ if ( string . IsNullOrEmpty ( newsStorage . Title ) )
383+ {
384+ Match titleMatch = Regex . Match ( pageBody , "\" headline\" : \" (.*)\" " ) ;
385+
386+ if ( titleMatch . Success && titleMatch . Groups [ 1 ] . Success )
387+ {
388+ newsStorage . Title = titleMatch . Groups [ 1 ] . Value ;
389+ }
390+ }
391+
358392 Match calendarMatch = Regex . Match ( pageBody . Replace ( "\r " , "" ) . Replace ( " " , "" ) . Replace ( "\n " , "" ) , "<p class=\" is-5 is-uppercase\" >(.*?)<" ) ;
359393 Match userMatch = Regex . Match ( pageBody , "\" author\" : \" (.*)\" " ) ;
360394 Match categoryMatch = Regex . Match ( pageBody , "\" category tag\" >(.*)<\\ /a><\\ /p>" ) ;
361395
362- if ( titleMatch . Success && titleMatch . Groups [ 1 ] . Success )
363- {
364- newsStorage . Title = titleMatch . Groups [ 1 ] . Value ;
365- }
366-
367396 if ( calendarMatch . Success && calendarMatch . Groups [ 1 ] . Success )
368397 {
369398 newsStorage . Calendar = calendarMatch . Groups [ 1 ] . Value ;
@@ -388,7 +417,6 @@ private async Task<NewsStorage> ParseLink(NewsStorage newsStorage)
388417 newsStorage . ThumbnailUrl = thumbnailMatch . Groups [ 1 ] . Value ;
389418 Int32 jpg = newsStorage . ThumbnailUrl . IndexOf ( ".jpg" ) ;
390419 Int32 png = newsStorage . ThumbnailUrl . IndexOf ( ".png" ) ;
391- Int32 webp = newsStorage . ThumbnailUrl . IndexOf ( ".webp" ) ;
392420
393421 if ( jpg > 0 )
394422 {
@@ -398,10 +426,6 @@ private async Task<NewsStorage> ParseLink(NewsStorage newsStorage)
398426 {
399427 newsStorage . ThumbnailUrl = ( newsStorage . ThumbnailUrl . Substring ( 0 , png ) + ".png" ) ;
400428 }
401- else if ( webp > 0 )
402- {
403- newsStorage . ThumbnailUrl = ( newsStorage . ThumbnailUrl . Substring ( 0 , webp ) + ".webp" ) ; // .NET doesn't support webp, so this doesn't work for now. Would need a third party library.
404- }
405429 }
406430
407431 Match thumbnailMatchAlt = Regex . Match ( pageBody , "<p dir=\" ltr\" ><img src=\" (.*)\" data-id=\" " ) ;
@@ -442,15 +466,15 @@ public async void ParseArticles(string url)
442466 {
443467 _articles . Clear ( ) ;
444468 ResetArticles ( ) ;
445- MatchCollection articleLinks = Regex . Matches ( pageBody , "<a class =\" news-tile-wrap \" href= \" (.*) \" > " ) ;
469+ MatchCollection articleLinks = Regex . Matches ( pageBody . Replace ( " \\ " , "" ) , "(? =\" title \" ) (.*?)(?=}) ") ;
446470
447471 for ( Int32 i = 0 ; i < articleLinks . Count ; i ++ )
448472 {
449473 Match link = articleLinks [ i ] ;
450474
451- if ( link . Success && link . Groups [ 1 ] . Success )
475+ if ( link . Success && link . Groups [ 1 ] . Success && link . Groups [ 1 ] . Value . Contains ( "slug" ) )
452476 {
453- _articles . Add ( new NewsStorage ( "https://www.rocketleague.com" + link . Groups [ 1 ] . Value ) ) ;
477+ _articles . Add ( new NewsStorage ( link . Groups [ 1 ] . Value ) ) ;
454478 }
455479 }
456480
0 commit comments