def add_show_intent_handler(handler_input):
    """ handler for Add Show Intent"""

    h = handler_input.request_envelope.context.system.user.access_token
    showtype = 'show'
    # If we are not auth, let the user know
    if h is None:
        reprompt = language.AUTH_ERROR
        handler_input.response_builder.speak(reprompt).ask(reprompt)
        return handler_input.response_builder.response
    headers = utils.build_headers(h, clientid)

    # get our persistent_attributes
    _perattr = handler_input.attributes_manager.persistent_attributes
    movie = get_slot_value(handler_input=handler_input, slot_name="showName")
    user_list = get_slot_value(handler_input=handler_input, slot_name="list_name")
    _list, _usecustomlist = utils.get_list(user_list, _perattr)
    # search for move and get the object
    b = trakt_api.search(movie, headers, showtype, False)
    if b['error']:
        # handle this
        handler_input.response_builder.speak(language.SHOW_404)
        return handler_input.response_builder.response

    y = b['show']
    # dig through our search and add the movie/show to our list or our Watchlist
    trakt_api.parse_search(b['type'], headers, y, _list, _usecustomlist, True)
    utils.notify(movie, b['type'], _list)
    handler_input.response_builder.speak(movie + " show has been added to your list " + str(_list))
    # .ask(reprompt)
    return handler_input.response_builder.response
def find_movie_handler(handler_input):
    """Handler for Find Movie Intent"""

    # Get the value of the users auth token
    h = handler_input.request_envelope.context.system.user.access_token
    # If we are not auth, let the user know
    if h is None:
        handler_input.response_builder.speak(language.AUTH_ERROR).ask(language.AUTH_ERROR)
        return handler_input.response_builder.response
    # Set all our headers for the trakt-api
    headers = utils.build_headers(h, clientid)
    movie = get_slot_value(handler_input=handler_input, slot_name="movieName")
    # TODO search the movie var and strip  "on my list" from the end incase Alexa f***s up
    #
    b = trakt_api.search(movie, headers, "movie", True)
    if b['error']:
        # handle this
        reprompt = '_'
        handler_input.response_builder.speak("I couldnt find the movie you requested").ask(reprompt)
        return handler_input.response_builder.response
    # check if its a show or a movie
    # force our movie/show object into a small var to make things easier
    y = b["movie"]

    t = trakt_api.search_lists(y, b, headers, "movie")
    if t['found']:
        # print(movie+" found on the list "+t['list'])
        reprompt = movie + " is already on the list " + t['list']
        handler_input.response_builder.speak(reprompt).ask(reprompt)
        return handler_input.response_builder.response
    else:
        reprompt = movie + " isn't on any of your lists."
        handler_input.response_builder.speak(reprompt).ask(reprompt)
        return handler_input.response_builder.response
def add_movie_intent_handler(handler_input):
    """Handler for Add Movie Intent."""

    # get our persistent_attributes
    # if the user has launched the app greet them
    # set out session attributes
    _perattr = handler_input.attributes_manager.persistent_attributes
    attr = handler_input.attributes_manager.session_attributes
    if is_request_type("LaunchRequest")(handler_input):
        # _usecustomlist = _perattr['usecustomlist']
        attr["movie"] = {}
        attr["show"] = {}
        attr['readBoxOffice'] = False
        attr['readMovies'] = False
        attr['readShows'] = False
        attr['readBoth'] = False
        attr['active_request'] = ''
        attr['repeat'] = ''
        handler_input.response_builder.speak("Welcome To Radar the Trakt.tv tracker").ask("")
        return handler_input.response_builder.response

    # Get the value of the users auth token
    h = handler_input.request_envelope.context.system.user.access_token
    # _list = 'watchlist'
    _usecustomlist = False
    # If we are not auth, let the user know
    if h is None:
        handler_input.response_builder.speak(language.AUTH_ERROR)
        return handler_input.response_builder.response
    # Set all our headers for the trakt-api
    headers = utils.build_headers(h, clientid)
    print("Header= " + str(headers))
    # Get the movie name and throw it onto the movie var
    movie = get_slot_value(handler_input=handler_input, slot_name="movieName")
    use_list = get_slot_value(handler_input=handler_input, slot_name="list_name")
    # reprompt = "Are you sure you want to add "+movie+' to your list ?'
    # user gave us nothing lets do some checks to make sure we have saved attributes
    _list, _usecustomlist = utils.get_list(use_list, _perattr)
    # search for move and get the object
    b = trakt_api.search(movie, headers, "movie", True)
    if b['error']:
        # handle this
        handler_input.response_builder.speak("I couldn't find the show you requested")
        return handler_input.response_builder.response
    # force our movie/show object into a small var to make things easier
    y = b["movie"]
    # dig through our search and add the movie/show to our list or our Watchlist
    if trakt_api.parse_search(b['type'], headers, y, _list, _usecustomlist, True):
        # media_name, media_type, a_list
        utils.notify(movie, b['type'], _list)
        handler_input.response_builder.speak(movie + " has been added to your " + _list + " list")  # .ask(reprompt)
    else:
        # TODO Fix the notify to allow errors
        # utils.notify(movie, b['type'], _list)
        handler_input.response_builder.speak("There was a problem adding " + movie + " to your list " + _list)
    return handler_input.response_builder.response
