Advertisement
Guest User

SABChill 1.6

a guest
Dec 9th, 2013
261
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. property vers : "SABchill 1.6"
  2. property setuprun : false
  3. property sabnzbd_ip : "127.0.0.1"
  4. property sabnzbd_port : "8080"
  5. property sabnzbd_api : "00000000000000000000"
  6. property sabnzbd_user : ""
  7. property sabnzbd_pass : ""
  8. property time_in_minutes : "30" -- minutes
  9. property always_ask_time : "true"
  10.  
  11. (*
  12.  
  13. 0.8
  14. First release, small distribution.
  15.  
  16. 1.0
  17. Fixed issues where setup loops, or asks for delay numerous times.
  18.  
  19. 1.1
  20. If sabnzbd_ip remains "12.0.0.1" then script will run setup.
  21.  
  22. 1.2
  23. If provided with the correct hostname and port, SABchill will open a web broser to make it easier to grab your API KEY.  Please note if you have web authentication turned on, you will need to sign in prior to seeing the API Key
  24. 1.5
  25. Added support to automatically pull the user api key, but I stick it in the existing interface, still gives the option to open the webpage at the correct location.
  26.  
  27. 1.6
  28. Fixed error handling of new routine.  Now if the username/pass or ip/port is wrong, we will reset those variables, and the script will ask until it is right
  29.  
  30. *)
  31.  
  32.  
  33. on run {}
  34.     tell me to activate
  35.     if setuprun is false then
  36.         my runsetup()
  37.     end if
  38.     if always_ask_time = "true" then
  39.         set time_in_minutes to text returned of (display dialog "How long would you like to pause SABnzbd for?" default answer time_in_minutes with title vers)
  40.     end if
  41.     ignoring application responses
  42.         set sabnzbd_respose to (do shell script "curl \"http://" & sabnzbd_ip & ":" & sabnzbd_port & "/sabnzbd/api?mode=config&name=set_pause&value=" & time_in_minutes & "&apikey=" & sabnzbd_api & "\";true")
  43.         if sabnzbd_respose does not contain "ok" then
  44.             if sabnzbd_respose contains "connect() timed out" or sabnzbd_respose contains "resolve host" then
  45.                 set setuprun to false
  46.                 display dialog "Connection Timed out. Please verify IP and Port
  47. IP: " & sabnzbd_ip & "
  48. Port: " & sabnzbd_port with title vers
  49.                 set sabnzbd_ip to "127.0.0.1"
  50.                 set sabnzbd_port to "8080"
  51.             end if
  52.             if sabnzbd_respose contains "API Key Incorrect" then
  53.                 set setuprun to false
  54.                 display dialog "API Key is wrong, please verify in SABnzbd-> Config -> General -> API Key
  55. Current Value: " & sabnzbd_api with title vers
  56.                 set sabnzbd_api to "00000000000000000000"
  57.             end if
  58.         end if
  59.     end ignoring
  60. end run
  61.  
  62. on runsetup()
  63.     --if sabnzbd_api = "00000000000000000000" and sabnzbd_ip = "127.0.0.1" then
  64.     if sabnzbd_ip = "127.0.0.1" or sabnzbd_port = "8080" then
  65.         set sabnzbd_ip to text returned of (display dialog "What is the IP address or domain of your SABnzbd installation?" default answer sabnzbd_ip with title vers)
  66.         set sabnzbd_port to text returned of (display dialog "What is the port of your SABnzbd installation?" default answer sabnzbd_port with title vers)
  67.     end if
  68.     if sabnzbd_user = "" or sabnzbd_pass = "" then
  69.         set sabnzbd_user to text returned of (display dialog "What is the USERNAME used to access?" default answer sabnzbd_user with title vers)
  70.         set sabnzbd_pass to text returned of (display dialog "What is the PASSWORD used to access?" default answer sabnzbd_pass with title vers)
  71.     end if
  72.     try
  73.         set sabnzbd_api to item 1 of stringtolist(item 2 of my stringtolist(do shell script "curl -u " & sabnzbd_user & ":" & sabnzbd_pass & " http://" & sabnzbd_ip & ":" & sabnzbd_port & "/config/general/", "var apikey = \""), "\";")
  74.     on error errmsg
  75.         if errmsg contains "Connection refused" then
  76.             try
  77.                 display dialog "The IP address \"" & sabnzbd_ip & "\" or port \"" & sabnzbd_port & "\" is incorrect." buttons "Cancel" default button 1
  78.             on error
  79.                 set sabnzbd_ip to "127.0.0.1"
  80.                 set sabnzbd_port to "8080"
  81.             end try
  82.         else if errmsg contains "Error 401 Unauthorized" then
  83.             try
  84.                 display dialog "The username \"" & sabnzbd_user & "\" or password \"" & sabnzbd_pass & "\" is incorrect." buttons "Cancel" default button 1
  85.             on error
  86.                 set sabnzbd_user to ""
  87.                 set sabnzbd_pass to ""
  88.             end try
  89.         else
  90.             display dialog errmsg
  91.         end if
  92.         set setuprun to false
  93.         my runsetup()
  94.     end try
  95.     set sabtempanswer to (display dialog "What is SABnzbd API key?
  96. SABnzbd-> Config -> General -> API Key
  97.  
  98. If this field is something besides 0's then the key shown was pulled from your installation." default answer sabnzbd_api buttons {"See my API key", "Cancel", "OK"} with title vers)
  99.    
  100.     set sabnzbd_api to text returned of sabtempanswer
  101.    
  102.    
  103.     if button returned of sabtempanswer = "See my API key" then
  104.         do shell script "open http://" & sabnzbd_ip & ":" & sabnzbd_port & "/config/general/"
  105.         delay 3
  106.         tell me to activate
  107.         my runsetup()
  108.     end if
  109.    
  110.     set always_ask_time to button returned of (display dialog "Prompt for pause length?
  111. Current value is " & time_in_minutes & " minutes." buttons {"False", "True"} default button "True" with title vers)
  112.     set setuprun to true
  113. end runsetup
  114. --takes a string of data like "hello, my name is Mike" and converts it into {"hello,","my","name","is","Mike"} so that you can specify first item, second item or walk down a list searching, listtostring() does the opposite.  listtostring is not used in this script, but I have included that code below, as these 2 should always be with each other.
  115. on stringtolist(theString, delim)
  116.     set oldelim to AppleScript's text item delimiters
  117.     set AppleScript's text item delimiters to delim
  118.     set dlist to (every text item of theString)
  119.     set AppleScript's text item delimiters to oldelim
  120.     return dlist
  121. end stringtolist
  122. on ListToString(theList, delim)
  123.     set oldelim to AppleScript's text item delimiters
  124.     set AppleScript's text item delimiters to delim
  125.     set alist to theList as string
  126.     set AppleScript's text item delimiters to oldelim
  127.     return alist
  128. end ListToString
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement