/******************************************************** 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("access_token", AuthorisedUser.AccessToken); // 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; }
/**************************************************************************************************************************/ /// <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; }