/// <summary> /// This method ensures the directory structure required is created and ready for use /// </summary> public static void CacheStructureBuilder() { //root caching directory for the current user var rootUserDir = $@"{CachingFileDir.RootCacheDirectory}\" + $@"{MD5Helper.CalculateMd5Hash(ObjectProvider.Settings.ConnectionInfo.PlexAccountToken)}\" + $@"{MD5Helper.CalculateMd5Hash(ObjectProvider.Settings.ConnectionInfo.PlexAddress)}"; //root directory where all images are stored var thumbDir = $@"{rootUserDir}\{CachingFileDir.ThumbRelativeDirectory}"; //root directory where all XML files are stored var xmlDir = $@"{rootUserDir}\{CachingFileDir.XmlRelativeDirectory}"; try { //ensure the images directory has been created if (!Directory.Exists(thumbDir)) { Directory.CreateDirectory(thumbDir); } //ensure the XML directory has been created if (!Directory.Exists(xmlDir)) { Directory.CreateDirectory(xmlDir); } } catch (Exception ex) { //log the error and exit LoggingHelpers.RecordException(ex.Message, "CacheDirBuildError"); } }
/// <summary> /// Writes API JSON to a file in <AssemblyDirectory>\update_files\debug /// </summary> /// <param name="apiResponse">The raw JSON itself</param> /// <param name="prettyPrint">Whether or not the JSON is formatted when the file is written (false for better performance, true for readability)</param> private static void LogApiResponse(string apiResponse, bool prettyPrint = false) { try { //check if the data is valid before trying to write it to a file if (!string.IsNullOrEmpty(apiResponse)) { //the reason we hash the time is for uniqueness; it'll be different each time //since the system time keeps ticking along. var timeHash = MD5Helper.CalculateMd5Hash(DateTime.Now.ToString()); //GitHub API responses are always in JSON format; the extension must reflect this. var fileName = @$ "apiResponse_{timeHash}.json"; var fileDir = $@"{Globals.UpdateRootDir}\debug"; var filePath = $@"{fileDir}\{fileName}"; //ensure this directory exists if (!Directory.Exists(fileDir)) { Directory.CreateDirectory(fileDir); } //finally, write the API contents to the file File.WriteAllText(filePath, prettyPrint ? apiResponse.FormatJson() : apiResponse); } } catch { //ignore all errors } }
/// <summary> /// Saves an exception object to a file /// </summary> /// <param name="ex"></param> public static void ExportError(this Exception ex) { try { //Date and time that this is being written var logTime = DateTime.Now.ToString(CultureInfo.CurrentCulture); //file name is MD5-hashed current date and time (for uniqueness) var fileName = $"UpdateError_{MD5Helper.CalculateMd5Hash(logTime)}.log"; //store all errors in the UpdateDirectory var errorsPath = $@"{Agent.UpdateDirectory}\errors"; //ensure the 'errors' folder exists if (!Directory.Exists(errorsPath)) { Directory.CreateDirectory(errorsPath); } //full path var filePath = $@"{errorsPath}\{fileName}"; //file contents var fileWrite = $"Occurred: {logTime}\n---\n\n{ex}"; //export error to log File.WriteAllText(filePath, fileWrite); } catch { //ignore } }
/// <summary> /// Writes API JSON to a file in <AssemblyDirectory>\update_files\debug /// </summary> /// <param name="apiResponse">The raw JSON itself</param> /// <param name="prettyPrint">Whether or not the JSON is formatted when the file is written (false for better performance, true for readability)</param> private void LogApiResponse(string apiResponse, bool prettyPrint = false) { try { //check if the data is valid before trying to write it to a file, //and only allow this function to run if in debug mode if (!string.IsNullOrWhiteSpace(apiResponse) && DebugMode) { //hash a new GUID for uniqueness var uniqueString = MD5Helper.CalculateMd5Hash(Guid.NewGuid().ToString()); //GitHub API responses are always in JSON format; the extension must reflect this. var fileName = @$ "apiResponse_{uniqueString}.json"; var fileDir = $@"{Globals.UpdateRootDir}\debug"; var filePath = $@"{fileDir}\{fileName}"; //ensure this directory exists if (!Directory.Exists(fileDir)) { Directory.CreateDirectory(fileDir); } //finally, write the API contents to the file File.WriteAllText(filePath, prettyPrint ? apiResponse.FormatJson() : apiResponse); } } catch { //ignore all errors } }
//for specifying credentials then calculating everything else public CachedPlexLogin(string username, string password) { var dt = DateTime.Now.ToString(CultureInfo.InvariantCulture); var content = username + "\n" + password + "\n" + dt; var hash = MD5Helper.CalculateMd5Hash(content); //set the object values Username = username; Password = password; WriteDateTime = dt; Md5Checksum = hash; }
public static string ServerCachePath(string accountToken) { var accountHash = MD5Helper.CalculateMd5Hash(accountToken); var fileName = accountHash + CachingFileExt.ServerListExt; var cachePath = $"{CachingFileDir.RootCacheDirectory}\\{accountHash}"; if (!Directory.Exists(cachePath)) { Directory.CreateDirectory(cachePath); } var fqPath = $"{cachePath}\\{fileName}"; return(fqPath); }
public bool VerifyThis() { try { var content = Username + "\n" + Password + "\n" + WriteDateTime; var actualHash = MD5Helper.CalculateMd5Hash(content); var storedHash = Md5Checksum; return(string.Equals(storedHash, actualHash)); } catch (Exception ex) { LoggingHelpers.RecordException(ex.Message, "PlexLoginVerifyError"); return(false); } }
public static string XmlCachePath(string sourceUrl) { var accountHash = MD5Helper.CalculateMd5Hash(ObjectProvider.Settings.ConnectionInfo.PlexAccountToken); var serverHash = MD5Helper.CalculateMd5Hash(ObjectProvider.Settings.ConnectionInfo.PlexAddress); var fileName = MD5Helper.CalculateMd5Hash(sourceUrl) + CachingFileExt.ApiXmlExt; var cachePath = $"{CachingFileDir.RootCacheDirectory}\\{accountHash}\\{serverHash}\\{CachingFileDir.XmlRelativeDirectory}"; if (!Directory.Exists(cachePath)) { Directory.CreateDirectory(cachePath); } var fqPath = $"{cachePath}\\{fileName}"; return(fqPath); }
public List <StringVariable> BuildFromDlInfo(StreamInfo stream) { DefaultStringVariables.ContentTitle.VariableValue = stream.ContentTitle; DefaultStringVariables.FileName.VariableValue = stream.Links.View; DefaultStringVariables.TokenHash.VariableValue = MD5Helper.CalculateMd5Hash(ObjectProvider.Settings.ConnectionInfo.PlexAccountToken); DefaultStringVariables.ServerPort.VariableValue = ObjectProvider.Settings.ConnectionInfo.PlexPort; DefaultStringVariables.ServerIp.VariableValue = ObjectProvider.Settings.ConnectionInfo.PlexAddress; DefaultStringVariables.ServerHash.VariableValue = MD5Helper.CalculateMd5Hash(ObjectProvider.Settings.ConnectionInfo.PlexAddress); return(new List <StringVariable> { DefaultStringVariables.ContentTitle, DefaultStringVariables.FileName, DefaultStringVariables.TokenHash, DefaultStringVariables.ServerPort, DefaultStringVariables.ServerIp, DefaultStringVariables.ServerHash }); }
public PxzRecordChecksum(PxzRecordContent content) { if (content.RawRecord.Length == 0) { return; } if (content.AutoRecord.Length == 0) { return; } var dec = MD5Helper.CalculateMd5Hash(content.AutoRecord); if (dec == null) { return; } RawMd5 = MD5Helper.CalculateMd5Hash(content.RawRecord); DecMd5 = MD5Helper.Md5ToHex(dec); }
public static void MetadataToFile(string fileName, PlexObject contentToExport, Bitmap poster = null, bool silent = false) { try { //try and obtain a poster if one wasn't provided var p = poster ?? ImageHandler.GetPoster(contentToExport); //create each new PXZ record in memory var rawMetadata = new PxzRecord(contentToExport.RawMetadata, @"raw"); var objMetadata = new PxzRecord(contentToExport.ToXml(), @"obj"); var ptrMetadata = new PxzRecord(p, @"poster"); //the records to save to the PXZ file are contained in a list var data = new List <PxzRecord> { rawMetadata, objMetadata, ptrMetadata }; //export the actor images (if any) if (contentToExport.Actors != null) { if (contentToExport.Actors.Count > 0) { //loop through each actor and attempt an image download foreach (var a in contentToExport.Actors) { //download var image = ImageHandler.GetImageFromUrl(a.ThumbnailUri); //verify if (image != Resources.unavailable) { //create a new record for the image var record = new PxzRecord(image, $"actor_{MD5Helper.CalculateMd5Hash(a.ThumbnailUri)}"); //add it to the collection data.Add(record); } } } } //embedded in PXZ indexing information var plexdlVersion = Assembly.GetEntryAssembly()?.GetName().Version; //initialise the PXZ file and flush it to disk var pxz = new PxzFile(data, plexdlVersion, BuildState.State); pxz.Save(fileName); //show a message indicating success if allowed if (!silent) { UIMessages.Info(@"Successfully exported metadata!"); } } catch (Exception ex) { if (!silent) { UIMessages.Error("An error occurred\n\n" + ex, @"Metadata Export Error"); } LoggingHelpers.RecordException(ex.Message, "XmlMetadataSaveError"); } }
public static PlexObject MetadataFromFile(PxzFile file, bool waitWindow = true, bool silent = false) { if (waitWindow) { return((PlexObject)WaitWindow.WaitWindow.Show(MetadataFromFile, @"Reading data", file, silent)); } if (file != null) { try { //load the object record (encoded XML that contains metadata/attributes) var rec = file.LoadRecord(@"obj"); //it'll be null if the load above failed for whatever reason if (rec != null) { var obj = FromXml(rec.Content.ToXmlDocument()); //apply raw XML from PXZ file obj.RawMetadata = file.LoadRecord(@"raw").Content.ToXmlDocument(); //apply poster from PXZ file obj.StreamInformation.ContentThumbnail = file.LoadRecord(@"poster").Content.ToImage(); //check if the PXZ has any actor thumbnails stored var bitmaps = file.SelectType(PxzRecordType.Bitmap); //find any actor images if (bitmaps != null) { //loop through each bitmap foreach (var b in bitmaps) { //check if the record is an actor thumbnail if (b.Header.Naming.RecordName.StartsWith(@"actor_")) { //find out which actor this image belongs to foreach (var a in obj.Actors) { //record naming is just the URL hashed var recordName = $"actor_{MD5Helper.CalculateMd5Hash(a.ThumbnailUri)}"; //verify if (recordName == b.Header.Naming.RecordName) { //apply image a.Thumbnail = b.Content.ToImage(); break; } } } } } //return the new PlexObject return(obj); } } catch (Exception ex) { if (!silent) { UIMessages.Error("An error occurred\n\n" + ex, @"Metadata Load Error"); } LoggingHelpers.RecordException(ex.Message, "XmlMetadataLoadError"); } } //default return(null); }