Beispiel #1
0
def download_lyrics(songs):
    """download / write lyric to file if it is needed"""

    url_lyric_163 = "http://music.163.com/api/song/lyric?id=%s&lv=1"
    
    percent_bar_factor = 0.4
    width = util.get_terminal_size()[1] -5
    bar_count = (int(width*percent_bar_factor)-2/10) # number of percent bar
    line = log.hl(u' %s'% ('+'*width), 'cyan')

    if songs[0].handler.dl_lyric:
        print log.hl(msg.fmt_dl_lyric_start, 'warning')
        print line

    for song in songs:
        if song.lyric_abs_path:
            print log.hl(u' %s '% song.lyric_filename,'cyan'),  #the ending comma is for hide the newline
            if song.song_type == 1: #xiami
                if song.handler.need_proxy_pool:
                    if song.lyric_link:
                        download_by_url(song.lyric_link, song.lyric_abs_path, show_progress=True, proxy={'http':song.handler.proxies.get_proxy()})
                else:
                    if song.lyric_link:
                        download_by_url(song.lyric_link, song.lyric_abs_path, show_progress=True)
            else: #163
                lyric_link = url_lyric_163 % song.song_id
                song.lyric_text = song.handler.read_link(lyric_link).json()['lrc']['lyric']
                import codecs
                with codecs.open(song.lyric_abs_path, 'w', 'utf-8') as f:
                    f.write(song.lyric_text)
            print log.hl(u' √','cyan')
    print line
Beispiel #2
0
def print_progress():
    #the factor of width used for progress bar
    percent_bar_factor = 0.4
    width = util.get_terminal_size()[1] -5
    bar_count = (int(width*percent_bar_factor)-2/10) # number of percent bar
    #line = log.hl(u' %s\n'% ('-'*90), 'cyan')
    line = log.hl(u' %s\n'% ('+'*width), 'cyan')
    sys.stdout.write(u'\x1b[2J\x1b[H') #clear screen
    sys.stdout.write(line)
    header = u' 保存目录:[%s] | 线程池:[%d] | 总进度:[%d/%d]\n'% (config.DOWNLOAD_DIR, config.THREAD_POOL_SIZE,done,total)
    header = util.rjust(header, width)
    sys.stdout.write(log.hl(u' %s'%header,'warning'))
    sys.stdout.write(line)
    for filename, percent in progress.items():
        bar = util.ljust('=' * int(percent * bar_count), bar_count)
        per100 = percent * 100 
        single_p =  "%s [%s] %.1f%%\n" % \
                (util.rjust(filename,(width - bar_count -10)), bar, per100) # the -10 is for the xx.x% and [ and ]
        sys.stdout.write(log.hl(single_p,'green'))

    if len(done2show):
        sys.stdout.write(line)
        sys.stdout.write(log.hl(util.rjust(u'最近完成(只显示%d个):\n'% config.SHOW_DONE_NUMBER, width),'warning'))
        sys.stdout.write(line)
        #display finished jobs
        for d in done2show:
            sys.stdout.write(log.hl(u' √ %s\n'% d,'cyan'))

    sys.stdout.flush()
