Skip to content

Instantly share code, notes, and snippets.

@neckothy
Created August 5, 2023 19:33
Show Gist options
  • Save neckothy/d3a3ce387a4cca06c496ea60f08853ae to your computer and use it in GitHub Desktop.
Save neckothy/d3a3ce387a4cca06c496ea60f08853ae to your computer and use it in GitHub Desktop.
fetch and save full-size covers from bookwalker
#!/bin/python
import sys
import re
import requests
# https://github.com/qsniyg/maxurl/issues/17
# https://github.com/qsniyg/maxurl/commit/5d5a77c08f9461c5d7c4ceac209fca240b776d7b#diff-1bc842c9e7dda2f26048e68021e82a3c290fb38639d0acf259392b26f4664fc8R21594-R21613
def get_real_url(url):
if "rimg.bookwalker.jp" in url:
url = re.sub(
r":\/\/[^/]*\/([0-9]+)\/[0-9a-zA-Z_]+(\.[^/.]*)$",
r"://c.bookwalker.jp/\1/t_700x780\2",
url,
)
if "c.bookwalker.jp" in url:
match = re.search(r":\/\/[^/]*\/([0-9]+)\/[^/]*(?:[?#].*)?$", url)
if match:
number = match[1]
reversed_number = int(number[::-1]) - 1
url = (
"https://c.bookwalker.jp/coverImage_"
+ str(reversed_number)
+ re.sub(r".*(\.[^/.?#]*)(?:[?#].*)?$", r"\1", url)
)
return url
def save_img(url, name):
ext = url.rsplit(".", maxsplit=1)[1]
with requests.get(url) as response:
response.raise_for_status()
img = response.content
with open(f"{name}.{ext}", "wb") as f:
f.write(img)
if __name__ == "__main__":
arg_count = len(sys.argv)
if arg_count == 1:
sys.exit("no url supplied")
input_url = sys.argv[1] if arg_count > 1 else None
file_name = sys.argv[2] if arg_count > 2 else None
if "rimg.bookwalker.jp" in input_url or "c.bookwalker.jp" in input_url:
cover_url = get_real_url(input_url)
else:
sys.exit("invalid url supplied")
if file_name:
save_img(cover_url, file_name)
else:
print(cover_url)
@neckothy
Copy link
Author

neckothy commented Aug 5, 2023

right click -> copy image link -> bwcover.py <url> <filename>
bwcover.py <url> cool-cover -> saves cool-cover.<ext>
bwcover.py <url> -> prints full-size url

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment