Last active
February 14, 2017 16:38
-
-
Save floweb/9448806 to your computer and use it in GitHub Desktop.
Consume Sickbeard API to retreive wanted/missed/today's episodes and use sxxexx to download them via The Piracy Bay
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
# Author : Florian Le Frioux | |
# Date : October 2014 | |
# Consume SB API to retreive wanted/missed/today's episodes | |
# and use sxxexx to download them via The Piracy Bay | |
# This script make use of: | |
# * Sickbeard API documentation: http://sickbeard.com/api/ | |
# * Sickbeard API calls inspired by this file: http://bit.ly/1i1K7Cq | |
# * sh: http://amoffat.github.io/sh | |
# * sxxexx: https://github.com/nicolargo/sxxexx | |
import urllib2 | |
import base64 | |
import json | |
from sh import sxxexx | |
SB_URL = 'http://tonurl.fr' | |
SB_APIKEY = '423635b0b57299eb6ddb2f911d18fb90' | |
SB_APIURL = '%s/api/%s' % (SB_URL, SB_APIKEY) | |
SB_USER = 'tonuser' | |
SB_PASS = 'tonpassword' | |
SB_AUTH = '%s:%s' % (SB_USER, SB_PASS) | |
DEBUG = False | |
def sickbeard_api(params=None, use_json=True, debug=DEBUG): | |
""" | |
This builds the sickbeard_api query (w/ Authorization) | |
and return the response | |
""" | |
try: | |
api_call = '%s%s' % (SB_APIURL, params) | |
r = urllib2.Request(api_call) | |
base64string = base64.encodestring(SB_AUTH) | |
r.add_header("Authorization", "Basic %s" % base64string) | |
data = urllib2.urlopen(r).read() | |
if debug: | |
print api_call | |
print data | |
if use_json: | |
data = json.JSONDecoder().decode(data) | |
if data['result'].rfind('success') >= 0: | |
return data['data'] | |
except: | |
raise Exception | |
def future(sort='date', types='missed|today|soon|later', paused=0): | |
""" | |
This builds the complete query (w/ params) to be passed to sickbeard_api() | |
""" | |
params = '/?cmd=future&sort=%s&type=%s&paused=%s' % (sort, types, paused) | |
data = sickbeard_api(params) | |
return data if data else False | |
def show_seasons(tvdbid, season=None): | |
""" | |
This builds the complete query (w/ params) to be passed to sickbeard_api() | |
""" | |
if not season: | |
params = '/?cmd=show.seasons&tvdbid=%s' % tvdbid | |
else: | |
params = '/?cmd=show.seasons&tvdbid=%s&season=%s' % (tvdbid, season) | |
data = sickbeard_api(params) | |
return data if data else False | |
def show_stats(tvdbid): | |
""" | |
This builds the complete query (w/ params) to be passed to sickbeard_api() | |
""" | |
params = '/?cmd=show.stats&tvdbid=%s' % tvdbid | |
data = sickbeard_api(params) | |
return data if data else False | |
def shows(sort='id', paused=None): | |
""" | |
This builds the complete query (w/ params) to be passed to sickbeard_api() | |
""" | |
params = '/?cmd=shows&sort=%s&paused=%s' % (sort, paused) | |
data = sickbeard_api(params) | |
return data if data else False | |
def download_ep_sxxexx(show_name, season, episode, debug=False): | |
""" | |
Search and start downloading episode (w/ Transmission) using sxxexx | |
""" | |
# internal fonction for the sh module cmds _out and _err | |
def process_output(line): | |
print line | |
# Search and start downloading them (w/ Transmission) using sxxexx | |
if debug: | |
sxxexx('-t', '"%s"' % show_name, '-s', season, | |
'-e', episode, '-d', '-D', | |
_out=process_output, _err=process_output) | |
else: | |
sxxexx('-t', '"%s"' % show_name, '-s', season, | |
'-e', episode, '-d', '-V') | |
def main(): | |
try: | |
# First, loop on shows to see if there's some "wanted episodes" | |
wanted_shows = [(show_id, v['show_name']) | |
for show_id, v in shows('id', 0).iteritems() | |
if show_stats(show_id) and | |
show_stats(show_id)['wanted'] > 0] | |
# Then, we loop over episodes from those wanted_shows, and | |
# for each show: show_seasons() will give us a multidimensional dict | |
# so we have to loop again to get the ep_list, and then | |
# see if there's some "wanted episodes" ! | |
wanted_eps = [{'show_name': show_name, | |
'season': season, 'episode': episode} | |
for i, (show_id, show_name) in enumerate(wanted_shows) | |
for season, ep_list in show_seasons(show_id).iteritems() | |
for episode, ep_stats in ep_list.iteritems() | |
if ep_stats['status'] == 'Wanted'] | |
# Check for missed|today's episodes | |
coming_eps = future(types='missed|today') | |
# Search and start downloading them | |
for ep in coming_eps['missed']: | |
download_ep_sxxexx(ep['show_name'], ep['season'], | |
ep['episode'], DEBUG) | |
for ep in coming_eps['today']: | |
download_ep_sxxexx(ep['show_name'], ep['season'], | |
ep['episode'], DEBUG) | |
for ep in wanted_eps: | |
download_ep_sxxexx(ep['show_name'], ep['season'], | |
ep['episode'], DEBUG) | |
except Exception, e: | |
print e | |
pass | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment