Advertisement
Guest User

Untitled

a guest
Oct 1st, 2013
700
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 9.95 KB | None | 0 0
  1.         #region InboxFeed
  2.         public static async Task<string> LoadUserInboxFeed(string UserId, string Username)
  3.         {
  4.             // Fetching
  5.             Uri InstaURI = new Uri("http://instagram.com");
  6.             string FetchURL = string.Format("https://instagram.com/api/v1/news/inbox/?user_id={0}", UserId);
  7.  
  8.  
  9.             var handler = new HttpClientHandler();
  10.             var req = new HttpClient(handler);
  11.  
  12.             req.DefaultRequestHeaders.Accept.ParseAdd("*/*");
  13.             req.DefaultRequestHeaders.AcceptLanguage.ParseAdd("en-US,en");
  14.             //req.DefaultRequestHeaders.AcceptLanguage.ParseAdd("identity");
  15.             req.DefaultRequestHeaders.Accept.ParseAdd("application/json, application/xml, text/json, text/x-json, text/javascript, text/xml");
  16.             req.DefaultRequestHeaders.Referrer = new Uri("http://instagram.com/#");
  17.             req.DefaultRequestHeaders.Host = "instagram.com";
  18.             req.DefaultRequestHeaders.Add("User-Agent", "Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2; WOW64; Trident/6.0; Touch)");
  19.             req.DefaultRequestHeaders.Pragma.ParseAdd("no-cache");
  20.  
  21.             handler.UseCookies = true;
  22.             handler.UseDefaultCredentials = true;
  23.  
  24.             // Cookie
  25.             _InstagramCookie.AddInstagramCookie(handler);
  26.             //
  27.  
  28.             // Create the request object
  29.             string GUID = Guid.NewGuid().ToString().ToLower();
  30.             InstaInboxFeedRequest feedReq = new InstaInboxFeedRequest();
  31.             feedReq.device_id = string.Format("android-{0}", GUID);
  32.             feedReq.guid = GUID;
  33.             feedReq.user_id = UserId;
  34.             feedReq.lofi = "1";
  35.             feedReq.ContentType = "application/x-www-form-urlencoded; charset=UTF-8";
  36.  
  37.             MemoryStream stream = new MemoryStream();
  38.             DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(InstaInboxFeedRequest));
  39.             serializer.WriteObject(stream, feedReq);
  40.             stream.Seek(0L, SeekOrigin.Begin);
  41.             string PostString = InstaUploader.BuildSignedString(new StreamReader(stream).ReadToEnd(), true);
  42.  
  43.             HttpContent hcontent = new StringContent(PostString, Encoding.UTF8);
  44.             hcontent.Headers.ContentType = new MediaTypeHeaderValue("application/x-www-form-urlencoded");
  45.             hcontent.Headers.ContentType.CharSet = "UTF-8";
  46.  
  47.             try
  48.             {
  49.                 HttpResponseMessage data = await req.PostAsync(FetchURL, hcontent);
  50.                 HttpContent content = data.Content;
  51.  
  52.                 //Debug.WriteLine(ReturnData);
  53.                 if (data.StatusCode == System.Net.HttpStatusCode.OK || data.StatusCode == System.Net.HttpStatusCode.BadRequest)
  54.                 {
  55.                     string ReturnData = await content.ReadAsStringAsync();
  56.                     //Debug.WriteLine(ReturnData);
  57.  
  58.                     return ReturnData;
  59.                 }
  60.             }
  61.             catch (Exception eex)
  62.             {
  63.                 Debug.WriteLine(eex.ToString());
  64.             }
  65.             return null;
  66.         }
  67.  
  68.         public static ObservableCollection<InboxFeedViewModel> ParseInboxFeed(string HTMLCode)
  69.         {
  70.             ObservableCollection<InboxFeedViewModel> data = new ObservableCollection<InboxFeedViewModel>();
  71.  
  72.             string ActivityStart = @"<ul class=""activity"">";
  73.             string ActivityEnd = "</ul>";
  74.  
  75.             int ActivityPos = 0;
  76.             while ((ActivityPos = HTMLCode.IndexOf(ActivityStart, ActivityPos)) != -1)
  77.             {
  78.                 ActivityPos += ActivityStart.Length;
  79.  
  80.                 int EndIndex = HTMLCode.IndexOf(ActivityEnd, ActivityPos);
  81.                 string RegionHTMLCode = HTMLCode.Substring(ActivityPos, EndIndex - ActivityPos).Trim();
  82.  
  83.                 Match match = Regex.Match(RegionHTMLCode, @"<li class=\""(?<GroupName>[a-zA-Z0-9\w\s\-]+?)""\>"); // match the header
  84.  
  85.                 if (match.Success)
  86.                 {
  87.                     string key = match.Groups["GroupName"].Value;
  88.  
  89.                     InboxFeedViewModel model = new InboxFeedViewModel();
  90.  
  91.                     // Look for time & relative time
  92.                     // <p class=\"timestamp\" data-timestamp=\"1372585058\">8 hours ago</p>\n
  93.                     Match match_time = Regex.Match(RegionHTMLCode, @"<p class=\""timestamp\"" data-timestamp=\""(?<TimeStamp>[0-9]+?)\"">(?<RelativeTime>[a-zA-Z0-9\w\s\-]+?)</p>\n");
  94.  
  95.                    // Look for 'from' profile
  96.                    // <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>
  97.                    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=");
  98.  
  99.                     // Look for profile id
  100.                     Match match_ProfileID = Regex.Match(RegionHTMLCode, "/profiles/profile_(?<UserId>[0-9]+?)_");
  101.  
  102.                     // Look for 'to' profile
  103.                     // <a class=\"thumbnail gutter single-image\" href=\"instagram://media?id=488166943906032646_266606754\"><img src=\"https://distilleryimage10.s3.amazonaws.com/8e9995dadfda11e2ab9022000a9f14bb_5.jpg\"/></a>
  104.                     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>");
  105.  
  106.                     // Description
  107.                     // 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>");
  108.  
  109.  
  110.                     if (match_time.Success && match_fromProfile.Success && match_ProfileID.Success /*&& match_Desc2.Success*/)
  111.                     {
  112.                         model.timestamp = long.Parse(match_time.Groups["TimeStamp"].Value);
  113.                         model.TimeStampRelative = match_time.Groups["RelativeTime"].Value;
  114.                         model.ImageFromURL = match_fromProfile.Groups["URL"].Value;
  115.                         model.Username = match_fromProfile.Groups["UserName"].Value;
  116.                         model.UsernameDesc = match_fromProfile.Groups["UserNameDesc"].Value;
  117.  
  118.                         if (match_toProfile.Success)// optional, this data isn't available for follows
  119.                         {
  120.                             model.OptionalMediaId = match_toProfile.Groups["MediaId"].Value;
  121.                             model.ImageToURL = match_toProfile.Groups["URL"].Value;
  122.                         }
  123.                         model.ProfileId = match_ProfileID.Groups["UserId"].Value;
  124.                         /* foreach (Group c in match_Desc2.Groups)
  125.                          {
  126.                              if (!c.Value.StartsWith("<a href=\"") && c.Length > 15)
  127.                                  model.PostDesc2 = c.Value.Trim();
  128.                          }*/
  129.                         // TODO: Parse a preview of the comment
  130.                         model.IsNewFeed = key.Contains(" unread");
  131.  
  132.                         switch (key)
  133.                         {
  134.                             case "fb-contact-joined-activity group gutter":
  135.                             case "fb-contact-joined-activity group gutter unread":
  136.                                 {
  137.                                     model.EnumType = InboxFeedTypeEnum.Follow;
  138.                                     model.PostDesc = "{0} from your Facebook contacts has joined Instagram.";
  139.                                     break;
  140.                                 }
  141.                                 break;
  142.                             case "group no-media follow-activity unread":
  143.                             case "group no-media follow-activity":
  144.                                 {
  145.                                     model.EnumType = InboxFeedTypeEnum.Follow;
  146.                                     model.PostDesc = "{0} started following you.";
  147.                                     break;
  148.                                 }
  149.                             case "like-activity group unread":
  150.                             case "like-activity group":
  151.                                 {
  152.                                     model.EnumType = InboxFeedTypeEnum.Like;
  153.                                     model.PostDesc = "{0} liked your photo.";
  154.                                     break;
  155.                                 }
  156.                             case "mention-activity group gutter unread":
  157.                             case "mention-activity group gutter":
  158.                                 {
  159.                                     model.EnumType = InboxFeedTypeEnum.Mention;
  160.                                     model.PostDesc = "{0} mentioned you in a comment.";
  161.                                     break;
  162.                                 }
  163.                             case "comment-activity group unread":
  164.                             case "comment-activity group":
  165.                                 {
  166.                                     model.EnumType = InboxFeedTypeEnum.Comment;
  167.                                     model.PostDesc = "{0} left a comment on your photo.";
  168.                                     break;
  169.                                 }
  170.                             default:
  171.                                 {
  172.                                     Debug.WriteLine(key);
  173.                                     break;
  174.                                 }
  175.                         }
  176.  
  177.                         data.Add(model);
  178.                     }
  179.                 }
  180.  
  181.             }
  182.             return data;
  183.         }
  184.         #endregion
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement