#Region ;**** Directives created by AutoIt3Wrapper_GUI **** #AutoIt3Wrapper_Res_Comment=http://omertron.com/pch/YAMJ_NFO_Convertor #AutoIt3Wrapper_Res_Description=Convert old style YAMJ v1.0.12 NFO files to the new XBMC NFO files #AutoIt3Wrapper_Res_Fileversion=0.3 #AutoIt3Wrapper_Res_LegalCopyright=(c) Omertron #AutoIt3Wrapper_AU3Check_Stop_OnWarning=y #EndRegion ;**** Directives created by AutoIt3Wrapper_GUI **** #cs ---------------------------------------------------------------------------- AutoIt Version: 3.2.12.1 Author: Stuart Boston Version: 0.3 Script Function: Convert old style NFO files into new style files Licence: Creative Commons - Attribution Share Alike Link: http://creativecommons.org/licenses/by-sa/3.0/ #ce ---------------------------------------------------------------------------- #Include #include #include #include #include ; Declare some variables Global $YAMJ_LogFile = "YAMJ_Convert_NFO.log" Global $YAMJ_Title = "Omertron's NFO Convertor v0.3" Global $YAMJ_Title2 = "Download from http://omertron.com/pch/YAMJ_NFO_Convertor" Global $mCountFiles = 0 ; Window dimensions Global $mWidth = 400, $mHeight = 160, $mLeft = 100, $mTop = 100 Global $MSG, $YAMJ_DIR, $BTNGO, $BTNEXIT, $BTNDIR, $NMT_PATH, $BTN_Backup, $BTN_Orig Global $YAMJ_Backup = False, $YAMJ_Original = False _Main() Func _Main() ; Create the main GUI GUICreate($YAMJ_Title, $mWidth, $mHeight, $mLeft, $mTop) ; Create the PC location control including the directory search button GUICtrlCreateLabel("Location of your movie directory to scan:", 10, 5) $YAMJ_DIR = GUICtrlCreateInput(@WorkingDir, 10, 20, $mWidth - 125, 20, $ES_READONLY) $BTNDIR = GUICtrlCreateButton("S&elect Dir", $mWidth - 110, 17, 100) GUICtrlSetTip(-1, "Choose the directory on your PC") ; Create the Checkboxes $BTN_Backup = GUICtrlCreateCheckbox ("Keep backup files", 10, 45, $mWidth/2 - 10) GUICtrlSetState($BTN_Backup, $GUI_CHECKED) $BTN_Orig = GUICtrlCreateCheckbox ("Retain original info in output file", $mWidth/2 + 10, 45, $mWidth/2 - 10) GUICtrlSetState($BTN_Orig, $GUI_CHECKED) ; Create the NMT Path controls GUICtrlCreateLabel("Progress", 10, 70) $NMT_PATH = GUICtrlCreateInput("", 10, 90, $mWidth - 20, 20, $ES_READONLY) ; Create the GO button $BTNGO = GUICtrlCreateButton("&GO", 10, $mHeight - 45, 100) GUICtrlSetTip(-1, "Start the process") ; Create the EXIT button $BTNEXIT = GUICtrlCreateButton("&Exit", $mWidth - 110, $mHeight - 45, 100) GUICtrlSetTip(-1, "Quit the program") GUICtrlCreateLabel($YAMJ_Title2, 10, $mHeight-15, $mWidth - 20, 20, $SS_CENTER) ; Display the GUI GUISetState() ; Run the GUI until the dialog is closed While 1 $MSG = GUIGetMsg() Select Case $MSG = $GUI_EVENT_CLOSE Or $MSG = $BTNEXIT Exit Case $MSG = $BTNGO $mTargetDir = GUICtrlRead($YAMJ_DIR) If (GUICtrlRead($BTN_Backup) = $GUI_CHECKED) Then $YAMJ_Backup = True If (GUICtrlRead($BTN_Orig) = $GUI_CHECKED) Then $YAMJ_Original = True Start_Log($mTargetDir) Process_Directory($mTargetDir) Write_Log("") Write_Log("Finished processing " & $mCountFiles & " NFO files") FileClose($YAMJ_LogFile) FileChangeDir($mTargetDir) $mResult = MsgBox(68, "Processing Complete", "Do you want to see the log file?") If $mResult = 6 Then Run("notepad.exe YAMJ_Convert_NFO.log") EndIf Exit Case $MSG = $BTNDIR $new_dir = FileSelectFolder("Select the folder", "", 2, GUICtrlRead($YAMJ_DIR)) If $new_dir <> "" Then GUICtrlSetData($YAMJ_DIR, $new_dir) EndIf EndSelect WEnd EndFunc ;******************************************************************************** ;***** ;***** Functions below here ;***** ;******************************************************************************** Func Start_Log($slTargetDir) FileChangeDir($slTargetDir) $YAMJ_LogFile = $slTargetDir & "\" & $YAMJ_LogFile ; Delete the logfile before writing to it FileDelete($YAMJ_LogFile) Write_Log($YAMJ_Title) Write_Log($YAMJ_Title2) Write_Log("----------------------------------------") Write_Log("Keep backup: " & $YAMJ_Backup) Write_Log("Keep original: " & $YAMJ_Original) Write_Log("----------------------------------------") Write_Log("") EndFunc ;-------------------------------------------------- Func Process_Directory($pdDirectory) FileChangeDir($pdDirectory) Scan_Directory($pdDirectory) $pdArray = _FileListToArray($pdDirectory, "*.*", 2) If Not @error Then For $pdLoop = 1 To $pdArray[0] Process_Directory($pdDirectory & "\" & $pdArray[$pdLoop]) Next EndIf EndFunc ;-------------------------------------------------- Func Scan_Directory($sdDirectory) Write_Log("Scanning Directory: " & $sdDirectory) $sdSearch = FileFindFirstFile("*.NFO") ; Check if the search was successful If $sdSearch = -1 Then Write_Log("No NFO files found.") Else While 1 $sdFilename = FileFindNextFile($sdSearch) If @error Then ExitLoop $mCountFiles = $mCountFiles + 1 Write_Log("Filename: " & $sdFilename) GUICtrlSetData($NMT_PATH, $sdDirectory & "\" & $sdFilename) Scan_File($sdFilename) Write_Log("----------------------------------------") WEnd EndIf ; Close the search handle FileClose($sdSearch) Return EndFunc ;-------------------------------------------------- Func Scan_File($sfFilename) ; Passed a filename, we read in lines of text until the EOF is reached ; Each line is searched for a valid IMDB reference, once found we quit ; and return the value $sfFile = FileOpen($sfFilename, 0) ; Check file opened ok If $sfFile = -1 Then Write_Log("Error Reading file: " & $sfFilename) $sfIMDB = -1 Else $sfIMDB = "" $sfPicture = "" Do $sfLine = FileReadLine($sfFile) If @error = -1 Then ExitLoop ; EOF If StringLen($sfLine) > 0 Then ; Look for the IMDb string If $sfIMDB = "" Then $sfIMDB = searchIMDB($sfLine) EndIf If $sfPicture = "" Then $sfPicture = searchPicture($sfLine) EndIf EndIf Until ( (StringLen($sfIMDB) > 0) And (StringLen($sfPicture) > 0) ) EndIf FileClose($sfFile) If $sfIMDB <> "" Then Write_Log("IMDB Found: " & $sfIMDB) EndIf If $sfPicture <> "" Then Write_Log("Picture found: " & $sfPicture) EndIf If ($sfIMDB == "") And ($sfPicture == "") Then Write_Log("No IMDB reference or Picture found in this file") EndIf If StringLen($sfIMDB & $sfPicture) > 0 Then Write_Log("Writing New NFO for " & $sfFilename) ; We have either an IMDB reference or a picture so call the write of NFO write_NFO($sfFilename, $sfIMDB, $sfPicture) EndIf EndFunc ;-------------------------------------------------- Func write_NFO($wnFilename, $wnIMDB, $wnPicture) Local $wnText = "" ; If we are keeping a backup or the original text If $YAMJ_Backup Or $YAMJ_Original Then ; Backup the NFO file before we start $wnFile = FileCopy($wnFilename, $wnFilename & ".bak", 1) if $wnFile = 0 Then Write_Log("Error backing up " & $wnFilename & "... Skipping this file") Return EndIf EndIf ; Now open the NFO file for writing $wnFile = FileOpen($wnFilename, 2) If $wnFile = 0 Then Write_Log("Error opening " & $wnFilename & " for writing. Skipping this file") Return EndIf ; If we are to keep the original text, then copy it to the new file If $YAMJ_Original Then $wnFileOld = FileOpen($wnFilename & ".bak", 0) While 1 $wnText = FileReadLine($wnFileOld) If @error = -1 Then ExitLoop FileWriteLine($wnFile, "") WEnd FileClose($wnFileOld) ; If we do not need the backup file, delete it If ($YAMJ_Backup = False) Then FileDelete($wnFilename & ".bak") EndIf FileWriteLine($wnFile, "") FileWriteLine($wnFile, " ") FileWriteLine($wnFile, " // currently unused in YAMJ") FileWriteLine($wnFile, " // 0 - 10 rating, can be decimal") FileWriteLine($wnFile, " ") FileWriteLine($wnFile, " // currently unused in YAMJ") FileWriteLine($wnFile, " // currently unused in YAMJ") FileWriteLine($wnFile, " // currently unused in YAMJ") FileWriteLine($wnFile, " ") FileWriteLine($wnFile, " // currently unused in YAMJ") FileWriteLine($wnFile, " ") FileWriteLine($wnFile, " " & $wnPicture & " // url of poster image. use URL formatting, such as http:// for internet resources or file:// for local resources") FileWriteLine($wnFile, " ") FileWriteLine($wnFile, " // currently unused in YAMJ") FileWriteLine($wnFile, " // currently unused in YAMJ") FileWriteLine($wnFile, " " & $wnIMDB & " // the IMDB id of the movie. includes the leading tt") FileWriteLine($wnFile, " // the allocine id of the movie. This should work for other plugins using their PLUGIN_ID as moviedb value") FileWriteLine($wnFile, " // the filmweb id of the movie. This should work for other plugins using their PLUGIN_ID as moviedb value") FileWriteLine($wnFile, " // currently unused since YAMJ determines path from searching the configured libraries") FileWriteLine($wnFile, " // multiple trailer records may exist") FileWriteLine($wnFile, " // multiple genre records may exist, including any custom ones") FileWriteLine($wnFile, " // currently unused in YAMJ") FileWriteLine($wnFile, " ") FileWriteLine($wnFile, " // multiple actor records may exist") FileWriteLine($wnFile, " ") FileWriteLine($wnFile, " // currently unused in YAMJ") FileWriteLine($wnFile, " ") FileWriteLine($wnFile, "") FileClose($wnFile) EndFunc ;-------------------------------------------------- Func searchIMDB($siLine) Local $siIMDB = "" Local $siLocation = 0 $siLocation = StringInStr($siLine, "tt", 2) While $siLocation > 0 $siIMDB = StringMid($siLine, $siLocation + 2, 7) If StringIsDigit($siIMDB) Then ; This looks good, probably the correct IMDB reference $siIMDB = StringMid($siLine, $siLocation, 9) ; Trigger the end of the while loop $siLocation = -1 Else ; Not looking good, this is probably not corrent so move on $siIMDB = "" ; Search for the next occurence of the string $siLocation = StringInStr($siLine, "tt", 2, 1, $siLocation + 2) EndIf WEnd Return $siIMDB EndFunc ;-------------------------------------------------- Func searchPicture($spLine) Local $spPicture = "" Local $spLocation = 0 $spPicture = StringRight($spLine, 4) If ( ($spPicture == ".jpg") Or ($spPicture == ".png") Or ($spPicture == ".gif") Or ($spPicture == "jpeg") ) Then $spLocation = StringInStr($spLine, "http", False) If $spLocation <=0 Then $spLocation = StringInStr($spLine, "file://", False) EndIf If $spLocation > 0 Then $spPicture = StringMid($spLine, $spLocation, StringLen($spLine) - $spLocation + 1) Else $spPicture = "" EndIf Else $spPicture = "" EndIf Return $spPicture EndFunc ;-------------------------------------------------- Func Write_Log($wlText) ; If there is text passed rather than a blank line then add HH:MM:SS If StringLen($wlText) > 0 Then $wlText = @HOUR & ":" & @MIN & ":" & @SEC & " - " & $wlText EndIf FileWriteLine($YAMJ_LogFile, $wlText) EndFunc