spotipy-dev / spotipy

A light weight Python library for the Spotify Web API
http://spotipy.readthedocs.org
MIT License
4.98k stars 953 forks source link

Issue with Tags in Playlist Descriptions Causing HTML Rendering Problems #1006

Closed Kudzmat closed 3 months ago

Kudzmat commented 1 year ago

Describe the bug

The .category_playlists() method in Spotipy is returning playlists with descriptions that contain HTML tags. While some descriptions render correctly, others have embedded HTML tags, causing issues when displaying playlists on HTML documents.

Here are two examples to illustrate the problem:

  1. Good Descriptions in the R&B playlists:

The pulse of R&B music today. Cover: Mahalia This is R&B in Canada, elevate your aura with Roy Woods. 100% British - 100% R&B. Cover: Kali Claire A collection R&B and Afropop cuts from West Africa’s finest artists. Cover: Chike Enjoy trendy & chill Korean R&B music. (Cover: UNE(으네))

  1. Bad Descriptions in Christian & Gospel:

The best mix of today's Christian music, all genres, worldwide. Cover: Forrest Frank and Chandler Moore of Maverick City Music Las canciones de fe que más se escuchan en Latinoamérica. Foto: Marcos Witt Experience the powerful messages and sounds of the current hits and classic Gospel songs. Cover: The hosts of this year's Stellar Awards , Tasha Cobbs Leonard and Jonathan McReynolds No need to wait! Experience today's best Worship music right here, right now, on Spotify. Cover: Passion Get lifted by the waves of faith-infused music. Cover: Stellar Award winning artist Pastor Mike Jr..

code snippet Python code snippet:

play_list_info = playlists['playlists']['items']

    for info in play_list_info:
        # Setting our information to variables
        playlist_name = info['name']
        description = info['description']
        link = info['external_urls']['spotify']
        image = info['images'][0]['url']

        # adding our information using the playlist name as a key and a list of the other info as values
        recommendations[playlist_name] = [description, link, image]
<section>
      <div class="container">
          <br>
        <div class="row">
          {% for name,info in recommendations.items %}
          <div class="col-sm-2">
              <h3>{{ name }}</h3>
              <a href="{{ info.1 }}">
                  <img src="{{ info.2 }}" class="img-fluid">
              </a>
            <figcaption><strong>{{ info.0 }}</strong></figcaption>
          </div>
          {% endfor %}
        </div>
    </div>
    </section>

In the "bad" description, the presence of the tag results in the rendering of an anchor link that may lead to unintended behavior and disrupt the appearance of the playlist on HTML documents.

Expected Behavior The .category_playlists() method should return playlist descriptions without any HTML tags or, alternatively, provide a sanitized version of the description suitable for direct rendering in HTML documents.

Steps to Reproduce Call the .category_playlists() method with appropriate parameters to retrieve playlists. Iterate through the playlist objects and access the description field. Observe that some descriptions contain HTML tags while others do not.

Environment:

  • OS: Mac
  • Python version: Python 3.9.13
  • spotipy version: 2.22.1
  • IDE: PyCharm

In conclusion

The presence of HTML tags in playlist descriptions makes it challenging to directly render playlists on web applications or HTML-based platforms. Please let me know if any further information is needed to resolve this bug.

dieser-niko commented 1 year ago

The spotipy library just returns the content received from Spotify. The only modification is to convert the text to JSON. If you have trouble with the HTML tags and just want to remove them, you might want to take a look at this neat solution: https://stackoverflow.com/a/12982689

Kudzmat commented 1 year ago

Thanks, man, this looks like a great solution to the problem!

dieser-niko commented 3 months ago

Closing as it appears that your issue has been resolved.