Beispiel #3
0
def print_progress():
    #the factor of width used for progress bar
    percent_bar_factor = 0.4
    width = util.get_terminal_size()[1] - 5
    bar_count = (int(width * percent_bar_factor) - 2 / 10
                 )  # number of percent bar
    #line = log.hl(u' %s\n'% ('-'*90), 'cyan')
    line = log.hl(u' %s\n' % ('+' * width), 'cyan')
    sep = log.hl(u' %s\n' % ('=' * width), 'cyan')
    sys.stdout.write(u'\x1b[2J\x1b[H')  #clear screen
    sys.stdout.write(line)
    header = u' 保存目录:[%s] | 线程池:[%d]\n' % (config.DOWNLOAD_DIR,
                                           config.THREAD_POOL_SIZE)
    #header = util.ljust(header, width)
    sys.stdout.write(log.hl(u' %s' % header, 'warning'))
    sys.stdout.write(line)

    fmt_progress = '%s [%s] %.1f%%\n'

    all_p = []  #all progress bars, filled by following for loop
    sum_percent = 0  # total percent for running job
    total_percent = 0

    for filename, percent in progress.items():
        sum_percent += percent
        bar = util.ljust('=' * int(percent * bar_count), bar_count)
        per100 = percent * 100
        single_p =  fmt_progress % \
                (util.rjust(filename,(width - bar_count -10)), bar, per100) # the -10 is for the xx.x% and [ and ]
        all_p.append(log.hl(single_p, 'green'))

    #calculate total progress percent
    total_percent = float(sum_percent + done) / total

    #global progress
    g_text = u'总进度[%d/%d]:' % (done, total)
    g_bar = util.ljust('#' * int(total_percent * bar_count), bar_count)
    g_progress =  fmt_progress % \
                (util.rjust(g_text,(width - bar_count -10)), g_bar, 100*total_percent) # the -10 is for the xx.x% and [ and ]

    #output all total progress bars
    sys.stdout.write(log.hl(u'%s' % g_progress, 'red'))
    sys.stdout.write(sep)

    #output all downloads' progress bars
    sys.stdout.write(''.join(all_p))

    if len(done2show):
        sys.stdout.write(line)
        sys.stdout.write(
            log.hl(u'  最近%d个完成任务:\n' % config.SHOW_DONE_NUMBER, 'warning'))
        sys.stdout.write(line)
        #display finished jobs
        for d in done2show:
            sys.stdout.write(log.hl(u' √ %s\n' % d, 'cyan'))

    sys.stdout.flush()
Beispiel #4
0
def print_progress():
    #the factor of width used for progress bar
    percent_bar_factor = 0.4
    width = util.get_terminal_size()[1] -5
    bar_count = (int(width*percent_bar_factor)-2/10) # number of percent bar
    #line = log.hl(u' %s\n'% ('-'*90), 'cyan')
    line = log.hl(u' %s\n'% ('+'*width), 'cyan')
    sep = log.hl(u' %s\n'% ('='*width), 'cyan')
    sys.stdout.write(u'\x1b[2J\x1b[H') #clear screen
    sys.stdout.write(line)
    header = msg.fmt_dl_header % (config.DOWNLOAD_DIR, config.THREAD_POOL_SIZE)
    #header = util.ljust(header, width)
    sys.stdout.write(log.hl(u' %s'%header,'warning'))
    sys.stdout.write(line)

    fmt_progress = '%s [%s] %.1f%%\n'


    all_p = [] #all progress bars, filled by following for loop
    sum_percent = 0 # total percent for running job
    total_percent = 0

    for filename, percent in progress.items():
        sum_percent += percent
        bar = util.ljust('=' * int(percent * bar_count), bar_count)
        per100 = percent * 100 
        single_p =  fmt_progress % \
                (util.rjust(filename,(width - bar_count -10)), bar, per100) # the -10 is for the xx.x% and [ and ]
        all_p.append(log.hl(single_p,'green'))
    
    #calculate total progress percent
    total_percent = float(sum_percent+done)/total
    
    #global progress
    g_text = msg.fmt_dl_progress % (done, total)
    g_bar = util.ljust('#' * int(total_percent* bar_count), bar_count)
    g_progress =  fmt_progress % \
                (util.rjust(g_text,(width - bar_count -10)), g_bar, 100*total_percent) # the -10 is for the xx.x% and [ and ]

    #output all total progress bars
    sys.stdout.write(log.hl(u'%s'%g_progress, 'red'))
    sys.stdout.write(sep)

    #output all downloads' progress bars
    sys.stdout.write(''.join(all_p))

    if len(done2show):
        sys.stdout.write(line)
        sys.stdout.write(log.hl(msg.fmt_dl_last_finished % config.SHOW_DONE_NUMBER,'warning'))
        sys.stdout.write(line)
        #display finished jobs
        for d in done2show:
            sys.stdout.write(log.hl(u' √ %s\n'% d,'cyan'))

    sys.stdout.write(line)
    sys.stdout.flush()
