# Buggy code
def getTraktToken(trakt_pin=None):
    trakt_settings = {
        "trakt_api_key": app.TRAKT_API_KEY,
        "trakt_api_secret": app.TRAKT_API_SECRET,
    }
    trakt_api = TraktApi(app.SSL_VERIFY, app.TRAKT_TIMEOUT, **trakt_settings)
    try:
        (access_token, refresh_token) = trakt_api.get_token(
            app.TRAKT_REFRESH_TOKEN, trakt_pin=trakt_pin
        )
    except MissingTokenException:
        ui.notifications.error("You need to get a PIN and authorize Medusa app")
        return "You need to get a PIN and authorize Medusa app"
    except TokenExpiredException:
        # Clear existing tokens
        app.TRAKT_ACCESS_TOKEN = ""
        app.TRAKT_REFRESH_TOKEN = ""
        ui.notifications.error(
            "TOKEN expired. Reload page, get a new PIN and authorize Medusa app"
        )
        return "TOKEN expired. Reload page, get a new PIN and authorize Medusa app"
    except TraktException:
        ui.notifications.error(
            "Connection error. Click 'Authorize Medusa' button again"
        )
        return "Connection error. Click 'Authorize Medusa' button again"
    if access_token:
        app.TRAKT_ACCESS_TOKEN = access_token
        app.TRAKT_REFRESH_TOKEN = refresh_token
    response = trakt_api.validate_account()
    if response:
        ui.notifications.message("Trakt Authorized")
        return "Trakt Authorized"
    ui.notifications.error("Connection error. Reload the page to get new token!")
    return "Trakt Not Authorized!"


