Skip to content

Instantly share code, notes, and snippets.

@GuilhermeHideki
Last active September 9, 2015 23:40
Show Gist options
  • Save GuilhermeHideki/87fb0252d8399b9f9b0c to your computer and use it in GitHub Desktop.
Save GuilhermeHideki/87fb0252d8399b9f9b0c to your computer and use it in GitHub Desktop.
uTorrent Regex Labeler
#Persistent
#SingleInstance,Force
#NoTrayIcon
DEBUG_ENABLED := false
TRACKER_SUPPORT := false
WINDOW_ADD_TORRENT := "Add New Torrent ahk_class #32770"
WINDOW_TORRENT_PROPERTIES := "Torrent Properties ahk_class #32770"
SetTitleMatchMode,2
var_PreviousWindows := ""
Gosub, sub_ConfigChecker
SetTimer,sub_CheckNewTorrent,250
SetTimer,sub_ClearPrevWindows,40000
Return
Main(winid,ByRef PrevWindowsString){
global ; Force global variables mode
; WinGetTitle, OutputVar [, WinTitle, WinText, ExcludeTitle, ExcludeText]
WinGetTitle, TorrentTitle, Add New Torrent ahk_id %winid%
if (TRACKER_SUPPORT){
; Click on [Advanced] "Button13"
ControlClick,Button13,Add New Torrent ahk_id %winid%
WinWait,WINDOW_TORRENT_PROPERTIES,,2.0
PropertiesWinExist := WinExist(WINDOW_TORRENT_PROPERTIES)
if (PropertiesWinExist){
ControlGetText,var_TrackerList,Edit1,Torrent Properties ahk_id %PropertiesWinExist%
WinKill,Torrent Properties ahk_id %PropertiesWinExist%
}
}
; Normalize the name of torrent
; e.g. replace {dot} with {space}
StringReplace,TorrentTitle,TorrentTitle,.,%A_Space%,1
goSub,sub_ConfigChecker
IniRead,ini_Regexes,Labeler_Settings.ini,REGEX
Loop,Parse,ini_Regexes,`n`r,
{
StringSplit, OutputArray, A_LoopField, =
key := OutputArray1
val := OutputArray2
FoundPos := RegExMatch(TorrentTitle, val,string)
if (DEBUG_ENABLED){
MsgBox {%key% : %val%}
MsgBox %TorrentTitle%
MsgBox %string%
}
if FoundPos != 0
fun_LabelChooser(winid,PrevWindowsString,key)
}
}
fun_LabelChooser(p_winid,byRef p_PrevWindowsString,p_key){
global DEBUG_ENABLED
p_PrevWindowsString .= " " p_winid " "
if (DEBUG_ENABLED){
msgbox Got into fun_LabelChooser and %p_key% key
MsgBox %p_PrevWindowsString%
}
Control,ChooseString,%p_key%,ComboBox2,Add New Torrent ahk_id %p_winid%
Return
}
sub_CheckNewTorrent:
p_WinID := WinExist(WINDOW_ADD_TORRENT)
if (p_WinID){
; Checks if this windows label was set previously within 40 seconds.
; Prevents the program from frequently updating the label of the same window
if var_PreviousWindows contains %p_WinID%
Return
SetTimer,sub_CheckNewTorrent,OFF
Main(p_WinID,var_PreviousWindows)
SetTimer,sub_CheckNewTorrent,ON
}
Return
/**
* Clears the string with previous windows that had their Labels set.
* So Let's say a window with the id 0x0efab2 had it's label set to 'Movie'.
* Then this window's id is added to the string so it's label isn't constantly
* updated. Every 40s this string is cleared.
*/
sub_ClearPrevWindows:
var_PreviousWindows := ""
SetTimer,sub_ClearPrevWindows
Return
/**
* Check if file exists
* Creates one if doesn't find
*/
sub_ConfigChecker:
if !(FileExist("Labeler_Settings.ini"))
GoSub,sub_IniWriteDefaultConfig
Return
sub_IniWriteDefaultConfig:
IniWrite,i)S\d+E\d+|season,Labeler_Settings.ini,REGEX,TV-Shows
Return
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment