Exemplo n.º 1
0
 def resume(self, tid):
     # set state to waiting and load_more()
     tid = int(tid)
     tasks = db.select_tasks(id=tid)
     if tasks:
         task = tasks[0]
         url = task["url"]
         output = task["output"]
         state = task["state"]
         thsize = task["thsize"]
         maxspeed = task["maxspeed"]
         headers = task["headers"]
         subdir = task["subdir"]
         if state not in (STATE_WAITING, STATE_PAUSED, STATE_ERROR):
             return
         if state != STATE_WAITING:
             db.update_tasks(tid, state=STATE_WAITING)
         self.download_more()
Exemplo n.º 2
0
 def resume(self, tid):
     # set state to waiting and load_more()
     tid = int(tid)
     tasks = db.select_tasks(id=tid)
     if tasks:
         task = tasks[0]
         url = task["url"]
         output = task["output"]
         state = task["state"]
         thsize = task["thsize"]
         maxspeed = task["maxspeed"]
         headers = task["headers"]
         subdir = task["subdir"]
         if state not in (STATE_WAITING, STATE_PAUSED, STATE_ERROR):
             return
         if state != STATE_WAITING:
             db.update_tasks(tid, state=STATE_WAITING)
         self.download_more()
Exemplo n.º 3
0
 def sort(self, ids):
     orders = []
     for order in range(len(ids)):
         db.update_tasks(ids[order], **{'order':order}) 
Exemplo n.º 4
0
 def pause(self, ids):
     db.update_tasks(ids, **{'state': STATE_PAUSED})
Exemplo n.º 5
0
    def _start(self, tid):
        # start a task existed
        tasks = db.select_tasks(id=tid)
        if not tasks:
            return

        db.update_tasks(tid, state=STATE_DOWNLOADING)

        #create axel task
        if os.fork(): # old process
            return # as 200 OK
        else: # sub process
            # get the options
            task = tasks[0]
            url = task["url"]
            output = task["output"]
            thsize = task["thsize"]
            maxspeed = task["maxspeed"]
            headers = task["headers"]
            subdir = task["subdir"]

            force_download = True

            output_file = os.path.join(conf.incomming, output)
            if os.path.exists(output_file) and not os.path.exists(output_file + '.st'):
              if force_download:
                os.remove(output_file)
              else:
                print 'file completed already, skip download'
                exit()

            args = [os.path.join(os.getcwd(), "axel"), "-a", "-n", str(thsize), "-s", str(maxspeed)]
            for header in headers.splitlines():
                args.append("-H")
                args.append(header)
            args.append(url)
            args.append("-o")
            args.append(output)
            os.system("mkdir -p %s" % os.path.join(conf.incomming, subdir))
            axel_process = subprocess.Popen(args, shell=False, stdout=subprocess.PIPE, cwd=conf.incomming)

            last_update_time = 0
            while 1:
                try:
                    line = axel_process.stdout.readline()
                    if not line:
                      break
                    line = line.strip()
                except:
                    returncode = axel_process.poll()
                    if returncode is not None:
                        # axel completed
                        if returncode:
                            db.update_tasks(tid, state=STATE_ERROR, errmsg="Error, axel exit with code: %s" % returncode)
                    break
                this_update_time = time.time()
                if line.startswith(":"):
                    done, total, thdone, speed, left, update_time = line[1:].split("|")
                    if done != total and last_update_time > 0 and this_update_time - last_update_time < 1:
                        continue
                elif line.startswith("HTTP/1."):
                    db.update_tasks(tid, state=STATE_ERROR, errmsg=line)
                    break
                else:
                    continue
                last_update_time = this_update_time
                tasks = db.select_tasks(id=tid)
                if tasks:
                    task = tasks[0]
                    state = task["state"]
                    if state == STATE_DOWNLOADING:
                        try:
                            if done == total:
                                #completed
                                db.update_tasks(tid, state=STATE_COMPLETED, left=0)
                                os.system("mkdir -p %s" % os.path.join(conf.downloads, subdir))
                                os.rename(output_file, os.path.join(conf.downloads, subdir, output))
                                break
                            db.update_tasks(tid, speed=speed, done=done, total=total, left=left)
                            continue
                        except Exception, e:
                            import traceback
                            traceback.print_exc()
                            db.update_tasks(tid, state=STATE_ERROR, errmsg="Error, axel exit with code: %s" % e)
                            try:
                                axel_process.terminate()
                            except:
                                pass
                    else:
                        #paused
                        axel_process.terminate()
                else:
                    #deleted
                    axel_process.terminate()
                    try:
                        os.remove(output_file)
                    except:
                        pass
                    try:
                        os.remove(output_file + ".st")
                    except:
                        pass
            returncode = axel_process.poll()
            if returncode is not None:
                # axel completed
                if returncode:
                    db.update_tasks(tid, state=STATE_ERROR, errmsg="Error, axel exit with code: %s" % returncode)
            self.download_more()
            sys.exit()
