-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9710eb4
commit 8498406
Showing
29 changed files
with
249 additions
and
28 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file added
BIN
+2.97 KB
tests/__responses__/12ef158cb1836d0c59e541b9214f829b8cc6ba63f1eeebbcb6e72a77947a9598.search
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file added
BIN
+2.83 KB
tests/__responses__/1d1887c576821bc21f11d997255183b5e84383c6b0d69bb4a066546dfdbf1e5f.search
Binary file not shown.
Binary file added
BIN
+71.8 KB
tests/__responses__/2444d7829ba9dd685da41ce76c36f98e6e048442d94667c9e79f55dedd4242d5.search
Binary file not shown.
Binary file added
BIN
+1.84 KB
tests/__responses__/5fd1cf7f7240a8a641ba44a0b2c694eca5630fba0e65320b77d1979f99b09f2b.search
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file added
BIN
+1.88 KB
tests/__responses__/9c1235763e8ac01b4c169c01144a5eaeab64b0ed455de071731ba0b6a4998ece.search
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
""" | ||
This file saves the httpx.Response object returned by the GET method. | ||
These saved responses are then used as mock responses in the tests. | ||
""" | ||
|
||
import pickle | ||
from hashlib import sha256 | ||
from pathlib import Path | ||
|
||
import httpx | ||
|
||
links = [ | ||
"https://nyaa.si/view/1422797", | ||
"https://nyaa.si/view/1544043", | ||
"https://nyaa.si/view/1586776", | ||
"https://nyaa.si/view/1694824", | ||
"https://nyaa.si/view/1755409", | ||
"https://nyaa.si/view/1765655", | ||
"https://nyaa.si/view/884488", | ||
# Called within .searc() | ||
"https://nyaa.si/view/1755409", | ||
"https://nyaa.si/view/1755409", | ||
"https://nyaa.si/view/1837736", | ||
"https://nyaa.si/view/1837420", | ||
] | ||
|
||
searches = { | ||
"akldlaskdjsaljdksd": "", | ||
"smol shelter": "https://nyaa.si?page=rss&f=0&c=0_0&q=smol%20shelter", | ||
'"[smol] Shelter (2016) (BD 1080p HEVC FLAC)"': "https://nyaa.si?page=rss&f=0&c=0_0&q=%22%5Bsmol%5D%20Shelter%20%282016%29%20%28BD%201080p%20HEVC%20FLAC%29%22", | ||
"vodes": "https://nyaa.si?page=rss&f=2&c=3_1&q=vodes", | ||
"mtbb": "https://nyaa.si?page=rss&f=0&c=1_2&q=mtbb", | ||
} | ||
|
||
|
||
def save_pages() -> None: | ||
for link in links: | ||
response = httpx.get(link) | ||
filename = Path(__file__).with_name(link.split("/")[-1]) | ||
|
||
print(f"Saving {link} ({response}) to {filename}") | ||
|
||
with open(filename, "wb") as file: | ||
pickle.dump(response, file=file) | ||
|
||
def save_torrents() -> None: | ||
for link in links: | ||
id = link.split("/")[-1] | ||
link = link.replace("view", "download") + ".torrent" | ||
response = httpx.get(link) | ||
filename = Path(__file__).with_name(f"{id}.torrent") | ||
|
||
print(f"Saving {link} ({response}) to {filename}") | ||
|
||
with open(filename, "wb") as file: | ||
pickle.dump(response, file=file) | ||
|
||
def save_searches() -> None: | ||
for query, url in searches.items(): | ||
|
||
response = httpx.get(url) | ||
name = sha256(query.encode()).hexdigest() | ||
filename = Path(__file__).with_name(f"{name}.search") | ||
|
||
print(f"Saving {url} ({response}) to {filename}") | ||
|
||
with open(filename, "wb") as file: | ||
pickle.dump(response, file=file) | ||
|
||
|
||
if __name__ == "__main__": | ||
save_pages() | ||
save_torrents() | ||
save_searches() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import pickle | ||
from hashlib import sha256 | ||
from pathlib import Path | ||
|
||
from httpx import Response | ||
|
||
BASE_DIR = Path(__file__).parent / "__responses__" | ||
|
||
# This workaround of loading a Response object and then re-casting is necessary because: https://github.com/lundberg/respx/issues/272 | ||
|
||
|
||
def get_response(nyaa_id: int) -> Response: | ||
""" | ||
Grab the response object corresponding to nyaa_id, | ||
unpickle it, and cast it in a new Response object. | ||
""" | ||
filename = BASE_DIR / str(nyaa_id) | ||
with open(filename, "rb") as file: | ||
response: Response = pickle.load(file) | ||
return Response(status_code=response.status_code, content=response.content) | ||
|
||
def get_torrent(nyaa_id: int) -> Response: | ||
""" | ||
Grab the response object of the torrent corresponding to nyaa_id, | ||
unpickle it, and cast it in a new Response object. | ||
""" | ||
filename = BASE_DIR / f"{nyaa_id}.torrent" | ||
with open(filename, "rb") as file: | ||
response: Response = pickle.load(file) | ||
return Response(status_code=response.status_code, content=response.content) | ||
|
||
def get_search(query: str) -> Response: | ||
""" | ||
Grab the response object of a search string, | ||
unpickle it, and cast it in a new Response object. | ||
This search string is saved with it's SHA265 digest being the filename. | ||
""" | ||
name = sha256(query.encode()).hexdigest() | ||
filename = BASE_DIR / f"{name}.search" | ||
with open(filename, "rb") as file: | ||
response: Response = pickle.load(file) | ||
return Response(status_code=response.status_code, content=response.content) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.