Repository navigation
Expand file tree
/
Copy pathXDCCRequest.py
More file actions
147 lines (103 loc) · 3.38 KB
/
Copy pathXDCCRequest.py
File metadata and controls
147 lines (103 loc) · 3.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# -*- coding: utf-8 -*-
# @author: jeix
import os
import select
import socket
import struct
import time
from pyload.plugin.Plugin import Abort
class XDCCRequest(object):
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, irc, progress=None):
ircbuffer = ""
lastUpdate = time.time()
cumRecvLen = 0
dccsock = self.createSocket()
dccsock.settimeout(self.timeout)
dccsock.connect((ip, port))
if os.path.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 os.path.exists(newfilename):
filename = newfilename
break
fh = open(filename, "wb")
# recv loop for dcc socket
while True:
if self.abort:
dccsock.close()
fh.close()
os.remove(filename)
raise Abort
self._keepAlive(irc, ircbuffer)
data = dccsock.recv(4096)
dataLen = len(data)
self.recv += dataLen
cumRecvLen += dataLen
now = time.time()
timespan = now - lastUpdate
if timespan > 1:
self.speed = cumRecvLen / timespan
cumRecvLen = 0
lastUpdate = now
if progress:
progress(self.percent)
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
def _keepAlive(self, sock, *readbuffer):
fdset = select.select([sock], [], [], 0)
if sock not in fdset[0]:
return
readbuffer += sock.recv(1024)
temp = readbuffer.split("\n")
readbuffer = temp.pop()
for line in temp:
line = line.rstrip()
first = line.split()
if first[0] == "PING":
sock.send("PONG %s\r\n" % first[1])
def abortDownloads(self):
self.abort = True
@property
def size(self):
return self.filesize
@property
def arrived(self):
return self.recv
@property
def percent(self):
return (self.recv * 100) / self.filesize if elf.filesize else 0
def close(self):
pass