Exemplo n.º 6
0
 def sort(self, ids):
     orders = []
     for order in range(len(ids)):
         db.update_tasks(ids[order], **{'order': order})
Exemplo n.º 7
0
 def pause(self, ids):
     db.update_tasks(ids, **{'state': STATE_PAUSED})
Exemplo n.º 8
0
    def _start(self, tid):
        # start a task existed
        tasks = db.select_tasks(id=tid)
        if not tasks:
            return

        db.update_tasks(tid, state=STATE_DOWNLOADING)

        #create axel task
        #print "====="
        #print os.fork()
        if os.fork():  # old process
            return  # as 200 OK
        else:  # sub process
            # get the options
            task = tasks[0]
            url = task["url"]
            output = task["output"]
            thsize = task["thsize"]
            maxspeed = task["maxspeed"]
            headers = task["headers"]
            #subdir = task["subdir"]
            downloads = task["downloads"]
            ua = task["ua"]

            force_download = conf.force_download
            output_file = os.path.join(downloads, output)
            if os.path.exists(output_file) and not os.path.exists(output_file +
                                                                  '.st'):
                if force_download:
                    os.remove(output_file)
                else:
                    print 'file completed already, skip download'
                    exit()
            os.system("mkdir -p %s" % os.path.join(downloads))
            args = [
                os.path.join(os.getcwd(), "axel"), "-a", "-n",
                str(thsize), "-s",
                str(maxspeed), "-U",
                str(ua)
            ]
            for header in headers.splitlines():
                args.append("-H")
                args.append(header)
            args.append("-o")
            args.append(output_file)
            args.append(url)
            axel_process = subprocess.Popen(args,
                                            shell=False,
                                            stdout=subprocess.PIPE,
                                            cwd=downloads)
            #print(args)

            last_update_time = 0
            while 1:
                try:
                    line = axel_process.stdout.readline()
                    if not line:
                        break
                    line = line.strip()
                except:
                    returncode = axel_process.poll()
                    if returncode is not None:
                        # axel completed
                        if returncode:
                            db.update_tasks(
                                tid,
                                state=STATE_ERROR,
                                errmsg="Error, axel exit with code: %s" %
                                returncode)
                    break
                this_update_time = time.time()
                if line.startswith(":"):
                    done, total, thdone, speed, left, update_time = line[
                        1:].split("|")
                    if done != total and last_update_time > 0 and this_update_time - last_update_time < 1:
                        continue
                elif line.startswith("HTTP/1."):
                    db.update_tasks(tid, state=STATE_ERROR, errmsg=line)
                    break
                else:
                    continue
                last_update_time = this_update_time
                tasks = db.select_tasks(id=tid)
                if tasks:
                    task = tasks[0]
                    state = task["state"]
                    if state == STATE_DOWNLOADING:
                        try:
                            if done == total:
                                #completed
                                db.update_tasks(tid,
                                                state=STATE_COMPLETED,
                                                left=0)
                                os.system("mkdir -p %s" %
                                          os.path.join(downloads))
                                #os.rename(output_file, os.path.join(conf.downloads, subdir, output))
                                break
                            db.update_tasks(tid,
                                            speed=speed,
                                            done=done,
                                            total=total,
                                            left=left)
                            continue
                        except Exception, e:
                            import traceback
                            traceback.print_exc()
                            db.update_tasks(
                                tid,
                                state=STATE_ERROR,
                                errmsg="Error, axel exit with code: %s" % e)
                            try:
                                axel_process.terminate()
                            except:
                                pass
                    else:
                        #paused
                        axel_process.terminate()
                else:
                    #deleted
                    axel_process.terminate()
                    try:
                        os.remove(output_file)
                    except:
                        pass
                    try:
                        os.remove(output_file + ".st")
                    except:
                        pass
            returncode = axel_process.poll()
            if returncode is not None:
                # axel completed
                if returncode:
                    db.update_tasks(tid,
                                    state=STATE_ERROR,
                                    errmsg="Error, axel exit with code: %s" %
                                    returncode)
            self.download_more()
            sys.exit()