Advertisement
Guest User

xbmc1.vbs

a guest
Sep 7th, 2013
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.88 KB | None | 0 0
  1. 'xbmc.vbs
  2. 'Script for updating xbmc library upon torrent completion, and showing notification on download.
  3. 'Largly stolen and adapted from http://forum.xbmc.org/showthread.php?t=87472
  4. '=================================================================================
  5. 'Notes Section: READ THIS!!!
  6. '
  7. 'If you have multiple instances of XBMC throughout your house, you can setup each of them below.
  8. 'This script is configured for two instances with shared libraries via MySQL, however it can be made to work with as many as you need.
  9. 'If you have multiple instances with individual databases (not shared via MySQL) you will need to uncomment relevant lines in the Implementation section.
  10. ' 111-113 for 2 instances, 111-116 for 3, 111-119 for 4, and 111-122 for 5.
  11. 'If you only have one xbmc instance, comment out lines 28, 91-93, and 135-137
  12. 'If you want more than five xbmc instances, add the variables in the Declaration section "Dim xbmcConnection6" "Dim xbmcConnection7" etc. and
  13. ' add the configs below in the Settings Section. You will also need to add lines to send notifications to each instance in the
  14. ' Implementation section. Ask for help on the Forum, if need be.
  15. 'You need to turn on the HTTP server in XBMC under Settings>Network and configure the username and password for this to work.
  16. ' In this script, the example username and password are both "xbmc"
  17. 'Also, please note that if your media is shared as a local share on your PC, you will need to comment out lines 84 and 85.
  18. '
  19. 'In uTorrent, Options > Preferences > Advanced > Run Program : Add the following
  20. 'When a Torrent Finishes: "C:\path\to\xbmc.vbs" -U "%N" "%D" "%L"
  21. 'When a Torrent Changes State: "C:\path\to\xbmc.vbs" -S "%M" "%N" "%L"
  22. '
  23. '=================================================================================
  24. 'Declaration section
  25. Dim objSvrHTTP
  26. Dim connectionString 'string to send to xbmc
  27. Dim xbmcConnection 'xbmc connection details, defined in Settings Section
  28. Dim xbmcConnection2
  29. 'Dim xbmcConnection3
  30. 'Dim xbmcConnection4
  31. 'Dim xbmcConnection5
  32. Dim message ' contains name of torrent when sending status message to xbmc
  33. Dim header ' contains status of torrent when sending status message to xbmc
  34. Dim myPath ' path of download in utorrent as provided via %D.
  35. Dim ignoreTorrent ' variable used to ignore torrents not containing BTN labels
  36. Dim label ' uTorrent label, %L
  37. Dim ulabel0 ' Labels in uTorrent you want reported to xbmc
  38. Dim ulabel1
  39. Dim ulabel2
  40. 'Dim ulabel3 ' uncomment if adding more labels
  41. 'Dim ulabel4
  42. 'Dim ulabel5
  43.  
  44. Set objSvrHTTP = CreateObject("MSXML2.XMLHTTP")
  45.  
  46. On Error Resume Next
  47. '=================================================================================
  48. 'Settings Section
  49. 'edit these lines to work with your setup
  50. xbmcConnection = "http://xbmc:xbmc@192.168.0.11:80/jsonrpc?request=" ' This connection is the one that will perform library updates.
  51. xbmcConnection2 = "http://xbmc:xbmc@192.168.0.10:80/jsonrpc?request="
  52. 'xbmcConnection3 = "http://xbmc:xbmc@192.168.0.2:8080/jsonrpc?request="
  53. 'xbmcConnection4 = "http://xbmc:xbmc@localhost:8080/jsonrpc?request="
  54. 'xbmcConnection5 = "http://xbmc:xbmc@192.168.0.5:8080/jsonrpc?request="
  55.  
  56. 'examples
  57. 'xbmcConnection = "http://xbmc:xbmc@localhost:8080/jsonrpc?request="
  58. 'xbmcConnection = "http://xbmc:myownpass@192.168.1.101:80/jsonrpc?request="
  59.  
  60. 'change values to the uTorrent labels you want this script to be used with.
  61. ulabel0 = "TV-Shows"
  62. 'ulabel1 = "label" ' uncomment if adding more labels
  63. 'ulabel2 = "label"
  64. 'ulabel3 = "label"
  65. 'ulabel4 = "label"
  66. 'ulabel5 = "label"
  67. '==================================================================================
  68. 'Implementation Section
  69. label = WScript.Arguments.Item(3)
  70.  
  71. Select Case label
  72. Case ulabel0, ulabel1, ulabel2, ulabel3, ulabel4, ulabel5 'if utorrent label matches labels defined in settings, execute script. Otherwise do nothing.
  73. ignoreTorrent = "false"
  74. Case Else
  75. ignoreTorrent = "true"
  76. End Select
  77.  
  78. 'check ignoreTorrent value and abort script if torrent is not a library item.
  79. if ignoreTorrent = "false" then
  80. 'if the first argument is -U (for update) then it performs the update procedure and sends a notification to xbmc.
  81. if WScript.Arguments.Item(0) = "-U" then
  82. 'Send a command to xbmc to display a notification when a torrent finishes
  83. myPath = WScript.Arguments.Item(2)
  84. if Left(myPath,2) = "\\" then
  85. myPath = Replace(myPath,"\\","smb://") 'This line makes network paths friendly for xbmc
  86. myPath = Replace(myPath,"\","/") 'This line also makes network paths friendly for xbmc
  87. ElseIf Mid(myPath,2,2) = ":\" then
  88. myPath = Replace(myPath,"\","\\")
  89. End If
  90. JSONstring = URLEncode(Replace("{''jsonrpc'':''2.0'',''method'':''GUI.ShowNotification'',''params'':{''title'':''uTorrent'',''message'':''Finished downloading " & WScript.Arguments.Item(1) & "''},''id'':1}","''",Chr(34)))
  91. connectionString = xbmcConnection & JSONstring
  92. objSvrHTTP.open "GET", connectionString, False
  93. objSvrHTTP.send
  94. connectionString = xbmcConnection2 & JSONstring
  95. objSvrHTTP.open "GET", connectionString, False
  96. objSvrHTTP.send
  97. 'uncomment the below sections (3 lines each) for each additional xbmc implementation.
  98. 'JSONstring = URLEncode(Replace("{''jsonrpc'':''2.0'',''method'':''GUI.ShowNotification'',''params'':{''title'':''uTorrent'',''message'':''Finished downloading " & WScript.Arguments.Item(1) & "''},''id'':1}","''",Chr(34)))
  99. 'connectionString = xbmcConnection3 & JSONstring
  100. 'objSvrHTTP.open "GET", connectionString, False
  101. 'objSvrHTTP.send
  102. 'connectionString = xbmcConnection4 & JSONstring
  103. 'objSvrHTTP.open "GET", connectionString, False
  104. 'objSvrHTTP.send
  105. 'connectionString = xbmcConnection5 & JSONstring
  106. 'objSvrHTTP.open "GET", connectionString, False
  107. 'objSvrHTTP.send
  108.  
  109. 'Send a command to xbmc that tells it to update the supplied directory
  110. JSONstring = URLEncode(Replace("{''jsonrpc'':''2.0'',''method'':''VideoLibrary.Scan'',''params'':{''directory'':''"& myPath &"''},''id'':1}","''",Chr(34)))
  111. connectionString = xbmcConnection & JSONstring
  112. WScript.Sleep 1000 * 10
  113. objSvrHTTP.open "GET", connectionString, False
  114. objSvrHTTP.send
  115. 'if you are using multiple XBMC WITHOUT MySQL, WHY?!?! Uncomment below (3 line sections) to enable each instance to update their individual libraries.
  116. 'connectionString = xbmcConnection2 & JSONstring
  117. 'objSvrHTTP.open "GET", connectionString, False
  118. 'objSvrHTTP.send
  119. 'connectionString = xbmcConnection3 & JSONstring
  120. 'objSvrHTTP.open "GET", connectionString4, False
  121. 'objSvrHTTP.send
  122. 'connectionString = xbmcConnection5 & JSONstring
  123. 'objSvrHTTP.open "GET", connectionString, False
  124. 'objSvrHTTP.send
  125.  
  126.  
  127. 'if the first argument is -S (for status) then it performs the status procedure and in this case only when the status is "Downloading".
  128. 'That way I get a notification in xbmc when a new torrent is added
  129. elseif WScript.Arguments.Item(0) = "-S" then
  130. 'check if the status is Downloading
  131. if WScript.Arguments.Item(1) = "Downloading" then
  132. header = WScript.Arguments.Item(1)
  133. message = WScript.Arguments.Item(2)
  134. JSONstring = URLEncode(Replace("{''jsonrpc'':''2.0'',''method'':''GUI.ShowNotification'',''params'':{''title'':''uTorrent'',''message'':''Downloading: " & message & "''},''id'':1}","''",Chr(34)))
  135. connectionString = xbmcConnection & JSONstring
  136. objSvrHTTP.open "GET", connectionString, False
  137. objSvrHTTP.send
  138. connectionString = xbmcConnection2 & JSONstring
  139. objSvrHTTP.open "GET", connectionString, False
  140. objSvrHTTP.send
  141. 'if you are using multiple XBMC installations, uncomment below (3 line sections) to enable each installation to recieve a notification
  142. 'connectionString3 = xbmcConnection & JSONstring
  143. 'objSvrHTTP.open "GET", connectionString, False
  144. 'objSvrHTTP.send
  145. 'connectionString4 = xbmcConnection & JSONstring
  146. 'objSvrHTTP.open "GET", connectionString, False
  147. 'objSvrHTTP.send
  148. 'connectionString5 = xbmcConnection & JSONstring
  149. 'objSvrHTTP.open "GET", connectionString, False
  150. 'objSvrHTTP.send
  151. end if
  152. end if
  153. elseif ignoreTorrent = "true" then
  154. WScript.Quit
  155.  
  156. End If
  157.  
  158. Function URLEncode(ByVal str)
  159. Dim strTemp, strChar
  160. Dim intPos, intASCII
  161. strTemp = ""
  162. strChar = ""
  163. For intPos = 1 To Len(str)
  164. intASCII = Asc(Mid(str, intPos, 1))
  165. If intASCII = 32 Then
  166. strTemp = strTemp & "+"
  167. ElseIf ((intASCII < 123) And (intASCII > 96)) Then
  168. strTemp = strTemp & Chr(intASCII)
  169. ElseIf ((intASCII < 91) And (intASCII > 64)) Then
  170. strTemp = strTemp & Chr(intASCII)
  171. ElseIf ((intASCII < 58) And (intASCII > 47)) Then
  172. strTemp = strTemp & Chr(intASCII)
  173. Else
  174. strChar = Trim(Hex(intASCII))
  175. If intASCII < 16 Then
  176. strTemp = strTemp & "%0" & strChar
  177. Else
  178. strTemp = strTemp & "%" & strChar
  179. End If
  180. End If
  181. Next
  182. URLEncode = strTemp
  183. End Function
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement