Last active
February 20, 2018 05:26
-
-
Save ninenine/4a21305fac528c2ed6e5 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() |
Good thing, you took the initiative.
Happy coding 😄
how do i download? anyone?
How do i make use of these codes ?
Downloading Daily Nation...
('Error Downloading File', HTTPError())
Downloading Business Daily...
('Error Downloading File', HTTPError())
Done.
how does this work
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
cool stuff...