Skip to content

Commit

Permalink
Added download to file with headers and cookies options
Browse files Browse the repository at this point in the history
  • Loading branch information
sirfoga committed Aug 19, 2017
1 parent a8b42c5 commit 7a4d136
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 11 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ All notable changes to this project will be documented in this file.
### Added
- language option in github api
- async utils
- download to file with headers and cookies options

### Removed
- print_item_info() in time.profile
Expand Down
27 changes: 17 additions & 10 deletions hal/internet/web.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,11 @@
r"(?:/?|[/?]\S+)$",
re.IGNORECASE
)
APP_VALID_HEADERS = {
"User-Agent": "Mozilla/5.0",
"Accept": "text/html,application/xhtml+xml,application/xml,"
"application/pdf;q=0.9,*/*;q=0.8 "
}


def is_url(candidate_url):
Expand Down Expand Up @@ -263,26 +268,28 @@ def download_url(url, local_file):
downloader.retrieve(url, local_file)


def download_pdf_to_file(url, local_file, chunk_size=1024):
def download_to_file(url, local_file, headers=APP_VALID_HEADERS, cookies=None,
chunk_size=1024):
"""
:param url: string
:param url: str
PDF url to download
:param local_file: string
:param local_file: str
Save url as this path
:param headers: {}
Headers to fetch url
:param cookies: {}
Cookies to fetch url
:param chunk_size: int
Download file in this specific chunk size
:return: void
Download link to local file
"""

headers = {
'User-Agent': 'Mozilla/5.0',
'Accept': 'text/html,application/xhtml+xml,application/xml,'
'application/pdf;q=0.9,*/*;q=0.8 '
}
if not cookies:
cookies = {}

req = requests.get(url, headers=headers, stream=True)
with open(local_file, 'wb') as local_download:
req = requests.get(url, headers=headers, cookies=cookies, stream=True)
with open(local_file, "wb") as local_download:
for chunk in req.iter_content(chunk_size):
if chunk:
local_download.write(chunk)
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@

setup(
name="PyHal",
version="4.6.1",
version="4.6.3",
author="sirfoga",
author_email="sirfoga@protonmail.com",
description="A multipurpose library to perform great stuff in the most "
Expand Down

0 comments on commit 7a4d136

Please sign in to comment.