Exemplo n.º 1
0
def ParsePLS(file):
    data = {}

    lines = file.read().decode('utf-8', 'replace').splitlines()

    if not lines or "[playlist]" not in lines.pop(0):
        return []

    for line in lines:
        try:
            head, val = line.strip().split("=", 1)
        except (TypeError, ValueError):
            continue
        else:
            head = head.lower()
            if head.startswith("length") and val == "-1":
                continue
            else:
                data[head] = val

    count = 1
    files = []
    warnings = []
    while True:
        if "file%d" % count in data:
            filename = text2fsn(data["file%d" % count])
            if filename.lower()[-4:] in [".pls", ".m3u"]:
                warnings.append(filename)
            else:
                irf = IRFile(filename)
                for key in ["title", "genre", "artist"]:
                    try:
                        irf[key] = data["%s%d" % (key, count)]
                    except KeyError:
                        pass
                try:
                    irf["~#length"] = int(data["length%d" % count])
                except (KeyError, TypeError, ValueError):
                    pass
                files.append(irf)
        else:
            break
        count += 1

    if warnings:
        WarningMessage(
            None, _("Unsupported file type"),
            _("Station lists can only contain locations of stations, "
              "not other station lists or playlists. The following locations "
              "cannot be loaded:\n%s") %
            "\n  ".join(map(util.escape, warnings))
        ).run()

    return files
Exemplo n.º 2
0
    def __add_stations(self, irfs: Collection[IRFile], uri: str) -> None:
        print_d(f"Got {len(irfs)} station(s) from {uri}")
        assert self.__fav_stations
        if not irfs:
            msg = ErrorMessage(
                self, _("No stations found"),
                _("No Internet radio stations were found at %s.") %
                util.escape(uri))
            msg.run()
            return

        fav_uris = {af("~uri") for af in self.__fav_stations}
        irfs = {af for af in irfs if af("~uri") not in fav_uris}
        if irfs:
            print_d(f"Adding {irfs} to favourites")
            self.__fav_stations.add(irfs)
        else:
            message = WarningMessage(
                self, _("Nothing to add"),
                _("All stations listed are already in your library."))
            message.run()
Exemplo n.º 3
0
    def __add_station(self, uri):
        irfs = add_station(uri)
        if irfs is None:
            # Error, rather than empty list
            return
        if not irfs:
            ErrorMessage(
                None, _("No stations found"),
                _("No Internet radio stations were found at %s.") %
                util.escape(uri)).run()
            return

        irfs = set(irfs) - set(self.__fav_stations)
        if not irfs:
            WarningMessage(
                None, _("Unable to add station"),
                _("All stations listed are already in your library.")).run()

        if irfs:
            self.__fav_stations.add(irfs)
Exemplo n.º 4
0
    def __add_station(self, uri):
        try:
            irfs = _get_stations_from(uri)
        except EnvironmentError as e:
            print_d("Got %s from %s" % (e, uri))
            msg = ("Couldn't add URL: <b>%s</b>)\n\n<tt>%s</tt>" %
                   (escape(str(e)), escape(uri)))
            ErrorMessage(None, _("Unable to add station"), msg).run()
            return
        if not irfs:
            ErrorMessage(
                None, _("No stations found"),
                _("No Internet radio stations were found at %s.") %
                util.escape(uri)).run()
            return

        irfs = set(irfs) - set(self.__fav_stations)
        if not irfs:
            WarningMessage(
                None, _("Unable to add station"),
                _("All stations listed are already in your library.")).run()

        if irfs:
            self.__fav_stations.add(irfs)