abhishekmj303 / ytm2spt

Transfer Playlists from YouTube Music to Spotify
GNU General Public License v3.0
106 stars 7 forks source link

Unicode logging error #2

Closed ukanuk closed 5 months ago

ukanuk commented 5 months ago

Thank you for this library, successfully got more than 80% of my songs imported (at least the ones that exist on Spotify; quite a few of my songs are remixes which are only on YouTube / Soundcloud, so obviously those aren't on Spotify at all).

This playlist won't import at all: https://www.youtube.com/watch?v=7OqwKfgLaeA&list=PLwfik5yb4m-q6MRkUgdIlLdsrHHVJFOMi

INFO - Youtube Playlist ID: PLwfik5yb4m-q6MRkUgdIlLdsrHHVJFOMi
INFO - Youtube Playlist Name: 70s/80s
INFO - Spotify Playlist ID: 6Z5nyHhh8paFK1nN1VmkXO
INFO - Spotify Playlist Name: 70s/80s
urllib3.exceptions.SSLError: EOF occurred in violation of protocol (_ssl.c:2406)

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "C:\Users\nukmi\AppData\Local\Programs\Python\Python312\Lib\site-packages\requests\adapters.py", line 486, in send
    resp = conn.urlopen(
           ^^^^^^^^^^^^^
  File "C:\Users\nukmi\AppData\Local\Programs\Python\Python312\Lib\site-packages\urllib3\connectionpool.py", line 847, in urlopen
    retries = retries.increment(
              ^^^^^^^^^^^^^^^^^^
  File "C:\Users\nukmi\AppData\Local\Programs\Python\Python312\Lib\site-packages\urllib3\util\retry.py", line 515, in increment
    raise MaxRetryError(_pool, url, reason) from reason  # type: ignore[arg-type]
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
urllib3.exceptions.MaxRetryError: HTTPSConnectionPool(host='api.spotify.com', port=443): Max retries exceeded with url: /v1/playlists/6Z5nyHhh8paFK1nN1VmkXO/images (Caused by SSLError(SSLEOFError(8, 'EOF occurred in violation of protocol (_ssl.c:2406)')))

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\nukmi\Downloads\ytm2spt-main\src\main.py", line 154, in <module>
    set_yt_thumbnail_as_sp_cover()
  File "C:\Users\nukmi\Downloads\ytm2spt-main\src\main.py", line 61, in set_yt_thumbnail_as_sp_cover
    sp.set_playlist_cover(encoded_img)
  File "C:\Users\nukmi\Downloads\ytm2spt-main\src\spotify.py", line 207, in set_playlist_cover
    response = requests.put(
               ^^^^^^^^^^^^^
  File "C:\Users\nukmi\AppData\Local\Programs\Python\Python312\Lib\site-packages\requests\api.py", line 130, in put
    return request("put", url, data=data, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\nukmi\AppData\Local\Programs\Python\Python312\Lib\site-packages\requests\api.py", line 59, in request
    return session.request(method=method, url=url, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\nukmi\AppData\Local\Programs\Python\Python312\Lib\site-packages\requests\sessions.py", line 589, in request
    resp = self.send(prep, **send_kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\nukmi\AppData\Local\Programs\Python\Python312\Lib\site-packages\requests\sessions.py", line 703, in send
    r = adapter.send(request, **kwargs)
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\nukmi\AppData\Local\Programs\Python\Python312\Lib\site-packages\requests\adapters.py", line 517, in send
    raise SSLError(e, request=request)
requests.exceptions.SSLError: HTTPSConnectionPool(host='api.spotify.com', port=443): Max retries exceeded with url: /v1/playlists/6Z5nyHhh8paFK1nN1VmkXO/images (Caused by SSLError(SSLEOFError(8, 'EOF occurred in violation of protocol (_ssl.c:2406)')))

Also got errors for a few songs on other playlists, thought you might be interested. Seems to be something to do with when it finds a Spotify song using unicode characters, e.g. YouTube Via Gra - LML converts to Spotify VIA Gra - ЛМЛ (Лучик мой, любимый) but returns these errors:

Fuzy match found [('VIA Gra', 100)]
--- Logging error ---
Traceback (most recent call last):
  File "C:\Users\nukmi\AppData\Local\Programs\Python\Python312\Lib\logging\__init__.py", line 1163, in emit
    stream.write(msg + self.terminator)
  File "C:\Users\nukmi\AppData\Local\Programs\Python\Python312\Lib\encodings\cp1252.py", line 19, in encode
    return codecs.charmap_encode(input,self.errors,encoding_table)[0]
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
UnicodeEncodeError: 'charmap' codec can't encode characters in position 17-19: character maps to <undefined>
Call stack:
  File "C:\Users\nukmi\Downloads\ytm2spt-main\src\main.py", line 185, in <module>
    ytm2spt_logger.info(
Message: 'VIA Gra - Виа Гра - ЛМЛ was added to playlist.'
Arguments: ()
INFO - VIA Gra - Виа Гра - ЛМЛ was added to playlist.

Side note, here's a few issues I ran into trying to use this on Windows. Perhaps you might consider adding this pitfalls to the README for other Windows users like me. It took me a half-day to tinker around and figure them out:

  1. Have to create folder named tmp in C:\ so that logging will work as expected
  2. set for Windows environment variables doesn't set them globally and python likely won't recognize them. Use setx instead, and put them in a batch file so they can be
  3. For SPOTIFY_REDIRECT_URI, ensure there are not single quotes around the URL; this will cause error INVALID_CLIENT: Invalid redirect URI when trying to run the script. Here's my batch file env.bat (with secrets obfuscated), note that I use double quotes to escape the string instead of single.
    setx SPOTIFY_USER_ID asdf0123
    setx SPOTIFY_CLIENT_ID asdf0123
    setx SPOTIFY_CLIENT_SECRET asdf0123
    setx SPOTIFY_REDIRECT_URI "http://localhost:8888/callback"
abhishekmj303 commented 5 months ago

@ukanuk Thanks for the detailed bug report. I'll look into this and try to resolve it.

abhishekmj303 commented 5 months ago

Feel free to reopen this if were not fixed with update. As I don't have windows I can't exactly test them.

ukanuk commented 5 months ago

I can confirm both updates are working on my Windows computer! Thank you!