/******************************************************** GETTERS **********************************************************/ /// <summary> /// <para>gets information about a location</para> /// </summary> /// <param name="LocationId"></param> /// <returns></returns> public LocationInfoData GetLocationInformation(String LocationId) { LocationInfoData LocInfo = null; try { // SET UP REQUEST URI UriBuilder BaseUri = new UriBuilder(Config.GetUriScheme() + "://" + Config.GetApiUriString() + "/locations/" + LocationId); // SET UP QUERY STRING NameValueCollection QueryString = System.Web.HttpUtility.ParseQueryString(String.Empty); QueryString.Add("access_token", AuthorisedUser.AccessToken); // SET THE QUERY STRINGS BaseUri.Query = QueryString.ToString(); // CREATE NEW FEEDS OBJECT AND FILL IN DATA LocInfo = new LocationInfoData(); // SEND REQUEST WebClient Client = new WebClient(); byte[] ResponseData = Client.DownloadData(BaseUri.Uri); String Response = Encoding.UTF8.GetString(ResponseData); // PARSE JSON dynamic ParsedJson = JsonConvert.DeserializeObject(Response); // CREATE META OBJECT MetaData Meta = new MetaData(); Meta.Code = ParsedJson.meta.code; LocInfo.Meta = Meta; // SET DATA LocationData Data = new LocationData(); Data.Id = ParsedJson.data.id; Data.Name = ParsedJson.data.name; Data.Latitude = ParsedJson.data.latitude; Data.Longitude = ParsedJson.data.longitude; LocInfo.Data = Data; } catch (WebException WEx) { // FETCHES ANY ERROR THROWN BY INSTAGRAM API Stream ResponseStream = WEx.Response.GetResponseStream(); if (ResponseStream != null) { StreamReader ResponseReader = new StreamReader(ResponseStream); if (ResponseReader != null) { // PARSE JSON dynamic ParsedJson = JsonConvert.DeserializeObject(ResponseReader.ReadToEnd()); // CREATE NEW META OBJECT AND FILL IN DATA MetaData Meta = new MetaData(); Meta.Code = ParsedJson.meta.code; Meta.ErrorType = ParsedJson.meta.error_type; Meta.ErrorMessage = ParsedJson.meta.error_message; LocInfo.Meta = Meta; } } } catch (Exception Ex) { Console.WriteLine(Ex.StackTrace); } return LocInfo; }
/******************************************************** GETTERS **********************************************************/ /// <summary> /// <para>gets all the posts of the user - identified by UserId</para> /// </summary> /// <param name="UserId"></param> /// <param name="Parameters"></param> /// <returns></returns> public Feeds GetUserPosts(String UserId, GetUserPostsParameters Parameters) { Feeds UserPosts = null; try { // SET UP REQUEST URI UriBuilder BaseUri = new UriBuilder(); BaseUri.Scheme = Config.GetUriScheme(); BaseUri.Host = Config.GetApiUriString(); BaseUri.Host += "/users/" + UserId + "/media/recent"; // SET UP QUERY STRING NameValueCollection QueryString = System.Web.HttpUtility.ParseQueryString(String.Empty); QueryString.Add("client_id", Config.GetClientId()); QueryString.Add("count", Parameters.Count.ToString()); QueryString.Add("max_timestamp", Parameters.MaxTimeStamp.ToString()); QueryString.Add("min_timestamp", Parameters.MinTimeStamp.ToString()); QueryString.Add("min_id", Parameters.MinId); QueryString.Add("max_id", Parameters.MaxId); // SET THE QUERY STRINGS BaseUri.Query = QueryString.ToString(); // CREATE NEW FEEDS OBJECT AND FILL IN DATA UserPosts = new Feeds(); // SEND REQUEST WebClient Client = new WebClient(); byte[] ResponseData = Client.DownloadData(BaseUri.Uri); String Response = Encoding.UTF8.GetString(ResponseData); // PARSE JSON dynamic ParsedJson = JsonConvert.DeserializeObject(Response); // CREATE META OBJECT MetaData Meta = new MetaData(); Meta.Code = ParsedJson.meta.code; UserPosts.Meta = Meta; // CREATE PAGINATION OBJECT PaginationIdData Pagination = new PaginationIdData(); Pagination.NextUrl = ParsedJson.pagination.next_url; Pagination.NextMaxId = ParsedJson.pagination.next_max_id; UserPosts.Pagination = Pagination; // SET FEEDS DATA UserPosts.Data = ParseFeeds(ParsedJson); } catch (WebException WEx) { // FETCHES ANY ERROR THROWN BY INSTAGRAM API Stream ResponseStream = WEx.Response.GetResponseStream(); if (ResponseStream != null) { StreamReader ResponseReader = new StreamReader(ResponseStream); if (ResponseReader != null) { // PARSE JSON dynamic ParsedJson = JsonConvert.DeserializeObject(ResponseReader.ReadToEnd()); // CREATE NEW META OBJECT AND FILL IN DATA MetaData Meta = new MetaData(); Meta.Code = ParsedJson.meta.code; Meta.ErrorType = ParsedJson.meta.error_type; Meta.ErrorMessage = ParsedJson.meta.error_message; UserPosts.Meta = Meta; } } } catch (Exception Ex) { Console.WriteLine(Ex.StackTrace); } return UserPosts; }
/******************************************************** DELETERS *********************************************************/ /// <summary> /// <para>remove a comment</para> /// </summary> /// <param name="MediaId"></param> /// <param name="CommentId"></param> /// <returns></returns> public MetaData DeleteMediaComment(String MediaId, String CommentId) { MetaData Meta = null; try { // SET UP REQUEST URI UriBuilder BaseUri = new UriBuilder(Config.GetUriScheme() + "://" + Config.GetApiUriString() + "/media/" + MediaId + "/comments/" + CommentId); // SET UP QUERY String NameValueCollection QueryStrings = System.Web.HttpUtility.ParseQueryString(String.Empty); QueryStrings.Add("access_token", AuthorisedUser.AccessToken); BaseUri.Query = QueryStrings.ToString(); // CREATE NEW USER FOLLOWS OBJECT Meta = new MetaData(); // SEND REQUEST WebClient Client = new WebClient(); byte[] ResponseData = Client.UploadData(BaseUri.Uri, "DELETE", new byte[] {0}); String Response = Encoding.UTF8.GetString(ResponseData); // PARSE JSON dynamic ParsedJson = JsonConvert.DeserializeObject(Response); // CREATE META OBJECT Meta.Code = ParsedJson.meta.code; } catch (WebException WEx) { // FETCHES ANY ERROR THROWN BY INSTAGRAM API Stream ResponseStream = WEx.Response.GetResponseStream(); if (ResponseStream != null) { StreamReader ResponseReader = new StreamReader(ResponseStream); if (ResponseReader != null) { if (ResponseReader.Peek() == 60) { Meta.Code = 500; Meta.ErrorMessage = "No data received"; Meta.ErrorType = "Scope error"; } else { // PARSE JSON dynamic ParsedJson = JsonConvert.DeserializeObject(ResponseReader.ReadToEnd()); // CREATE NEW META OBJECT AND FILL IN DATA Meta.Code = ParsedJson.meta.code; Meta.ErrorType = ParsedJson.meta.error_type; Meta.ErrorMessage = ParsedJson.meta.error_message; } } } } catch (Exception Ex) { Console.WriteLine(Ex.Message); Console.WriteLine(Ex.StackTrace); } return Meta; }
/**************************************************************************************************************************/ /// <summary> /// <para>searched for location by geographic coordinate</para> /// </summary> /// <param name="Latitude"></param> /// <param name="Longitude"></param> /// <param name="Parameters"></param> /// <returns></returns> public LocationSearch GetLocationSearchResult(String Latitude, String Longitude, GetLocationSearchResultParameters Parameters) { LocationSearch SearchResult = null; try { // SET UP REQUEST URI UriBuilder BaseUri = new UriBuilder(Config.GetUriScheme() + "://" + Config.GetApiUriString() + "/locations/search"); // SET UP QUERY STRING NameValueCollection QueryString = System.Web.HttpUtility.ParseQueryString(String.Empty); QueryString.Add("access_token", AuthorisedUser.AccessToken); QueryString.Add("lat", Latitude); QueryString.Add("lng", Longitude); QueryString.Add("distance", Parameters.Distance); QueryString.Add("facebook_places_id", Parameters.FacebookPlacesId); QueryString.Add("foursquare_v2_id", Parameters.FoursquareV2Id); // SET THE QUERY STRING BaseUri.Query = QueryString.ToString(); Console.WriteLine(BaseUri.Uri.ToString()); // SEND REQUEST WebClient Client = new WebClient(); byte[] ResponseData = Client.DownloadData(BaseUri.Uri); String Response = Encoding.UTF8.GetString(ResponseData); // PARSE JSON dynamic ParsedJson = JsonConvert.DeserializeObject(Response); // CREATE SEARCH RESULT OBJECT SearchResult = new LocationSearch(); // CREATE META OBJECT MetaData Meta = new MetaData(); Meta.Code = ParsedJson.meta.code; SearchResult.Meta = Meta; // SET DATA FIELD IN SEARCHRESULT List<LocationData> Data = new List<LocationData>(); foreach (dynamic EachLocation in ParsedJson.data) { // CREATE NEW LOCATIONDATA OBJECT LocationData Location = new LocationData(); Location.Id = EachLocation.id; Location.Name = EachLocation.name; Location.Latitude = EachLocation.latitude; Location.Longitude = EachLocation.longitude; // ADD LOCATION TO THE LIST Data.Add(Location); } SearchResult.Data = Data; } catch (WebException WEx) { // FETCHES ANY ERROR THROWN BY INSTAGRAM API Stream ResponseStream = WEx.Response.GetResponseStream(); if (ResponseStream != null) { StreamReader ResponseReader = new StreamReader(ResponseStream); if (ResponseReader != null) { // PARSE JSON dynamic ParsedJson = JsonConvert.DeserializeObject(ResponseReader.ReadToEnd()); // CREATE NEW META OBJECT AND FILL IN DATA MetaData Meta = new MetaData(); Meta.Code = ParsedJson.meta.code; Meta.ErrorType = ParsedJson.meta.error_type; Meta.ErrorMessage = ParsedJson.meta.error_message; SearchResult.Meta = Meta; } } } catch (Exception Ex) { Console.WriteLine(Ex.StackTrace); } return SearchResult; }
/******************************************************** GETTERS **********************************************************/ /// <summary> /// <para>gets the list of recent comments on a media object</para> /// </summary> /// <param name="MediaId"></param> /// <returns></returns> public MediaComments GetMediaRecentComments(String MediaId) { MediaComments Comments = null; try { // SET UP REQUEST URI UriBuilder BaseUri = new UriBuilder(Config.GetUriScheme() + "://" + Config.GetApiUriString() + "/media/" + MediaId + "/comments"); // SET UP QUERY STRING NameValueCollection QueryString = System.Web.HttpUtility.ParseQueryString(String.Empty); QueryString.Add("access_token", AuthorisedUser.AccessToken); // SET THE QUERY STRINGS BaseUri.Query = QueryString.ToString(); // CREATE NEW FEEDS OBJECT AND FILL IN DATA Comments = new MediaComments(); // SEND REQUEST WebClient Client = new WebClient(); byte[] ResponseData = Client.DownloadData(BaseUri.Uri); String Response = Encoding.UTF8.GetString(ResponseData); // PARSE JSON dynamic ParsedJson = JsonConvert.DeserializeObject(Response); // CREATE META OBJECT MetaData Meta = new MetaData(); Meta.Code = ParsedJson.meta.code; Comments.Meta = Meta; // SET FEED DATA List<Comment> Data = new List<Comment>(); foreach (dynamic EachComment in ParsedJson.data) { Comment Comment = new Comment(); Comment.CreatedTime = new DateTime(long.Parse(EachComment.created_time.ToString())); Comment.Id = EachComment.id; Comment.Text = EachComment.text; User CommentedBy = new User(); CommentedBy.Id = EachComment.from.id; CommentedBy.UserName = EachComment.from.username; CommentedBy.ProfilePicture = EachComment.from.profile_picture; CommentedBy.FullName = EachComment.from.full_name; Comment.From = CommentedBy; Data.Add(Comment); } Comments.Data = Data; } catch (WebException WEx) { // FETCHES ANY ERROR THROWN BY INSTAGRAM API Stream ResponseStream = WEx.Response.GetResponseStream(); if (ResponseStream != null) { StreamReader ResponseReader = new StreamReader(ResponseStream); if (ResponseReader != null) { // PARSE JSON dynamic ParsedJson = JsonConvert.DeserializeObject(ResponseReader.ReadToEnd()); // CREATE NEW META OBJECT AND FILL IN DATA MetaData Meta = new MetaData(); Meta.Code = ParsedJson.meta.code; Meta.ErrorType = ParsedJson.meta.error_type; Meta.ErrorMessage = ParsedJson.meta.error_message; Comments.Meta = Meta; } } } catch (Exception Ex) { Console.WriteLine(Ex.StackTrace); } return Comments; }
/******************************************************** POSTERS **********************************************************/ /// <summary> /// <para>creates a comment on a media object</para> /// <para>restricted to specific applications</para> /// <para>refer https://instagram.com/developer/endpoints/comments/#post_media_comments</para> /// </summary> /// <param name="MediaId"></param> /// <param name="Text"></param> /// <returns></returns> public MetaData PostMediaComment(String MediaId, String Text) { MetaData Meta = null; try { // SET UP REQUEST URI UriBuilder BaseUri = new UriBuilder(); BaseUri.Scheme = Config.GetUriScheme(); BaseUri.Host = Config.GetApiUriString(); BaseUri.Path = "media/" + MediaId + "/comments"; // SET UP QUERY String NameValueCollection PostStrings = System.Web.HttpUtility.ParseQueryString(String.Empty); PostStrings.Add("access_token", AuthorisedUser.AccessToken); PostStrings.Add("text", Text); // CREATE NEW USER FOLLOWS OBJECT Meta = new MetaData(); // SEND REQUEST WebClient Client = new WebClient(); byte[] ResponseData = Client.UploadValues(BaseUri.Uri, PostStrings); String Response = Encoding.UTF8.GetString(ResponseData); // PARSE JSON dynamic ParsedJson = JsonConvert.DeserializeObject(Response); // CREATE META OBJECT Meta.Code = ParsedJson.meta.code; } catch (WebException WEx) { // FETCHES ANY ERROR THROWN BY INSTAGRAM API Stream ResponseStream = WEx.Response.GetResponseStream(); if (ResponseStream != null) { StreamReader ResponseReader = new StreamReader(ResponseStream); if (ResponseReader != null) { // PARSE JSON dynamic ParsedJson = JsonConvert.DeserializeObject(ResponseReader.ReadToEnd()); // CREATE NEW META OBJECT AND FILL IN DATA Meta.Code = ParsedJson.meta.code; Meta.ErrorType = ParsedJson.meta.error_type; Meta.ErrorMessage = ParsedJson.meta.error_message; } } } catch (Exception Ex) { Console.WriteLine(Ex.StackTrace); } return Meta; }
/******************************************************** GETTERS *********************************************************/ /// <summary> /// <para>gets the basic user information - identified by UserId</para> /// </summary> /// <param name="UserId"></param> /// <returns></returns> public User GetUserInformation(String UserId) { User RequestedUser = null; try { // SET UP REQUEST URI UriBuilder BaseUri = new UriBuilder(); BaseUri.Scheme = Config.GetUriScheme(); BaseUri.Host = Config.GetApiUriString(); BaseUri.Path = "users/" + UserId + "/"; // SET UP QUERY String NameValueCollection QueryString = System.Web.HttpUtility.ParseQueryString(String.Empty); QueryString.Add("access_token", AuthorisedUser.AccessToken); // SET THE QUERY StringS BaseUri.Query = QueryString.ToString(); // SEND REQUEST WebClient Client = new WebClient(); byte[] ResponseData = Client.DownloadData(BaseUri.Uri); String Response = Encoding.UTF8.GetString(ResponseData); // PARSE JSON dynamic ParsedJson = JsonConvert.DeserializeObject(Response); // CREATE NEW USER OBJECT AND FILL IN DATA RequestedUser = new User(); RequestedUser.Id = ParsedJson.id; RequestedUser.UserName = ParsedJson.data.username; RequestedUser.FullName = ParsedJson.data.full_name; RequestedUser.Bio = ParsedJson.data.bio; RequestedUser.Website = ParsedJson.data.website; RequestedUser.ProfilePicture = ParsedJson.data.profile_picture; RequestedUser.MediaCount = ParsedJson.data.counts.media; RequestedUser.FollowsCount = ParsedJson.data.counts.follows; RequestedUser.FollowedByCount = ParsedJson.data.counts.followed_by; // CREATE META OBJECT MetaData Meta = new MetaData(); Meta.Code = ParsedJson.meta.code; RequestedUser.Meta = Meta; } catch (WebException WEx) { // FETCHES ANY ERROR THROWN BY INSTAGRAM API Stream ResponseStream = WEx.Response.GetResponseStream(); if (ResponseStream != null) { StreamReader ResponseReader = new StreamReader(ResponseStream); if (ResponseReader != null) { // PARSE JSON dynamic ParsedJson = JsonConvert.DeserializeObject(ResponseReader.ReadToEnd()); // CREATE NEW USER OBJECT AND FILL IN DATA RequestedUser = new User(); // CREATE META OBJECT MetaData Meta = new MetaData(); Meta.Code = ParsedJson.meta.code; Meta.ErrorType = ParsedJson.meta.error_type; Meta.ErrorMessage = ParsedJson.meta.error_message; RequestedUser.Meta = Meta; } } } catch (Exception Ex) { Console.WriteLine(Ex.StackTrace); } return RequestedUser; }
/**************************************************************************************************************************/ /// <summary> /// <para>gets the search result</para> /// </summary> /// <param name="Parameters"></param> /// <returns></returns> public UserSearch GetUserSearchResult(GetUserSearchResultParameters Parameters) { UserSearch SearchResult = null; try { // SET UP REQUEST URI UriBuilder BaseUri = new UriBuilder(); BaseUri.Scheme = Config.GetUriScheme(); BaseUri.Host = Config.GetApiUriString(); BaseUri.Host += "/users/search"; // SET UP QUERY STRING NameValueCollection QueryString = System.Web.HttpUtility.ParseQueryString(String.Empty); QueryString.Add("access_token", AuthorisedUser.AccessToken); QueryString.Add("q", Parameters.Query); QueryString.Add("count", Parameters.Count.ToString()); // SET THE QUERY STRING BaseUri.Query = QueryString.ToString(); // SEND REQUEST WebClient Client = new WebClient(); byte[] ResponseData = Client.DownloadData(BaseUri.Uri); String Response = Encoding.UTF8.GetString(ResponseData); // PARSE JSON dynamic ParsedJson = JsonConvert.DeserializeObject(Response); // CREATE SEARCH RESULT OBJECT SearchResult = new UserSearch(); // CREATE META OBJECT MetaData Meta = new MetaData(); Meta.Code = ParsedJson.meta.code; SearchResult.Meta = Meta; // SET DATA FIELD IN SEARCHRESULT List<User> SearchedUsers = new List<User>(); foreach (dynamic EachUser in ParsedJson.data) { // CREATE NEW USER OBJECT User EUser = new User(); EUser.UserName = EachUser.username; EUser.Bio = EachUser.bio; EUser.Website = EachUser.website; EUser.ProfilePicture = EachUser.profile_picture; EUser.FullName = EachUser.full_name; EUser.Id = EachUser.id; // ADD EUSER TO THE LIST SearchedUsers.Add(EUser); } SearchResult.Data = SearchedUsers; } catch (WebException WEx) { // FETCHES ANY ERROR THROWN BY INSTAGRAM API Stream ResponseStream = WEx.Response.GetResponseStream(); if (ResponseStream != null) { StreamReader ResponseReader = new StreamReader(ResponseStream); if (ResponseReader != null) { // PARSE JSON dynamic ParsedJson = JsonConvert.DeserializeObject(ResponseReader.ReadToEnd()); // CREATE NEW META OBJECT AND FILL IN DATA MetaData Meta = new MetaData(); Meta.Code = ParsedJson.meta.code; Meta.ErrorType = ParsedJson.meta.error_type; Meta.ErrorMessage = ParsedJson.meta.error_message; SearchResult.Meta = Meta; } } } catch (Exception Ex) { Console.WriteLine(Ex.StackTrace); } return SearchResult; }
/**************************************************************************************************************************/ /// <summary> /// <para>searches for tags by name</para> /// </summary> /// <param name="TagName"></param> /// <returns></returns> public TagSearch GetTagSearchResult(String TagName) { TagSearch SearchResult = null; try { // SET UP REQUEST URI UriBuilder BaseUri = new UriBuilder(); BaseUri.Scheme = Config.GetUriScheme(); BaseUri.Host = Config.GetApiUriString(); BaseUri.Host += "/tags/search"; // SET UP QUERY STRING NameValueCollection QueryString = System.Web.HttpUtility.ParseQueryString(String.Empty); QueryString.Add("access_token", AuthorisedUser.AccessToken); QueryString.Add("q", TagName); // SET THE QUERY STRING BaseUri.Query = QueryString.ToString(); // SEND REQUEST WebClient Client = new WebClient(); byte[] ResponseData = Client.DownloadData(BaseUri.Uri); String Response = Encoding.UTF8.GetString(ResponseData); // PARSE JSON dynamic ParsedJson = JsonConvert.DeserializeObject(Response); // CREATE SEARCH RESULT OBJECT SearchResult = new TagSearch(); // CREATE META OBJECT MetaData Meta = new MetaData(); Meta.Code = ParsedJson.meta.code; SearchResult.Meta = Meta; // SET DATA FIELD IN SEARCHRESULT List<TagInfo> Data = new List<TagInfo>(); foreach (dynamic EachTag in ParsedJson.data) { // CREATE NEW TAG INFO OBJECT TagInfo TInfo = new TagInfo(); TInfo.MediaCount = long.Parse(EachTag.media_count.ToString()); TInfo.Name = EachTag.name; // ADD TAG INFO TO THE LIST Data.Add(TInfo); } SearchResult.Data = Data; } catch (WebException WEx) { // FETCHES ANY ERROR THROWN BY INSTAGRAM API Stream ResponseStream = WEx.Response.GetResponseStream(); if (ResponseStream != null) { StreamReader ResponseReader = new StreamReader(ResponseStream); if (ResponseReader != null) { // PARSE JSON dynamic ParsedJson = JsonConvert.DeserializeObject(ResponseReader.ReadToEnd()); // CREATE NEW META OBJECT AND FILL IN DATA MetaData Meta = new MetaData(); Meta.Code = ParsedJson.meta.code; Meta.ErrorType = ParsedJson.meta.error_type; Meta.ErrorMessage = ParsedJson.meta.error_message; SearchResult.Meta = Meta; } } } catch (Exception Ex) { Console.WriteLine(Ex.StackTrace); } return SearchResult; }
/**************************************************************************************************************************/ /// <summary> /// <para>searched for media in a given area</para> /// </summary> /// <param name="Latitude"></param> /// <param name="Longitude"></param> /// <param name="Parameters"></param> /// <returns></returns> public MediaSearch GetMediaSearchResult(String Latitude, String Longitude, GetMediaSearchResultParameters Parameters) { MediaSearch Media = null; try { // SET UP REQUEST URI UriBuilder BaseUri = new UriBuilder(); BaseUri.Scheme = Config.GetUriScheme(); BaseUri.Host = Config.GetApiUriString(); BaseUri.Host += "/media/search"; // SET UP QUERY STRING NameValueCollection QueryString = System.Web.HttpUtility.ParseQueryString(String.Empty); QueryString.Add("lat", Latitude); QueryString.Add("lng", Longitude); QueryString.Add("access_token", AuthorisedUser.AccessToken); QueryString.Add("count", Parameters.Count.ToString()); QueryString.Add("min_timestamp", Parameters.MinTimestamp); QueryString.Add("max_timestamp", Parameters.Maxtimestamp); QueryString.Add("distance", Parameters.Distance); // SET THE QUERY STRINGS BaseUri.Query = QueryString.ToString(); // CREATE NEW FEEDS OBJECT AND FILL IN DATA Media = new MediaSearch(); // SEND REQUEST WebClient Client = new WebClient(); byte[] ResponseData = Client.DownloadData(BaseUri.Uri); String Response = Encoding.UTF8.GetString(ResponseData); // PARSE JSON dynamic ParsedJson = JsonConvert.DeserializeObject(Response); // CREATE META OBJECT MetaData Meta = new MetaData(); Meta.Code = ParsedJson.meta.code; Media.Meta = Meta; // SET FEEDS DATA Media.Data = ParseMediaSearchFeeds(ParsedJson); } catch (WebException WEx) { // FETCHES ANY ERROR THROWN BY INSTAGRAM API Stream ResponseStream = WEx.Response.GetResponseStream(); if (ResponseStream != null) { StreamReader ResponseReader = new StreamReader(ResponseStream); if (ResponseReader != null) { // PARSE JSON dynamic ParsedJson = JsonConvert.DeserializeObject(ResponseReader.ReadToEnd()); // CREATE NEW META OBJECT AND FILL IN DATA MetaData Meta = new MetaData(); Meta.Code = ParsedJson.meta.code; Meta.ErrorType = ParsedJson.meta.error_type; Meta.ErrorMessage = ParsedJson.meta.error_message; Media.Meta = Meta; } } } catch (Exception Ex) { Console.WriteLine(Ex.StackTrace); } return Media; }
/**************************************************************************************************************************/ /// <summary> /// <para>gets the media information using shortcode</para> /// </summary> /// <param name="ShortCode"></param> /// <returns></returns> public MediaData GetMediaInformationShotcode(String ShortCode) { MediaData Media = null; try { // SET UP REQUEST URI UriBuilder BaseUri = new UriBuilder(Config.GetUriScheme() + "://" + Config.GetApiUriString() + "/media/shortcode/" + ShortCode); //BaseUri.Scheme = Config.GetUriScheme(); //BaseUri.Host = Config.GetApiUriString(); //BaseUri.Host += "/media/shortcode/" + ShortCode; // SET UP QUERY STRING NameValueCollection QueryString = System.Web.HttpUtility.ParseQueryString(String.Empty); QueryString.Add("access_token", AuthorisedUser.AccessToken); // SET THE QUERY STRINGS BaseUri.Query = QueryString.ToString(); // CREATE NEW FEEDS OBJECT AND FILL IN DATA Media = new MediaData(); // SEND REQUEST WebClient Client = new WebClient(); byte[] ResponseData = Client.DownloadData(BaseUri.Uri); String Response = Encoding.UTF8.GetString(ResponseData); // PARSE JSON dynamic ParsedJson = JsonConvert.DeserializeObject(Response); // CREATE META OBJECT MetaData Meta = new MetaData(); Meta.Code = ParsedJson.meta.code; Media.Meta = Meta; // SET FEED DATA Media.Feed = ParseFeed(ParsedJson); } catch (WebException WEx) { // FETCHES ANY ERROR THROWN BY INSTAGRAM API Stream ResponseStream = WEx.Response.GetResponseStream(); if (ResponseStream != null) { StreamReader ResponseReader = new StreamReader(ResponseStream); if (ResponseReader != null) { // PARSE JSON dynamic ParsedJson = JsonConvert.DeserializeObject(ResponseReader.ReadToEnd()); // CREATE NEW META OBJECT AND FILL IN DATA MetaData Meta = new MetaData(); Meta.Code = ParsedJson.meta.code; Meta.ErrorType = ParsedJson.meta.error_type; Meta.ErrorMessage = ParsedJson.meta.error_message; Media.Meta = Meta; } } } catch (Exception Ex) { Console.WriteLine(Ex.StackTrace); } return Media; }
/******************************************************** GETTERS **********************************************************/ /// <summary> /// <para>gets information about a tag object</para> /// </summary> /// <param name="TagName"></param> /// <returns></returns> public TagData GetTagInformation(String TagName) { TagData Tag = null; try { // SET UP REQUEST URI UriBuilder BaseUri = new UriBuilder(Config.GetUriScheme() + "://" + Config.GetApiUriString() + "/tags/" + TagName); // SET UP QUERY STRING NameValueCollection QueryString = System.Web.HttpUtility.ParseQueryString(String.Empty); QueryString.Add("client_id", Config.GetClientId()); // SET THE QUERY STRINGS BaseUri.Query = QueryString.ToString(); // CREATE NEW FEEDS OBJECT AND FILL IN DATA Tag = new TagData(); // SEND REQUEST WebClient Client = new WebClient(); byte[] ResponseData = Client.DownloadData(BaseUri.Uri); String Response = Encoding.UTF8.GetString(ResponseData); // PARSE JSON dynamic ParsedJson = JsonConvert.DeserializeObject(Response); // CREATE META OBJECT MetaData Meta = new MetaData(); Meta.Code = ParsedJson.meta.code; Tag.Meta = Meta; // SET DATA TagInfo TagInfo = new TagInfo(); TagInfo.MediaCount = long.Parse(ParsedJson.data.media_count.ToString()); TagInfo.Name = ParsedJson.data.name; Tag.Data = TagInfo; } catch (WebException WEx) { // FETCHES ANY ERROR THROWN BY INSTAGRAM API Stream ResponseStream = WEx.Response.GetResponseStream(); if (ResponseStream != null) { StreamReader ResponseReader = new StreamReader(ResponseStream); if (ResponseReader != null) { // PARSE JSON dynamic ParsedJson = JsonConvert.DeserializeObject(ResponseReader.ReadToEnd()); // CREATE NEW META OBJECT AND FILL IN DATA MetaData Meta = new MetaData(); Meta.Code = ParsedJson.meta.code; Meta.ErrorType = ParsedJson.meta.error_type; Meta.ErrorMessage = ParsedJson.meta.error_message; Tag.Meta = Meta; } } } catch (Exception Ex) { Console.WriteLine(Ex.StackTrace); } return Tag; }
/************************************************************** OTHER METHODS ****************************************************************/ /// <summary> /// <para>verifies and sets the authorised user</para> /// </summary> private void FetchAccessToken() { try { WebClient Client = new WebClient(); UriBuilder AuthenticationTokenRequestUri = new UriBuilder(); NameValueCollection PostStrings = System.Web.HttpUtility.ParseQueryString(String.Empty); // SET THE POST VALUES PostStrings.Add("client_id", this.Config.GetClientId()); PostStrings.Add("client_secret", this.Config.GetClientSecret()); PostStrings.Add("grant_type", this.GrantType); PostStrings.Add("redirect_uri", this.Config.GetRedirectUriString()); PostStrings.Add("code", this.Code); // SET UP REQUEST URI AuthenticationTokenRequestUri.Scheme = this.Config.GetUriScheme(); AuthenticationTokenRequestUri.Host = this.AccessTokenUri; // STORE VALUES IN AUTHUSER AuthorisedUser = new AuthUser(); // CREATE NEW META OBJECT AND FILL IN DATA Meta = new MetaData(); // SEND POST REQUEST byte[] ResponseBytes = Client.UploadValues(AuthenticationTokenRequestUri.Uri, PostStrings); String Response = Encoding.UTF8.GetString(ResponseBytes); // PARSE JSON dynamic ParsedJson = JsonConvert.DeserializeObject(Response); Meta.Code = 200; AuthorisedUser.Meta = Meta; AuthorisedUser.AccessToken = ParsedJson.access_token; AuthorisedUser.UserId = ParsedJson.user.id; AuthorisedUser.UserName = ParsedJson.user.username; AuthorisedUser.FullName = ParsedJson.user.full_name; AuthorisedUser.Bio = ParsedJson.user.bio; AuthorisedUser.Website = ParsedJson.user.website; AuthorisedUser.ProfilePicture = ParsedJson.user.profile_picture; } catch (WebException WEx) { // FETCHES ANY ERROR THROWN BY INSTAGRAM API Stream ResponseStream = WEx.Response.GetResponseStream(); if (ResponseStream != null) { StreamReader ResponseReader = new StreamReader(ResponseStream); if (ResponseReader != null) { // PARSE JSON dynamic ParsedJson = JsonConvert.DeserializeObject(ResponseReader.ReadToEnd()); // CREATE NEW META OBJECT AND FILL IN DATA Meta.Code = ParsedJson.code; Meta.ErrorType = ParsedJson.error_type; Meta.ErrorMessage = ParsedJson.error_message; AuthorisedUser.Meta = Meta; } } } catch (Exception Ex) { Console.WriteLine(Ex.StackTrace); } }