def test_set_apikey(self): client = podcast_api.Client() assert client.request_headers.get("X-ListenAPI-Key") is None api_key = "abcd" client = podcast_api.Client(api_key=api_key) assert client.request_headers.get("X-ListenAPI-Key") == api_key
def test_fetch_podcast_languages_with_mock(self): client = podcast_api.Client() response = client.fetch_podcast_languages() assert len(response.json().get("languages", [])) > 0 assert response.request.method == "GET" url = urlparse(response.url) assert url.path == "/api/v2/languages"
def test_just_listen_with_mock(self): client = podcast_api.Client() response = client.just_listen() assert response.json().get("audio_length_sec", 0) > 0 assert response.request.method == "GET" url = urlparse(response.url) assert url.path == "/api/v2/just_listen"
def test_trending_searches_with_mock(self): client = podcast_api.Client() response = client.fetch_trending_searches() assert len(response.json().get("terms", [])) > 0 assert response.request.method == "GET" url = urlparse(response.url) assert url.path == "/api/v2/trending_searches"
def test_fetch_podcast_regions_with_mock(self): client = podcast_api.Client() response = client.fetch_podcast_regions() assert len(response.json().get("regions", {}).keys()) > 0 assert response.request.method == "GET" url = urlparse(response.url) assert url.path == "/api/v2/regions"
def search(request): q = request.GET.get('q') sort_by_date = request.GET.get('sort_by_date') result_type = request.GET.get('type') offset = request.GET.get('offset', '0') try: # If api_key is None, then we'll connect to api mock servers which return fake data client = podcast_api.Client(api_key=LISTEN_API_KEY) response = client.search(q=q, sort_by_date=sort_by_date, type=result_type, offset=offset) except errors.APIConnectionError: return http.JsonResponse({}, status=503) except errors.AuthenticationError: return http.JsonResponse({}, status=401) except errors.InvalidRequestError: return http.JsonResponse({}, status=400) except errors.NotFoundError: return http.JsonResponse({}, status=404) except errors.RateLimitError: return http.JsonResponse({}, status=429) except errors.ListenApiError: return http.JsonResponse({}, status=500) else: return http.JsonResponse(response.json())
def test_fetch_episode_by_id_with_mock(self): client = podcast_api.Client() episode_id = "asdfsdaf" response = client.fetch_episode_by_id(id=episode_id) assert len(response.json().get("podcast", {}).get("rss")) > 0 assert response.request.method == "GET" url = urlparse(response.url) assert url.path == "/api/v2/episodes/%s" % episode_id
def test_fetch_podcast_by_id_with_mock(self): client = podcast_api.Client() podcast_id = "asdfsdaf" response = client.fetch_podcast_by_id(id=podcast_id) assert len(response.json().get("episodes", [])) > 0 assert response.request.method == "GET" url = urlparse(response.url) assert url.path == "/api/v2/podcasts/%s" % podcast_id
def test_fetch_my_playlists_with_mock(self): client = podcast_api.Client() page = 2 response = client.fetch_my_playlists(page=page) assert len(response.json().get("playlists", [])) > 0 assert response.request.method == "GET" url = urlparse(response.url) assert url.path == "/api/v2/playlists"
def test_delete_podcast_with_mock(self): client = podcast_api.Client() podcast_id = "asdfasdfdf" response = client.delete_podcast(id=podcast_id) assert len(response.json().get("status", "")) > 0 assert response.request.method == "DELETE" url = urlparse(response.url) assert url.path == "/api/v2/podcasts/%s" % podcast_id
def test_fetch_recommendations_for_episode_with_mock(self): client = podcast_api.Client() episode_id = "adfsddf" response = client.fetch_recommendations_for_episode(id=episode_id) assert len(response.json().get("recommendations", [])) > 0 assert response.request.method == "GET" url = urlparse(response.url) assert url.path == "/api/v2/episodes/%s/recommendations" % episode_id
def test_batch_fetch_episodes_with_mock(self): client = podcast_api.Client() ids = "996,777,888,100220" response = client.batch_fetch_episodes(ids=ids) assert parse_qs(response.request.body)["ids"][0] == ids assert len(response.json().get("episodes", [])) > 0 assert response.request.method == "POST" url = urlparse(response.url) assert url.path == "/api/v2/episodes"
def test_submit_podcast_with_mock(self): client = podcast_api.Client() rss = "http://myrss.com/rss" response = client.submit_podcast(rss=rss) assert parse_qs(response.request.body)["rss"][0] == rss assert len(response.json().get("status", "")) > 0 assert response.request.method == "POST" url = urlparse(response.url) assert url.path == "/api/v2/podcasts/submit"
def test_fetch_best_podcasts_with_mock(self): client = podcast_api.Client() genre_id = 23 response = client.fetch_best_podcasts(genre_id=genre_id) assert response.json().get("total", 0) > 0 assert response.request.method == "GET" url = urlparse(response.url) assert url.path == "/api/v2/best_podcasts" params = parse_qs(url.query) assert params["genre_id"][0] == str(genre_id)
def test_fetch_curated_podcasts_lists_with_mock(self): client = podcast_api.Client() page = 2 response = client.fetch_curated_podcasts_lists(page=page) assert response.json().get("total") > 0 assert response.request.method == "GET" url = urlparse(response.url) params = parse_qs(url.query) assert params["page"][0] == str(page) assert url.path == "/api/v2/curated_podcasts"
def test_related_searches_with_mock(self): client = podcast_api.Client() term = "dummy" response = client.fetch_related_searches(q=term) assert len(response.json().get("terms", [])) > 0 assert response.request.method == "GET" url = urlparse(response.url) assert url.path == "/api/v2/related_searches" params = parse_qs(url.query) assert params["q"][0] == term
def test_spellcheck_with_mock(self): client = podcast_api.Client() term = "dummy" response = client.spellcheck(q=term) assert len(response.json().get("tokens", [])) > 0 assert response.request.method == "GET" url = urlparse(response.url) assert url.path == "/api/v2/spellcheck" params = parse_qs(url.query) assert params["q"][0] == term
def test_fetch_podcast_genres_with_mock(self): client = podcast_api.Client() top_level_only = 1 response = client.fetch_podcast_genres(top_level_only=top_level_only) assert len(response.json().get("genres", [])) > 0 assert response.request.method == "GET" url = urlparse(response.url) params = parse_qs(url.query) assert params["top_level_only"][0] == str(top_level_only) assert url.path == "/api/v2/genres"
def test_typeahead_with_mock(self): client = podcast_api.Client() term = "dummy" show_podcasts = 1 response = client.typeahead(q=term, show_podcasts=show_podcasts) assert len(response.json().get("terms", [])) > 0 assert response.request.method == "GET" url = urlparse(response.url) assert url.path == "/api/v2/typeahead" params = parse_qs(url.query) assert params["q"][0] == term assert params["show_podcasts"][0] == "1"
def test_search_with_mock(self): client = podcast_api.Client() term = "dummy" sort_by_date = 1 response = client.search(q=term, sort_by_date=sort_by_date) assert len(response.json().get("results", [])) > 0 assert response.request.method == "GET" url = urlparse(response.url) assert url.path == "/api/v2/search" params = parse_qs(url.query) assert params["q"][0] == term assert params["sort_by_date"][0] == "1"
def test_search_with_authentication_error(self): api_key = "wrong key" client = podcast_api.Client(api_key=api_key) term = "dummy" sort_by_date = 1 try: client.search(q=term, sort_by_date=sort_by_date) except AuthenticationError: pass except Exception: assert False else: assert False
import json import os from listennotes import podcast_api, errors # Get your api key here: https://www.listennotes.com/api/dashboard/ api_key = os.environ.get("LISTEN_API_KEY", None) client = podcast_api.Client(api_key=api_key) # # Boilerplate to make an api call # try: response = client.typeahead(q="startup", show_podcasts=1) print(json.dumps(response.json(), indent=2)) except errors.APIConnectionError: print("Failed ot connect to Listen API servers") except errors.AuthenticationError: print("Wrong api key, or your account has been suspended!") except errors.InvalidRequestError: print("Wrong parameters!") except errors.NotFoundError: print("Endpoint not exist or the podcast / episode not exist!") except errors.RateLimitError: print("Reached your quota limit, or rate limit.") except errors.ListenApiError: print("Something wrong on Listen Notes servers") except Exception: print("Other errors that may not be related to Listen API") else: