Skip to content

Instantly share code, notes, and snippets.

@ninenine
Last active February 20, 2018 05:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save ninenine/4a21305fac528c2ed6e5 to your computer and use it in GitHub Desktop.
Save ninenine/4a21305fac528c2ed6e5 to your computer and use it in GitHub Desktop.
Download Kenyan Daily Nation and Business Daily
#!/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()
@mainakibui
Copy link

cool stuff...

@pollpaul
Copy link

Good thing, you took the initiative.
Happy coding 😄

@enmat
Copy link

enmat commented May 30, 2016

how do i download? anyone?

@mutindap
Copy link

mutindap commented Jun 2, 2016

How do i make use of these codes ?

@sgachies
Copy link

sgachies commented Jun 3, 2016

Downloading Daily Nation...
('Error Downloading File', HTTPError())
Downloading Business Daily...
('Error Downloading File', HTTPError())
Done.

@datGuy88
Copy link

how does this work

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment