gui.py 文件源码

python
阅读 23 收藏 0 点赞 0 评论 0

项目:Music-Scraper 作者: srivatsan-ramesh 项目源码 文件源码
def download_item():
        """
        It downloads the song from the url and saves it in the file.
        """
        if GUI.run_download or GUI.gui_stopped:
            return
        GUI.run_download = True
        filename = GUI.strings[GUI.position - 1]
        url = GUI.url_dict[filename]
        # User agent is specified to prevent some websites from blocking the software
        req = request.Request(url, headers={'User-Agent': 'Mozilla/5.0'})
        u = request.urlopen(req)
        f = open(filename, 'wb')

        file_size_dl = 0
        block_sz = 8192
        while GUI.run_download:
            buffer = u.read(block_sz)
            if not buffer:
                break
            file_size_dl += len(buffer)
            f.write(buffer)
            # The percentage downloaded
            status = "Downloading " + filename + " [%3.2f%%]" % (file_size_dl * 100. / GUI.size_dict[filename])
            GUI.box.erase()
            GUI.box.addstr(1, 1, status, GUI.high_light_text)
            GUI.box.addstr(2, 1, 'Size: %.2fMB' % (GUI.size_dict[filename]/1024/1024))
            # Cancel button to cancel the download
            GUI.box.addstr(curses.LINES - 1, 0, "C:Cancel Download", GUI.high_light_text)
            GUI.screen.refresh()
            GUI.box.refresh()

        f.close()
        GUI.run_download = False
        GUI.key = curses.KEY_DOWN
        GUI.update_screen()
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号