Skip to content

Instantly share code, notes, and snippets.

@yammesicka
Last active May 17, 2016 19:23
Show Gist options
  • Save yammesicka/0dee936be83e6d1973fab59513d898fd to your computer and use it in GitHub Desktop.
Save yammesicka/0dee936be83e6d1973fab59513d898fd to your computer and use it in GitHub Desktop.
Digital Whisper - Download all issues.
import requests
def to_hex(decimal, padding=0, upper=False):
assert isinstance(decimal, int)
hex_part = hex(decimal).split("x")[1]
if padding and len(hex_part) < padding:
hex_part = "0"*(padding - len(hex_part)) + hex_part
if upper:
hex_part = hex_part.upper()
return "0x" + hex_part
def stream_to_file(filename, stream):
with open(filename, "wb") as issue_file:
for chunk in stream.iter_content(chunk_size=1024):
if chunk:
issue_file.write(chunk)
if __name__ == '__main__':
DOMAIN = "http://www.digitalwhisper.co.il"
URL = DOMAIN + "/files/Zines/{hex}/DigitalWhisper{dec}.pdf"
can_find_issue = True
issue_number = 0
while can_find_issue:
issue_number += 1
issue_hex = to_hex(issue_number, padding=2, upper=True)
link = URL.format(hex=issue_hex, dec=issue_number)
next_issue = requests.get(link, stream=True)
if next_issue.ok:
print("Downloading issue: {}".format(issue_number))
filename = str(issue_number) + ".pdf"
stream_to_file(filename, next_issue)
else:
can_find_issue = False
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment