WxDownloader

Windows

wxdownloader.jpg

Mac OS X

downloadermac.jpg

Linux -Gnome

wxdownloader_linux.jpg
#!/usr/bin/env python

import wx
import sys, os
from urllib import urlopen

class Frame(wx.Frame):

    def __init__(self, parent, id, title):
        wx.Frame.__init__(self, parent, id, title)
        panel = wx.Panel(self)

    #------------------- WIDGET --------------------
        topLbl = wx.StaticText(panel, -1, "WxDownloader")
        topLbl.SetFont(wx.Font(18, wx.SWISS, wx.NORMAL, wx.BOLD))

        urlLbl = wx.StaticText(panel, -1, "URL:")
        global urlfile 
        urlfile = wx.TextCtrl(panel, -1, "");
        urlfile.SetValue("http://")

        downloadBtn = wx.Button(panel, -1, "Download")
        self.Bind(wx.EVT_BUTTON, self.OnDownload, downloadBtn)

        cancelBtn = wx.Button(panel, -1, "Quit")
        self.Bind(wx.EVT_BUTTON, self.OnQuit, cancelBtn)

    #------------------- SIZER --------------------
        mainSizer = wx.BoxSizer(wx.VERTICAL)
        mainSizer.Add(topLbl, 0, wx.ALL, 5)
        mainSizer.Add(wx.StaticLine(panel), 0, wx.EXPAND|wx.TOP|wx.BOTTOM, 5)

        urlSizer = wx.FlexGridSizer(cols=2, hgap=5, vgap=5)
        urlSizer.AddGrowableCol(1)
        urlSizer.Add(urlLbl, 0, wx.ALIGN_RIGHT|wx.ALIGN_CENTER_VERTICAL)
        urlSizer.Add(urlfile, 0, wx.EXPAND)
        mainSizer.Add(urlSizer, 0, wx.EXPAND|wx.ALL, 10)

        btnSizer = wx.BoxSizer(wx.HORIZONTAL)

        btnSizer.Add((20,20), 1)
        btnSizer.Add(downloadBtn)
        btnSizer.Add((20,20), 1)
        btnSizer.Add(cancelBtn)
        btnSizer.Add((20,20), 1)

        mainSizer.Add(btnSizer, 0, wx.EXPAND|wx.BOTTOM, 10)

        panel.SetSizer(mainSizer)

    #------------------- DOWNLOAD FUNCTIONS --------------------
    def getURLName(self, url):
        directory=os.curdir

        name="%s%s%s" % (
            directory,
            os.sep,
            url.split("/")[-1]
        )

        return name

    def createDownload(self, url):
        instream=urlopen(url, None)

        filename=instream.info().getheader("Content-Length")
        if filename==None:
            filename="temp"

        return (instream, filename)

    def download(self, instream, outstream):
        outstream.write(instream.read())
        outstream.close()

    #------------------- DOWNLOAD --------------------
    #----------------NO ERROR CHECKING ---------------
    def OnDownload(self, event):
        progressMax = 100
        dialog = wx.ProgressDialog("Downloading...", "Time remaining", progressMax, style=wx.PD_APP_MODAL | wx.PD_CAN_ABORT | wx.PD_ELAPSED_TIME | wx.PD_REMAINING_TIME)
        keepGoing = True
        count = 0

        url = urlfile.GetValue()

        outfile=open(self.getURLName(url), "wb")
        fileName=outfile.name.split(os.sep)[-1]

        url, length=self.createDownload(url)
        if not length:
            length="?"

        if length!="?":
            length=float(length)
        bytesRead=0.0

        for line in url:
            if not keepGoing or count == progressMax:
                url.close()
                outfile.close()
                dialog.Destroy()

            bytesRead+=len(line)

            if length!="?":
                count = 100*bytesRead/length
                (keepGoing, skip) = dialog.Update(count)
            outfile.write(line)

        url.close()
        outfile.close()
        dialog.Destroy()

    #------------------- QUIT --------------------
    def OnQuit(self, event):
            self.Destroy()

class App(wx.App):

    def __init__(self, redirect=True, filename="logs.txt"):
        wx.App.__init__(self, redirect, filename)

    def OnInit(self):
        self.frame = Frame(parent=None, id=-1, title='WxDownloader')
        self.frame.Show()
        self.SetTopWindow(self.frame)
        return True

if __name__ == '__main__':
    app = App(redirect=True)
    fred = app.MainLoop()
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License