/// <summary> /// Sends episode sync data to Trakt /// </summary> /// <param name="syncData">The sync data to send</param> /// <param name="mode">The sync mode to use</param> public static TraktResponse SyncEpisodeLibrary(TraktEpisodeSync syncData, TraktSyncModes mode) { // check that we have everything we need // server can accept title/year if imdb id is not supplied if (syncData == null || string.IsNullOrEmpty(syncData.SeriesID) && string.IsNullOrEmpty(syncData.Title) && string.IsNullOrEmpty(syncData.Year)) { TraktResponse error = new TraktResponse { Error = "Not enough information to send to server", Status = "failure" }; return error; } // serialize Scrobble object to JSON and send to server string response = Transmit(string.Format(TraktURIs.SyncEpisodeLibrary, mode), syncData.ToJSON()); // return success or failure return response.FromJSON<TraktResponse>(); }
/// <summary> /// Sends Scrobble data to Trakt /// </summary> /// <param name="scrobbleData">The Data to send</param> /// <param name="status">The mode to send it as</param> /// <returns>The response from Trakt</returns> public static TraktResponse ScrobbleEpisodeState(TraktEpisodeScrobble scrobbleData, TraktScrobbleStates status) { // check that we have everything we need // server can accept title if movie id is not supplied if (status != TraktScrobbleStates.cancelwatching) { if (scrobbleData == null) { TraktResponse error = new TraktResponse { Error = "Not enough information to send to server", Status = "failure" }; return error; } } // serialize Scrobble object to JSON and send to server string response = Transmit(string.Format(TraktURIs.ScrobbleShow, status), scrobbleData.ToJSON()); // return success or failure return response.FromJSON<TraktResponse>(); }
/// <summary> /// Add/Remove episode to/from watchlist /// </summary> /// <param name="syncData">The sync data to send</param> /// <param name="mode">The sync mode to use</param> /// <returns>The response from trakt</returns> public static TraktResponse SyncEpisodeWatchList(TraktEpisodeSync syncData, TraktSyncModes mode) { // check that we have everything we need if (syncData == null || syncData.EpisodeList.Count == 0) { TraktResponse error = new TraktResponse { Error = "Not enough information to send to server", Status = "failure" }; return error; } // serialize Scrobble object to JSON and send to server string response = Transmit(string.Format(TraktURIs.SyncEpisodeWatchList, mode), syncData.ToJSON()); // return success or failure return response.FromJSON<TraktResponse>(); }
/// <summary> /// Communicates to and from Trakt /// </summary> /// <param name="address">The URI to use</param> /// <param name="data">The Data to send</param> /// <returns>The response from Trakt</returns> private static string Transmit(string address, string data) { if (OnDataSend != null) OnDataSend(address, data); try { ServicePointManager.Expect100Continue = false; WebClient client = new CompressionWebClient(true) { Encoding = Encoding.UTF8 }; client.Headers.Add("user-agent", UserAgent); // wait for a response from the server string response = client.UploadString(address, data); // received data, pass it back if (OnDataReceived != null) OnDataReceived(response); return response; } catch (WebException e) { if (OnDataError != null) OnDataError(e.Message); if (e.Status == WebExceptionStatus.ProtocolError) { var response = ((HttpWebResponse)e.Response); try { using (var stream = response.GetResponseStream()) { using (var reader = new StreamReader(stream)) { return reader.ReadToEnd(); } } } catch { } } // create a proper response object TraktResponse error = new TraktResponse { Status = "failure", Error = e.Message }; return error.ToJSON(); } }