From 3a728c2f224799cbcc47e4becd58270f86f4e6ac Mon Sep 17 00:00:00 2001 From: chliny Date: Fri, 27 Sep 2013 16:27:59 +0800 Subject: [PATCH] =?UTF-8?q?=E5=B0=9D=E8=AF=95=E6=94=AF=E6=8C=81magnet?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- xfdown.py | 34 ++++++++++++++++++++++++++++++---- 1 file changed, 30 insertions(+), 4 deletions(-) diff --git a/xfdown.py b/xfdown.py index 4a0b1ea..7753bcf 100755 --- a/xfdown.py +++ b/xfdown.py @@ -353,12 +353,20 @@ def __addtask(self): url = uniurl.encode("utf8") filename=self.getfilename_url(url) - if os.path.isfile(url): - import libtorrent - torinfo = libtorrent.torrent_info(uniurl) + if os.path.isfile(url) or url.startswith("magnet:"): + from libtorrent import torrent_info + pid = 1 + if url.startswith("magnet:"): + pid = os.fork() + if pid: + return + torinfo = self.__getmeta(url) + else: + torinfo = torrent_info(uniurl) + torhash = torinfo.info_hash() if torhash.is_all_zeros(): - return + return False bthash = str(torhash).upper() #print bthash @@ -406,6 +414,9 @@ def __addtask(self): urlv2="http://lixian.qq.com/handler/xfjson2012.php" istr = self.__request(urlv2,data2) #print istr + if not pid: + sys.exit(1) + else: data={"down_link":url,\ "filename":filename,\ @@ -415,6 +426,21 @@ def __addtask(self): istr = self.__request(urlv,data) print istr + def __getmeta(self,magneturl): + from libtorrent import session + from libtorrent import add_magnet_uri + ise = session() + parm = {"save_path":"/tmp/"} + maghandler = add_magnet_uri(ise,magneturl,parm) + beg = int(time.time()) + while (not maghandler.has_metadata()): + #print maghandler.status().state + time.sleep(2) + if int(time.time()) - beg > 5*60: + print "download meta data failed!" + sys.exit(-1) + return maghandler.get_torrent_info() + def __toUnicode(self,word): if isinstance(word,unicode): print word