@@ -11,6 +11,9 @@ namespace CodeRedLauncher.Controls
1111{
1212 public partial class CRNews : UserControl
1313 {
14+ private string m_altUrl = "https://raw.githubusercontent.com/CodeRedModding/CodeRed-Retrievers/main/Public/News.cr" ; // Psyonix started blocking requests to their site...have to use my own now.
15+ private string m_altThumbnail = "https://i.imgur.com/g9ssgL7.png" ;
16+
1417 private IconStore m_calendarIcons = new IconStore ( ) ;
1518 private IconStore m_authorIcons = new IconStore ( ) ;
1619 private IconStore m_categoryIcons = new IconStore ( ) ;
@@ -33,13 +36,16 @@ private class NewsStorage
3336
3437 public NewsStorage ( string bodyContent )
3538 {
39+ Timestamp = "Rocket League" ;
40+ Author = "Psyonix Team" ;
3641 ParseJson ( bodyContent ) ;
3742 }
3843
3944 public void ParseJson ( string bodyContent )
4045 {
4146 if ( bodyContent . Contains ( "slug" ) )
4247 {
48+ bodyContent = bodyContent . Replace ( "\" : \" " , "\" :\" " ) ;
4349 Match titleMatch = Regex . Match ( bodyContent , "(?<=\" title\" :\" )(.*?)(?=\" )" ) ;
4450 Match urlMatch = Regex . Match ( bodyContent , "(?<=\" slug\" :\" )(.*?)(?=\" )" ) ;
4551 Match imageMatch = Regex . Match ( bodyContent , "(?<=\" imageUrl\" :\" )(.*?)(?=\" )" ) ;
@@ -415,6 +421,7 @@ private async Task<NewsStorage> ParseLink(NewsStorage newsStorage)
415421 newsStorage . ThumbnailUrl = thumbnailMatch . Groups [ 1 ] . Value ;
416422 Int32 jpg = newsStorage . ThumbnailUrl . IndexOf ( ".jpg" ) ;
417423 Int32 png = newsStorage . ThumbnailUrl . IndexOf ( ".png" ) ;
424+ Int32 webp = newsStorage . ThumbnailUrl . IndexOf ( ".webp" ) ;
418425
419426 if ( jpg > 0 )
420427 {
@@ -424,6 +431,10 @@ private async Task<NewsStorage> ParseLink(NewsStorage newsStorage)
424431 {
425432 newsStorage . ThumbnailUrl = ( newsStorage . ThumbnailUrl . Substring ( 0 , png ) + ".png" ) ;
426433 }
434+ else if ( webp > 0 )
435+ {
436+ newsStorage . ThumbnailUrl = ( newsStorage . ThumbnailUrl . Substring ( 0 , webp ) + ".webp" ) ;
437+ }
427438 }
428439
429440 Match thumbnailMatchAlt = Regex . Match ( pageBody , "<p dir=\" ltr\" ><img src=\" (.*)\" data-id=\" " ) ;
@@ -454,44 +465,63 @@ private async Task<NewsStorage> ParseLink(NewsStorage newsStorage)
454465 return newsStorage ;
455466 }
456467
457- public async void ParseArticles ( string url )
468+ public async void ParseArticles ( string url , bool bRecursive = false )
458469 {
459470 if ( ! string . IsNullOrEmpty ( url ) )
460471 {
461- return ; // Fuck Psyonix for blocking requests to their news url, billion dollar company can't give us an actual api to use.
462-
472+ bool fallback = false ;
463473 string pageBody = await Downloaders . DownloadPage ( url ) ;
464474
465- if ( ! string . IsNullOrEmpty ( pageBody ) )
475+ if ( ! string . IsNullOrEmpty ( pageBody ) )
466476 {
467477 m_articles . Clear ( ) ;
468478 ResetArticles ( ) ;
469- MatchCollection articleLinks = Regex . Matches ( pageBody . Replace ( "\\ " , "" ) , "(?=\" title\" )(.*?)(?=})" ) ;
479+
480+ MatchCollection articleLinks = Regex . Matches ( pageBody . Replace ( "\\ " , "" ) , ( bRecursive ? "(?s)(?=\" title).*?(?=})" : "(?=\" title\" )(.*?)(?=})" ) ) ;
470481
471482 Int32 articles = 0 ;
472483
473484 for ( Int32 i = 0 ; i < articleLinks . Count ; i ++ )
474485 {
475486 Match link = articleLinks [ i ] ;
476487
477- if ( link . Success && link . Groups [ 1 ] . Success && link . Groups [ 1 ] . Value . Contains ( "slug" ) )
488+ if ( link . Success && link . Groups [ 0 ] . Success && link . Groups [ 0 ] . ToString ( ) . Contains ( "slug" ) )
478489 {
479490 articles ++ ;
480- m_articles . Add ( new NewsStorage ( link . Groups [ 1 ] . Value ) ) ;
491+ m_articles . Add ( new NewsStorage ( link . Groups [ 0 ] . ToString ( ) ) ) ;
481492 }
482493
483- if ( articles > = m_maxIndexes )
494+ if ( articles = = m_maxIndexes )
484495 {
485496 break ;
486497 }
487498 }
488499
489- LoadAllIndexes ( ) ; // Parse and download everything in the background.
490- LoadPreviousArticle ( ) ;
500+ if ( articles > 0 )
501+ {
502+ Logger . Write ( "Found news article links!" ) ;
503+ LoadAllIndexes ( ) ; // Parse and download everything in the background.
504+ LoadPreviousArticle ( ) ;
505+ return ;
506+ }
507+ else
508+ {
509+ fallback = true ;
510+ }
491511 }
492512 else
493513 {
494- Logger . Write ( "Failed to download news article info!" , LogLevel . LEVEL_WARN ) ;
514+ fallback = true ;
515+ }
516+
517+ if ( fallback && ! bRecursive )
518+ {
519+ Logger . Write ( "Couldn't find official news links, resorting to fallback url!" ) ;
520+ ParseArticles ( m_altUrl , true ) ;
521+ }
522+ else if ( bRecursive )
523+ {
524+ Logger . Write ( "Fallback url failed, no news links found!" ) ;
495525 }
496526 }
497527 }
@@ -524,7 +554,7 @@ public async void LoadAllIndexes()
524554 if ( newsStorage . ThumbnailImage == null )
525555 {
526556 newsStorage . ThumbnailUrl = "" ;
527- newsStorage . ThumbnailUrlAlt = "https://i.imgur.com/dmpY0zQ.png" ;
557+ newsStorage . ThumbnailUrlAlt = m_altThumbnail ;
528558 newsStorage . ThumbnailImage = await Downloaders . DownloadImage ( newsStorage . ThumbnailUrlAlt ) ;
529559 }
530560 }
@@ -570,7 +600,7 @@ private async void LoadIndex()
570600 if ( newsStorage . ThumbnailImage == null )
571601 {
572602 newsStorage . ThumbnailUrl = "" ;
573- newsStorage . ThumbnailUrlAlt = "https://i.imgur.com/dmpY0zQ.png" ;
603+ newsStorage . ThumbnailUrlAlt = m_altThumbnail ;
574604 newsStorage . ThumbnailImage = await Downloaders . DownloadImage ( newsStorage . ThumbnailUrlAlt ) ;
575605 }
576606
@@ -611,22 +641,34 @@ private async void LoadIndex()
611641
612642 private void PreviousBtn_Click ( object sender , EventArgs e )
613643 {
614- LoadPreviousArticle ( ) ;
644+ if ( PreviousBtn . BackgroundImage != null )
645+ {
646+ LoadPreviousArticle ( ) ;
647+ }
615648 }
616649
617650 private void PreviousBtn_DoubleClick ( object sender , EventArgs e )
618651 {
619- LoadPreviousArticle ( ) ;
652+ if ( PreviousBtn . BackgroundImage != null )
653+ {
654+ LoadPreviousArticle ( ) ;
655+ }
620656 }
621657
622658 private void NextBtn_Click ( object sender , EventArgs e )
623659 {
624- LoadNextArticle ( ) ;
660+ if ( NextBtn . BackgroundImage != null )
661+ {
662+ LoadNextArticle ( ) ;
663+ }
625664 }
626665
627666 private void NextBtn_DoubleClick ( object sender , EventArgs e )
628667 {
629- LoadNextArticle ( ) ;
668+ if ( NextBtn . BackgroundImage != null )
669+ {
670+ LoadNextArticle ( ) ;
671+ }
630672 }
631673
632674 private void ThumbnailImg_Click ( object sender , EventArgs e )
0 commit comments