public async Task <List <TwitterPostModel> > LoadTwitterPosts() { using (Profiler.StepStatic("Load Twitter Posts")) { // TODO - Replace with API service call. var screenname = "";// Data.Repository.Options.TwitterScreenName; var twitterFeed = await Repository.GetTwitterFeedAsync(); var model = new List <TwitterPostModel>(); if (twitterFeed != null) { //try //{ JArray jsonDat = JArray.Parse(twitterFeed.TimelineJson); for (int x = 0; x < jsonDat.Count(); x++) { List <Replacement> replacements = new List <Replacement>(); DateTime postedTime = DateTime.ParseExact(jsonDat[x]["created_at"].ToString(), "ddd MMM dd HH:mm:ss +ffff yyyy", new System.Globalization.CultureInfo("en-CA")); string text = jsonDat[x]["text"].ToString(); string initialScreenName = jsonDat[x]["user"]["screen_name"].ToString(); string initialUserName = jsonDat[x]["user"]["name"].ToString(); string entryScreenName = jsonDat[x]["user"]["screen_name"].ToString(); string entryUserName = jsonDat[x]["user"]["name"].ToString(); string initialUserImageUrl = jsonDat[x]["user"]["profile_image_url_https"].ToString(); JArray hashtags = JArray.Parse(jsonDat[x]["entities"]["hashtags"].ToString()); JArray urls = JArray.Parse(jsonDat[x]["entities"]["urls"].ToString()); JArray userMentions = JArray.Parse(jsonDat[x]["entities"]["user_mentions"].ToString()); JArray medias = new JArray(); if (jsonDat[x]["entities"]["media"] != null) { medias = JArray.Parse(jsonDat[x]["entities"]["media"].ToString()); } if (jsonDat[x]["retweeted_status"] != null) { initialScreenName = jsonDat[x]["retweeted_status"]["user"]["screen_name"].ToString(); initialUserName = jsonDat[x]["retweeted_status"]["user"]["name"].ToString(); initialUserImageUrl = jsonDat[x]["retweeted_status"]["user"]["profile_image_url_https"].ToString(); text = jsonDat[x]["retweeted_status"]["text"].ToString(); hashtags = JArray.Parse(jsonDat[x]["retweeted_status"]["entities"]["hashtags"].ToString()); urls = JArray.Parse(jsonDat[x]["retweeted_status"]["entities"]["urls"].ToString()); userMentions = JArray.Parse(jsonDat[x]["retweeted_status"]["entities"]["user_mentions"].ToString()); if (jsonDat[x]["retweeted_status"]["entities"]["media"] != null) { medias = JArray.Parse(jsonDat[x]["retweeted_status"]["entities"]["media"].ToString()); } } for (int y = hashtags.Count() - 1; y >= 0; y--) { JArray indexes = JArray.Parse(hashtags[y]["indices"].ToString()); replacements.Add(new Replacement() { Start = int.Parse(indexes[0].ToString()), End = int.Parse(indexes[1].ToString()), Text = "<a href=\"https://twitter.com/hashtag/" + hashtags[y]["text"].ToString() + "?src=hash\" target=\"_blank\">#" + hashtags[y]["text"].ToString() + "</a>" }); } for (int y = urls.Count() - 1; y >= 0; y--) { JArray indexes = JArray.Parse(urls[y]["indices"].ToString()); replacements.Add(new Replacement() { Start = int.Parse(indexes[0].ToString()), End = int.Parse(indexes[1].ToString()), Text = "<a href=\"" + urls[y]["url"].ToString() + "\" data-expanded-url=\"" + urls[y]["expanded_url"].ToString() + "\" title=\"" + urls[y]["expanded_url"].ToString() + "\" target=\"_blank\">" + urls[y]["display_url"].ToString() + "</a>" }); } for (int y = userMentions.Count() - 1; y >= 0; y--) { JArray indexes = JArray.Parse(userMentions[y]["indices"].ToString()); replacements.Add(new Replacement() { Start = int.Parse(indexes[0].ToString()), End = int.Parse(indexes[1].ToString()), Text = "<a href=\"https://twitter.com/" + userMentions[y]["screen_name"].ToString() + "\" target=\"_blank\">@" + userMentions[y]["screen_name"].ToString() + "</a>" }); } for (int y = medias.Count() - 1; y >= 0; y--) { JArray indexes = JArray.Parse(medias[y]["indices"].ToString()); string type = medias[y]["type"].ToString(); if (type == "photo") { replacements.Add(new Replacement() { Start = int.Parse(indexes[0].ToString()), End = int.Parse(indexes[1].ToString()), Text = "<a href=\"" + medias[y]["expanded_url"].ToString() + "\" target=\"_blank\"><img src=\"" + new Uri(medias[y]["media_url_https"].ToString()).ToProxyUrl() + "\"></a>" }); } } replacements.Sort(); foreach (var replacement in replacements) { text = text.Remove(replacement.Start) + replacement.Text + text.Substring(replacement.End); } model.Add(new TwitterPostModel { UserName = initialUserName, ScreenName = initialScreenName, UserImageUri = new Uri(initialUserImageUrl), Posted = postedTime.ToString("MMMM d, yyyy h:mm tt"), TimeAgo = ProviderHelpers.FriendlyTimeSpan(postedTime), Content = text, RetweetedText = screenname == initialScreenName ? "" : "Retweeted by <a href=\"https://twitter.com/" + entryScreenName + "\" target=\"_blank\" color=\"gray\">" + entryUserName + "</a>" }); } /*} * catch (Exception ex) * { * Program.ReportException(null, ex); * }*/ } ////TODO: Replace mock data with calls to Twitter API //var model = new List<TwitterPostModel> //{ // new TwitterPostModel // { // TimeAgo = "2m", // Posted = "9:50am - 21 May 2015", // Content = @"Offered at @<a href=""#"">MyBCLibrary</a>, Summer Reading Club builds literacy skills & encourages creative discovery, <a href=""#"">http://ow.ly/PQV2C</a>" // }, // new TwitterPostModel // { // TimeAgo = "5h", // Posted = "9:30am - 21 May 2015", // Content = @"For updates on major #<a href=""#"">BCWildfires</a>, follow @<a href=""#"">BCGovFireInfo</a> - To report wildfire, call 1-800-663-5555 or *5555 on a cellphone" // }, // new TwitterPostModel // { // TimeAgo = "6h", // Posted = "9:30am - 20 May 2015", // Content = @"$598k helps students in northwestern #<a href=""#"">BC</a> develop skills for in-demand trades jobs, <a href=""#"">http://ow.ly/PIFHc</a> #<a href=""#"">findyourfit</a> #<a href=""#"">Terrace</a>" // } //}; return(model); } }