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

add timeout argument #30

Open
wants to merge 1 commit into
base: main
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
6 changes: 4 additions & 2 deletions scidownl/api/scihub.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ def scihub_download(
paper_type: str = 'doi',
scihub_url: str = None,
out: str = None,
proxies: dict = None
proxies: dict = None,
timeout: int = None,
) -> None:
"""Download a paper from SciHub.

Expand All @@ -33,6 +34,7 @@ def scihub_download(
source_type=paper_type,
scihub_url=scihub_url,
out=out,
proxies=proxies
proxies=proxies,
timeout=timeout
).run()

3 changes: 2 additions & 1 deletion scidownl/core/crawler.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,10 @@ def crawl(self) -> HtmlContent:
'request': self.source[self.source.type]
}
proxies = self.task.context.get('proxies', {}) if self.task is not None else {}
timeout = self.task.context.get('timeout', None) if self.task is not None else None
logger.info(f"<- Request: scihub_url={self.scihub_url}, source={self.source}, proxies={proxies}")

res = self.sess.post(self.scihub_url, data=request_params, proxies=proxies)
res = self.sess.post(self.scihub_url, data=request_params, proxies=proxies, timeout=timeout)
logger.info(f"-> Response: status_code={res.status_code}, content_length={len(res.content.decode())}")

if res.status_code not in ScihubCrawler.OK_STATUS_CODES:
Expand Down
3 changes: 2 additions & 1 deletion scidownl/core/downloader.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ def download(self, out: str) -> str:
try:
url = self.information.get_url()
proxies = self.task.context.get('proxies', {}) if self.task is not None else {}
res = requests.get(url, stream=True, proxies=proxies)
timeout = self.task.context.get('timeout', None) if self.task is not None else None
res = requests.get(url, stream=True, proxies=proxies, timeout=timeout)
total_length = res.headers.get('content-length')

with open(out, "wb") as f:
Expand Down
5 changes: 4 additions & 1 deletion scidownl/core/task.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ def __init__(self,
scihub_url: str = None,
scihub_url_chooser_cls=default_chooser_cls,
out: str = None,
proxies: dict = None):
proxies: dict = None,
timeout: int = None):
super().__init__()
self.source_keyword = source_keyword
self.scihub_url_chooser_cls = scihub_url_chooser_cls
Expand All @@ -37,8 +38,10 @@ def __init__(self,
self.source_class = source_classes.get(source_type, DoiSource)
self.out = out
self.proxies = proxies or {}
self.timeout = timeout
self.context['status'] = 'initialized'
self.context['proxies'] = self.proxies
self.context['timeout'] = timeout
self.service = ScihubUrlService()
self.updater = CrawlingScihubDomainUpdater()

Expand Down