Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #region InboxFeed
- public static async Task<string> LoadUserInboxFeed(string UserId, string Username)
- {
- // Fetching
- Uri InstaURI = new Uri("http://instagram.com");
- string FetchURL = string.Format("https://instagram.com/api/v1/news/inbox/?user_id={0}", UserId);
- var handler = new HttpClientHandler();
- var req = new HttpClient(handler);
- req.DefaultRequestHeaders.Accept.ParseAdd("*/*");
- req.DefaultRequestHeaders.AcceptLanguage.ParseAdd("en-US,en");
- //req.DefaultRequestHeaders.AcceptLanguage.ParseAdd("identity");
- req.DefaultRequestHeaders.Accept.ParseAdd("application/json, application/xml, text/json, text/x-json, text/javascript, text/xml");
- req.DefaultRequestHeaders.Referrer = new Uri("http://instagram.com/#");
- req.DefaultRequestHeaders.Host = "instagram.com";
- req.DefaultRequestHeaders.Add("User-Agent", "Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2; WOW64; Trident/6.0; Touch)");
- req.DefaultRequestHeaders.Pragma.ParseAdd("no-cache");
- handler.UseCookies = true;
- handler.UseDefaultCredentials = true;
- // Cookie
- _InstagramCookie.AddInstagramCookie(handler);
- //
- // Create the request object
- string GUID = Guid.NewGuid().ToString().ToLower();
- InstaInboxFeedRequest feedReq = new InstaInboxFeedRequest();
- feedReq.device_id = string.Format("android-{0}", GUID);
- feedReq.guid = GUID;
- feedReq.user_id = UserId;
- feedReq.lofi = "1";
- feedReq.ContentType = "application/x-www-form-urlencoded; charset=UTF-8";
- MemoryStream stream = new MemoryStream();
- DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(InstaInboxFeedRequest));
- serializer.WriteObject(stream, feedReq);
- stream.Seek(0L, SeekOrigin.Begin);
- string PostString = InstaUploader.BuildSignedString(new StreamReader(stream).ReadToEnd(), true);
- HttpContent hcontent = new StringContent(PostString, Encoding.UTF8);
- hcontent.Headers.ContentType = new MediaTypeHeaderValue("application/x-www-form-urlencoded");
- hcontent.Headers.ContentType.CharSet = "UTF-8";
- try
- {
- HttpResponseMessage data = await req.PostAsync(FetchURL, hcontent);
- HttpContent content = data.Content;
- //Debug.WriteLine(ReturnData);
- if (data.StatusCode == System.Net.HttpStatusCode.OK || data.StatusCode == System.Net.HttpStatusCode.BadRequest)
- {
- string ReturnData = await content.ReadAsStringAsync();
- //Debug.WriteLine(ReturnData);
- return ReturnData;
- }
- }
- catch (Exception eex)
- {
- Debug.WriteLine(eex.ToString());
- }
- return null;
- }
- public static ObservableCollection<InboxFeedViewModel> ParseInboxFeed(string HTMLCode)
- {
- ObservableCollection<InboxFeedViewModel> data = new ObservableCollection<InboxFeedViewModel>();
- string ActivityStart = @"<ul class=""activity"">";
- string ActivityEnd = "</ul>";
- int ActivityPos = 0;
- while ((ActivityPos = HTMLCode.IndexOf(ActivityStart, ActivityPos)) != -1)
- {
- ActivityPos += ActivityStart.Length;
- int EndIndex = HTMLCode.IndexOf(ActivityEnd, ActivityPos);
- string RegionHTMLCode = HTMLCode.Substring(ActivityPos, EndIndex - ActivityPos).Trim();
- Match match = Regex.Match(RegionHTMLCode, @"<li class=\""(?<GroupName>[a-zA-Z0-9\w\s\-]+?)""\>"); // match the header
- if (match.Success)
- {
- string key = match.Groups["GroupName"].Value;
- InboxFeedViewModel model = new InboxFeedViewModel();
- // Look for time & relative time
- // <p class=\"timestamp\" data-timestamp=\"1372585058\">8 hours ago</p>\n
- Match match_time = Regex.Match(RegionHTMLCode, @"<p class=\""timestamp\"" data-timestamp=\""(?<TimeStamp>[0-9]+?)\"">(?<RelativeTime>[a-zA-Z0-9\w\s\-]+?)</p>\n");
- // Look for 'from' profile
- // <a class=\"single-image profile-pic thumbnail\" href=\"instagram://user?username=palomagoori\"><img aria-label=\"Profile picture of palomagoori\" src=\"http://images.ak.instagram.com/profiles/profile_436131405_75sq_1372247970.jpg\" onerror=\"this.src='//distillery.s3.amazonaws.com/profiles/anonymousUser.jpg';\"></a>
- Match match_fromProfile = Regex.Match(RegionHTMLCode, @"<a class=\""single-image profile-pic thumbnail\"" href=\""instagram://user\?username=(?<UserName>[a-zA-Z0-9_]+?)\""><img aria-label=\""(?<UserNameDesc>[a-zA-Z0-9_ ]+?)\"" src=\""(?<URL>[a-zA-Z0-9:/._]+?)\"" onerror=");
- // Look for profile id
- Match match_ProfileID = Regex.Match(RegionHTMLCode, "/profiles/profile_(?<UserId>[0-9]+?)_");
- // Look for 'to' profile
- // <a class=\"thumbnail gutter single-image\" href=\"instagram://media?id=488166943906032646_266606754\"><img src=\"https://distilleryimage10.s3.amazonaws.com/8e9995dadfda11e2ab9022000a9f14bb_5.jpg\"/></a>
- Match match_toProfile = Regex.Match(RegionHTMLCode, @"<a class=\""thumbnail gutter single-image\"" href=\""instagram://media\?id=(?<MediaId>[a-zA-Z0-9_]+?)\""><img src=\""(?<URL>[a-zA-Z0-9:/._]+?)\""/></a>");
- // Description
- // Match match_Desc2 = Regex.Match(RegionHTMLCode, @"<a href=\""instagram://user\?username=(?<UserName>[a-zA-Z0-9_]+?)\"">(?<UserName2>[a-zA-Z0-9_]+?)</a>(?<Desc>[a-zA-Z0-9_ .\w\s\-]+?)</p>");
- if (match_time.Success && match_fromProfile.Success && match_ProfileID.Success /*&& match_Desc2.Success*/)
- {
- model.timestamp = long.Parse(match_time.Groups["TimeStamp"].Value);
- model.TimeStampRelative = match_time.Groups["RelativeTime"].Value;
- model.ImageFromURL = match_fromProfile.Groups["URL"].Value;
- model.Username = match_fromProfile.Groups["UserName"].Value;
- model.UsernameDesc = match_fromProfile.Groups["UserNameDesc"].Value;
- if (match_toProfile.Success)// optional, this data isn't available for follows
- {
- model.OptionalMediaId = match_toProfile.Groups["MediaId"].Value;
- model.ImageToURL = match_toProfile.Groups["URL"].Value;
- }
- model.ProfileId = match_ProfileID.Groups["UserId"].Value;
- /* foreach (Group c in match_Desc2.Groups)
- {
- if (!c.Value.StartsWith("<a href=\"") && c.Length > 15)
- model.PostDesc2 = c.Value.Trim();
- }*/
- // TODO: Parse a preview of the comment
- model.IsNewFeed = key.Contains(" unread");
- switch (key)
- {
- case "fb-contact-joined-activity group gutter":
- case "fb-contact-joined-activity group gutter unread":
- {
- model.EnumType = InboxFeedTypeEnum.Follow;
- model.PostDesc = "{0} from your Facebook contacts has joined Instagram.";
- break;
- }
- break;
- case "group no-media follow-activity unread":
- case "group no-media follow-activity":
- {
- model.EnumType = InboxFeedTypeEnum.Follow;
- model.PostDesc = "{0} started following you.";
- break;
- }
- case "like-activity group unread":
- case "like-activity group":
- {
- model.EnumType = InboxFeedTypeEnum.Like;
- model.PostDesc = "{0} liked your photo.";
- break;
- }
- case "mention-activity group gutter unread":
- case "mention-activity group gutter":
- {
- model.EnumType = InboxFeedTypeEnum.Mention;
- model.PostDesc = "{0} mentioned you in a comment.";
- break;
- }
- case "comment-activity group unread":
- case "comment-activity group":
- {
- model.EnumType = InboxFeedTypeEnum.Comment;
- model.PostDesc = "{0} left a comment on your photo.";
- break;
- }
- default:
- {
- Debug.WriteLine(key);
- break;
- }
- }
- data.Add(model);
- }
- }
- }
- return data;
- }
- #endregion
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement