Esempio n. 1
0
    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)
Esempio n. 2
0
        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)
Esempio n. 3
0
    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')
Esempio n. 4
0
    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)
Esempio n. 5
0
    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')
Esempio n. 6
0
    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)
Esempio n. 7
0
    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)