Example #1
0
        def download_catalogue():
            def find_song_info(catalogue):
                for info in catalogue.infolist():
                    if info.filename.endswith("song_info.xml"):
                        return info.filename
                return None

            def download_progress(copy, complete, total, self):
                self.__load_progress.props.task_progress = min(
                    float(complete) / total, 1.0)

            def download_finished(copy, success, self):
                if not success:
                    print("catalog download failed")
                    print(copy.get_error())
                    return

                print("catalog download successful")
                # done downloading, unzip to real location
                catalog_zip = zipfile.ZipFile(magnatune_song_info_temp)
                catalog = open(magnatune_song_info, 'wb')
                filename = find_song_info(catalog_zip)
                if filename is None:
                    RB.error_dialog(
                        title=_("Unable to load catalog"),
                        message=
                        _("Rhythmbox could not understand the Magnatune catalog, please file a bug."
                          ))
                    return
                catalog.write(catalog_zip.read(filename))
                catalog.close()
                catalog_zip.close()

                df = Gio.file_new_for_path(magnatune_song_info_temp)
                df.delete(None)
                self.__catalogue_loader = None

                self.__load_progress.props.task_outcome = RB.TaskOutcome.COMPLETE

                load_catalogue()

            try:
                df = Gio.file_new_for_path(magnatune_song_info_temp)
                df.delete(None)
            except:
                pass

            self.__load_progress = RB.TaskProgressSimple.new()
            self.__load_progress.props.task_label = _(
                "Loading Magnatune catalog")
            self.props.shell.props.task_list.add_task(self.__load_progress)

            self.__catalog_loader = RB.AsyncCopy()
            self.__catalog_loader.set_progress(download_progress, self)
            self.__catalog_loader.start(magnatune_song_info_uri,
                                        magnatune_song_info_temp,
                                        download_finished, self)
        def download_catalogue():
            def find_song_info(catalogue):
                for info in catalogue.infolist():
                    if info.filename.endswith("song_info.xml"):
                        return info.filename
                return None

            def download_progress(copy, complete, total, self):
                self.__load_progress = (complete, total)
                self.__notify_status_changed()

            def download_finished(copy, success, self):
                if not success:
                    print "catalog download failed"
                    print copy.get_error()
                    return

                print "catalog download successful"
                # done downloading, unzip to real location
                catalog_zip = zipfile.ZipFile(magnatune_song_info_temp)
                catalog = open(magnatune_song_info, 'w')
                filename = find_song_info(catalog_zip)
                if filename is None:
                    RB.error_dialog(
                        title=_("Unable to load catalog"),
                        message=
                        _("Rhythmbox could not understand the Magnatune catalog, please file a bug."
                          ))
                    return
                catalog.write(catalog_zip.read(filename))
                catalog.close()
                catalog_zip.close()

                df = Gio.file_new_for_path(magnatune_song_info_temp)
                df.delete(None)
                self.__updating = False
                self.__catalogue_loader = None
                self.__notify_status_changed()

                load_catalogue()

            self.__updating = True

            try:
                df = Gio.file_new_for_path(magnatune_song_info_temp)
                df.delete(None)
            except:
                pass
            self.__catalog_loader = RB.AsyncCopy()
            self.__catalog_loader.set_progress(download_progress, self)
            self.__catalog_loader.start(magnatune_song_info_uri,
                                        magnatune_song_info_temp,
                                        download_finished, self)
