summaryrefslogtreecommitdiffstats
path: root/module/network
diff options
context:
space:
mode:
authorGravatar Jeix <devnull@localhost> 2011-02-11 20:17:20 +0100
committerGravatar Jeix <devnull@localhost> 2011-02-11 20:17:20 +0100
commit4a6cfe415a0d362bf30f22fac16b732195f97232 (patch)
treef90318302830b967b4734fc1e7d82eda6d52a8fc /module/network
parentsocket code fixes (diff)
downloadpyload-4a6cfe415a0d362bf30f22fac16b732195f97232.tar.xz
GUI updates, closed #181, updated XDCC, fixed FileSonic
Diffstat (limited to 'module/network')
-rw-r--r--module/network/RequestFactory.py7
-rw-r--r--module/network/XDCCRequest.py132
2 files changed, 138 insertions, 1 deletions
diff --git a/module/network/RequestFactory.py b/module/network/RequestFactory.py
index 8340d06f7..90a48fe3c 100644
--- a/module/network/RequestFactory.py
+++ b/module/network/RequestFactory.py
@@ -24,6 +24,8 @@ from Bucket import Bucket
from HTTPRequest import HTTPRequest
from CookieJar import CookieJar
+from XDCCRequest import XDCCRequest
+
class RequestFactory():
def __init__(self, core):
self.lock = Lock()
@@ -35,9 +37,12 @@ class RequestFactory():
def iface(self):
return self.core.config["download"]["interface"]
- def getRequest(self, pluginName, account=None):
+ def getRequest(self, pluginName, account=None, type="HTTP"):
self.lock.acquire()
+ if type == "XDCC":
+ return XDCCRequest(proxies=self.getProxies())
+
req = Browser(self.iface(), self.bucket, self.getProxies())
if account:
diff --git a/module/network/XDCCRequest.py b/module/network/XDCCRequest.py
new file mode 100644
index 000000000..126662bb8
--- /dev/null
+++ b/module/network/XDCCRequest.py
@@ -0,0 +1,132 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+"""
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 3 of the License,
+ or (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
+ See the GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, see <http://www.gnu.org/licenses/>.
+
+ @author: jeix
+"""
+
+import socket
+import re
+
+from os.path import exists
+
+from time import time
+
+import struct
+from select import select
+
+class XDCCError(Exception):
+ pass
+
+class XDCCRequest():
+ def __init__(self, timeout=30, proxies={}):
+
+ self.proxies = proxies
+ self.timeout = timeout
+
+ self.filesize = 0
+ self.recv = 0
+ self.speed = 0
+
+ self.abort = False
+
+
+ def createSocket(self):
+ # proxytype = None
+ # proxy = None
+ # if self.proxies.has_key("socks5"):
+ # proxytype = socks.PROXY_TYPE_SOCKS5
+ # proxy = self.proxies["socks5"]
+ # elif self.proxies.has_key("socks4"):
+ # proxytype = socks.PROXY_TYPE_SOCKS4
+ # proxy = self.proxies["socks4"]
+ # if proxytype:
+ # sock = socks.socksocket()
+ # t = _parse_proxy(proxy)
+ # sock.setproxy(proxytype, addr=t[3].split(":")[0], port=int(t[3].split(":")[1]), username=t[1], password=t[2])
+ # else:
+ # sock = socket.socket()
+ # return sock
+
+ return socket.socket()
+
+ def download(self, ip, port, filename, progressNotify=None):
+
+ lastRecv = time()
+
+ dccsock = self.createSocket()
+
+ dccsock.settimeout(self.timeout)
+ dccsock.connect((ip, port))
+
+ if exists(filename):
+ i = 0
+ nameParts = filename.rpartition(".")
+ while True:
+ newfilename = "%s-%d%s%s" % (nameParts[0], i, nameParts[1], nameParts[2])
+ i += 1
+
+ if not exists(newfilename):
+ filename = newfilename
+ break
+
+ fh = open(filename, "wb")
+
+ # recv loop for dcc socket
+ while True:
+ if self.abort:
+ break
+
+ data = dccsock.recv(4096)
+ dataLen = len(data)
+ self.recv += dataLen
+
+ now = time()
+ timespan = now - lastRecv
+ if timespan:
+ self.speed = dataLen / timespan
+ if progressNotify:
+ progressNotify(self.percent)
+ lastRecv = now
+
+ if not data:
+ break
+
+ fh.write(data)
+
+ # acknowledge data by sending number of recceived bytes
+ dccsock.send(struct.pack('!I', self.recv))
+
+ dccsock.close()
+ fh.close()
+
+ return filename
+
+
+ @property
+ def size(self):
+ return self.filesize
+
+ @property
+ def arrived(self):
+ return self.recv
+
+ @property
+ def percent(self):
+ if not self.filesize: return 0
+ return (self.recv * 100) / self.filesize
+
+ def close(self):
+ pass