Skip to content

Commit

Permalink
Add session to fix timeout for requesting ln
Browse files Browse the repository at this point in the history
  • Loading branch information
quantrancse committed Oct 25, 2021
1 parent 0de58dc commit 13045b6
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 11 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ python hako2epub.py -u light_novel_url

* I only tested on some of my favorite light novels.
* Sometime can not get images from some image hosts.
* Sometimes it will take a long time to download or update the light novels (maybe only the first light novel in the list). Most cases are under 10 seconds. If you are over that time, you should use a VPN (1.1.1.1 Cloudflare WARP) to avoid this.
* If you update the light novel that was renamed, it will download the whole light novel again. To avoid this, please manually rename the path of the epub file to the new light novel name exactly like the current name format. Also rename the light novel in the `ln_info.json` file.

<!-- CONTRIBUTING -->
Expand Down
25 changes: 14 additions & 11 deletions hako2epub.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@
HEADERS = {
'user-agent': ('Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.47 Safari/537.36')}

tool_version = '2.0.1'
tool_version = '2.0.2'
bs4_html_parser = 'html.parser'
ln_request = requests.Session()


def print_format(name='', info='', info_style='bold fg:orange', prefix='! '):
Expand All @@ -32,7 +33,7 @@ def check_for_tool_updates():
try:
release_api = 'https://api.github.com/repos/quantrancse/hako2epub/releases/latest'
response = requests.get(
release_api, headers=HEADERS, timeout=10).json()
release_api, headers=HEADERS, timeout=5).json()
latest_release = response['tag_name'][1:]
if tool_version != latest_release:
print_format('Current tool version: ',
Expand Down Expand Up @@ -84,8 +85,8 @@ def get_image(self, image_url):
if 'imgur.com' in image_url and '.' not in image_url[-5:]:
image_url += '.jpg'
try:
image = Image.open(requests.get(
image_url, headers=HEADERS, stream=True, timeout=10).raw).convert('RGB')
image = Image.open(ln_request.get(
image_url, headers=HEADERS, stream=True, timeout=5).raw).convert('RGB')
except Exception:
print('Can not get image: ' + image_url)
return image
Expand Down Expand Up @@ -118,7 +119,7 @@ def check_update_ln(self, old_ln, mode=''):
print_format('Checking update: ', old_ln.get('ln_name'))
old_ln_url = old_ln.get('ln_url')
try:
request = requests.get(old_ln_url, headers=HEADERS, timeout=10)
request = ln_request.get(old_ln_url, headers=HEADERS, timeout=5)
soup = BeautifulSoup(request.text, bs4_html_parser)
new_ln = LNInfo()
new_ln = new_ln.get_ln_info(old_ln_url, soup, 'update')
Expand Down Expand Up @@ -438,8 +439,8 @@ def make_chapter_content(self, chapter_list):
i = chapter_list[0]
chapter_url = chapter_list[1]

request = requests.get(
chapter_url, headers=HEADERS, timeout=10)
request = ln_request.get(
chapter_url, headers=HEADERS, timeout=5)
soup = BeautifulSoup(request.text, bs4_html_parser)

xhtml_file = 'chap_%s.xhtml' % str(i + 1)
Expand Down Expand Up @@ -519,8 +520,8 @@ def bind_epub_book(self):
self.book.add_item(intro_page)

try:
self.book.set_cover('cover.jpeg', requests.get(
self.volume.cover_img, headers=HEADERS, stream=True, timeout=10).content)
self.book.set_cover('cover.jpeg', ln_request.get(
self.volume.cover_img, headers=HEADERS, stream=True, timeout=5).content)
except Exception:
print('Error: Can not set cover image!')
print('--------------------')
Expand Down Expand Up @@ -755,7 +756,8 @@ def set_ln_volume_chapter_list(self):
def set_ln_volume_list(self, volume_urls):
for volume_url in volume_urls:
try:
request = requests.get(volume_url, headers=HEADERS, timeout=10)
request = ln_request.get(
volume_url, headers=HEADERS, timeout=5)
soup = BeautifulSoup(request.text, bs4_html_parser)
self.volume_list.append(Volume(volume_url, soup))
except Exception:
Expand Down Expand Up @@ -820,7 +822,8 @@ def start(self, ln_url, mode):
UpdateLN().check_update(ln_url)
else:
try:
request = requests.get(ln_url, headers=HEADERS, timeout=10)
request = ln_request.get(
ln_url, headers=HEADERS, timeout=5)
soup = BeautifulSoup(request.text, bs4_html_parser)
if not soup.find('section', 'volume-list'):
print('Invalid url. Please try again.')
Expand Down

0 comments on commit 13045b6

Please sign in to comment.