# Traceback (UnavailableException)
2016-11-16 22:13:21 DEBUG    SEARCHQUEUE-MANUAL-319545 :: [nzb.su] :: [1b1c26a] Unable to match LA.FAMIGLIA.(2016).S01E02.1080P.HDTV.DD.5.1.X64 to a show in your database. Parser result: title: LA FAMIGLIA, alias: LA FAMIGLIA 2016, year: 2016, season: 1, episode: [2, 64], screen_size: 1080p, format: HDTV, audio_codec: AC3, audio_channels: 5.1, type: episode, absolute_episode: [], quality: 1080p HDTV
2016-11-16 22:13:31 DEBUG    SEARCHQUEUE-MANUAL-319545 :: [nzb.su] :: [1b1c26a] Unable to match Band.of.Brothers.S01E02.Day.of.Days.1080p.Hybrid.Remux.AVC.DTS-HD.MA.5.1-AJP69 to a show in your database. Parser result: title: Band of Brothers, season: 1, episode: [2], episode_title: Day of Days, screen_size: 1080p, other: Remux, audio_codec: DTS, audio_profile: HDMA, audio_channels: 5.1, release_group: AJP69, type: episode, absolute_episode: [], quality: Unknown
2016-11-16 22:13:43 DEBUG    SEARCHQUEUE-MANUAL-319545 :: [nzb.su] :: [1b1c26a] Unable to match Berlin.Station.-.S01E02.-.Lights.Don&#039;t.Run.On.Loyalty.(720p) to a show in your database. Parser result: title: Berlin Station, season: 1, episode: [2], episode_title: Lights Don& 039;t Run On Loyalty, screen_size: 720p, type: episode, absolute_episode: [], quality: Unknown
2016-11-16 22:13:44 DEBUG    POSTPROCESSOR :: [1b1c26a] Using cached parse result for 'NCIS.New.Orleans.S03E06.720p.HDTV.X264-DIMENSION[ettv].mkv'
2016-11-16 22:13:44 DEBUG    POSTPROCESSOR :: [1b1c26a] Processing folder Quantico.S02E07.LLCFlutter.AMZN.1080p.WEBRip.DD5.1.x264-DRACULA
2016-11-16 22:13:56 DEBUG    SEARCHQUEUE-MANUAL-319545 :: [nzb.su] :: [1b1c26a] Unable to match Berlin.Station.-.S01E02.-.Lights.Don't.Run.On.Loyalty.(720p) to a show in your database. Parser result: title: Berlin Station, season: 1, episode: [2], episode_title: Lights Don't Run On Loyalty, screen_size: 720p, type: episode, absolute_episode: [], quality: Unknown
2016-11-16 22:13:56 DEBUG    SEARCHQUEUE-MANUAL-319545 :: [nzb.su] :: [1b1c26a] Adding to cache item found in manual search: Sweet.Vicious.S01E02.1080p.WEB.x264-TURBO
2016-11-16 22:13:56 DEBUG    SEARCHQUEUE-MANUAL-319545 :: [nzb.su] :: [1b1c26a] Using cached parse result for 'Sweet.Vicious.S01E02.1080p.WEB.x264-TURBO'
2016-11-16 22:13:56 DEBUG    SEARCHQUEUE-MANUAL-319545 :: [nzb.su] :: [1b1c26a] Added RSS item: [Sweet.Vicious.S01E02.1080p.WEB.x264-TURBO] to cache: [nzb_su]
2016-11-16 22:13:56 DEBUG    SEARCHQUEUE-MANUAL-319545 :: [nzb.su] :: [1b1c26a] Adding to cache item found in manual search: Good.Behavior.S01E02.1080p.HDTV.x264-SERIOUSLY
2016-11-16 22:13:56 DEBUG    SEARCHQUEUE-MANUAL-319545 :: [nzb.su] :: [1b1c26a] Using cached parse result for 'Good.Behavior.S01E02.1080p.HDTV.x264-SERIOUSLY'
2016-11-16 22:13:57 DEBUG    SEARCHQUEUE-MANUAL-319545 :: [nzb.su] :: [1b1c26a] Added RSS item: [Good.Behavior.S01E02.1080p.HDTV.x264-SERIOUSLY] to cache: [nzb_su]
2016-11-16 22:13:57 DEBUG    SEARCHQUEUE-MANUAL-319545 :: [nzb.su] :: [1b1c26a] Adding to cache item found in manual search: Good.Behavior.S01E02.720p.HDTV.x264-FLEET
2016-11-16 22:13:57 DEBUG    SEARCHQUEUE-MANUAL-319545 :: [nzb.su] :: [1b1c26a] Using cached parse result for 'Good.Behavior.S01E02.720p.HDTV.x264-FLEET'
2016-11-16 22:13:57 DEBUG    SEARCHQUEUE-MANUAL-319545 :: [nzb.su] :: [1b1c26a] Added RSS item: [Good.Behavior.S01E02.720p.HDTV.x264-FLEET] to cache: [nzb_su]
2016-11-16 22:13:57 DEBUG    SEARCHQUEUE-MANUAL-319545 :: [nzb.su] :: [1b1c26a] Adding to cache item found in manual search: People.Magazine.Investigates.S01E02.The.Long.Island.Serial.Killer-New.Revelations.720p.HDTV.x264-W4F
2016-11-16 22:13:57 DEBUG    SEARCHQUEUE-MANUAL-319545 :: [nzb.su] :: [1b1c26a] Using cached parse result for 'People.Magazine.Investigates.S01E02.The.Long.Island.Serial.Killer-New.Revelations.720p.HDTV.x264-W4F'
2016-11-16 22:13:57 DEBUG    SEARCHQUEUE-MANUAL-319545 :: [nzb.su] :: [1b1c26a] Added RSS item: [People.Magazine.Investigates.S01E02.The.Long.Island.Serial.Killer-New.Revelations.720p.HDTV.x264-W4F] to cache: [nzb_su]
2016-11-16 22:13:57 DEBUG    SEARCHQUEUE-MANUAL-319545 :: [nzb.su] :: [1b1c26a] Adding to cache item found in manual search: Frontier.2016.S01E02.720p.HDTV.x264
2016-11-16 22:13:57 DEBUG    SEARCHQUEUE-MANUAL-319545 :: [nzb.su] :: [1b1c26a] Using cached parse result for 'Frontier.2016.S01E02.720p.HDTV.x264'
2016-11-16 22:13:57 DEBUG    SEARCHQUEUE-MANUAL-319545 :: [nzb.su] :: [1b1c26a] Added RSS item: [Frontier.2016.S01E02.720p.HDTV.x264] to cache: [nzb_su]
2016-11-16 22:13:57 DEBUG    SEARCHQUEUE-MANUAL-319545 :: [nzb.su] :: [1b1c26a] Adding to cache item found in manual search: Frontier.2016.S01E02.720p.HDTV.x264-KILLERS
2016-11-16 22:13:57 DEBUG    SEARCHQUEUE-MANUAL-319545 :: [nzb.su] :: [1b1c26a] Using cached parse result for 'Frontier.2016.S01E02.720p.HDTV.x264-KILLERS'
2016-11-16 22:13:57 DEBUG    SEARCHQUEUE-MANUAL-319545 :: [nzb.su] :: [1b1c26a] Added RSS item: [Frontier.2016.S01E02.720p.HDTV.x264-KILLERS] to cache: [nzb_su]
2016-11-16 22:13:57 DEBUG    SEARCHQUEUE-MANUAL-319545 :: [nzb.su] :: [1b1c26a] Adding to cache item found in manual search: Sweet.Vicious.S01E02.The.Writings.on.the.Wall.720p.HDTV.x264-W4F
2016-11-16 22:13:57 DEBUG    SEARCHQUEUE-MANUAL-319545 :: [nzb.su] :: [1b1c26a] Using cached parse result for 'Sweet.Vicious.S01E02.The.Writings.on.the.Wall.720p.HDTV.x264-W4F'
2016-11-16 22:13:57 DEBUG    SEARCHQUEUE-MANUAL-319545 :: [nzb.su] :: [1b1c26a] Added RSS item: [Sweet.Vicious.S01E02.The.Writings.on.the.Wall.720p.HDTV.x264-W4F] to cache: [nzb_su]
2016-11-16 22:13:57 DEBUG    SEARCHQUEUE-MANUAL-319545 :: [nzb.su] :: [1b1c26a] Adding to cache item found in manual search: Vice.Does.America.S01E02.720p.HDTV.x264-YesTV
2016-11-16 22:13:57 DEBUG    SEARCHQUEUE-MANUAL-319545 :: [nzb.su] :: [1b1c26a] Using cached parse result for 'Vice.Does.America.S01E02.720p.HDTV.x264-YesTV'
2016-11-16 22:13:57 DEBUG    SEARCHQUEUE-MANUAL-319545 :: [nzb.su] :: [1b1c26a] Added RSS item: [Vice.Does.America.S01E02.720p.HDTV.x264-YesTV] to cache: [nzb_su]
2016-11-16 22:13:57 DEBUG    SEARCHQUEUE-MANUAL-319545 :: [nzb.su] :: [1b1c26a] Adding to cache item found in manual search: Entourage.E02.720p.HDTV.x264-AREA11
2016-11-16 22:13:57 DEBUG    SEARCHQUEUE-MANUAL-319545 :: [nzb.su] :: [1b1c26a] Using cached parse result for 'Entourage.E02.720p.HDTV.x264-AREA11'
2016-11-16 22:13:57 DEBUG    SEARCHQUEUE-MANUAL-319545 :: [nzb.su] :: [1b1c26a] Added RSS item: [Entourage.E02.720p.HDTV.x264-AREA11] to cache: [nzb_su]
2016-11-16 22:13:57 DEBUG    SEARCHQUEUE-MANUAL-319545 :: [nzb.su] :: [1b1c26a] Adding to cache item found in manual search: People.Just.Do.Nothing.S01E02.DVDRip.x264-OUIJA
2016-11-16 22:13:57 DEBUG    SEARCHQUEUE-MANUAL-319545 :: [nzb.su] :: [1b1c26a] Using cached parse result for 'People.Just.Do.Nothing.S01E02.DVDRip.x264-OUIJA'
2016-11-16 22:13:57 DEBUG    SEARCHQUEUE-MANUAL-319545 :: [nzb.su] :: [1b1c26a] Added RSS item: [People.Just.Do.Nothing.S01E02.DVDRip.x264-OUIJA] to cache: [nzb_su]
2016-11-16 22:13:57 DEBUG    SEARCHQUEUE-MANUAL-319545 :: [nzb.su] :: [1b1c26a] Adding to cache item found in manual search: 60.Days.In.S01E02.DVDRip.X264-PFa
2016-11-16 22:13:57 DEBUG    SEARCHQUEUE-MANUAL-319545 :: [nzb.su] :: [1b1c26a] Using cached parse result for '60.Days.In.S01E02.DVDRip.X264-PFa'
2016-11-16 22:13:57 DEBUG    SEARCHQUEUE-MANUAL-319545 :: [nzb.su] :: [1b1c26a] Added RSS item: [60.Days.In.S01E02.DVDRip.X264-PFa] to cache: [nzb_su]
2016-11-16 22:13:57 DEBUG    SEARCHQUEUE-MANUAL-319545 :: [nzb.su] :: [1b1c26a] Adding to cache item found in manual search: Billions.S01E02.BDRip.x264-DEMAND
2016-11-16 22:13:57 DEBUG    SEARCHQUEUE-MANUAL-319545 :: [nzb.su] :: [1b1c26a] Using cached parse result for 'Billions.S01E02.BDRip.x264-DEMAND'
2016-11-16 22:13:57 DEBUG    SEARCHQUEUE-MANUAL-319545 :: [nzb.su] :: [1b1c26a] Added RSS item: [Billions.S01E02.BDRip.x264-DEMAND] to cache: [nzb_su]
2016-11-16 22:13:57 DEBUG    SEARCHQUEUE-MANUAL-319545 :: [nzb.su] :: [1b1c26a] Adding to cache item found in manual search: Good.Behavior.S01E02.HDTV.x264-FLEET
2016-11-16 22:13:57 DEBUG    SEARCHQUEUE-MANUAL-319545 :: [nzb.su] :: [1b1c26a] Using cached parse result for 'Good.Behavior.S01E02.HDTV.x264-FLEET'
2016-11-16 22:13:57 DEBUG    SEARCHQUEUE-MANUAL-319545 :: [nzb.su] :: [1b1c26a] Added RSS item: [Good.Behavior.S01E02.HDTV.x264-FLEET] to cache: [nzb_su]
2016-11-16 22:13:57 DEBUG    SEARCHQUEUE-MANUAL-319545 :: [nzb.su] :: [1b1c26a] Adding to cache item found in manual search: People.Magazine.Investigates.S01E02.The.Long.Island.Serial.Killer-New.Revelations.HDTV.x264-W4F
2016-11-16 22:13:57 DEBUG    SEARCHQUEUE-MANUAL-319545 :: [nzb.su] :: [1b1c26a] Using cached parse result for 'People.Magazine.Investigates.S01E02.The.Long.Island.Serial.Killer-New.Revelations.HDTV.x264-W4F'
2016-11-16 22:13:57 DEBUG    SEARCHQUEUE-MANUAL-319545 :: [nzb.su] :: [1b1c26a] Added RSS item: [People.Magazine.Investigates.S01E02.The.Long.Island.Serial.Killer-New.Revelations.HDTV.x264-W4F] to cache: [nzb_su]
2016-11-16 22:13:57 DEBUG    SEARCHQUEUE-MANUAL-319545 :: [nzb.su] :: [1b1c26a] Adding to cache item found in manual search: Frontier.2016.S01E02.HDTV.x264-KILLERS
2016-11-16 22:13:57 DEBUG    SEARCHQUEUE-MANUAL-319545 :: [nzb.su] :: [1b1c26a] Using cached parse result for 'Frontier.2016.S01E02.HDTV.x264-KILLERS'
2016-11-16 22:13:57 DEBUG    SEARCHQUEUE-MANUAL-319545 :: [nzb.su] :: [1b1c26a] Added RSS item: [Frontier.2016.S01E02.HDTV.x264-KILLERS] to cache: [nzb_su]
2016-11-16 22:13:57 DEBUG    SEARCHQUEUE-MANUAL-319545 :: [nzb.su] :: [1b1c26a] Adding to cache item found in manual search: Sweet.Vicious.S01E02.The.Writings.on.the.Wall.HDTV.x264-W4F
2016-11-16 22:13:57 DEBUG    SEARCHQUEUE-MANUAL-319545 :: [nzb.su] :: [1b1c26a] Using cached parse result for 'Sweet.Vicious.S01E02.The.Writings.on.the.Wall.HDTV.x264-W4F'
2016-11-16 22:13:57 DEBUG    SEARCHQUEUE-MANUAL-319545 :: [nzb.su] :: [1b1c26a] Added RSS item: [Sweet.Vicious.S01E02.The.Writings.on.the.Wall.HDTV.x264-W4F] to cache: [nzb_su]
2016-11-16 22:13:57 DEBUG    SEARCHQUEUE-MANUAL-319545 :: [nzb.su] :: [1b1c26a] Adding to cache item found in manual search: Vice.Does.America.S01E02.HDTV.x264-YesTV
2016-11-16 22:13:57 DEBUG    SEARCHQUEUE-MANUAL-319545 :: [nzb.su] :: [1b1c26a] Using cached parse result for 'Vice.Does.America.S01E02.HDTV.x264-YesTV'
2016-11-16 22:13:57 DEBUG    SEARCHQUEUE-MANUAL-319545 :: [nzb.su] :: [1b1c26a] Added RSS item: [Vice.Does.America.S01E02.HDTV.x264-YesTV] to cache: [nzb_su]
2016-11-16 22:13:57 DEBUG    SEARCHQUEUE-MANUAL-319545 :: [nzb.su] :: [1b1c26a] Mass updating cache table with manual results for provider: nzb.su
2016-11-16 22:13:57 DEBUG    SEARCHQUEUE-MANUAL-319545 :: [nzb.su] :: [1b1c26a] Transaction with 17 queries executed
2016-11-16 22:13:57 INFO     SEARCHQUEUE-MANUAL-319545 :: [Cpasbien] :: [1b1c26a] Performing episode search for The Traffickers
2016-11-16 22:13:57 DEBUG    SEARCHQUEUE-MANUAL-319545 :: [Cpasbien] :: [1b1c26a] Search mode: Episode
2016-11-16 22:13:57 DEBUG    SEARCHQUEUE-MANUAL-319545 :: [Cpasbien] :: [1b1c26a] Search string: The Traffickers S01E02
2016-11-16 22:13:58 DEBUG    Thread-15 :: [1b1c26a] Testing Pushbullet authentication and retrieving the device list.
2016-11-16 22:14:00 DEBUG    Thread-15 :: [1b1c26a] Trakt may have some issues and it's unavailable. Try again later please
2016-11-16 22:14:00 ERROR    Thread-15 :: [1b1c26a] Exception generated: Trakt may have some issues and it's unavailable. Try again later please
Traceback (most recent call last):
  File "/home/home/SickRage/medusa/server/web/core/base.py", line 271, in async_call
    result = function(**kwargs)
  File "/home/home/SickRage/medusa/server/web/home/handler.py", line 426, in getTraktToken
    response = trakt_api.validate_account()
  File "/home/home/SickRage/lib/traktor/trakt.py", line 173, in validate_account
    resp = self.request('users/settings')
  File "/home/home/SickRage/lib/traktor/trakt.py", line 143, in request
    raise UnavailableException(u"Trakt may have some issues and it\'s unavailable. Try again later please")
