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

adding proxy auth to pypub #24

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
16 changes: 11 additions & 5 deletions pypub/chapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import html
import os.path
import urllib.request
import requests
from io import BytesIO
from dataclasses import dataclass
from typing import Optional
Expand Down Expand Up @@ -42,13 +43,16 @@ def __repr__(self) -> str:

#** Functions **#

def urlrequest(url: str, timeout: int = 10):
def urlrequest(url: str, timeout: int = 10, proxies: object = None):
"""
complete a url-request to the specified url
"""

headers = {'User-Agent': user_agent}
req = urllib.request.Request(url, headers=headers)
return urllib.request.urlopen(req, timeout=timeout)

response = requests.get(url, headers=headers, proxies=proxies, timeout = timeout)

return response

def htmltostring(root: pyxml.html.HtmlElement) -> bytes:
"""
Expand Down Expand Up @@ -189,6 +193,7 @@ def create_chapter_from_url(
title: Optional[str] = None,
title_xpath: Optional[str] = None,
content_xpath: Optional[str] = None,
proxies: Optional[object] = None
) -> Chapter:
"""
generate a chapter object from the given file
Expand All @@ -197,9 +202,10 @@ def create_chapter_from_url(
:param title: title used for the given chpater
:param title_xpath: xpath used to find title in html
:param content_xpath: xpath used to find content in html
:param proxies: proxy object for auth
:param factory: chapter factory override (for customization)
"""
res = urlrequest(url, timeout=10)
html = convert_content(url, res.read())
res = urlrequest(url, timeout=10, proxies = proxies)
html = convert_content(url, res.content)
return create_chapter_from_html(html,
title, url, title_xpath, content_xpath)
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ jinja2>=3.1.2
dataclasses>=0.6
pillow>=10.0.0
filetype>=1.2.0
requests>=2.0.0