urlfetch.py :  » Network » Rufus-BitTorrent-Client » Rufus_0.7.0_src » Python Open Source

Home
Python Open Source
1.3.1.2 Python
2.Ajax
3.Aspect Oriented
4.Blog
5.Build
6.Business Application
7.Chart Report
8.Content Management Systems
9.Cryptographic
10.Database
11.Development
12.Editor
13.Email
14.ERP
15.Game 2D 3D
16.GIS
17.GUI
18.IDE
19.Installer
20.IRC
21.Issue Tracker
22.Language Interface
23.Log
24.Math
25.Media Sound Audio
26.Mobile
27.Network
28.Parser
29.PDF
30.Project Management
31.RSS
32.Search
33.Security
34.Template Engines
35.Test
36.UML
37.USB Serial
38.Web Frameworks
39.Web Server
40.Web Services
41.Web Unit
42.Wiki
43.Windows
44.XML
Python Open Source » Network » Rufus BitTorrent Client 
Rufus BitTorrent Client » Rufus_0.7.0_src » urlfetch.py
#! /usr/bin/env python
# -*- coding: utf-8 -*-
#-----------------------------------------------------------------------------
# Name:        urlfetch.py
# Purpose:     
#
# Author:      Jeremy Arendt
#
# Created:     2004/20/02
# RCS-ID:      $Id: urlfetch.py,v 1.3 2005/09/16 07:41:38 Inigo Exp $
# Copyright:   (c) 2002
# Licence:     See G3.LICENCE
#-----------------------------------------------------------------------------
import wx
import sys
from threading import Thread
from BitTorrent.zurllib import URLError,urlopen,Request
from os.path import split,join
from urlparse import urlparse
from socket import gaierror,timeout

from leoxv import parseloadurl

if sys.platform == "win32":   
    win32_flag = True
else:
    win32_flag = False

class URLDialog(wx.Dialog):
    def __init__(self, parent):
        wx.Dialog.__init__(self, parent, title = _("Add .torrent from URL"))
        self.label1 = wx.StaticText(self, -1, _("URL:"))
        self.edit1 = wx.TextCtrl(self, -1, "", size = (300, -1))
        self.label2 = wx.StaticText(self, -1, _("Referrer:"))
        self.edit2 = wx.TextCtrl(self, -1, "", size = (300, -1))
        self.okbutt = wx.Button(self, wx.ID_OK)
        self.cancelbutt = wx.Button(self, wx.ID_CANCEL)
        self.guessbutt = wx.Button(self, -1, _("Calc referrer"))
        self.label2.SetToolTipString(_("Needed if torrent site uses referer checking."))
        self.edit2.SetToolTipString(_("Needed if torrent site uses referer checking."))
        self.guessbutt.SetToolTipString(_("Guesses possible referrer"))
        boxsizer = wx.FlexGridSizer(cols=1, rows=1)
        buttsizer = wx.FlexGridSizer(cols=5, rows=1, vgap=2, hgap=2)
        urlsizer = wx.FlexGridSizer(cols=2, rows=3, vgap=2, hgap=2)
        urlsizer.Add(self.label1, 0, wx.ALL|wx.ALIGN_RIGHT|wx.ALIGN_CENTER_VERTICAL|wx.FIXED_MINSIZE, 4)
        urlsizer.Add(self.edit1, 0, wx.RIGHT|wx.EXPAND|wx.FIXED_MINSIZE, 1)
        urlsizer.Add(self.label2, 0, wx.ALL|wx.ALIGN_RIGHT|wx.ALIGN_CENTER_VERTICAL|wx.FIXED_MINSIZE, 4)
        urlsizer.Add(self.edit2, 0, wx.RIGHT|wx.EXPAND|wx.FIXED_MINSIZE, 1)
        urlsizer.AddGrowableCol(1)
        boxsizer.Add(urlsizer, 1, wx.EXPAND, 0)
        buttsizer.Add( (-1,-1) )
        buttsizer.Add(self.okbutt, 0, wx.ALL|wx.FIXED_MINSIZE, 4)
        buttsizer.Add(self.cancelbutt, 0, wx.ALL|wx.FIXED_MINSIZE, 4)
        buttsizer.Add(self.guessbutt, 0, wx.ALL|wx.FIXED_MINSIZE, 4)
        buttsizer.Add( (-1,-1) )
        buttsizer.AddGrowableCol(0)
        buttsizer.AddGrowableCol(2)
        boxsizer.Add(buttsizer, 1, wx.EXPAND, 0)
        self.SetAutoLayout(True)
        self.SetSizer(boxsizer)
        self.Fit()
        boxsizer.SetSizeHints(self)
        self.Layout()

        self.Bind(wx.EVT_BUTTON, self.OnGuess, self.guessbutt)


    def OnGuess(self, event):
        target = self.edit1.GetValue()
        if target != "":
            self.edit2.SetValue(split(target)[0])


class T_FetchFile(Thread):

    def __init__(self, callback, callback_params, url, destpath, referer=None):

        Thread.__init__(self)

        self.success = False

        self.callback = callback
        self.params = callback_params
        self.url = url
        self.path = destpath
        self.name = "Fetch"
        self.referer = referer
        
    def run(self):

        if win32_flag:
            try:
                filename = join(self.path, split(urlparse(self.url)[2])[1])
                req = Request(self.url)
                #d0c - added referer to header
                if self.referer != None:
                    req.add_header('Referer', self.referer)
                h = urlopen(req)
                #d0c end
            except (OSError, ValueError, URLError, gaierror, timeout), msg:
                print "Error fetching url", msg
                return False
        else:
            try:           
                filename = join(self.path, split(urlparse(self.url)[2])[1])          
                url = parseloadurl(self.url)          
                h = (url)        
            except:
                try:
                    filename = join(self.path, split(urlparse(self.url)[2])[1])
                    req = Request(self.url)
                    #d0c - added referer to header
                    if self.referer != None:
                        req.add_header('Referer', self.referer)
                    h = urlopen(req)
                    #d0c end
                except msg:
                    print "Error fetching url"
                    return False     

        try:
            f = open(filename, 'wb')
            f.write(h.read())
            f.close()
            self.success = True
            self.callback( self.params, [filename] )
            return True
        except IOError, msg:
            self.success = False
            print msg
            return False


def dummy_callback(params1, params2):
    print params1, params2
    

if __name__ == "__main__":                                                       #referer - d0c
    f = T_FetchFile(dummy_callback, "test", "http://localhost/test.torrent", "", "http://localhost")
    _ = lambda x: x # needed for gettext
    f.start()
    
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.