/// <summary> /// Determines the rip log score out of 100. /// </summary> /// <param name="log">Log file contents.</param> /// <param name="authKey">WhatCD users' authentication key.</param> /// <returns>Log score out of 100.</returns> public int GetFlacLogScore(string log, string authKey) { var postData = string.Format("action=takeupload&auth={0}&log_contents={1}", authKey, HttpUtility.UrlEncode(log)); var wrw = new WebRequestWrapper(new Uri(this.BaseWhatCDUri, "logchecker.php"), WebRequestWrapper.RequestMethod.POST, "application/x-www-form-urlencoded", ref _cookieJar, postData); using (var response = wrw.PerformWebRequest()) using (var reader = new StreamReader(response)) { return ExtractLogScore(reader.ReadToEnd()); } }
/// <summary> /// Logs off the current What.CD session. /// </summary> /// <param name="authKey">WhatCD users' authentication key.</param> public void Logoff(string authKey) { var wrw = new WebRequestWrapper(new Uri(this.BaseWhatCDUri, string.Format("logout.php?auth={0}", authKey)), WebRequestWrapper.RequestMethod.POST, "application/x-www-form-urlencoded", ref _cookieJar); using (wrw.PerformWebRequest()) { } }
/// <summary> /// Performs a JSON request. /// </summary> /// <param name="uri">Base URI.</param> /// <param name="query">Request arguments (appended to the base URI).</param> /// <returns>Raw Json response.</returns> public string RequestJson(Uri uri, string query) { var fullUri = new Uri(uri, query); var wrw = new WebRequestWrapper(fullUri, WebRequestWrapper.RequestMethod.GET, "application/json; charset=utf-8", ref _cookieJar); using (var response = wrw.PerformWebRequest()) using (var reader = new StreamReader(response)) { var json = reader.ReadToEnd(); if (json.TrimStart().StartsWith("{\"status\":\"failure\"", StringComparison.OrdinalIgnoreCase)) throw new WebException(string.Format("Request for {0} returned Json status failure: {1}", fullUri, json)); return json; } }
/// <summary> /// Logs a user in to What.CD and stores session cookies. /// </summary> /// <param name="username">What.CD account username.</param> /// <param name="password">What.CD account password.</param> private void Login(string username, string password) { var wrw = new WebRequestWrapper(new Uri(this.BaseWhatCDUri, "login.php"), WebRequestWrapper.RequestMethod.POST, "application/x-www-form-urlencoded", ref _cookieJar, string.Format("username={0}&password={1}", Uri.EscapeDataString(username), Uri.EscapeDataString(password))); using (wrw.PerformWebRequest()) { } }
/// <summary> /// Performs a binary request. /// </summary> /// <param name="uri">Base URI.</param> /// <param name="query">Request arguments (appended to the base URI).</param> /// <returns>Raw binary response.</returns> public byte[] RequestBytes(Uri uri, string query, out string contentDisposition, out string contentType) { var wrw = new WebRequestWrapper(new Uri(uri, query), WebRequestWrapper.RequestMethod.GET, "application/json; charset=utf-8", ref _cookieJar); using (var response = wrw.PerformWebRequest()) { var result = new MemoryStream(); var buffer = new byte[4096]; int read; while ((read = response.Read(buffer, 0, buffer.Length)) > 0) { result.Write(buffer, 0, read); } contentDisposition = wrw.WebHeaders["Content-Disposition"].ToString(); contentType = wrw.WebHeaders["Content-Type"].ToString(); return result.ToArray(); } }