/// <summary> /// Creates a list based on a JSON Array /// </summary> /// <typeparam name="T"></typeparam> /// <param name="jsonArray"></param> /// <returns></returns> public static IEnumerable <T> FromJSONArray <T>(this string jsonArray) { if (string.IsNullOrEmpty(jsonArray)) { return(new List <T>()); } try { using (var ms = new MemoryStream(Encoding.UTF8.GetBytes(jsonArray))) { var ser = new DataContractJsonSerializer(typeof(IEnumerable <T>)); var result = (IEnumerable <T>)ser.ReadObject(ms); if (result == null) { return(new List <T>()); } else { return(result); } } } catch (Exception e) { TraktLogger.Error("Error deserializing json: {0}", e.Message); return(new List <T>()); } }
/// <summary> /// Logs the result of Trakt api call /// </summary> /// <typeparam name="T">Response Type of message</typeparam> /// <param name="response">The response object holding the message to log</param> internal static bool LogTraktResponse <T>(T response) { try { if (response == null || (response as TraktResponse).Status == null) { // server is probably temporarily unavailable // return true even though it failed, so we can try again // currently the return value is only being used in livetv/recordings TraktLogger.Error("Response from server was unexpected."); return(true); } // check response error status if ((response as TraktResponse).Status != "success") { if ((response as TraktResponse).Error == "The remote server returned an error: (401) Unauthorized.") { TraktLogger.Error("401 Unauthorized, Please check your Username and Password"); } else { TraktLogger.Error((response as TraktResponse).Error); } return(false); } else { // success if (!string.IsNullOrEmpty((response as TraktResponse).Message)) { TraktLogger.Info("Response: {0}", (response as TraktResponse).Message); } else { // no message returned on movie sync success if ((response is TraktSyncResponse)) { string message = "Response: Items Inserted: {0}, Items Already Exist: {1}, Items Skipped: {2}"; TraktLogger.Info(message, (response as TraktSyncResponse).Inserted, (response as TraktSyncResponse).AlreadyExist, (response as TraktSyncResponse).Skipped); } } return(true); } } catch (Exception) { TraktLogger.Info("Response: {0}", "Failed to interpret response from server"); return(false); } }
/// <summary> /// Creates an object from JSON /// </summary> /// <typeparam name="T"></typeparam> /// <param name="json"></param> /// <returns></returns> public static T FromJSON <T>(this string json) { if (string.IsNullOrEmpty(json)) { return(default(T)); } try { using (var ms = new MemoryStream(Encoding.UTF8.GetBytes(json.ToCharArray()))) { var ser = new DataContractJsonSerializer(typeof(T)); return((T)ser.ReadObject(ms)); } } catch (Exception e) { TraktLogger.Error("Error deserializing json: {0}", e.Message); return(default(T)); } }
private static void TraktAPI_OnDataError(string error) { TraktLogger.Error("WebException: {0}", error); }
private static void TmdbAPI_OnDataError(string error) { TraktLogger.Error(error); }
private void StartSync() { SetSyncControlProperties(true); var syncThread = new Thread(() => { if (TraktSettings.AccountStatus != ConnectionState.Connected) { // stop sync SetSyncControlProperties(false); TraktSettings.AccountStatus = ConnectionState.Pending; return; } TraktLogger.Info("Library and Playback Sync started for all enabled plugins"); // get data from online and store in cache so its readily available for plugin sync // data will also be used in user activity feed on the dashboard if (!TraktCache.RefreshData()) { return; } foreach (var item in clbPlugins.CheckedItems) { try { switch (item.ToString()) { case "Moving Pictures": var movingPictures = new MovingPictures(TraktSettings.MovingPictures); movingPictures.SyncLibrary(); movingPictures.SyncProgress(); break; case "MP-TVSeries": var tvSeries = new TVSeries(TraktSettings.TVSeries); tvSeries.SyncLibrary(); tvSeries.SyncProgress(); break; case "My Videos": var myVideos = new MyVideos(TraktSettings.MyVideos); myVideos.SyncLibrary(); myVideos.SyncProgress(); break; case "My Films": var myFilms = new MyFilmsHandler(TraktSettings.MyFilms); myFilms.SyncLibrary(); myFilms.SyncProgress(); break; } } catch (Exception ex) { TraktLogger.Error("Error synchronising library, Plugin = '{0}', Error = '{1}'", item.ToString(), ex.Message); continue; } } // save user activity cache TraktCache.Save(); TraktLogger.Info("Library and Playback Sync completed for all enabled plugins"); SetSyncControlProperties(false); if (SilentMode || AutoCloseAfterSync) { CloseConfig(); } }) { Name = "Sync", IsBackground = true }; syncThread.Start(); }