# -*- coding: utf-8 -*-
import json

import requests


class TheTVDB(object):
    token = None
    def __init__(self, username, api_key, user_key):
        self.api_key = api_key
        self.user_key = user_key

    def get_imdb_id(self, tvdb_id):
        # TODO Cache
        if not self.token:
            self._refresh_token()

        url = "https://api.thetvdb.com/series/{tvdb_id}".format(tvdb_id=tvdb_id)
        headers = {
            'Authorization': 'Bearer {token}'.format(token=self.token)
        }
        r = requests.get(url, headers=headers)

        if r.status_code == 200:
            tv_show = r.json()
            return tv_show['data']['imdbId']
        else:
            return None

    def _refresh_token(self):
        data = {
            'apikey': config.TVDB_API_KEY,
            'userkey': config.TVDB_USER_KEY,
            'username': config.TVDB_USERNAME,
        }

        url = "https://api.thetvdb.com/login"
        r = requests.post(url, json=data)

        if r.status_code == 200:
            result = r.json()
            self.token = result['token']
        else:
            return None


@@ -141,16 +141,49 @@
 r_key):%0A
+        self.username = username%0A
         
@@ -809,27 +809,20 @@
 y': 
-config.TVDB_API_KEY
+self.api_key
 ,%0A  
@@ -846,28 +846,21 @@
 y': 
-config.TVDB_USER_KEY
+self.user_key
 ,%0A  
@@ -885,28 +885,21 @@
 e': 
-config.TVDB_USERNAME
+self.username
 ,%0A  

refresh token bugfix