def remove_movie_intent_handler(handler_input):
    # Get the value of the users auth token
    h = handler_input.request_envelope.context.system.user.access_token
    # If we are not auth, let the user know
    if h is None:
        handler_input.response_builder.speak(language.AUTH_ERROR).ask(language.AUTH_ERROR)
        return handler_input.response_builder.response
    # Set all our headers for the trakt-api
    headers = utils.build_headers(h, clientid)
    # TODO make sure we change I,II,II type movies to 1,2,3
    # and vice versa
    user_list = get_slot_value(handler_input=handler_input, slot_name="list_name")
    movie = str(get_slot_value(handler_input=handler_input, slot_name="movieName"))

    # get our persistent_attributes
    attr = handler_input.attributes_manager.persistent_attributes
    _list, _usecustomlist = utils.get_list(user_list, attr)

    # if our list isnt empty then we can go ahead amd deal with the request
    if _usecustomlist:
        url = 'https://api.trakt.tv/users/me/lists/' + _list + '/items/movies'
        r = requests.get(url, headers=headers)

        if r.status_code == 200 or r.status_code == 201:
            dcode = json.loads(r.text)
            # print(json.dumps(json.loads(r.text), sort_keys=True, indent=4))
            i = 0
            _moviefound = False
            while i < len(dcode):
                # print(dcode[i]['name'])
                # print(json.dumps(dcode[i], sort_keys=True, indent=4))
                o = dcode[i]["movie"]['title']
                # print(str(o) + " is our title")
                # if our movie name matches the movie send the request to delete it
                if o.lower() == movie.lower():
                    _moviefound = True
                    # print("we found it")
                    # print(json.dumps(dcode[i], sort_keys=True, indent=4))
                    if trakt_api.parse_delete_search("movie", headers, dcode[i]["movie"], _list, _usecustomlist):
                        handler_input.response_builder.speak(f"I have deleted {o} from the list {_list}")
                        return handler_input.response_builder.response
                        # return  # print("we finished and deleted")  # exit("deleted")
                    else:
                        # return
                        handler_input.response_builder.speak(f"I had trouble deleting {o} from the list {_list}")
                        return handler_input.response_builder.response
                        # print("we found the film but there was an error deleting")  # exit("not deleted")
                i += 1
            # if we failed to find the movie
            if _moviefound is False:
                # print("we couldnt find the film")
                handler_input.response_builder.speak(f"I couldn't find {movie} on the list {_list}")
                return handler_input.response_builder.response
        # if our first request to trakt fails
        else:
            handler_input.response_builder.speak("I couldn't contact Trakt.tv API ." + url)
            return handler_input.response_builder.response
    # if our user didnt give us a list or they are using the watch list
    else:
        # WE DIDNT RECIEVE A LIST
        # TODO make sure we change I,II,II type movies to 1,2,3
        # and vice versa
        # search for movie and get the object
        b = trakt_api.search(movie, headers, "movie", False)
        if b['error']:
            # handle this
            reprompt = "I couldn't find the movie you requested"
            handler_input.response_builder.speak(reprompt).ask(reprompt)
            return handler_input.response_builder.response
        # force our movie/show object into a small var to make things easier
        y = b["movie"]
        if trakt_api.parse_delete_search("movie", headers, y, _list, False):
            # media_name, media_type, a_list
            utils.notify(movie, b['type'], _list, "removed")
            handler_input.response_builder.speak(f"I have deleted {movie} from the list {_list}")
            return handler_input.response_builder.response
        else:
            handler_input.response_builder.speak(f"I couldn't delete {movie} from the list {_list}")
            return handler_input.response_builder.response