/// <summary> /// Get post associated with post number. /// </summary> /// <returns><see cref="ChanPost">ChanPost</see> detailing the post</returns> /// /// <exception cref="ArgumentNullException">Thrown if board is null.</exception> /// <exception cref="ArgumentException">Thrown if board is empty or whitespace.</exception> /// <exception cref="ArgumentOutOfRangeException">Thrown if postNo <= 0.</exception> /// /// <param name="board">Board where post is located.</param> /// <param name="postNo">Post number.</param> public static ChanPost GetPost(string board, int postNo) { board.ThrowIfNullOrWhiteSpace("board"); if (postNo < 1) { throw new ArgumentOutOfRangeException("post", "Cannot be 0 or negative."); } var jsonReq = string.Format("http://archive.moe/_/api/chan/post/?board={0}&num={1}", board, postNo); var json = WebString.Download(jsonReq); if (!json.Success) { return(new ChanPost(json)); } dynamic postJson = JsonConvert.DeserializeObject(json.Document); int threadNo = postJson.thread_num; string subject = postJson.title; string comment = postJson.comment_sanitized; return(new ChanPost(json.Location, board, ChanTools.GetBoardName(board), threadNo, postNo, subject, comment)); }
public WeatherConditions GetConditions(WeatherLocation location) { if (location == null) { throw new ArgumentNullException(nameof(location)); } if (!location.IsValidQuery) { throw new ArgumentException("WeatherLocation must have a valid value.", nameof(location)); } var queryResult = WebString.Download(MakeQuery(location)); if (!queryResult.Success) { return(new WeatherConditions(queryResult)); } var json = JObject.Parse(queryResult.Document); var message = (string)json["message"]; if (string.IsNullOrEmpty(message)) { return(new WeatherConditions(queryResult.Location, json)); } else { var ex = new JsonErrorException(message); return(new WeatherConditions(queryResult.Location, ex)); } }
/// <summary> /// Get info of a Danbooru post. /// </summary> /// <returns><see cref="DanboPost">DanboPost</see> detailing a post.</returns> /// <exception cref="ArgumentOutOfRangeException">Thrown if postNo is <= 0.</exception> /// <param name="postNo">Post number.</param> public static DanboPost GetPostInfo(int postNo) { if (postNo < 1) { throw new ArgumentOutOfRangeException(nameof(postNo), "Can't be 0 or negative."); } var jsonReq = string.Format("https://danbooru.donmai.us/posts/{0}.json", postNo); var json = WebString.Download(jsonReq); if (!json.Success) { return(new DanboPost(json)); } dynamic postJson = JsonConvert.DeserializeObject(json.Document); string copyrights = postJson.tag_string_copyright; string characters = postJson.tag_string_character; string artists = postJson.tag_string_artist; string general = postJson.tag_string_general; string meta = postJson.tag_string_meta; string all = postJson.tag_string; string rating = postJson.rating; return(new DanboPost(json.Location, postNo, copyrights, characters, artists, general, meta, all, rating)); }
/// <summary> /// Get post associated with post number. /// </summary> /// <returns><see cref="ChanPost">ChanPost</see> detailing the post.</returns> /// /// <exception cref="ArgumentNullException">Thrown if board is null.</exception> /// <exception cref="ArgumentException">Thrown if board is empty or whitespace.</exception> /// <exception cref="ArgumentOutOfRangeException">Thrown if threadNo and/or postNo <= 0.</exception> /// /// <param name="board">Board where thread/post is located.</param> /// <param name="threadNo">Thread number.</param> /// <param name="postNo">Post number.</param> public static ChanPost GetPost(string board, int threadNo, int postNo) { board.ThrowIfNullOrWhiteSpace("board"); if (threadNo < 1) { throw new ArgumentOutOfRangeException("thread", "Cannot be 0 or negative."); } else if (postNo < 1) { throw new ArgumentOutOfRangeException("post", "Cannot be 0 or negative."); } var jsonReq = string.Format("http://a.4cdn.org/{0}/res/{1}.json", board, threadNo); var json = WebString.Download(jsonReq); if (!json.Success) { return(new ChanPost(json)); } dynamic threadJson = JsonConvert.DeserializeObject(json.Document); string subject = null; string comment = null; // Simple linear search, seems fast enough for the 100s of posts it needs to look through. foreach (var post in threadJson.posts) { if (post.no == postNo) { subject = post.sub; if (!string.IsNullOrEmpty(subject)) { subject = HttpUtility.HtmlDecode(subject); } comment = post.com; if (!string.IsNullOrEmpty(comment)) { comment = Fix4chanPost(comment); } break; } } return(new ChanPost(json.Location, board, ChanTools.GetBoardName(board), threadNo, postNo, subject, comment)); }
public static SearchResults Search(string query) { if (query == null) { throw new ArgumentNullException(nameof(query)); } var results = WebString.Download(GoogleUrl(query)); if (!results.Success) { return(new SearchResults(results)); } var matches = resultsRegexp.Matches(results.Document); var parsedResults = ParseMatches(matches); return(new SearchResults(results, Deduplication(parsedResults))); }
/// <summary> /// Get info of a Gelbooru post. /// </summary> /// <returns><see cref="BooruPost">BooruPost</see> detailing a post.</returns> /// <exception cref="ArgumentOutOfRangeException">Thrown if postNo is <= 0.</exception> /// <param name="postNo">Post number.</param> public static BooruPost GetPostInfo(int postNo) { if (postNo < 1) { throw new ArgumentOutOfRangeException(nameof(postNo), "Can't be 0 or negative."); } var xmlReq = string.Format("https://gelbooru.com/index.php?page=dapi&s=post&q=index&id={0}", postNo); var xml = WebString.Download(xmlReq); if (!xml.Success) { return(new BooruPost(xml)); } var postXml = XElement.Parse(xml.Document).Element("post"); string tags = postXml.Attribute("tags").Value; string rated = postXml.Attribute("rating").Value; return(new BooruPost(xml.Location, postNo, tags, rated)); }