def get_artist_info(artistid, artist=None): '''Get artist info, if cached, just return it. Artist pic is also retrieved and saved to info['pic'] ''' if artistid == 0: url = ''.join([ SEARCH, 'stype=artistinfo&artist=', Utils.encode_uri(artist), ]) else: url = ''.join([ SEARCH, 'stype=artistinfo&artistid=', str(artistid), ]) req_content = urlopen(url) if not req_content: return None info = Utils.json_loads_single(req_content.decode()) if not info: return None # set logo size to 100x100 pic_path = info['pic'] url = get_artist_pic_url(pic_path) if url: info['pic'] = get_image(url) else: info['pic'] = None return info
def get_artist_info(artistid, artist=None): ''' Get artist info, if cached, just return it. Artist pic is also retrieved and saved to info['pic'] ''' if artistid == 0: url = ''.join([ SEARCH, 'stype=artistinfo&artist=', Utils.encode_uri(artist), ]) else: url = ''.join([ SEARCH, 'stype=artistinfo&artistid=', str(artistid), ]) #print('Net.get_artist_info, url:', url) req_content = urlopen(url) if req_content is None: return None try: info = Utils.json_loads_single(req_content.decode()) except Exception as e: print('Error: Net.get_artist_info:', e, 'with url:', url) return None # set logo size to 100x100 pic_path = info['pic'] url = get_artist_pic_url(pic_path) if url: info['pic'] = get_image(url) else: info['pic'] = None return info
def _wrap(req): received_size = 0 can_play_emited = False content_length = int(req.headers.get('Content-Length')) print('size of file: ', round(content_length / 2**20, 2), 'M') fh = open(song_path, 'wb') while True: if self.force_quit: del req fh.close() os.remove(song_path) return chunk = req.read(CHUNK) received_size += len(chunk) percent = int(received_size/content_length * 100) self.emit('chunk-received', percent) print('percentage:', percent) # this signal only emit once. if (received_size > CHUNK_TO_PLAY or percent > 40) \ and not can_play_emited: print('song can be played now') can_play_emited = True self.emit('can-play', song_path, 'OK') if not chunk: break fh.write(chunk) fh.close() print('song downloaded') self.emit('downloaded', song_path) Utils.iconvtag(song_path, song)
def _download_song(self, song, use_mv): cached, song_link, song_path = get_song_link( song, self.app.conf, use_mv=use_mv) # check song already cached if cached: self.emit('can-play', song_path) self.emit('downloaded', song_path) return # this song has no link to download if not song_link: self.emit('network-error', song_link) return chunk_to_play = CHUNK_TO_PLAY if self.app.conf['use-ape']: chunk_to_play = CHUNK_APE_TO_PLAY if use_mv: chunk_to_play = CHUNK_MV_TO_PLAY for retried in range(MAXTIMES): try: req = request.urlopen(song_link) received_size = 0 can_play_emited = False content_length = int(req.headers.get('Content-Length')) fh = open(song_path, 'wb') while True: if self.force_quit: del req fh.close() os.remove(song_path) return chunk = req.read(CHUNK) received_size += len(chunk) percent = received_size / content_length self.emit('chunk-received', percent) # this signal only emit once. if ((received_size > chunk_to_play or percent > 40) and not can_play_emited): can_play_emited = True self.emit('can-play', song_path) if not chunk: break fh.write(chunk) fh.close() self.emit('downloaded', song_path) Utils.iconvtag(song_path, song) return except URLError as e: pass except FileNotFoundError as e: self.emit('disk-error', song_path) return if os.path.exists(song_path): os.remove(song_path) self.emit('network-error', song_link)
def get_artist_info(artistid, artist=None): '''Get artist info, if cached, just return it. Artist pic is also retrieved and saved to info['pic'] ''' if artistid == 0: url = ''.join([ SEARCH, 'stype=artistinfo&artist=', Utils.encode_uri(artist), ]) else: url = ''.join([ SEARCH, 'stype=artistinfo&artistid=', str(artistid), ]) #print('Net.get_artist_info, url:', url) req_content = urlopen(url) if not req_content: return None try: info = Utils.json_loads_single(req_content.decode()) except ValueError as e: print('Error: Net.get_artist_info:', e, 'with url:', url) return None # set logo size to 100x100 pic_path = info['pic'] url = get_artist_pic_url(pic_path) if url: info['pic'] = get_image(url) else: info['pic'] = None return info
def _parse_lrc(): url = ('http://newlyric.kuwo.cn/newlyric.lrc?' + Utils.encode_lrc_url(rid)) req_content = urlopen(url, use_cache=False, retries=8) if not req_content: return None try: lrc = Utils.decode_lrc_content(req_content) return lrc except Exception as e: return None
def _parse_lrc(): url = ('http://newlyric.kuwo.cn/newlyric.lrc?' + Utils.encode_lrc_url(rid)) req_content = urlopen(url, use_cache=False) if req_content is None: return None try: lrc = Utils.decode_lrc_content(req_content) except Exception as e: print('Error: Net.get_lrc:', e, 'with url:', url) return None return lrc
def _parse_lrc(): url = ('http://newlyric.kuwo.cn/newlyric.lrc?' + Utils.encode_lrc_url(rid)) #print('Net._parse_lrc(), url:', url) req_content = urlopen(url, use_cache=False, retries=8) if not req_content: return None try: lrc = Utils.decode_lrc_content(req_content) return lrc except Exception as e: print('Error: Net.get_lrc:', e, 'with url:', url) return None
def get_artist_mv(artistid, page): ''' http://search.kuwo.cn/r.s?stype=mvlist&artistid=336&sortby=0&rn=20&pn=0 ''' url = ''.join([ SEARCH, 'stype=mvlist&sortby=0&rn=', str(ICON_NUM), '&artistid=', str(artistid), '&pn=', str(page), ]) print('Net.get_artist_mv(), url:', url) req_content = urlopen(url) if req_content is None: return (None, 0) try: mvs_wrap = Utils.json_loads_single(req_content.decode()) except Error as e: print('Error: Net.get_artist_mv:', e, 'with url:', url) return (None, 0) mvs = mvs_wrap['mvlist'] pages = math.ceil(int(mvs_wrap['total']) / ICON_NUM) return (mvs, pages)
def get_artist_songs_by_id(artistid, page): ''' http://search.kuwo.cn/r.s?stype=artist2music&artistid=266&pn=0&rn=20 ''' url = ''.join([ SEARCH, 'stype=artist2music&artistid=', str(artistid), '&rn=', str(SONG_NUM), '&pn=', str(page), ]) #print('Net.get_artist_songs_by_id()', url) req_content = urlopen(url) if req_content is None: return (None, 0) try: songs_wrap = Utils.json_loads_single(req_content.decode()) except Exception as e: print('Error: Net.get-artist_songs_by_id:', e, 'with url:', url) return (None, 0) songs = songs_wrap['musiclist'] pages = math.ceil(int(songs_wrap['total']) / SONG_NUM) return (songs, pages)
def search_artists(keyword, page): url = ''.join([ SEARCH, 'ft=artist&pn=', str(page), '&rn=', str(ICON_NUM), '&newsearch=1&primitive=0&cluster=0', '&itemset=newkm&rformat=json&encoding=utf8&all=', parse.quote(keyword), ]) #print('Net.search_artists(), ', url) if url not in req_cache: req_content = urlopen(url, use_cache=False) if req_content is None: return (None, 0, 0) req_cache[url] = req_content try: artists_wrap = Utils.json_loads_single(req_cache[url].decode()) except Exception as e: print('Error: Net.search_artists():', e, 'with url:', url) return (None, 0, 0) hit = int(artists_wrap['TOTAL']) pages = math.ceil(hit / SONG_NUM) artists = artists_wrap['abslist'] return (artists, hit, pages)
def search_songs(keyword, page): url = ''.join([ SEARCH, 'ft=music&rn=', str(SONG_NUM), '&newsearch=1&primitive=0&cluster=0', '&itemset=newkm&rformat=json&encoding=utf8&all=', parse.quote(keyword), '&pn=', str(page), ]) #print('search songs:', url) if url not in req_cache: req_content = urlopen(url, use_cache=False) if not req_content: return (None, 0, 0) req_cache[url] = req_content try: songs_wrap = Utils.json_loads_single(req_cache[url].decode()) except ValueError as e: print('Error: Net.search_song:', e, 'with url:', url) return (None, 0, 0) hit = int(songs_wrap['TOTAL']) pages = math.ceil(hit / SONG_NUM) songs = songs_wrap['abslist'] return (songs, hit, pages)
def search_albums(keyword, page): url = ''.join([ SEARCH, 'ft=album&pn=', str(page), '&rn=', str(ICON_NUM), '&newsearch=1&primitive=0&cluster=0', '&itemset=newkm&rformat=json&encoding=utf8&all=', parse.quote(keyword), ]) #print('search_albums:', url) if url not in req_cache: req_content = urlopen(url, use_cache=False) if not req_content: return (None, 0, 0) req_cache[url] = req_content try: albums_wrap = Utils.json_loads_single(req_cache[url].decode()) except ValueError as e: print('Error: Net.search_albums():', e, 'with url:', url) return (None, 0, 0) hit = int(albums_wrap['total']) pages = math.ceil(hit / ICON_NUM) albums = albums_wrap['albumlist'] return (albums, hit, pages)
def get_artist_similar(artistid, page): ''' http://search.kuwo.cn/r.s?stype=similarartist&artistid=336&sortby=0&rn=20&pn=0 Only has 10 items ''' url = ''.join([ SEARCH, 'stype=similarartist&sortby=0&rn=', str(ICON_NUM), '&pn=', str(page), '&artistid=', str(artistid), ]) #print('Net.get_artist_similar(), url:', url) req_content = urlopen(url) if req_content is None: return (None, 0) try: artists_wrap = Utils.json_loads_single(req_content.decode()) except Exception as e: print('Error: Net.get_artist_similar:', e, 'with url:', url) return (None, 0) artists = artists_wrap['artistlist'] pages = math.ceil(int(artists_wrap['total']) / ICON_NUM) return (artists, pages)
def get_album(albumid): url = '{0}stype=albuminfo&albumid={1}'.format(SEARCH, albumid) req_content = urlopen(url) if not req_content: return None songs_wrap = Utils.json_loads_single(req_content.decode()) if not songs_wrap: return None songs = songs_wrap['musiclist'] return songs
def get_recommend_lists(artist): url = ''.join([ 'http://artistpicserver.kuwo.cn/pic.web?', 'type=big_artist_pic&pictype=url&content=list&&id=0&from=pc', '&name=', Utils.encode_uri(artist), ]) req_content = urlopen(url) if not req_content: return None return req_content.decode()
def get_radio_songs(nid, offset): url = ''.join([ 'http://gxh2.kuwo.cn/newradio.nr?', 'type=4&uid=0&login=0&size=20&fid=', str(nid), '&offset=', str(offset), ]) req_content = urlopen(url) if not req_content: return None songs = Utils.parse_radio_songs(req_content.decode('gbk')) return songs
def get_album(albumid): url = ''.join([ SEARCH, 'stype=albuminfo&albumid=', str(albumid), ]) req_content = urlopen(url) if not req_content: return None try: songs_wrap = Utils.json_loads_single(req_content.decode()) except ValueError as e: return None songs = songs_wrap['musiclist'] return songs
def get_album(albumid): url = ''.join([ SEARCH, 'stype=albuminfo&albumid=', str(albumid), ]) #print('get_album():', url) req_content = urlopen(url) if req_content is None: return None try: songs_wrap = Utils.json_loads_single(req_content.decode()) except Exception as e: print('Error: Net.get_album()', e, 'with url:', url) return None songs = songs_wrap['musiclist'] return songs
def get_artist_songs_by_id(artistid, page): url = ''.join([ SEARCH, 'stype=artist2music&artistid=', str(artistid), '&rn=', str(SONG_NUM), '&pn=', str(page), ]) req_content = urlopen(url) if not req_content: return (None, 0) songs_wrap = Utils.json_loads_single(req_content.decode()) if not songs_wrap: return (None, 0) songs = songs_wrap['musiclist'] pages = math.ceil(int(songs_wrap['total']) / SONG_NUM) return (songs, pages)
def get_artist_mv(artistid, page): url = ''.join([ SEARCH, 'stype=mvlist&sortby=0&rn=', str(ICON_NUM), '&artistid=', str(artistid), '&pn=', str(page), ]) req_content = urlopen(url) if not req_content: return (None, 0) mvs_wrap = Utils.json_loads_single(req_content.decode()) if not mvs_wrap: return (None, 0) mvs = mvs_wrap['mvlist'] pages = math.ceil(int(mvs_wrap['total']) / ICON_NUM) return (mvs, pages)
def get_artist_songs(artist, page): url = ''.join([ SEARCH, 'ft=music&itemset=newkw&newsearch=1&cluster=0&rn=', str(SONG_NUM), '&pn=', str(page), '&primitive=0&rformat=json&encoding=UTF8&artist=', artist, ]) req_content = urlopen(url) if not req_content: return (None, 0) songs_wrap = Utils.json_loads_single(req_content.decode()) if not songs_wrap: return (None, 0) songs = songs_wrap['abslist'] pages = math.ceil(int(songs_wrap['TOTAL']) / SONG_NUM) return (songs, pages)
def get_artist_albums(artistid, page): url = ''.join([ SEARCH, 'stype=albumlist&sortby=1&rn=', str(ICON_NUM), '&artistid=', str(artistid), '&pn=', str(page), ]) req_content = urlopen(url) if not req_content: return (None, 0) try: albums_wrap = Utils.json_loads_single(req_content.decode()) except ValueError as e: return (None, 0) albums = albums_wrap['albumlist'] pages = math.ceil(int(albums_wrap['total']) / ICON_NUM) return (albums, pages)
def get_artist_similar(artistid, page): '''Only has 10 items''' url = ''.join([ SEARCH, 'stype=similarartist&sortby=0&rn=', str(ICON_NUM), '&pn=', str(page), '&artistid=', str(artistid), ]) req_content = urlopen(url) if not req_content: return (None, 0) artists_wrap = Utils.json_loads_single(req_content.decode()) if not artists_wrap: return (None, 0) artists = artists_wrap['artistlist'] pages = math.ceil(int(artists_wrap['total']) / ICON_NUM) return (artists, pages)
def get_artists(catid, page, prefix): url = ''.join([ ARTIST, 'stype=artistlist&order=hot&rn=', str(ICON_NUM), '&category=', str(catid), '&pn=', str(page), ]) if len(prefix) > 0: url = url + '&prefix=' + prefix req_content = urlopen(url) if not req_content: return (None, 0) artists_wrap = Utils.json_loads_single(req_content.decode()) if not artists_wrap: return (None, 0) pages = int(artists_wrap['total']) artists = artists_wrap['artistlist'] return (artists, pages)
def get_artist_songs_by_id(artistid, page): url = ''.join([ SEARCH, 'stype=artist2music&artistid=', str(artistid), '&rn=', str(SONG_NUM), '&pn=', str(page), ]) #print('Net.get_artist_songs_by_id()', url) req_content = urlopen(url) if not req_content: return (None, 0) try: songs_wrap = Utils.json_loads_single(req_content.decode()) except ValueError as e: print('Error: Net.get-artist_songs_by_id:', e, 'with url:', url) return (None, 0) songs = songs_wrap['musiclist'] pages = math.ceil(int(songs_wrap['total']) / SONG_NUM) return (songs, pages)
def get_artist_mv(artistid, page): url = ''.join([ SEARCH, 'stype=mvlist&sortby=0&rn=', str(ICON_NUM), '&artistid=', str(artistid), '&pn=', str(page), ]) #print('Net.get_artist_mv(), url:', url) req_content = urlopen(url) if not req_content: return (None, 0) try: mvs_wrap = Utils.json_loads_single(req_content.decode()) except ValueError as e: print('Error: Net.get_artist_mv:', e, 'with url:', url) return (None, 0) mvs = mvs_wrap['mvlist'] pages = math.ceil(int(mvs_wrap['total']) / ICON_NUM) return (mvs, pages)
def get_artist_songs(artist, page): url = ''.join([ SEARCH, 'ft=music&itemset=newkw&newsearch=1&cluster=0&rn=', str(SONG_NUM), '&pn=', str(page), '&primitive=0&rformat=json&encoding=UTF8&artist=', artist, ]) #print('Net.get_artist_songs()', url) req_content = urlopen(url) if req_content is None: return (None, 0) try: songs_wrap = Utils.json_loads_single(req_content.decode()) except Exception as e: print('Error: Net.get_artist_songs:', e, 'with url:', url) return (None, 0) songs = songs_wrap['abslist'] pages = math.ceil(int(songs_wrap['TOTAL']) / SONG_NUM) return (songs, pages)
def search_artists(keyword, page): url = ''.join([ SEARCH, 'ft=artist&pn=', str(page), '&rn=', str(ICON_NUM), '&newsearch=1&primitive=0&cluster=0', '&itemset=newkm&rformat=json&encoding=utf8&all=', parse.quote(keyword), ]) if url not in req_cache: req_content = urlopen(url, use_cache=False) if not req_content: return (None, 0, 0) req_cache[url] = req_content artists_wrap = Utils.json_loads_single(req_cache[url].decode()) if not artists_wrap: return (None, 0, 0) hit = int(artists_wrap['TOTAL']) pages = math.ceil(hit / SONG_NUM) artists = artists_wrap['abslist'] return (artists, hit, pages)
def get_artist_similar(artistid, page): '''Only has 10 items''' url = ''.join([ SEARCH, 'stype=similarartist&sortby=0&rn=', str(ICON_NUM), '&pn=', str(page), '&artistid=', str(artistid), ]) #print('Net.get_artist_similar(), url:', url) req_content = urlopen(url) if not req_content: return (None, 0) try: artists_wrap = Utils.json_loads_single(req_content.decode()) except ValueError as e: print('Error: Net.get_artist_similar:', e, 'with url:', url) return (None, 0) artists = artists_wrap['artistlist'] pages = math.ceil(int(artists_wrap['total']) / ICON_NUM) return (artists, pages)
def get_artist_albums(artistid, page): '''http://search.kuwo.cn/r.s?stype=albumlist&artistid=336&sortby=1&rn=20&pn=0 ''' url = ''.join([ SEARCH, 'stype=albumlist&sortby=1&rn=', str(ICON_NUM), '&artistid=', str(artistid), '&pn=', str(page), ]) #print('Net.get_artist_albums(), url:', url) req_content = urlopen(url) if req_content is None: return (None, 0) try: albums_wrap = Utils.json_loads_single(req_content.decode()) except Exception as e: print('Error:', e) return (None, 0) albums = albums_wrap['albumlist'] pages = math.ceil(int(albums_wrap['total']) / ICON_NUM) return (albums, pages)
def get_artists(catid, page, prefix): url = ''.join([ ARTIST, 'stype=artistlist&order=hot&rn=', str(ICON_NUM), '&category=', str(catid), '&pn=', str(page), ]) if len(prefix) > 0: url = url + '&prefix=' + prefix #print('Net.get_artists(), url:', url) req_content = urlopen(url) if not req_content: return (None, 0) try: artists_wrap = Utils.json_loads_single(req_content.decode()) except ValueError as e: print('Error: Net.get_artists:', e, 'with url:', url) return (None, 0) pages = int(artists_wrap['total']) artists = artists_wrap['artistlist'] return (artists, pages)
def open_cache_folder(self, btn): Utils.open_folder(self.app.conf['song-dir'])
def _download_song(self, song, use_mv): song_link, song_path = get_song_link(song, self.app.conf, use_mv=use_mv) print('Async_song.download_song:', song_link, song_path) #print('Net.AsyncSong._download_song() song link:', song_link) chunk_to_play = CHUNK_TO_PLAY if self.app.conf['use-ape']: chunk_to_play = CHUNK_APE_TO_PLAY if use_mv: chunk_to_play = CHUNK_MV_TO_PLAY # this song has no link to download if song_link is False: self.emit('can-play', song_path, 'URLError') return # check lock song exists if song_link is True: self.emit('can-play', song_path, 'OK') self.emit('downloaded', song_path) return for retried in range(MAXTIMES): try: req = request.urlopen(song_link) received_size = 0 can_play_emited = False content_length = int(req.headers.get('Content-Length')) fh = open(song_path, 'wb') while True: if self.force_quit: del req fh.close() os.remove(song_path) return chunk = req.read(CHUNK) received_size += len(chunk) percent = int(received_size / content_length * 100) print(percent) self.emit('chunk-received', percent) # this signal only emit once. if ((received_size > chunk_to_play or percent > 40) and not can_play_emited): can_play_emited = True self.emit('can-play', song_path, 'OK') if not chunk: break fh.write(chunk) fh.close() self.emit('downloaded', song_path) Utils.iconvtag(song_path, song) return except URLError as e: print('AsyncSong._download_song()', e, 'with song_link:', song_link) if os.path.exists(song_path): os.remove(song_path) self.emit('can-play', song_path, 'URLError') else: self.emit('can-play', song_path, 'FileNotFoundError')
def _download_song(self, song, use_mv): cached, song_link, song_path = get_song_link(song, self.app.conf, use_mv=use_mv) # temp file to store data tmp_song_path = song_path + '.part' # check song already cached if cached: self.emit('can-play', song_path) self.emit('downloaded', song_path) return # this song has no link to download if not song_link: self.emit('network-error', song_link) return chunk_to_play = CHUNK_TO_PLAY if self.app.conf['use-ape']: chunk_to_play = CHUNK_APE_TO_PLAY if use_mv: chunk_to_play = CHUNK_MV_TO_PLAY for retried in range(MAXTIMES): try: req = request.urlopen(song_link) received_size = 0 can_play_emited = False content_length = int(req.headers.get('Content-Length')) fh = open(tmp_song_path, 'wb') while True: if self.force_quit: fh.close() if os.path.exists(song_path): os.remove(song_path) return chunk = req.read(CHUNK) received_size += len(chunk) percent = received_size / content_length self.emit('chunk-received', percent) # this signal only emit once. if ((received_size > chunk_to_play or percent > 40) and not can_play_emited): can_play_emited = True self.emit('can-play', tmp_song_path) if not chunk: break fh.write(chunk) fh.close() # download successfully if received_size == content_length: os.rename(tmp_song_path, song_path) self.emit('downloaded', song_path) Utils.iconvtag(song_path, song) return # remove temp file elif os.path.exists(tmp_song_path): os.remove(tmp_song_path) except URLError as e: print('URLError:', e) except FileNotFoundError as e: self.emit('disk-error', song_path) if os.path.exists(tmp_song_path): os.remove(tmp_song_path) return if os.path.exists(tmp_song_path): os.remove(tmp_song_path) self.emit('network-error', song_link)
def _download_song(self, song, use_mv): cached, song_link, song_path = get_song_link(song, self.app.conf, use_mv=use_mv) # temp file to store data tmp_song_path = '{0}-{1}.part'.format(song_path, int(time.time())) # check song already cached if cached: self.emit('can-play', song_path) self.emit('downloaded', song_path) return # this song has no link to download if not song_link: logger.debug('download_song(): %s.' % song) self.emit('network-error', song_link) return if use_mv: chunk_to_play = CHUNK_MV_TO_PLAY else: chunk_to_play = CHUNK_TO_PLAY for retried in range(RETRIES): try: req = request.urlopen(song_link, timeout=TIMEOUT) received_size = 0 can_play_emited = False content_length = int(req.headers.get('Content-Length')) fh = open(tmp_song_path, 'wb') while True: if self.force_quit: if not fh.closed: fh.close() if os.path.exists(song_path): os.remove(song_path) return chunk = req.read(CHUNK) received_size += len(chunk) percent = received_size / content_length if int(percent * 100) % 5 == 0: self.emit('chunk-received', percent) # this signal only emit once. if ((received_size > chunk_to_play or percent > 0.4) and not can_play_emited): self.emit('can-play', tmp_song_path) can_play_emited = True if not chunk: break fh.write(chunk) fh.close() # download successfully if received_size == content_length: os.rename(tmp_song_path, song_path) self.emit('downloaded', song_path) Utils.iconvtag(song_path, song) return else: logger.warn('Net.received_size: %s, content_length: %s' % (received_size, content_length)) self.emit('network-error', song_link) # remove temp file if os.path.exists(tmp_song_path): os.remove(tmp_song_path) break except URLError: logger.error(traceback.format_exc()) except FileNotFoundError: logger.error(traceback.format_exc()) self.emit('disk-error', song_path) if os.path.exists(tmp_song_path): os.remove(tmp_song_path) return if os.path.exists(tmp_song_path): os.remove(tmp_song_path) self.emit('network-error', song_link)
def _download_song(self, song, use_mv): song_link, song_path = get_song_link(song, self.app.conf, use_mv=use_mv) #print('Net.AsyncSong._download_song() song link:', song_link) chunk_to_play = CHUNK_TO_PLAY if self.app.conf['use-ape']: chunk_to_play = CHUNK_APE_TO_PLAY if use_mv: chunk_to_play = CHUNK_MV_TO_PLAY # this song has no link to download if song_link is False: self.emit('can-play', song_path, 'URLError') return # check lock song exists if song_link is True: self.emit('can-play', song_path, 'OK') self.emit('downloaded', song_path) return for retried in range(MAXTIMES): try: req = request.urlopen(song_link) received_size = 0 can_play_emited = False content_length = int(req.headers.get('Content-Length')) fh = open(song_path, 'wb') while True: if self.force_quit: del req fh.close() os.remove(song_path) return chunk = req.read(CHUNK) received_size += len(chunk) percent = int(received_size/content_length * 100) self.emit('chunk-received', percent) # this signal only emit once. if (received_size > chunk_to_play or percent > 40) \ and not can_play_emited: can_play_emited = True self.emit('can-play', song_path, 'OK') if not chunk: break fh.write(chunk) fh.close() self.emit('downloaded', song_path) Utils.iconvtag(song_path, song) return except Exception as e: print('AsyncSong._download_song()', e, 'with song_link:', song_link) if os.path.exists(song_path): os.remove(song_path) self.emit('can-play', song_path, 'URLError') else: self.emit('can-play', song_path, 'FileNotFoundError')