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

Catch requests.exceptions.ChunkedEncodingError and similar #20

Merged
merged 3 commits into from
Sep 1, 2016
Merged
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
2 changes: 1 addition & 1 deletion waybackpack/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
from .asset import Asset
from .pack import Pack
from .cdx import search
__version__ = "0.3.2"
from .version import __version__
1 change: 1 addition & 0 deletions waybackpack/asset.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ def fetch(self,
url = self.get_archive_url(raw)
res = session.get(url)
content = res.content
if content == None: return

if raw:
return content
Expand Down
16 changes: 15 additions & 1 deletion waybackpack/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,18 @@
from .session import Session
from .pack import Pack
from .cdx import search
from .version import __version__
from .settings import DEFAULT_USER_AGENT, DEFAULT_ROOT
import argparse
import logging

def parse_args():
parser = argparse.ArgumentParser()

parser.add_argument("--version",
action="version",
version="%(prog)s " + __version__)

parser.add_argument("url",
help="The URL of the resource you want to download.")

Expand Down Expand Up @@ -48,6 +54,10 @@ def parse_args():
parser.add_argument("--collapse",
help="An archive.org `collapse` parameter. Cf.: https://github.com/internetarchive/wayback/blob/master/wayback-cdx-server/README.md#collapsing")

parser.add_argument("--ignore-errors",
help="Don't crash on non-HTTP errors e.g., the requests library's ChunkedEncodingError. Instead, log error and continue. Cf. https://github.com/jsvine/waybackpack/issues/19",
action="store_true")

parser.add_argument("--quiet",
action="store_true",
help="Don't log progress to stderr.")
Expand All @@ -58,7 +68,10 @@ def parse_args():
def main():
args = parse_args()

logging.basicConfig(level=(logging.WARN if args.quiet else logging.INFO))
logging.basicConfig(
level=(logging.WARN if args.quiet else logging.INFO),
format="%(levelname)s:%(name)s: %(message)s"
)

session = Session(
user_agent=args.user_agent,
Expand Down Expand Up @@ -86,6 +99,7 @@ def main():
args.dir,
raw=args.raw,
root=args.root,
ignore_errors=args.ignore_errors
)
else:
flag = "id_" if args.raw else ""
Expand Down
26 changes: 20 additions & 6 deletions waybackpack/pack.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ def __init__(self,

def download_to(self, directory,
raw=False,
root=DEFAULT_ROOT):
root=DEFAULT_ROOT,
ignore_errors=False):

for asset in self.assets:
path_head, path_tail = os.path.split(self.parsed_url.path)
Expand All @@ -57,11 +58,24 @@ def download_to(self, directory,
asset.timestamp)
)

content = asset.fetch(
session=self.session,
raw=raw,
root=root
)
try:
content = asset.fetch(
session=self.session,
raw=raw,
root=root
)
except Exception as e:
if ignore_errors == True:
ex_name = ".".join([ e.__module__, e.__class__.__name__ ])
logger.warn("ERROR -- {0} @ {1} -- {2}: {3}".format(
asset.original_url,
asset.timestamp,
ex_name,
e
))
return
else:
raise

try:
os.makedirs(filedir)
Expand Down
5 changes: 4 additions & 1 deletion waybackpack/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,16 @@ def __init__(self, follow_redirects=False, user_agent=DEFAULT_USER_AGENT):
self.user_agent = user_agent

def get(self, url, **kwargs):
headers = { "User-Agent": self.user_agent }
headers = {
"User-Agent": self.user_agent,
}
response_is_final = False
while (response_is_final == False):
res = requests.get(
url,
allow_redirects=self.follow_redirects,
headers=headers,
stream=True,
**kwargs
)
if res.status_code != 200:
Expand Down
1 change: 1 addition & 0 deletions waybackpack/version.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
__version__ = "0.3.2"