Beispiel #5
0
def download_lyrics(songs):
    """download / write lyric to file if it is needed"""

    url_lyric_163 = "http://music.163.com/api/song/lyric?id=%s&lv=1"

    percent_bar_factor = 0.4
    width = util.get_terminal_size()[1] - 5
    bar_count = (int(width * percent_bar_factor) - 2 / 10
                 )  # number of percent bar
    line = log.hl(u' %s' % ('+' * width), 'cyan')

    if songs[0].handler.dl_lyric == True:
        print log.hl(msg.fmt_dl_lyric_start, 'warning')
        print line

        for song in songs:
            if song.lyric_abs_path:
                print log.hl(
                    u' %s ' % song.lyric_filename,
                    'cyan'),  #the ending comma is for hide the newline
                if song.song_type == 1:  #xiami
                    if song.handler.need_proxy_pool:
                        if song.lyric_link:
                            download_url(song.lyric_link,
                                         song.lyric_abs_path,
                                         show_progress=True,
                                         proxy={
                                             'http':
                                             song.handler.proxies.get_proxy()
                                         })
                    else:
                        if song.lyric_link:
                            download_url(song.lyric_link,
                                         song.lyric_abs_path,
                                         show_progress=True)
                    print log.hl(u' √', 'cyan')
                else:  #163
                    lyric_link = url_lyric_163 % song.song_id
                    lyric_json = song.handler.read_link(lyric_link).json()
                    if not lyric_json or not lyric_json.has_key(
                            'lrc') or not lyric_json['lrc'].has_key('lyric'):
                        print log.hl(u' ✘ Not Found', 'red')
                        continue
                    song.lyric_text = song.handler.read_link(
                        lyric_link).json()['lrc']['lyric']
                    import codecs
                    with codecs.open(song.lyric_abs_path, 'w', 'utf-8') as f:
                        f.write(song.lyric_text)
                    print log.hl(u' √', 'cyan')
        print line
Beispiel #6
0
def print_progress():
    """ print progress info """
    #the factor of width used for progress bar
    percent_bar_factor = 0.4
    width = util.get_terminal_size()[1] -5
    bar_count = (int(width*percent_bar_factor)-2/10) # number of percent bar
    #line = log.hl(u' %s\n'% ('-'*90), 'cyan')
    line = log.hl(u' %s\n'% ('+'*width), 'cyan')
    sep = log.hl(u' %s\n'% ('='*width), 'cyan')
    sys.stdout.write(u'\x1b[2J\x1b[H') #clear screen
    sys.stdout.write(line)
    header = msg.fmt_dl_header % (config.DOWNLOAD_DIR, config.THREAD_POOL_SIZE)
    #header = util.ljust(header, width)
    sys.stdout.write(log.hl(u' %s'%header,'warning'))
    sys.stdout.write(line)

    fmt_progress = '%s [%s] %.1f%% (%dkib/s)\n'


    all_p = [] #all progress bars, filled by following for loop
    sum_percent = 0 # total percent for running job
    sum_rate = 0 # total rate for running job
    total_percent = 0

    for filename, prog_obj in progress.items():
        percent = prog_obj.percent()
        rate = prog_obj.rate()
        #sum for the total progress
        sum_percent += percent
        sum_rate += rate

        bar = util.ljust('=' * int(percent * bar_count), bar_count)
        per100 = percent * 100 
        single_p =  fmt_progress % \
                (util.rjust(filename,(width - bar_count -22)), bar, per100,rate) # the -20 is for the xx.x% and [ and ] xx.xkb/s (spaces)
        all_p.append(log.hl(single_p,'green'))
    
    #calculate total progress percent
    total_percent = float(sum_percent+done)/total
    
    #global progress
    g_text = msg.fmt_dl_progress % (done, total)
    g_bar = util.ljust('#' * int(total_percent* bar_count), bar_count)
    g_progress =  fmt_progress % \
                (util.rjust(g_text,(width - bar_count -22)), g_bar, 100*total_percent,sum_rate) # the -20 is for the xx.x% and [ and ] xx.xkb/s (spaces)

    #output all total progress bars
    sys.stdout.write(log.hl(u'%s'%g_progress, 'red'))
    sys.stdout.write(sep)

    #output all downloads' progress bars
    sys.stdout.write(''.join(all_p))

    # finished jobs
    if len(done2show):
        sys.stdout.write(line)
        sys.stdout.write(log.hl(msg.fmt_dl_last_finished % config.SHOW_DONE_NUMBER,'warning'))
        sys.stdout.write(line)
        #display finished jobs
        for d in done2show:
            sys.stdout.write(log.hl(u' √ %s\n'% d,'cyan'))

    #failed downloads
    if len(failed_list):
        sys.stdout.write(line)
        sys.stdout.write(log.hl(msg.fmt_dl_failed_jobs,'error'))
        sys.stdout.write(line)
        #display failed jobs
        for failed_song in failed_list:
            sys.stdout.write(log.hl(u' ✘ %s\n' % failed_song.filename,'red'))


    sys.stdout.write(line)
    sys.stdout.flush()
