Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reduce risk of false positives in the link_replace method #308

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions safaribooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -613,11 +613,21 @@ def url_is_absolute(url):
def is_image_link(url: str):
return pathlib.Path(url).suffix[1:].lower() in ["jpg", "jpeg", "png", "gif"]

@staticmethod
def is_html_link(url: str):
return pathlib.Path(url).suffix[1:].lower().split('#')[0] in ["htm", "html", "xhtml"]

@staticmethod
def is_image_implied(url: str):
return any(x in url for x in ["cover", "images", "graphics"])

def is_possible_image(self, link):
return self.is_image_link(link) or (not self.is_html_link(link) and self.is_image_implied(link))

def link_replace(self, link):
if link and not link.startswith("mailto"):
if not self.url_is_absolute(link):
if any(x in link for x in ["cover", "images", "graphics"]) or \
self.is_image_link(link):
if (self.is_possible_image(link)):
image = link.split("/")[-1]
return "Images/" + image

Expand Down