__init__.py 3.56 KB
Newer Older
1
2
3
4
from __future__ import unicode_literals

from urlparse import urljoin

5
from oauthlib.oauth2 import BackendApplicationClient, TokenExpiredError
echel0n's avatar
echel0n committed
6
7
from requests_oauthlib import OAuth2Session

8
9
10
11
import sickrage
from sickrage.core.api.exceptions import unauthorized, error


echel0n's avatar
echel0n committed
12
13
14
class CustomBackendApplicationClient(BackendApplicationClient):
    def prepare_refresh_body(self, body='', refresh_token=None, scope=None, **kwargs):
        return super(CustomBackendApplicationClient, self).prepare_refresh_body(boby=body, scope=scope, **kwargs)
15
16


17
class API(object):
18
    def __init__(self, client_id=None, client_secret=None):
echel0n's avatar
echel0n committed
19
20
        self.api_url = 'https://api.sickrage.ca/'
        self.token_url = urljoin(self.api_url, 'oauth/v2/token')
21
22
23
24
25
26
27
28
29
30
        self._client_id = client_id
        self._client_secret = client_secret
        self._session = None
        self._token = None

    @property
    def credentials(self):
        return {
            'client_id': self._client_id or sickrage.app.config.api_client_id,
            'client_secret': self._client_secret or sickrage.app.config.api_client_secret
31
        }
echel0n's avatar
echel0n committed
32

33
34
35
    @property
    def session(self):
        if self._session is None:
echel0n's avatar
echel0n committed
36
37
            self._session = OAuth2Session(
                client=CustomBackendApplicationClient(client_id=self.credentials['client_id']),
38
39
40
41
42
43
44
45
46
47
                token=self.token,
                token_updater=self.token_updater,
                auto_refresh_url=self.token_url,
                auto_refresh_kwargs=self.credentials
            )
        return self._session

    @property
    def token(self):
        if self._token is None:
echel0n's avatar
echel0n committed
48
49
            self._token = OAuth2Session(
                client=CustomBackendApplicationClient(client_id=self.credentials['client_id'])
50
51
52
53
54
55
            ).fetch_token(
                token_url=self.token_url,
                timeout=30,
                **self.credentials
            )
        return self._token
echel0n's avatar
echel0n committed
56

57
58
    def token_updater(self, token):
        self._token = token
59
60
61
62
63
64

    def _request(self, method, url, **kwargs):
        if not sickrage.app.config.enable_api:
            return

        try:
65
            resp = self.session.request(method, urljoin(self.api_url, url), timeout=30, **kwargs)
66
67
68
69
70
71
72
73
74
75
76
77
78
79
        except Exception as e:
            raise error(e.message)

        # handle requests exceptions
        if resp.status_code == 401:
            raise unauthorized(resp.json()['message'])
        elif resp.status_code >= 400:
            raise error(resp.json()['message'])

        return resp.json()

    def user_profile(self):
        return self._request('GET', 'users/me')

echel0n's avatar
echel0n committed
80
    def add_provider_cache_result(self, data):
echel0n's avatar
echel0n committed
81
        self._request('POST', 'v1/providers/cache/results', json=data)
82

echel0n's avatar
echel0n committed
83
    def get_provider_cache_results(self, provider, indexerid=None):
echel0n's avatar
echel0n committed
84
85
86
        query = ('v1/providers/cache/results/{}'.format(provider),
                 'v1/providers/cache/results/{}/indexerids/{}'.format(provider, indexerid))[indexerid is not None]

87
        return self._request('GET', query)
88

echel0n's avatar
echel0n committed
89
90
91
92
93
94
95
    def get_torrent_cache_results(self, hash=None):
        query = ('v1/torrents/cache/results',
                 'v1/torrents/cache/results/{}'.format(hash))[hash is not None]

        return self._request('GET', query)

    def add_torrent_cache_result(self, url):
96
97
98
99
100
101
102
103
104
105
106
        self._request('POST', 'v1/torrents/cache/results', data=dict({'url': url}))

    def search_by_imdb_title(self, title):
        query = 'v1/imdb/search_by_title/{}'.format(title)

        return self._request('GET', query)

    def search_by_imdb_id(self, id):
        query = 'v1/imdb/search_by_id/{}'.format(id)

        return self._request('GET', query)