Beispiel #7
0
    def print_progress(self):
        self.__update_status()

        def output(txt):
            sys.stdout.write(txt)

        width = util.get_terminal_size()[1] - 5
        bar_count = (int(width * self.__class__.percent_bar_factor) - 2 / 10
                     )  # number of percent bar
        line = log.hl(u' %s\n' % ('+' * width), 'cyan')
        sep = log.hl(u' %s\n' % ('=' * width), 'cyan')
        output(u'\x1b[2J\x1b[H')  #clear screen
        sys.stdout.write(line)
        header = fmt_dl_header % (self.title, self.pool_size)
        sys.stdout.write(log.hl(u' %s' % header, 'warning'))
        sys.stdout.write(line)

        all_p = []  #all progress bars, filled by following for loop
        sum_percent = 0  # total percent for running job
        sum_rate = 0  # total rate for running job
        total_percent = 0

        for job in [j for j in self.jobs if j.status == JobStatus.Running]:
            prog_obj = job.progress
            percent = prog_obj.percent()
            #sum for the total progress
            sum_percent += percent

            bar = util.ljust('=' * int(percent * bar_count), bar_count)
            per100 = percent * 100
            single_p =  fmt_progress % \
                    (util.rjust(prog_obj.name,(width - bar_count)-32), bar, per100) # the -20 is for the xx.x% and [ and ] xx.xkb/s (spaces)
            all_p.append(log.hl(single_p, 'green'))

        #calculate total progress percent
        total_percent = float(sum_percent + self.done) / self.total

        #global progress
        g_text = fmt_dl_progress % (self.done, self.total)
        g_bar = util.ljust('#' * int(total_percent * bar_count), bar_count)
        g_progress =  fmt_progress % \
                    (util.rjust(g_text,(width - bar_count - 32)), g_bar, 100*total_percent) # the -20 is for the xx.x% and [ and ] xx.xkb/s (spaces)

        #output all total progress bars
        sys.stdout.write(log.hl(u'%s' % g_progress, 'red'))
        sys.stdout.write(sep)

        #output all downloads' progress bars
        sys.stdout.write(''.join(all_p))

        # finished jobs
        if len(self.done2show):
            sys.stdout.write(line)
            sys.stdout.write(
                log.hl(fmt_dl_last_finished % SHOW_DONE_NUMBER, 'warning'))
            sys.stdout.write(line)
            #display finished jobs
            for job in self.done2show:
                sys.stdout.write(log.hl(u' √ %s\n' % job.name, 'cyan'))

        #failed downloads
        if len(self.failed_list):
            sys.stdout.write(line)
            sys.stdout.write(log.hl(fmt_dl_failed_jobs, 'error'))
            sys.stdout.write(line)
            #display failed jobs
            for failed_job in self.failed_list:
                sys.stdout.write(log.hl(u' ✘ %s\n' % failed_job.name, 'red'))

        sys.stdout.write(line)
        sys.stdout.flush()