def test_user_auth(): # try to get the username from environment variable LBXD_USERNAME = os.environ.get("LBXD_USERNAME", None) class UsernameMissingError(Exception): pass if LBXD_USERNAME is None: raise UsernameMissingError( "Auth methods require a Letterboxd username.") # try to get the user password from environment variable LBXD_PASSWORD = os.environ.get("LBXD_PASSWORD", None) class PasswordMissingError(Exception): pass if LBXD_PASSWORD is None: raise PasswordMissingError( "Auth methods require a Letterboxd password.") lbxd = Letterboxd() # make login user = lbxd.auth() assert isinstance != None user.login(LBXD_USERNAME, LBXD_PASSWORD) logging.debug("user.token: {}".format(user.token)) assert isinstance(user.token, str)
def test_member_watchlist(): # set up LBXD_USERNAME, LBXD_PASSWORD = load_user_pass() lbxd = Letterboxd() test_user = lbxd.user(LBXD_USERNAME, LBXD_PASSWORD) # get the watchlist member_id = test_user.me["member"]["id"] logging.debug(f"member_id: {member_id}") assert isinstance(member_id, str) member = lbxd.member(member_id=member_id) logging.debug(f"member: {member}") assert isinstance(member, Member) # watchlist_request watchlist_request = {"perPage": 20} films_response = member.watchlist(watchlist_request=watchlist_request) logging.debug(f"films_response (watchlist): {films_response}") assert isinstance(films_response, dict) assert set(films_response_keys()).issubset( films_response.keys()), "All keys should be in the FilmsResponse" # Test the first movie in the watchlist if len(films_response["items"]) > 0: film_summary = films_response["items"][0] logging.debug(f"film_summary: {film_summary}") logging.debug(f"film_summary.keys(): {film_summary.keys()}") assert set(film_summary_keys()).issubset( film_summary.keys()), "All keys should be in the FilmSummary"
def test_film_availability(): # This data is first-party only, so it doesn't return any data for normal users lbxd = Letterboxd() film_instance = lbxd.film(film_id="2bbs") # Raiders of the Lost Ark film_availability_response = film_instance.availability() logging.debug(f"film_availability_response: {film_availability_response}") assert isinstance(film_availability_response, dict)
def test_films(): """ Test API call to /films """ lbxd = Letterboxd() films_request = { "perPage": 25, "sort": "ReleaseDateEarliestFirst", # "filmId": ["2bbs", "imdb:tt0087469", "tmdb:89"], # "genre": "9a", # Science Fiction # "decade": 1980, # "year": 1989, # "service": "amazon", # Amazon # "where": ["NotReleased", "InWatchlist"], "member": "3P", "memberRelationship": "Favorited", "includeFriends": "All", # "tagCode": "stubs", # "tagger": "11Ht", # "includeTaggerFriends": "Only", } films = lbxd.films() films_response = films.films(films_request=films_request) logging.debug(f"films_response: {films_response}") assert isinstance(films_response, dict) assert set(films_response_keys()).issubset( films_response.keys() ), "All keys should be in FilmsResponse" # Debug print a simple list of the movies film_num = 1 for film in films_response["items"]: logging.debug(f"{film_num}. {film['name']}") film_num += 1
def test_user_me(): LBXD_USERNAME, LBXD_PASSWORD = load_user_pass() lbxd = Letterboxd() # login test_user = lbxd.user(LBXD_USERNAME, LBXD_PASSWORD) me_dict = test_user.me logging.debug(f"me_dict: {me_dict}") assert isinstance(me_dict, dict)
def test_user_auth_bad(): LBXD_USERNAME = "******" LBXD_PASSWORD = "******" lbxd = Letterboxd() # make login try: lbxd.user(LBXD_USERNAME, LBXD_PASSWORD) except Exception as e: logging.error(e.args)
def test_user_auth(): LBXD_USERNAME, LBXD_PASSWORD = load_user_pass() lbxd = Letterboxd() # make login test_user = lbxd.user(LBXD_USERNAME, LBXD_PASSWORD) assert isinstance(test_user, User) test_token = test_user.token logging.debug(f"test_user.token: {test_token}") assert isinstance(test_token, str) assert lbxd.api.user.token is test_token
def test_film_details(): """Tests API call to get a film's details""" # Assume use of environment variables for api key and secret lbxd = Letterboxd() film_instance = lbxd.film(film_id="2bbs") # Raiders of the Lost Ark assert isinstance(film_instance, Film) film = film_instance.details() logging.debug(f"film: {film}") assert isinstance(film, dict) assert film["id"] == "2bbs", "The ID should be in the response" assert set(film_keys()).issubset(film.keys()), "All keys should be in Film"
def test_film_report(): LBXD_USERNAME, LBXD_PASSWORD = load_user_pass() lbxd = Letterboxd() # login, even though we don't use this value lbxd.user(LBXD_USERNAME, LBXD_PASSWORD) film_instance = lbxd.film(film_id="9mMS") # Kirk Cameron’s Saving Christmas report_film_request = {"reason": "Other", "message": "TEST — IGNORE"} success = film_instance.report( film_id="9mMS", report_film_request=report_film_request ) logging.debug(f"success: {success}") assert success is True
def test_film_patch_me(): LBXD_USERNAME, LBXD_PASSWORD = load_user_pass() lbxd = Letterboxd() # login lbxd.user(LBXD_USERNAME, LBXD_PASSWORD) # Test the film with a movie this user hasn't seen, and isn't likely to ever see. film_instance = lbxd.film(film_id="1HIc") # Shark Attack 3: Megalodon # Add it to my watchlist film_relationship_update_request = {"inWatchlist": True} film_relationship_update_response = film_instance.me_update( film_relationship_update_request=film_relationship_update_request ) logging.debug( f"film_relationship_update_response: {film_relationship_update_response}" ) assert isinstance(film_relationship_update_response, dict) assert set(film_relationship_update_response_keys()).issubset( film_relationship_update_response.keys() ), "All keys should be in FilmRelationshipUpdateResponse" assert isinstance(film_relationship_update_response["data"], dict) film_relationship = film_relationship_update_response["data"] assert set(film_relationship_keys()).issubset( film_relationship.keys() ), "All keys should be in FilmRelationship" assert isinstance(film_relationship_update_response["messages"], list) # Mark it watched, liked, and rate it film_relationship_update_request = {"watched": True, "liked": True, "rating": 2.5} film_relationship_update_response = film_instance.me_update( film_relationship_update_request=film_relationship_update_request ) logging.debug( f"film_relationship_update_response: {film_relationship_update_response}" ) assert isinstance(film_relationship_update_response, dict) # Remove activity and reset rating film_relationship_update_request = { "watched": False, "liked": False, "inWatchlist": False, "rating": "null", # I had this as Null, but my params cleaner was stripping it out. } film_relationship_update_response = film_instance.me_update( film_relationship_update_request=film_relationship_update_request ) logging.debug( f"film_relationship_update_response: {film_relationship_update_response}" ) assert isinstance(film_relationship_update_response, dict)
def test_films_genres(): """ Test API call to /films/genres """ lbxd = Letterboxd() films = lbxd.films() genres_response = films.genres() logging.debug(f"genres_response: {genres_response}") assert isinstance(genres_response, dict) assert set(genres_response_keys()).issubset( genres_response.keys() ), "All keys should be in GenresResponse" genre = genres_response["items"][0] logging.debug(f"genre: {genre}") assert set(genre_keys()).issubset(genre.keys()), "All keys should be in the Genre"
def test_film_info(): """Tests API call to get a film's info""" # Assume use of environment variables for api key and secret lbxd = Letterboxd() film_instance = lbxd.film(film_id="2bbs") # Raiders of the Lost Ark response = film_instance.info() logging.debug("response: {}".format(response)) assert isinstance(response, requests.Response) assert response.status_code == 200 response_json = response.json() logging.debug("response_json: {}".format(response_json)) assert isinstance(response_json, dict) assert response_json["id"] == "2bbs", "The ID should be in the response" assert set(film_keys()).issubset( response_json.keys()), "All keys should be in the response"
def test_films_services(): """ Test API call to /films/film-services """ lbxd = Letterboxd() # login, so that we can see all of the services available to this member LBXD_USERNAME, LBXD_PASSWORD = load_user_pass() lbxd.user(LBXD_USERNAME, LBXD_PASSWORD) films = lbxd.films() film_services_response = films.services() logging.debug(f"film_services_response: {film_services_response}") assert isinstance(film_services_response, dict) assert set(film_services_response_keys()).issubset( film_services_response.keys() ), "All keys should be in FilmServicesResponse" assert isinstance(film_services_response["items"], list) service = film_services_response["items"][0] assert set(service_keys()).issubset(service.keys()), "All keys should be in Service"
def new(api_base=API_BASE_URL, api_key="", api_secret=""): """ Create a new instance of the Letterboxd class :param api_base: str - the base URL of the API endpoints, including version number :param api_key: str - API key provided by Letterboxd :param api_secret: str - API shared secret provided by Letterboxd :return: Letterboxd instance """ lbxd = Letterboxd(api_base=api_base, api_key=api_key, api_secret=api_secret) return lbxd
def test_film_members(): LBXD_USERNAME, LBXD_PASSWORD = load_user_pass() lbxd = Letterboxd() # login lbxd.user(LBXD_USERNAME, LBXD_PASSWORD) film_instance = lbxd.film(film_id="2bbs") # Raiders of the Lost Ark member_film_relationships_request = { "perPage": 100, "sort": "Name", "member": "11Ht", "memberRelationship": "IsFollowedBy", "filmRelationship": "Liked", } member_film_relationships_response = film_instance.members( member_film_relationships_request=member_film_relationships_request ) logging.debug( f"member_film_relationships_response: {member_film_relationships_response}" ) logging.debug( f"member_film_relationships_response.keys(): {member_film_relationships_response.keys()}" ) assert isinstance(member_film_relationships_response, dict) assert set(member_film_relationships_response_keys()).issubset( member_film_relationships_response.keys() ), "All keys should be in MemberFilmRelationshipsResponse" assert isinstance(member_film_relationships_response["items"], list) member_film_relationship = member_film_relationships_response["items"][0] logging.debug(f"member_film_relationship: {member_film_relationship}") assert isinstance(member_film_relationship["member"], dict) member_summary = member_film_relationship["member"] logging.debug(f"member_summary: {member_summary}") assert set(member_summary_keys()).issubset( member_summary.keys() ), "All keys should be in MemberSummary" assert isinstance(member_film_relationship["relationship"], dict) film_relationship = member_film_relationship["relationship"] logging.debug(f"film_relationship: {film_relationship}") assert set(film_relationship_keys()).issubset( film_relationship.keys() ), "All keys should be in FilmRelationship"
def test_search(): LBXD_USERNAME, LBXD_PASSWORD = load_user_pass() lbxd = Letterboxd() search_request = { "perPage": 5, "input": "smith", "searchMethod": "FullText", "include": "ContributorSearchItem", "contributionType": "Director", } search_response = lbxd.search(search_request=search_request) logging.debug(f"search_response: {search_response}") assert isinstance(search_response, dict) # TODO: test returned keys assert set(search_response_keys()).issubset( search_response.keys()), "All keys should be in SearchResponse" abstract_search_item = search_response["items"][0] assert set(abstract_search_item_keys()).issubset(abstract_search_item.keys( )), "All keys should be in the AbstractSearchItem"
def test_user_me_update(): """ :return: """ # login LBXD_USERNAME, LBXD_PASSWORD = load_user_pass() lbxd = Letterboxd() test_user = lbxd.user(LBXD_USERNAME, LBXD_PASSWORD) # test member_settings_update_request = { "emailWhenFollowed": True, "emailComments": True, "emailNews": True, "emailRushes": True, } member_settings_update_response = test_user.me_update( member_settings_update_request=member_settings_update_request ) logging.debug(f"member_settings_update_response: {member_settings_update_response}") assert isinstance(member_settings_update_response, dict) assert set(member_settings_update_response_keys()).issubset( member_settings_update_response.keys() ), "All keys should be in MemberSettingsUpdateResponse"
def test_film_me(): LBXD_USERNAME, LBXD_PASSWORD = load_user_pass() lbxd = Letterboxd() # login lbxd.user(LBXD_USERNAME, LBXD_PASSWORD) film_instance = lbxd.film(film_id="2bbs") # Raiders of the Lost Ark film_relationship = film_instance.me() logging.debug(f"film_relationship 1: {film_relationship}") assert isinstance(film_relationship, dict) assert set(film_relationship_keys()).issubset( film_relationship.keys() ), "All keys should be in FilmRelationship, against film with relationship" # test against film with no relationships film_instance = lbxd.film(film_id="Xwa") # Shark Attack 2 film_relationship = film_instance.me() logging.debug(f"film_relationship 2: {film_relationship}") assert set(film_relationship_keys()).issubset( film_relationship.keys() ), "All keys should be in FilmRelationship, against film with no relationship"
from letterboxd.letterboxd import Letterboxd # Assuming use of environment variables: lbxd = Letterboxd() # If not using environment variables, instead instantiate Letterboxd() with your secrets: # API_KEY = 'YOUR_KEY_HERE' # API_SECRET = 'YOUR_SECRET_HERE' # lbxd = Letterboxd(api_key=API_KEY, api_secret=API_SECRET) # FIXME: ruby implementation # lbxd.login 'YOUR_USERNAME_HERE', 'YOUR_PASSWORD_HERE' # helpers.show_json(lbxd.get_me)
def test_letterboxd(): lbxd = Letterboxd() assert isinstance(lbxd, Letterboxd)