Mirx

Sonarr_Example_API_get_and_post.py

May 3rd, 2018
900
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.54 KB | None | 0 0
  1. #!/usr/bin/env python
  2. #
  3.  
  4. ###################################################################################################
  5. ### SONARR POST-PROCESSING SCRIPT                                                               ###
  6. #                                                                                               ###
  7. # Script to force a rescanSeries on an 2nd instance of Sonarr.                                  ###
  8. #                                                                                               ###
  9. # Example usecases are:                                                                         ###
  10. #                                                                                               ###
  11. # 1) Fire & forget dual deployments                                                             ###
  12. #       This bypasses the 12 hour discscan cycle and checks if new files are present on         ###                                                                                             ###
  13. #       the disc. This decreases unneeded grabs by the 2nd Sonarr instance.                     ###
  14. #                                                                                               ###
  15. #                       !!! Make sure to alter your log file location !!!                       ###
  16. #                           !!! Make sure to alter your API key !!!                             ###
  17. #                         !!! Make sure to alter your IP/port config !!!                        ###
  18. #                                                                                               ###
  19. ###################################################################################################
  20.  
  21. from datetime import datetime
  22. from os import environ, path
  23. from sys import argv
  24. import json
  25. import urllib2
  26.  
  27.                         ### Variables ###
  28.    
  29.     # Get the SeriesID from the environment variables or argv
  30.     # Manual troubleshoot mode example command: python scriptname.py Arrow
  31. if len(argv) >= 2:
  32.     sonarr_series_title = argv[1]
  33. else:
  34.     sonarr_series_title = environ.get('sonarr_series_title')
  35.  
  36.     #Logfile location and open it unbuffered so you can read it during execution.
  37. fp = open('/volume1/Sonarr/update_2nd_sonarr.log', 'a',0)
  38.  
  39.                         ### Processing script ###
  40.  
  41.         ### Grab the series ID from 2nd Sonarr instance ###
  42.  
  43.     #Accessing the URL and GET the series information from the 2nd Sonarr instance
  44. SeriesDump = json.load(urllib2.urlopen('http://192.168.0.1:8989/api/series?apikey=Your_API_Key'))
  45.  
  46.     #Extract the SeriesID from the API response
  47. for ID in SeriesDump:
  48.     if ID["title"] == sonarr_series_title:
  49.         SeriesID = ID["id"]
  50.         break
  51.  
  52.         ### Trigger disc rescan on 2nd Sonarr Instance ###
  53.        
  54.     #JSON payload which command to perform
  55. data = {'name': 'rescanSeries', 'seriesId' : SeriesID}
  56.  
  57.     #Accessing the URL en POSTing the command
  58. req = urllib2.Request('http://192.168.0.1:8989/api/command?apikey=Your_API_Key')
  59. req.add_header('Content-Type', 'application/json')
  60.  
  61. response = urllib2.urlopen(req, json.dumps(data))
  62.  
  63.     #Timestamp variable for logfile
  64. sttime = datetime.now().strftime('%Y%m%d %H:%M:%S - ')
  65.  
  66.     #Write to logfile
  67. fp.write(sttime + sonarr_series_title + ' was rescanned due to grab on 1st Sonarr instance.\n\n')
  68.  
  69. fp.close()
  70. raise SystemExit
Advertisement
Add Comment
Please, Sign In to add comment