public async Task Parse(UrlSource twitterSource) { var result = await client.GetStringAsync(twitterSource.Url); JArray jsonDat = JArray.Parse(result); foreach(var tweet in jsonDat.Children()) { string user = "******" + tweet["user"]["screen_name"].ToString(); string createdTime = tweet["created_at"].ToString(); string tweettext = tweet["text"].ToString(); string profileBanner = tweet["user"]["profile_banner_url"].ToString(); const string format = "ddd MMM dd HH:mm:ss zzzz yyyy"; DateTime dateTimeResult = DateTime.ParseExact(createdTime, format, CultureInfo.InvariantCulture); createdTime = dateTimeResult.ToString("ddd, d MMM yyyy"); TwitterData.Add(new Item { Title = user, Subtitle = createdTime, Description = tweettext, Image = profileBanner, Group = twitterSource.Group, UrlSource = twitterSource }); } }
public async Task Parse(UrlSource youtubeSource) { var _UserAgent = "Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2; WOW64; Trident/6.0)"; httpClient.DefaultRequestHeaders.Add("user-agent", _UserAgent); var result = await httpClient.GetStringAsync(youtubeSource.Url); JObject jsonDat = JObject.Parse(result); string nextPageToken = null; try { nextPageToken = jsonDat["nextPageToken"].ToString(); } catch { nextPageToken = null; } JArray jsonItems = JArray.Parse(jsonDat["items"].ToString()); string youtubeHtmlTemplate = "<p><a href=\"{0}\"><img src=\"{1}\" alt=\"\" width=300></a></p><p><a style=\"font-size: 15px; font-weight: bold; font-decoration: none;\" href=\"{0}\">{2}</a></p><p>{3}</p>"; string youtubePrefix; youtubePrefix = AppSettings.ForceYoutubeVideosToLoadFullScreen ? "/watch_popup?v=" : "/watch?v="; foreach (var youtubeItem in jsonItems) { var description = youtubeItem["snippet"]["description"].ToString(); if(description != "This video is private.") YoutubeData.Add(new Item { Title = youtubeItem["snippet"]["title"].ToString(), Subtitle = youtubeItem["snippet"]["publishedAt"].ToString(), Description = string.Format(youtubeHtmlTemplate, "https://www.youtube.com" + youtubePrefix + youtubeItem["snippet"]["resourceId"]["videoId"], youtubeItem["snippet"]["thumbnails"]["high"]["url"].ToString(), youtubeItem["snippet"]["title"].ToString(), description), Image = youtubeItem["snippet"]["thumbnails"]["medium"]["url"].ToString(), Group = youtubeSource.Group, UrlSource = currentYoutubeSource }); } if (nextPageToken != null) await Parse(new UrlSource {Url = currentYoutubeSource.Url + "&pageToken=" + nextPageToken, Group = currentYoutubeSource.Group}); }
public async Task<List<Item>> GetItems() { if(AppSettings.YoutubePublicAPIKey.Length < 30 && !AppSettings.EnableRemoteUrlSourceService) ServiceLocator.MessageService.ShowErrorAsync("YoutubeService is enabled but YoutubePublicAPIKey appears to be invalid please check this value in AppSettings.cs", "Application Error"); try { YoutubeData = new List<Item>(); foreach (var youtubeSource in AppSettings.YoutubeAddressCollection) { youtubeSource.Type = "YoutubeSource"; currentYoutubeSource = youtubeSource; await Parse(youtubeSource); } } catch { ServiceLocator.MessageService.ShowErrorAsync("Error when retrieving items from YoutubeService", "Application Error"); } return YoutubeData; }
public async Task Parse(UrlSource rssSource) { try { var httpClient = new HttpClient(); var _UserAgent = "Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2; WOW64; Trident/6.0)"; httpClient.DefaultRequestHeaders.Add("user-agent", _UserAgent); var response = await httpClient.GetStringAsync(rssSource.Url); XNamespace xmlns = "http://www.w3.org/2005/Atom"; XNamespace media = "http://search.yahoo.com/mrss/"; XNamespace content = "http://purl.org/rss/1.0/modules/content/"; XDocument Feed = XDocument.Parse(response); string group = rssSource.Group.Length > 1 ? rssSource.Group : Feed.Descendants("channel").Select(e => (string)e.Element("title").Value).First(); IEnumerable<Item> items = new List<Item>(); if (rssSource.Url.StartsWith("http://gdata.youtube.com/feeds/api/playlists/")) //parse Youtube Playlist RSS { //0 is link, 1 is image, 2 is title, 3 is description string youtubeHtmlTemplate = "<p><a href=\"{0}\"><img src=\"{1}\" alt=\"\" width=300></a></p><p><a style=\"font-size: 15px; font-weight: bold; font-decoration: none;\" href=\"{0}\">{2}</a></p><p>{3}</p>"; items = from item in Feed.Descendants("item") select new Item() { Title = item.Element("title").Value, Subtitle = item.Element("pubDate").Value, Description = item.Descendants(media + "thumbnail").Count() > 0 ? string.Format(youtubeHtmlTemplate, item.Element("link").Value, item.Descendants(media + "thumbnail").Select(e => (string)e.Attribute("url")).FirstOrDefault(), item.Element("title").Value, item.Element("description").Value.Substring(0, Math.Min(580, item.Element("description").Value.Length))) : string.Empty, Image = item.Descendants(media + "thumbnail") != null ? item.Descendants(media + "thumbnail").Select(e => (string)e.Attribute("url")).FirstOrDefault() : string.Empty, Group = @group, UrlSource = rssSource }; items = items.Where(x => x.Description != string.Empty); } else { string audio_template = "<audio src=\"{0}\" controls autoplay>Your browser does not support the <code>audio</code> element.<br/><a href=\"{0}\">Link to file</a>.</audio><br/>"; var feeditems = AppSettings.RssMaxItemsPerFeed < 0 ? Feed.Descendants("item") : Feed.Descendants("item").Take(AppSettings.RssMaxItemsPerFeed); items = from item in feeditems let body = item.Descendants(content + "encoded").FirstOrDefault() // TODO: perhaps this needs to use the url's MIME type to determine the tag for audio, video, PDFs, etc.? let parsed = (item.Element("enclosure") != null ? string.Format(audio_template, (string)(item.Element("enclosure").Attribute("url"))) : string.Empty) + (item.Element("description") != null ? (string)(item.Element("description").Value) : string.Empty) + (item.Element("link") != null ? " <a href=" + (string)(item.Element("link").Value) + ">Link</a>" : string.Empty) select new Item() { Title = item.Element("title") != null ? item.Element("title").Value : string.Empty, Subtitle = item.Element("pubDate") != null ? item.Element("pubDate").Value : DateTime.Now.ToString(), Description = body != null ? body.Value : parsed, Image = item.Descendants(media + "thumbnail") != null ? item.Descendants(media + "thumbnail").Select(e => (string)e.Attribute("url")).FirstOrDefault() : "", Group = @group, UrlSource = rssSource }; } if (items.ToList().Count > 0) { foreach (var item in items) { if (item.Image == null) //Attempt to parse an image out of the description if one is not returned in the RSS item.Image = Regex.Match(item.Description, "(https?:)?//?[^'\"<>]+?.(jpg|jpeg|gif|png)").Value; if (item.Image == string.Empty) //Unable to locate any image, so fallback to logo item.Image = "/Assets/Logo.png"; //Format dates to look cleaner DateTime dateTimeResult = new DateTime(); if (DateTime.TryParse(item.Subtitle, out dateTimeResult)) item.Subtitle = dateTimeResult.ToString("ddd, d MMM yyyy"); if (AppSettings.ForceYoutubeVideosToLoadFullScreen) item.Description = item.Description.Replace("/watch?v=", "/watch_popup?v="); // Fix "shortcut" urls item.Description = item.Description.Replace("src=\"//", "src=\"http://"); item.Description = item.Description.Replace("src='//", "src='http://"); RssData.Add(item); }; } else { await ServiceLocator.MessageService.ShowErrorAsync("Zero items retrieved from " + rssSource.Url, "Application Error"); } } catch (Exception e) { ServiceLocator.MessageService.ShowErrorAsync("Error when retrieving items from RssService: " + e.Message + "\nUrl: " + rssSource.Url, "Application Error"); } }