def plugin_songs(self, songs): # Check this is a launch, not a configure if self.chosen_site: url_pat = self.get_url_pattern(self.chosen_site) pat = Pattern(url_pat) urls = set() for song in songs: # Generate a sanitised AudioFile; allow through most tags subs = AudioFile() for k in (USER_TAGS + MACHINE_TAGS): vals = song.comma(k) if vals: try: encoded = unicode(vals).encode('utf-8') subs[k] = (encoded if k == 'website' else quote_plus(encoded)) # Dodgy unicode problems except KeyError: print_d("Problem with %s tag values: %r" % (k, vals)) url = str(pat.format(subs)) if not url: print_w("Couldn't build URL using \"%s\"." "Check your pattern?" % url_pat) return # Grr, set.add() should return boolean... if url not in urls: urls.add(url) website(url)
def plugin_songs(self, songs): # Check this is a launch, not a configure if self.chosen_site: url_pat = self.get_url_pattern(self.chosen_site) pat = Pattern(url_pat) urls = set() for song in songs: # Generate a sanitised AudioFile; allow through most tags subs = AudioFile() for k in (USER_TAGS + MACHINE_TAGS): vals = song.comma(k) if vals: try: encoded = text_type(vals).encode('utf-8') subs[k] = (encoded if k == 'website' else quote_plus(encoded)) # Dodgy unicode problems except KeyError: print_d("Problem with %s tag values: %r" % (k, vals)) url = str(pat.format(subs)) if not url: print_w("Couldn't build URL using \"%s\"." "Check your pattern?" % url_pat) return # Grr, set.add() should return boolean... if url not in urls: urls.add(url) website(url)
def __new__(klass, value, escaped=True): """Create a new URI object. By default, the URI is assumed to be escaped already. Pass escaped=False if you need the URI escaped (this is imperfect now). The URI returned will be equivalent, but not necessarily equal, to the value passed in.""" values = list(urlparse(value)) if not escaped: # FIXME: Handle netloc # FIXME: Handle query args values[2] = quote_plus(values[2], safe="/~") value = _urlunparse(values) obj = str.__new__(klass, value) # len() > 1 to not interpret windows paths as URIs # there are no schemes if not obj.scheme or not len(obj.scheme) > 1: raise ValueError("URIs must have a scheme, such as 'http://'") elif not (obj.netloc or obj.path): raise ValueError("URIs must have a network location or path") else: return obj
def website_for(pat: Pattern, song: AudioFile) -> Optional[str]: """Gets a utf-8 encoded string for a website from the given pattern""" # Generate a sanitised AudioFile; allow through most tags subs = AudioFile() # See issue 2762 for k in (USER_TAGS + MACHINE_TAGS + ['~filename']): vals = song.comma(k) if vals: try: # Escaping ~filename stops ~dirname ~basename etc working # But not escaping means ? % & will cause problems. # Who knows what user wants to do with /, seems better raw. subs[k] = (vals if k in ['website', '~filename'] else quote_plus(vals)) except KeyError: print_d("Problem with %s tag values: %r" % (k, vals)) return pat.format(subs) or None
def _format(self, key, value): return quote_plus(value.encode('utf8'))
def _format(self, key, value): return quote_plus(value.encode("utf8"))