diff --git a/fimfarchive/fetchers.py b/fimfarchive/fetchers.py index 2dcb242..e726b89 100644 --- a/fimfarchive/fetchers.py +++ b/fimfarchive/fetchers.py @@ -147,8 +147,8 @@ class FimfictionFetcher(Fetcher): prefetch_meta = True prefetch_data = False - data_path = 'https://www.fimfiction.net/download_story.php' - meta_path = 'https://www.fimfiction.net/api/story.php' + data_path = 'https://www.fimfiction.net/story/download/{}/html' + meta_path = 'https://www.fimfiction.net/api/story.php?story={}' flavors = frozenset(( StorySource.FIMFICTION, @@ -156,25 +156,28 @@ class FimfictionFetcher(Fetcher): MetaPurity.DIRTY, )) - def get(self, url, **kwargs): + def get(self, url): """ Performs an HTTP GET request. Args: url: Target of the HTTP request. - **kwargs: HTTP query parameters. Returns: Response: A new `Response` object. Raises: - StorySourceError: If the server does not return HTTP 200 OK. + InvalidStoryError: If access to the resource was denied. + StorySourceError: If the request fails for any other reason. """ try: - response = requests.get(url, params=kwargs, timeout=60) + response = requests.get(url, timeout=60) except OSError as e: raise StorySourceError("Could not read from server.") from e + if response.status_code == 403: + raise InvalidStoryError("Access to resource was denied.") + if not response.ok: raise StorySourceError( "Server responded with HTTP {} {}." @@ -184,13 +187,14 @@ class FimfictionFetcher(Fetcher): return response def fetch_data(self, key): - response = self.get(self.data_path, story=key, html=True) + url = self.data_path.format(key) + response = self.get(url) data = response.content if len(data) == 0: raise InvalidStoryError("Server returned empty response body.") - if b'

' not in data: + if b'

' not in data: raise InvalidStoryError("Server did not return any chapters.") if not data.endswith(b''): @@ -199,7 +203,8 @@ class FimfictionFetcher(Fetcher): return data def fetch_meta(self, key): - response = self.get(self.meta_path, story=key) + url = self.meta_path.format(key) + response = self.get(url) try: meta = response.json() diff --git a/tests/test_fetchers.py b/tests/test_fetchers.py index a6a908e..6a67cf8 100644 --- a/tests/test_fetchers.py +++ b/tests/test_fetchers.py @@ -35,7 +35,7 @@ INVALID_STORY_KEY = 7 EMPTY_STORY_KEY = 8 PROTECTED_STORY_KEY = 208799 -FIMFARCHIVE_PATH = 'fimfarchive-20161201.zip' +FIMFARCHIVE_PATH = 'fimfarchive-20170601.zip' class TestFetcher: @@ -205,6 +205,7 @@ class TestFimfictionFetcher: data = fetcher.fetch_data(VALID_STORY_KEY) assert len(data) != 0 + @pytest.mark.xfail(reason='knighty/fimfiction-issues#139') def test_fetch_data_for_invalid_story(self, fetcher): """ Tests `InvalidStoryError` is raised if story is invalid.