55using System . Threading . Tasks ;
66using System . Collections . Generic ;
77using System . Text . RegularExpressions ;
8+ using System . Net ;
89
910namespace CodeRedLauncher . Controls
1011{
@@ -16,18 +17,18 @@ public partial class CRNewsPanel : UserControl
1617 private class NewsStorage
1718 {
1819 public string NewsUrl { get ; set ; }
19- public string ThumbnailUrl { get ; set ; }
20+ public string ThumbnailUrl_Main { get ; set ; }
21+ public string ThumbnailUrl_Alt { get ; set ; }
2022 public Image ThumbnailImage { get ; set ; } = null ;
2123 public string Title { get ; set ; }
2224 public string Calendar { get ; set ; }
2325 public string User { get ; set ; }
2426 public string Category { get ; set ; }
2527 public bool Parsed { get ; set ; } = false ;
2628
27- public NewsStorage ( string newsUrl , string thumbnailUrl = null )
29+ public NewsStorage ( string newsUrl )
2830 {
2931 NewsUrl = newsUrl ;
30- ThumbnailUrl = thumbnailUrl ;
3132 }
3233 }
3334
@@ -115,22 +116,21 @@ private async Task<NewsStorage> ParseLink(NewsStorage newsStorage)
115116 Logger . Write ( "Failed to retrieve news category for url \" " + newsStorage . NewsUrl + "\" !" , LogLevel . LEVEL_WARN ) ;
116117 }
117118
118- if ( String . IsNullOrEmpty ( newsStorage . ThumbnailUrl ) )
119+ if ( String . IsNullOrEmpty ( newsStorage . ThumbnailUrl_Main ) )
119120 {
120- Match thumbnailMatch = Regex . Match ( pageBody , "og:image:url \" content=\" (.*)\" /><meta property=" , RegexOptions . RightToLeft ) ; // This can change a lot...
121+ Match thumbnailMatch = Regex . Match ( pageBody , "property= \" og:image\" content=\" (.*)\" /><meta property=\" og:image:url" ) ;
121122
122123 if ( thumbnailMatch . Success && thumbnailMatch . Groups [ 1 ] . Success )
123124 {
124- newsStorage . ThumbnailUrl = thumbnailMatch . Groups [ 1 ] . Value ;
125+ newsStorage . ThumbnailUrl_Main = thumbnailMatch . Groups [ 1 ] . Value ;
126+ newsStorage . ThumbnailUrl_Main = ( newsStorage . ThumbnailUrl_Main . Substring ( 0 , newsStorage . ThumbnailUrl_Main . IndexOf ( ".jpg" ) ) + ".jpg" ) ;
125127 }
126- else
127- {
128- Match thumbnailMatchAlt = Regex . Match ( pageBody , "<p dir=\" ltr\" ><img src=\" (.*)\" data-id=\" " , RegexOptions . RightToLeft ) ; // This usually happens with community spotlights.
129128
130- if ( thumbnailMatchAlt . Success && thumbnailMatchAlt . Groups [ 1 ] . Success )
131- {
132- newsStorage . ThumbnailUrl = thumbnailMatchAlt . Groups [ 1 ] . Value ;
133- }
129+ Match thumbnailMatchAlt = Regex . Match ( pageBody , "<p dir=\" ltr\" ><img src=\" (.*)\" data-id=\" " ) ;
130+
131+ if ( thumbnailMatchAlt . Success && thumbnailMatchAlt . Groups [ 1 ] . Success )
132+ {
133+ newsStorage . ThumbnailUrl_Alt = thumbnailMatchAlt . Groups [ 1 ] . Value ;
134134 }
135135 }
136136
@@ -196,34 +196,52 @@ private async void LoadCurrentIndex()
196196 if ( ( CurrentIndex > - 1 ) && ( CurrentIndex < NewsArticles . Count ) )
197197 {
198198 ResetArticles ( ) ;
199- NewsStorage article = NewsArticles [ CurrentIndex ] ;
199+ NewsStorage newsStorage = NewsArticles [ CurrentIndex ] ;
200200
201- if ( ! article . Parsed )
201+ if ( ! newsStorage . Parsed )
202202 {
203203 NewsArticles [ CurrentIndex ] = await ParseLink ( NewsArticles [ CurrentIndex ] ) ;
204- article = NewsArticles [ CurrentIndex ] ;
204+ newsStorage = NewsArticles [ CurrentIndex ] ;
205205 }
206206
207- PublishDate = article . Calendar ;
208- PublishAuthor = article . User ;
209- NewsCategory = article . Category ;
210- Title = article . Title ;
207+ PublishDate = newsStorage . Calendar ;
208+ PublishAuthor = newsStorage . User ;
209+ NewsCategory = newsStorage . Category ;
210+ Title = newsStorage . Title ;
211211
212- if ( article . ThumbnailImage == null )
212+ if ( newsStorage . ThumbnailImage == null )
213213 {
214- if ( ! String . IsNullOrEmpty ( article . ThumbnailUrl ) )
214+ if ( ! String . IsNullOrEmpty ( newsStorage . ThumbnailUrl_Main ) )
215215 {
216- ThumbnailImg . LoadAsync ( article . ThumbnailUrl ) ;
216+ newsStorage . ThumbnailImage = await Downloaders . DownloadImage ( newsStorage . ThumbnailUrl_Main ) ;
217+
218+ if ( ( newsStorage . ThumbnailImage == null ) && ! String . IsNullOrEmpty ( newsStorage . ThumbnailUrl_Alt ) )
219+ {
220+ newsStorage . ThumbnailImage = await Downloaders . DownloadImage ( newsStorage . ThumbnailUrl_Alt ) ;
221+ }
222+
223+ // If no thumbnail was found we gotta use our own image.
224+ if ( newsStorage . ThumbnailImage == null )
225+ {
226+ // https://i.imgur.com/dmpY0zQ.png
227+ newsStorage . ThumbnailUrl_Main = "https://i.imgur.com/dmpY0zQ.png" ;
228+ newsStorage . ThumbnailUrl_Alt = "" ;
229+ newsStorage . ThumbnailImage = await Downloaders . DownloadImage ( newsStorage . ThumbnailUrl_Main ) ;
230+ }
231+
232+ NewsArticles [ CurrentIndex ] = newsStorage ;
233+ ThumbnailImg . BackgroundImage = newsStorage . ThumbnailImage ;
234+ ThumbnailImg . BackgroundImageLayout = ImageLayout . Stretch ;
217235 }
218236 else
219237 {
220- ThumbnailImg . BackgroundImageLayout = ImageLayout . Center ;
221238 ThumbnailImg . BackgroundImage = Properties . Resources . Warning_White ;
239+ ThumbnailImg . BackgroundImageLayout = ImageLayout . Center ;
222240 }
223241 }
224242 else
225243 {
226- ThumbnailImg . BackgroundImage = article . ThumbnailImage ;
244+ ThumbnailImg . BackgroundImage = newsStorage . ThumbnailImage ;
227245 ThumbnailImg . BackgroundImageLayout = ImageLayout . Stretch ;
228246 }
229247
@@ -295,34 +313,6 @@ public void LoadNextArticle()
295313 }
296314 }
297315
298- private void ThumbnailImg_LoadCompleted ( object sender , System . ComponentModel . AsyncCompletedEventArgs e )
299- {
300- if ( NewsArticles . Count > 0 )
301- {
302- if ( ( CurrentIndex > - 1 ) && ( CurrentIndex < NewsArticles . Count ) )
303- {
304- if ( NewsArticles [ CurrentIndex ] . ThumbnailImage == null )
305- {
306- NewsArticles [ CurrentIndex ] . ThumbnailImage = ThumbnailImg . Image ;
307- }
308-
309- if ( ThumbnailImg . Image != null )
310- {
311- ThumbnailImg . BackgroundImage = ThumbnailImg . Image ;
312- ThumbnailImg . Image = null ;
313- }
314-
315- ThumbnailImg . BackgroundImageLayout = ImageLayout . Stretch ;
316- }
317- }
318- else
319- {
320- ResetArticles ( ) ;
321- CurrentIndex = - 1 ;
322- }
323- }
324-
325-
326316 private void PreviousBtn_Click ( object sender , EventArgs e )
327317 {
328318 LoadPreviousArticle ( ) ;
0 commit comments