Skip to content

Instantly share code, notes, and snippets.

@tecoholic
Created September 26, 2011 16:46
Show Gist options
  • Save tecoholic/1242694 to your computer and use it in GitHub Desktop.
Save tecoholic/1242694 to your computer and use it in GitHub Desktop.
This Python Script downloads a file from internet at set intervals. Useful for data/log file downloads
#!/usr/bin/env python
import os
import sys
import urllib2
import time
from datetime import datetime
# The following parameters can be changed to suite the requirements of user
url = "http://apps.arunmozhi.in/GPRS_Usage.html"
hr = 0
mi = 1
sec = 0
interval = (hr*3600)+(mi*60)+sec
def constructFilename(url):
today = datetime.now()
fil, ext = os.path.splitext(url.split("/")[-1])
fil += today.strftime('_%Y_%m_%d')
filename = fil+ext
return filename
def main():
print "Opening URL: %s" % (url)
print "Date & Time: "+datetime.now().__str__()
f = urllib2.urlopen(url)
filesize = float(f.info().getheader("Content-Length"))
if filesize == None:
print "FileSize Error"
else:
filename = constructFilename(url)
local = open(filename,"wb")
bytesRead = 0.0
oldpercent = 0
for line in f:
bytesRead += len(line)
newpercent = 100*bytesRead/filesize
if newpercent-oldpercent > 1:
print "%s: %.02f/%.02f kb (%d%%)" %(
filename,
bytesRead/1024.0,
filesize/1024.0,
newpercent
)
oldpercent = newpercent
local.write(line)
local.close()
f.close()
if bytesRead == filesize:
print "File download Done!"
else:
print "Error: File download interupted"
if __name__ == '__main__':
while(1):
main()
time.sleep(interval)
@chekamarue
Copy link

Clean and simple ! Good Job

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