Example #3
0
    def __download_album(self, audio_dl_uri, sku):
        def update_progress(self):
            complete, total = map(sum, zip(*self.__downloads.values()))
            if total > 0:
                self.__download_progress.props.task_progress = min(
                    float(complete) / total, 1.0)

        def download_progress(copy, complete, total, self):
            self.__downloads[audio_dl_uri] = (complete, total)
            update_progress(self)

        def download_finished(copy, success, self):
            del self.__downloads[audio_dl_uri]
            del self.__copies[audio_dl_uri]
            update_progress(self)

            print("download of %s finished: %s" % (audio_dl_uri, success))
            if success:
                threading.Thread(target=unzip_album).start()
            else:
                remove_download_files()

            if len(self.__downloads) == 0:
                self.__download_progress.props.task_outcome = RB.TaskOutcome.COMPLETE
                self.__download_progress = None

        def unzip_album():
            # just use the first library location
            library = Gio.Settings("org.gnome.rhythmbox.rhythmdb")
            library_location = Gio.file_new_for_uri(library['locations'][0])

            print("unzipping %s" % dest.get_path())
            album = zipfile.ZipFile(dest.get_path())
            for track in album.namelist():
                track_uri = library_location.resolve_relative_path(
                    track).get_uri()
                print("zip file entry: %s => %s" % (track, track_uri))

                track_uri = RB.sanitize_uri_for_filesystem(track_uri)
                RB.uri_create_parent_dirs(track_uri)

                track_out = Gio.file_new_for_uri(track_uri).create(
                    Gio.FileCreateFlags.NONE, None)
                if track_out is not None:
                    track_out.write(album.read(track), None)
                    track_out.close(None)
                    print("adding %s to library" % track_uri)
                    self.__db.add_uri(track_uri)

            album.close()
            remove_download_files()

        def remove_download_files():
            print("removing download files")
            in_progress.delete(None)
            dest.delete(None)

        in_progress = magnatune_in_progress_dir.resolve_relative_path(
            "in_progress_" + sku)
        dest = magnatune_in_progress_dir.resolve_relative_path(sku)

        in_progress.replace_contents(
            audio_dl_uri.encode('utf-8'), None, False,
            Gio.FileCreateFlags.PRIVATE
            | Gio.FileCreateFlags.REPLACE_DESTINATION, None)

        try:
            # For some reason, Gio.FileCopyFlags.OVERWRITE doesn't work for copy_async
            dest.delete(None)
        except:
            pass

        if self.__download_progress is None:
            self.__download_progress = RB.TaskProgressSimple.new()
            self.__download_progress.props.task_label = _(
                "Downloading from Magnatune")
            self.__download_progress.connect('cancel', self.cancel_downloads)

        dl = RB.AsyncCopy()
        dl.set_progress(download_progress, self)
        dl.start(audio_dl_uri, dest.get_uri(), download_finished, self)
        self.__downloads[audio_dl_uri] = (0, 0)  # (current, total)
        self.__copies[audio_dl_uri] = dl
    def __download_album(self, audio_dl_uri, sku):
        def download_progress(copy, complete, total, self):
            self.__downloads[audio_dl_uri] = (complete, total)
            self.__notify_status_changed()

        def download_finished(copy, success, self):
            del self.__downloads[audio_dl_uri]
            del self.__copies[audio_dl_uri]

            print "download of %s finished: %s" % (audio_dl_uri, success)
            if success:
                threading.Thread(target=unzip_album).start()
            else:
                remove_download_files()

            if len(self.__downloads) == 0:  # All downloads are complete
                shell = self.props.shell
                manager = shell.props.ui_manager
                manager.get_action(
                    "/MagnatuneSourceViewPopup/MagnatuneCancelDownload"
                ).set_sensitive(False)
                if success:
                    shell.notify_custom(
                        4000, _("Finished Downloading"),
                        _("All Magnatune downloads have been completed."),
                        None, False)

            self.__notify_status_changed()

        def unzip_album():
            # just use the first library location
            library = Gio.Settings("org.gnome.rhythmbox.rhythmdb")
            library_location = Gio.file_new_for_uri(library['locations'][0])

            print "unzipping %s" % dest.get_path()
            album = zipfile.ZipFile(dest.get_path())
            for track in album.namelist():
                track_uri = library_location.resolve_relative_path(
                    track).get_uri()
                print "zip file entry: %s => %s" % (track, track_uri)

                track_uri = RB.sanitize_uri_for_filesystem(track_uri)
                RB.uri_create_parent_dirs(track_uri)

                track_out = Gio.file_new_for_uri(track_uri).create(
                    Gio.FileCreateFlags.NONE, None)
                if track_out is not None:
                    track_out.write(album.read(track), None)
                    track_out.close(None)
                    print "adding %s to library" % track_uri
                    self.__db.add_uri(track_uri)

            album.close()
            remove_download_files()

        def remove_download_files():
            print "removing download files"
            in_progress.delete(None)
            dest.delete(None)

        in_progress = magnatune_in_progress_dir.resolve_relative_path(
            "in_progress_" + sku)
        dest = magnatune_in_progress_dir.resolve_relative_path(sku)

        in_progress.replace_contents(
            str(audio_dl_uri), None, False, Gio.FileCreateFlags.PRIVATE
            | Gio.FileCreateFlags.REPLACE_DESTINATION, None)

        shell = self.props.shell
        manager = shell.props.ui_manager
        manager.get_action("/MagnatuneSourceViewPopup/MagnatuneCancelDownload"
                           ).set_sensitive(True)

        try:
            # For some reason, Gio.FileCopyFlags.OVERWRITE doesn't work for copy_async
            dest.delete(None)
        except:
            pass

        dl = RB.AsyncCopy()
        dl.set_progress(download_progress, self)
        dl.start(audio_dl_uri, dest.get_uri(), download_finished, self)
        self.__downloads[audio_dl_uri] = (0, 0)  # (current, total)
        self.__copies[audio_dl_uri] = dl