public static async Task <OneNewsModel> Parse(string htmlUrl) { TaskCompletionSource <OneNewsModel> tcs = new TaskCompletionSource <OneNewsModel>(); using (var client = new HttpClient()) { var response = await client.GetAsync(htmlUrl); string url = await response.Content.ReadAsStringAsync(); try { OneNewsModel news = new OneNewsModel(); HtmlDocument hd = new HtmlDocument(); hd.LoadHtml(url); HtmlNode itemDecription = hd.DocumentNode.Descendants("div").Where(d => d.Attributes.Contains("class") && d.Attributes["class"].Value.Contains("itemFullText")).ToList()[0]; string descr = itemDecription.InnerText.Trim().Replace("<p>", "").Replace("</p>", "").Replace("\r", ""); string[] els = descr.Split(new string[] { "\n" }, StringSplitOptions.RemoveEmptyEntries); descr = ""; foreach (string s in els) { descr = descr + s.Trim().Replace(""", "\"").Replace(" ", "") .Replace("«", "\"").Replace("»", "\"").Replace("–", "–").Replace("—", "—") + "\n"; } news.Description = descr; HtmlNode itemVideo = hd.DocumentNode.Descendants("div").Where(d => d.Attributes.Contains("class") && d.Attributes["class"].Value.Contains("avPlayerBlock")).ToList()[0]; string videoId = itemVideo.Element("iframe").Attributes["src"].Value.ToString(); string temp = videoId.Substring(0, videoId.IndexOf('?')); news.YouTubeID = temp.Substring(29, temp.Length - 29); tcs.SetResult(news); } catch (Exception ex) { tcs.SetException(ex); } } return(tcs.Task.Result); }
protected async override void OnNavigatedTo(NavigationEventArgs e) { SystemNavigationManager.GetForCurrentView().AppViewBackButtonVisibility = AppViewBackButtonVisibility.Visible; SystemNavigationManager.GetForCurrentView().BackRequested += NewsElement_BackRequested; DisplayInformation.AutoRotationPreferences = DisplayOrientations.Portrait | DisplayOrientations.Landscape | DisplayOrientations.LandscapeFlipped; //ConnectionProfile InternetConnectionProfile = NetworkInformation.GetInternetConnectionProfile(); //NetworkConnectivityLevel connection = InternetConnectionProfile.GetNetworkConnectivityLevel(); //bool internetAccess = !(connection == NetworkConnectivityLevel.None || connection == NetworkConnectivityLevel.LocalAccess); bool internetAccess = NetworkInterface.GetIsNetworkAvailable(); if (internetAccess && e.Parameter != null) { DisplayInformation.GetForCurrentView().OrientationChanged += NewsElement_OrientationChanged; string[] param = (string[])e.Parameter; HeaderNews.Text = param[0]; DateNews.Text = param[1]; OneNewsModel oneNews = await NewsElementParser.Parse(param[2]); DescrNews.Text = oneNews.Description; YouTubeQuality quality = YouTubeQuality.Quality360P; if (Settings.Settings.QualityYouTubeVideo != null) { switch (Settings.Settings.QualityYouTubeVideo) { case "360p": quality = YouTubeQuality.Quality360P; break; case "480p": quality = YouTubeQuality.Quality480P; break; case "720p": quality = YouTubeQuality.Quality720P; break; case "1080p": quality = YouTubeQuality.Quality1080P; break; } } YouTubeUri videoUri = null; bool error = false; try { videoUri = await YouTube.GetVideoUriAsync(oneNews.YouTubeID, quality); } catch (YouTubeUriNotFoundException) { error = true; } if (!error && videoUri != null) { playerYouTube.MediaPlayer.SetUriSource(videoUri.Uri); } else { var dialog = new MessageDialog("Не удалось загрузить видео."); await dialog.ShowAsync(); Frame.GoBack(); } } else { var dialog = new MessageDialog("Проверьте соединение с интернетом"); await dialog.ShowAsync(); Frame.GoBack(); } }