Example #1
0
File: dl.py Project: agranium/pydl
    def run(self, queue_file):
        """
        ダウンローダーを起動する

        Parameters
        ----------------
        queue_file: str
            キューファイルのパス      
        """
        queue = Queue(queue_file)
        self.login()

        while not queue.finished:
            if self.downloading_count == 0 and queue.hasQueue:
                # ダウンロードに追加
                self.downloading_count += 1
                url = queue.next()
                queue.save()
                self.fromURL(url)

            for item in self.finishedItems:
                # ダウンロード完了時処理
                self.downloading_count -= 1
                self.moveToDownloadDirectory(item)
                url = queue.findFromDownloading(Path(item).name)
                queue.toFinished(url)
                queue.save()
            time.sleep(1)
Example #2
0
class Scheduler(Thread):

    file_semaphore = Semaphore()

    def __init__(self,controller,dispatch,error):
        Thread.__init__(self)
        self.policy = policy.get_policy()
        self.controller = controller
        self.do_dispatch = dispatch
        self.error = error
        self.queue = Queue()
        self.queue.load()
        self.lock = Lock()
        self._quit = Event()

        self.add_queue = SyncQueue(0)
        self.num_run = 0
        self.log = get_logger()

    def job(self,id):
        return self.queue.get(id)

    def jobs(self):
        return self.queue.get()

    def save(self):
        self.queue.save()

    def add(self,item):
        if self.lock.acquire(0):
            self.queue.add(item)
            self.lock.release()
        else:
            self.add_queue.put(item)

    def add_url(self,url,referer=None):
        try:
            fd = urlopen(url,referer=referer)
            meta = fd.read()
            fullurl = fd.geturl()
            fd.close()
            save_torrent = 1
            if fullurl.startswith('file://'):
                torrent_file = urllib.unquote(url)
                if torrent_file.startswith('file://'):
                    torrent_file = torrent_file[7:]
            elif fullurl.startswith('file:/') and fullurl[8] == '|':
                torrent_file = urllib.unquote(url)
                torrent_file = torrent_file[7]+':'+torrent_file[9:]
                if torrent_file.find('Temporary Internet Files') >= 0:
                    filename = os.path.split(url)[1]
                    torrent_file = os.path.join(self.policy(policy.TORRENT_PATH),
                                                filename)
            else:
                filename = urllib.unquote(os.path.split(url)[1])
                torrent_file = os.path.join(self.policy(policy.TORRENT_PATH),
                                            filename)

            torrent_path = os.path.dirname(torrent_file)
            if not os.path.exists(torrent_path):
                os.mkdir(torrent_path)

            if save_torrent:
                fd = open(torrent_file,'wb')
                fd.write(meta)
                fd.close()

            #~ d = Download() #cbt
            
            try: #cbt
                rd = bdecode(meta) #cbt
                if rd['cbt_user'] == self.policy(policy.CBT_LOGIN): #cbt
                    dest_path = rd['cbt_path'] #cbt
            except: #cbt
                    dest_path = self.policy(policy.DEST_PATH) #cbt

            self.add(QueueEntry(torrent_file, dest_path=dest_path)) #cbt

        except Exception,why:
            return str(why)