-
-
Save pmaingi/26144cfe34b206f200f2 to your computer and use it in GitHub Desktop.
Download Kenyan Daily Nation and Business Daily
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import urllib2 | |
from datetime import date | |
def main(): | |
today = date.today() | |
suffix = getDateSuffix(today) | |
fdate = "%s %s%s %s" % (today.strftime('%b'),today.strftime('%d').lstrip('0'),suffix,today.strftime('%Y')) | |
print("Downloading Daily Nation...") | |
download_file("http://downloads.realviewtechnologies.com/Nation Media/Daily Nation/%s.pdf" % fdate) | |
print("Downloading Business Daily...") | |
download_file("http://downloads.realviewtechnologies.com/Nation Media/Business Daily/%s.pdf" % fdate) | |
print("Done.") | |
def getDateSuffix(t): | |
if 4 <= t.day <= 20 or 24 <= t.day <= 30: | |
return "th" | |
else: | |
return ["st", "nd", "rd"][t.day % 10 - 1] | |
def download_file(download_url): | |
file_name ="-".join(download_url.split('/')[-2:]) | |
url = download_url.replace(" ","%20") | |
try: | |
u = urllib2.urlopen(url) | |
f = open(file_name, 'wb') | |
meta = u.info() | |
file_size = int(meta.getheaders("Content-Length")[0]) | |
print "Downloading: %s Bytes: %s" % (file_name, file_size) | |
file_size_dl = 0 | |
block_sz = 8192 | |
while True: | |
buffer = u.read(block_sz) | |
if not buffer: | |
break | |
file_size_dl += len(buffer) | |
f.write(buffer) | |
status = r"%10d [%3.2f%%]" % (file_size_dl, file_size_dl * 100. / file_size) | |
status = status + chr(8)*(len(status)+1) | |
print status, | |
f.close() | |
print("Download Complete") | |
except Exception, err: | |
print("Error Downloading File", err) | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment