private static void GetEpFromTVDBByRecTime(ref RecTV show) { try { var tvdb = new TVDB(apikey); DateTime epRecTime = show.recDateTime; var shows = tvdb.Search(show.title); File.AppendAllText(logPath, "\tFound: \t" + shows.Count + " matching titles. Checking episodes..." + Environment.NewLine); foreach (var s in shows) { var episodes = s.Episodes.Where(ep => ep.FirstAired == epRecTime.Date).ToList(); if (episodes.Count > 0) { show.epName = episodes[0].Title; show.epID = 'S' + episodes[0].SeasonNumber.ToString() + 'E' + episodes[0].EpisodeNumber.ToString(); FormatEpID(ref show); File.AppendAllText(logPath, "\tFound a matching episode name. Taking the first match: " + show.epID + Environment.NewLine); break; } } } catch (Exception ex) { File.AppendAllText(logPath, DateTime.Now.ToString() + ": " + ex.Message + Environment.NewLine + ex.StackTrace + Environment.NewLine); Console.WriteLine("Errors occurred. Check the log file at " + logPath); } }
private static void GetEpIDFromTVDB(ref RecTV show) { try { char[] ignoreChars = new char[] { ' ', ';', ',', '\r', '\t', '\n' }; var tvdb = new TVDB(apikey); string epName = show.epName.ToLower(); var shows = tvdb.Search(show.title); File.AppendAllText(logPath, "\tFound: \t" + shows.Count + " matching titles. Checking episodes..." + Environment.NewLine); foreach (var s in shows) { var episodes = s.Episodes.Where(ep => Regex.Replace(ep.Title.ToLower(), "[^0-9a-zA-Z]+", "") == Regex.Replace(epName, "[^0-9a-zA-Z]+", "")).ToList(); if (episodes.Count > 0) { show.epID = 'S' + episodes[0].SeasonNumber.ToString() + 'E' + episodes[0].EpisodeNumber.ToString(); FormatEpID(ref show); File.AppendAllText(logPath, "\tFound a matching episode name. Taking the first match: " + show.epID + Environment.NewLine); break; } } } catch (Exception ex) { File.AppendAllText(logPath, DateTime.Now.ToString() + ": " + ex.Message + Environment.NewLine + ex.StackTrace + Environment.NewLine); Console.WriteLine("Errors occurred. Check the log file at " + logPath); } }
private static void sortTV(List <string> tvShows, string destination) { try { foreach (string showPath in tvShows) { File.AppendAllText(logPath, DateTime.Now.ToString() + ": Attempting to sort " + showPath.Trim() + Environment.NewLine); RecTV show = GetRecTVDetails(showPath.Trim()); File.AppendAllText(logPath, "\tTitle:\t\t" + show.title + Environment.NewLine); File.AppendAllText(logPath, "\tEpisodeID:\t" + show.epID + Environment.NewLine); File.AppendAllText(logPath, "\tEpisode:\t" + show.epName + Environment.NewLine); File.AppendAllText(logPath, "\tRecord Time:\t" + show.recDateTime.ToString() + Environment.NewLine); if (show.sortable) { RenameAndMove(ref show, ref destination); } } } catch (Exception ex) { File.AppendAllText(logPath, DateTime.Now.ToString() + ": " + ex.Message + Environment.NewLine + ex.StackTrace + Environment.NewLine); Console.WriteLine("Errors occurred. Check the log file at " + logPath); } }
private static void FormatEpID(ref RecTV show) { try { string[] epIDParts = show.epID.Split(new char[] { 'S', 'E' }); string seasonNum = epIDParts[1].PadLeft(2, '0'); string epNum = epIDParts[2].PadLeft(2, '0'); show.epID = 'S' + seasonNum + 'E' + epNum; } catch (Exception ex) { File.AppendAllText(logPath, DateTime.Now.ToString() + ": " + ex.Message + Environment.NewLine + ex.StackTrace + Environment.NewLine); Console.WriteLine("Errors occurred. Check the log file at " + logPath); } }
private static void GetEpFromTVDBByDesc(ref RecTV show) { try { var tvdb = new TVDB(apikey); string epDesc = show.epDesc.ToLower(); var shows = tvdb.Search(show.title); File.AppendAllText(logPath, "\tFound: \t" + shows.Count + " matching titles. Checking episodes..." + Environment.NewLine); switch (show.title.ToLower()) { case "saturday night live": Regex rgxStart = new Regex("^Host "); epDesc = rgxStart.Replace(epDesc, ""); Regex rgxEnd = new Regex(" performs.$"); epDesc = rgxEnd.Replace(epDesc, ""); Regex rgxEnd2 = new Regex(" hosts and performs.$"); epDesc = rgxEnd2.Replace(epDesc, ""); epDesc = epDesc.Replace("; ", "/"); break; } foreach (var s in shows) { var episodes = s.Episodes.Where(ep => ep.Description.ToLower() == epDesc).ToList(); if (episodes.Count > 0) { show.epName = episodes[0].Title; show.epID = 'S' + episodes[0].SeasonNumber.ToString() + 'E' + episodes[0].EpisodeNumber.ToString(); FormatEpID(ref show); File.AppendAllText(logPath, "\tFound a matching episode name. Taking the first match: " + show.epID + Environment.NewLine); break; } } } catch (Exception ex) { File.AppendAllText(logPath, DateTime.Now.ToString() + ": " + ex.Message + Environment.NewLine + ex.StackTrace + Environment.NewLine); Console.WriteLine("Errors occurred. Check the log file at " + logPath); } }
private static void GetEpisodeInfoFromMetaData(ref RecTV show) { try { Shell32.Shell shell = new Shell32.Shell(); Shell32.Folder objFolder; objFolder = shell.NameSpace(show.parentDir); //get the index of the path item int index = -1; FileInfo fh = new FileInfo(show.filePath); DirectoryInfo dh = fh.Directory; FileSystemInfo[] dirContent = dh.GetFileSystemInfos(); int desktopINIOffset = 0; if (dirContent.Where(f => f.Name == "desktop.ini").ToList().Count() > 0) { desktopINIOffset = 1; } int tempRecOffset = 0; if (dirContent.Where(f => f.Name == "TempRec").ToList().Count() > 0) { tempRecOffset = 1; } int tempSBEOffset = 0; if (dirContent.Where(f => f.Name == "TempSBE").ToList().Count() > 0) { tempSBEOffset = 1; } int thumbsOffset = 0; if (dirContent.Where(f => f.Name == "Thumbs.db").ToList().Count() > 0) { thumbsOffset = 1; } for (int i = 0; i < dh.GetFileSystemInfos().Count(); i++) { if (dh.GetFileSystemInfos().ElementAt(i).Name == fh.Name) //we've found the item in the folder { if (fh.Name.CompareTo("Thumbs.db") > 0) { index = i - desktopINIOffset - tempRecOffset - tempSBEOffset - thumbsOffset; } else { if (fh.Name.CompareTo("TempSBE") > 0) { index = i - desktopINIOffset - tempRecOffset - tempSBEOffset; } else { if (fh.Name.CompareTo("TempRec") > 0) { index = i - desktopINIOffset - tempRecOffset; } else { if (fh.Name.CompareTo("desktop.ini") > 0) { index = i - desktopINIOffset; } else { index = i; } } } } break; } } if (index > -1) { FolderItem fi = objFolder.Items().Item(index); string name = fi.Name; if (name == show.fileName + show.fileExt) { //Episode name is 254 string episodeName = objFolder.GetDetailsOf(fi, 254); show.epName = episodeName; if (episodeName != "") { File.AppendAllText(logPath, "\tFound episode name: " + episodeName + " for file " + name + Environment.NewLine); } //Episode description is 259 string episodeDesc = objFolder.GetDetailsOf(fi, 259); show.epDesc = episodeDesc; } else { File.AppendAllText(logPath, "\tError: " + show.fileName + show.fileExt + " does not match " + name + Environment.NewLine); } } } catch (Exception ex) { File.AppendAllText(logPath, DateTime.Now.ToString() + ": " + ex.Message + Environment.NewLine + ex.StackTrace + Environment.NewLine); Console.WriteLine("Errors occurred. Check the log file at " + logPath); } }
private static void RenameAndMove(ref RecTV show, ref string destination) { try { List <string> noSeasonFolders = ConfigurationManager.AppSettings["NoSeasonFolders"].Split(',').ToList(); string destPath = FindShowTitleFolder(ref destination, show.title); string newFileName = show.title + " - "; string title = show.title; if (!noSeasonFolders.Where(s => s.ToLower().Equals(title.ToLower())).Any()) { if (show.epID != "" && show.epID != null) { newFileName += show.epID + " - "; int season = Convert.ToInt32(show.epID.Substring(1, 2)); if (!Directory.Exists(destPath + @"\" + "Season " + season)) { Directory.CreateDirectory(destPath + @"\" + "Season " + season); } destPath += @"\" + "Season " + season; } } if (show.epName != "" && show.epName != null) { newFileName += show.epName; //Add date for unique name if episode name already exist if (File.Exists(destPath + @"\" + newFileName + show.fileExt)) { newFileName += " - " + show.recDateTime.ToString("yyyy_MM_dd_hh_mm_ss"); } } else { if (show.epID != "" && show.epID != null) { newFileName = newFileName.Trim().TrimEnd('-').Trim(); } else { newFileName += show.recDateTime.ToString("yyyy_MM_dd_hh_mm_ss"); } } newFileName += show.fileExt; //clean bad characters newFileName = String.Join(" ", newFileName.Split(Path.GetInvalidFileNameChars())); if (!File.Exists(destPath + @"\" + newFileName)) { File.AppendAllText(logPath, "\tMoving to:\t" + destPath + @"\" + newFileName + Environment.NewLine); //move file try { File.Move(show.filePath, destPath + @"\" + newFileName); } catch (IOException e) { File.AppendAllText(logPath, "\tError: Cannot move. File is still being used." + Environment.NewLine); } } else { File.AppendAllText(logPath, "\tError: cannot move. The file " + destPath + @"\" + newFileName + " already exists." + Environment.NewLine); } } catch (Exception ex) { File.AppendAllText(logPath, DateTime.Now.ToString() + ": " + ex.Message + Environment.NewLine + ex.StackTrace + Environment.NewLine); Console.WriteLine("Errors occurred. Check the log file at " + logPath); } }
private static RecTV GetRecTVDetails(string showPath) { RecTV show = new RecTV(showPath); show.sortable = true; try { FileInfo showFH = new FileInfo(showPath.Trim()); show.parentDir = showFH.DirectoryName; show.fileExt = showFH.Extension; show.fileName = showFH.Name.Replace(show.fileExt, "").TrimEnd('.'); string episodeIDPattern = @"^S\d{1,2}E\d{1,2}$"; string dateTimePattern = @"^\d{4}_\d{2}_\d{2}_\d{2}_\d{2}_\d{2}$"; string[] parts = show.fileName.Split(new string[] { " - " }, StringSplitOptions.None); switch (parts.Count()) { case 2: show.title = parts[0]; Match m1 = Regex.Match(parts[1].ToUpper(), episodeIDPattern); if (m1.Success) { show.epID = parts[1].ToUpper(); } else { Match m2 = Regex.Match(parts[1], dateTimePattern); if (m2.Success) { show.recDateTime = DateTime.ParseExact(parts[1], "yyyy_MM_dd_HH_mm_ss", System.Globalization.CultureInfo.InvariantCulture); } else { show.epName = parts[1]; } } break; case 3: show.title = parts[0]; Match m3 = Regex.Match(parts[1].ToUpper(), episodeIDPattern); if (m3.Success) { show.epID = parts[1].ToUpper(); } else { show.epName = parts[1]; } Match m4 = Regex.Match(parts[2], dateTimePattern); if (m4.Success) { show.recDateTime = DateTime.ParseExact(parts[2], "yyyy_MM_dd_HH_mm_ss", System.Globalization.CultureInfo.InvariantCulture); } else { show.epName = parts[2]; } break; case 4: case 5: case 6: case 7: case 8: case 9: case 10: show.sortable = false; File.AppendAllText(logPath, "\tCan't sort. Must be an extra hyphen in the Title or Episode name." + Environment.NewLine); break; } if (show.recDateTime.Year == 0001) { show.recDateTime = showFH.CreationTime; } if (show.epID != "" && show.epID != null) { FormatEpID(ref show); } else { if (show.title != "" && show.title != null) { //Read extended properties if (show.epName == null || show.epName == "") { GetEpisodeInfoFromMetaData(ref show); } if (show.epName != "" && show.epName != null) { GetEpIDFromTVDB(ref show); } else { if (show.epDesc != "" && show.epDesc != null) { GetEpFromTVDBByDesc(ref show); if (show.epID == "" || show.epID == null) { GetEpFromTVDBByRecTime(ref show); } } else { GetEpFromTVDBByRecTime(ref show); } } } } } catch (Exception ex) { show.sortable = false; File.AppendAllText(logPath, DateTime.Now.ToString() + ": " + ex.Message + Environment.NewLine + ex.StackTrace + Environment.NewLine); Console.WriteLine("Errors occurred. Check the log file at " + logPath); } return(show); }