UnavailableException: Trakt may have some issues and it's unavailable. Try again later please

# Fixed code
def getTraktToken(trakt_pin=None):
    trakt_settings = {
        "trakt_api_key": app.TRAKT_API_KEY,
        "trakt_api_secret": app.TRAKT_API_SECRET,
    }
    trakt_api = TraktApi(app.SSL_VERIFY, app.TRAKT_TIMEOUT, **trakt_settings)
    response = None
    try:
        (access_token, refresh_token) = trakt_api.get_token(
            app.TRAKT_REFRESH_TOKEN, trakt_pin=trakt_pin
        )
        if access_token and refresh_token:
            app.TRAKT_ACCESS_TOKEN = access_token
            app.TRAKT_REFRESH_TOKEN = refresh_token
            response = trakt_api.validate_account()
    except MissingTokenException:
        ui.notifications.error("You need to get a PIN and authorize Medusa app")
        return "You need to get a PIN and authorize Medusa app"
    except TokenExpiredException:
        # Clear existing tokens
        app.TRAKT_ACCESS_TOKEN = ""
        app.TRAKT_REFRESH_TOKEN = ""
        ui.notifications.error(
            "TOKEN expired. Reload page, get a new PIN and authorize Medusa app"
        )
        return "TOKEN expired. Reload page, get a new PIN and authorize Medusa app"
    except TraktException:
        ui.notifications.error(
            "Connection error. Click 'Authorize Medusa' button again"
        )
        return "Connection error. Click 'Authorize Medusa' button again"
    if response:
        ui.notifications.message("Trakt Authorized")
        return "Trakt Authorized"
    ui.notifications.error("Connection error. Reload the page to get new token!")
    return "Trakt Not Authorized!"