From 329b472551cbfd593832231855367364247e5c5d Mon Sep 17 00:00:00 2001 From: Sean Reifschneider Date: Sat, 6 Jan 2024 18:09:43 -0700 Subject: [PATCH] Switching to be pip installable --- .github/workflows/main.yml | 32 +++++ .github/workflows/release.yml | 47 ++++++++ .gitignore | 1 + LICENSE | 121 +++++++++++++++++++ README.md | 4 + pyproject.toml | 21 ++++ spotify2ytmusic/__init__.py | 0 spotify2ytmusic/cli.py | 221 ++++++++++++++++++++++++++++++++++ 8 files changed, 447 insertions(+) create mode 100644 .github/workflows/main.yml create mode 100644 .github/workflows/release.yml create mode 100644 LICENSE create mode 100644 pyproject.toml create mode 100644 spotify2ytmusic/__init__.py create mode 100644 spotify2ytmusic/cli.py diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml new file mode 100644 index 0000000..0d536aa --- /dev/null +++ b/.github/workflows/main.yml @@ -0,0 +1,32 @@ +name: Python package + +on: [push] + +jobs: + build: + runs-on: ubuntu-latest + strategy: + matrix: + python-version: ["3.10", "3.11", "3.12"] + + steps: + - uses: actions/checkout@v4 + - name: Set up Python ${{ matrix.python-version }} + uses: actions/setup-python@v4 + with: + python-version: ${{ matrix.python-version }} + - name: Install dependencies + run: | + python -m pip install --upgrade pip + pip install flake8 pytest + if [ -f requirements.txt ]; then pip install -r requirements.txt; fi + if [ -f pyproject.toml ]; then pip install .; fi + - name: Lint with flake8 + run: | + # stop the build if there are Python syntax errors or undefined names + flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics + # exit-zero treats all errors as warnings. The GitHub editor is 127 chars wide + flake8 . --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics +# - name: Run tests +# run: | +# tests/runtests diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml new file mode 100644 index 0000000..6ff6086 --- /dev/null +++ b/.github/workflows/release.yml @@ -0,0 +1,47 @@ +# This workflow uses actions that are not certified by GitHub. +# They are provided by a third-party and are governed by +# separate terms of service, privacy policy, and support +# documentation. + +# To get started +# +# Go to: https://pypi.org/manage/account/publishing/ +# Fill it out. +# Commit this workflow and create a release. + +name: Upload Python Package + +# See: https://github.com/pypa/gh-action-pypi-publish +on: + release: + types: [released] + +jobs: + deploy: + environment: + name: pypi + url: https://pypi.org/p/spotify2ytmusic + runs-on: ubuntu-latest + permissions: + id-token: write + steps: + - uses: actions/checkout@v4 + - name: Set up Python + uses: actions/setup-python@v4 + with: + python-version: '3.x' + - name: Install dependencies + run: | + python -m pip install --upgrade pip + pip install build + - name: Extract version from tag + run: echo "RELEASE_VERSION=${GITHUB_REF#refs/tags/}" >> $GITHUB_ENV + - name: Update version in pyproject.toml + run: | + sed -i "s/version = \".*\"/version = \"${RELEASE_VERSION}\"/" pyproject.toml + - name: Build package + run: python -m build + - name: Publish package + uses: pypa/gh-action-pypi-publish@release/v1 + with: + password: ${{ secrets.PYPI_API_TOKEN }} diff --git a/.gitignore b/.gitignore index ba6815a..b0919fa 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ oauth.json playlists.json +__pycache__ diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..0e259d4 --- /dev/null +++ b/LICENSE @@ -0,0 +1,121 @@ +Creative Commons Legal Code + +CC0 1.0 Universal + + CREATIVE COMMONS CORPORATION IS NOT A LAW FIRM AND DOES NOT PROVIDE + LEGAL SERVICES. DISTRIBUTION OF THIS DOCUMENT DOES NOT CREATE AN + ATTORNEY-CLIENT RELATIONSHIP. CREATIVE COMMONS PROVIDES THIS + INFORMATION ON AN "AS-IS" BASIS. CREATIVE COMMONS MAKES NO WARRANTIES + REGARDING THE USE OF THIS DOCUMENT OR THE INFORMATION OR WORKS + PROVIDED HEREUNDER, AND DISCLAIMS LIABILITY FOR DAMAGES RESULTING FROM + THE USE OF THIS DOCUMENT OR THE INFORMATION OR WORKS PROVIDED + HEREUNDER. + +Statement of Purpose + +The laws of most jurisdictions throughout the world automatically confer +exclusive Copyright and Related Rights (defined below) upon the creator +and subsequent owner(s) (each and all, an "owner") of an original work of +authorship and/or a database (each, a "Work"). + +Certain owners wish to permanently relinquish those rights to a Work for +the purpose of contributing to a commons of creative, cultural and +scientific works ("Commons") that the public can reliably and without fear +of later claims of infringement build upon, modify, incorporate in other +works, reuse and redistribute as freely as possible in any form whatsoever +and for any purposes, including without limitation commercial purposes. +These owners may contribute to the Commons to promote the ideal of a free +culture and the further production of creative, cultural and scientific +works, or to gain reputation or greater distribution for their Work in +part through the use and efforts of others. + +For these and/or other purposes and motivations, and without any +expectation of additional consideration or compensation, the person +associating CC0 with a Work (the "Affirmer"), to the extent that he or she +is an owner of Copyright and Related Rights in the Work, voluntarily +elects to apply CC0 to the Work and publicly distribute the Work under its +terms, with knowledge of his or her Copyright and Related Rights in the +Work and the meaning and intended legal effect of CC0 on those rights. + +1. Copyright and Related Rights. A Work made available under CC0 may be +protected by copyright and related or neighboring rights ("Copyright and +Related Rights"). Copyright and Related Rights include, but are not +limited to, the following: + + i. the right to reproduce, adapt, distribute, perform, display, + communicate, and translate a Work; + ii. moral rights retained by the original author(s) and/or performer(s); +iii. publicity and privacy rights pertaining to a person's image or + likeness depicted in a Work; + iv. rights protecting against unfair competition in regards to a Work, + subject to the limitations in paragraph 4(a), below; + v. rights protecting the extraction, dissemination, use and reuse of data + in a Work; + vi. database rights (such as those arising under Directive 96/9/EC of the + European Parliament and of the Council of 11 March 1996 on the legal + protection of databases, and under any national implementation + thereof, including any amended or successor version of such + directive); and +vii. other similar, equivalent or corresponding rights throughout the + world based on applicable law or treaty, and any national + implementations thereof. + +2. Waiver. To the greatest extent permitted by, but not in contravention +of, applicable law, Affirmer hereby overtly, fully, permanently, +irrevocably and unconditionally waives, abandons, and surrenders all of +Affirmer's Copyright and Related Rights and associated claims and causes +of action, whether now known or unknown (including existing as well as +future claims and causes of action), in the Work (i) in all territories +worldwide, (ii) for the maximum duration provided by applicable law or +treaty (including future time extensions), (iii) in any current or future +medium and for any number of copies, and (iv) for any purpose whatsoever, +including without limitation commercial, advertising or promotional +purposes (the "Waiver"). Affirmer makes the Waiver for the benefit of each +member of the public at large and to the detriment of Affirmer's heirs and +successors, fully intending that such Waiver shall not be subject to +revocation, rescission, cancellation, termination, or any other legal or +equitable action to disrupt the quiet enjoyment of the Work by the public +as contemplated by Affirmer's express Statement of Purpose. + +3. Public License Fallback. Should any part of the Waiver for any reason +be judged legally invalid or ineffective under applicable law, then the +Waiver shall be preserved to the maximum extent permitted taking into +account Affirmer's express Statement of Purpose. In addition, to the +extent the Waiver is so judged Affirmer hereby grants to each affected +person a royalty-free, non transferable, non sublicensable, non exclusive, +irrevocable and unconditional license to exercise Affirmer's Copyright and +Related Rights in the Work (i) in all territories worldwide, (ii) for the +maximum duration provided by applicable law or treaty (including future +time extensions), (iii) in any current or future medium and for any number +of copies, and (iv) for any purpose whatsoever, including without +limitation commercial, advertising or promotional purposes (the +"License"). The License shall be deemed effective as of the date CC0 was +applied by Affirmer to the Work. Should any part of the License for any +reason be judged legally invalid or ineffective under applicable law, such +partial invalidity or ineffectiveness shall not invalidate the remainder +of the License, and in such case Affirmer hereby affirms that he or she +will not (i) exercise any of his or her remaining Copyright and Related +Rights in the Work or (ii) assert any associated claims and causes of +action with respect to the Work, in either case contrary to Affirmer's +express Statement of Purpose. + +4. Limitations and Disclaimers. + + a. No trademark or patent rights held by Affirmer are waived, abandoned, + surrendered, licensed or otherwise affected by this document. + b. Affirmer offers the Work as-is and makes no representations or + warranties of any kind concerning the Work, express, implied, + statutory or otherwise, including without limitation warranties of + title, merchantability, fitness for a particular purpose, non + infringement, or the absence of latent or other defects, accuracy, or + the present or absence of errors, whether or not discoverable, all to + the greatest extent permissible under applicable law. + c. Affirmer disclaims responsibility for clearing rights of other persons + that may apply to the Work or any use thereof, including without + limitation any person's Copyright and Related Rights in the Work. + Further, Affirmer disclaims responsibility for obtaining any necessary + consents, permissions or other rights required for any use of the + Work. + d. Affirmer understands and acknowledges that Creative Commons is not a + party to this document and has no duty or obligation with respect to + this CC0 or use of the Work. diff --git a/README.md b/README.md index a6b149c..e9969fe 100644 --- a/README.md +++ b/README.md @@ -78,4 +78,8 @@ will not duplicate entries on the playlist. Try running with "--track-sleep=3" argument to do a 3 second sleep between tracks. This will take much longer, but may succeed where faster rates have failed. +## License + +Creative Commons Zero v1.0 Universal + [//]: # ( vim: set tw=90 ts=4 sw=4 ai: ) diff --git a/pyproject.toml b/pyproject.toml new file mode 100644 index 0000000..f4ba164 --- /dev/null +++ b/pyproject.toml @@ -0,0 +1,21 @@ +[tool.poetry] +name = "spotify_to_ytmusic" +version = "0.1.0" +description = "Copy Spotify playlists to YTMusic/YouTube Music" +authors = ["Sean Reifschneider "] +license = "Creative Commons Zero v1.0 Universal" +readme = "README.md" + +[tool.poetry.dependencies] +python = "^3.10" +ytmusicapi = "*" + +[build-system] +requires = ["poetry-core"] +build-backend = "poetry.core.masonry.api" + +[tool.poetry.scripts] +s2yt_load_liked = "spotify_to_ytmusic.cli:load_liked" +s2yt_copy_playlist = "spotify_to_ytmusic.cli:copy_playlist" +s2yt_create_playlist = "spotify_to_ytmusic.cli:create_playlist" +s2yt_list_playlists = "spotify_to_ytmusic.cli:list_playlists" diff --git a/spotify2ytmusic/__init__.py b/spotify2ytmusic/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/spotify2ytmusic/cli.py b/spotify2ytmusic/cli.py new file mode 100644 index 0000000..3e510e1 --- /dev/null +++ b/spotify2ytmusic/cli.py @@ -0,0 +1,221 @@ +#!/usr/bin/env python3 + +import json +import sys +import os +import time +from argparse import ArgumentParser +from ytmusicapi import YTMusic +from typing import Optional + + + +def list_playlists(): + """ + List the playlists on Spotify and YTMusic + """ + yt = YTMusic("oauth.json") + + spotify_pls = json.load(open("playlists.json", "r")) + + # Liked music + print('== Spotify') + for src_pl in spotify_pls["playlists"]: + print(f"{src_pl.get('id')} - {src_pl['name']:50} ({len(src_pl['tracks'])} tracks)") + + print() + print('== YTMusic') + for pl in yt.get_library_playlists(limit=5000): + print(f"{pl['playlistId']} - {pl['title']:40} ({pl.get('count', '?')} tracks)") + + +def create_playlist(): + """ + Create a YTMusic playlist + """ + if len(sys.argv) != 2: + print(f"usage: {os.path.basename(sys.argv[0])} ") + sys.exit(1) + + pl_name = sys.argv[1] + + yt = YTMusic("oauth.json") + + id = yt.create_playlist(title=pl_name, description=pl_name) + print(f'Playlist ID: {id}') + + +def lookup_song(yt, track_name, artist_name, album_name): + """Look up a song on YTMusic + + Given the Spotify track information, it does a lookup for the album by the same + artist on YTMusic, then looks at the first 3 hits looking for a track with exactly + the same name. In the event that it can't find that exact track, it then does + a search of songs for the track name by the same artist and simply returns the + first hit. + + The idea is that finding the album and artist and then looking for the exact track + match will be more likely to be accurate than searching for the song and artist and + relying on the YTMusic algorithm to figure things out, especially for short tracks + that might be have many contradictory hits like "Survival by Yes". + """ + albums = yt.search(query=f"{album_name} by {artist_name}", filter="albums") + for album in albums[:3]: + # print(album) + # print(f"ALBUM: {album['browseId']} - {album['title']} - {album['artists'][0]['name']}") + + try: + for track in yt.get_album(album["browseId"])["tracks"]: + if track["title"] == track_name: + return track + # print(f"{track['videoId']} - {track['title']} - {track['artists'][0]['name']}") + except Exception as e: + print(f"Unable to lookup album ({e}), continuing...") + + songs = yt.search(query=f"{track_name} by {artist_name}", filter="songs") + return songs[0] + + # This would need to do fuzzy matching + for song in songs: + if ( + song["title"] == track_name + and song["artists"][0]["name"] == artist_name + and song["album"]["name"] == album_name + ): + return song + # print(f"SONG: {song['videoId']} - {song['title']} - {song['artists'][0]['name']} - {song['album']['name']}") + + raise ValueError(f"Did not find {track_name} by {artist_name} from {album_name}") + + +def load_liked(): + """ + Load the "Liked Songs" playlist from Spotify into YTMusic. + """ + def parse_arguments(): + parser = ArgumentParser() + parser.add_argument( + "--track-sleep", + type=float, + default=0.1, + help="Time to sleep between each track that is added (default: 0.1)", + ) + parser.add_argument( + "--dry-run", + action="store_true", + help="Do not add songs to destination playlist (default: False)", + ) + + return parser.parse_args() + + + args = parse_arguments() + + copier(None, None, args.dry_run, args.track_sleep) + + +def copy_playlist(): + """ + Copy a Spotify playlist to a YTMusic playlist + """ + def parse_arguments(): + parser = ArgumentParser() + parser.add_argument( + "--track-sleep", + type=float, + default=0.1, + help="Time to sleep between each track that is added (default: 0.1)", + ) + parser.add_argument( + "--dry-run", + action="store_true", + help="Do not add songs to destination playlist (default: False)", + ) + parser.add_argument( + "spotify_playlist_id", type=str, help="ID of the Spotify playlist to copy from" + ) + parser.add_argument( + "ytmusic_playlist_id", type=str, help="ID of the YTMusic playlist to copy to" + ) + + return parser.parse_args() + + + args = parse_arguments() + src_pl_id = args.spotify_playlist_id + dst_pl_id = args.ytmusic_playlist_id + + copier(src_pl_id, dst_pl_id, args.dry_run, args.track_sleep) + + +def copier(src_pl_id: Optional[str] = None, dst_pl_id: Optional[str] = None, dry_run: bool = False, track_sleep: float = 0.1): + yt = YTMusic("oauth.json") + + spotify_pls = json.load(open("playlists.json", "r")) + if dst_pl_id is not None: + yt_pl = yt.get_playlist(playlistId=dst_pl_id) + print(f"== Youtube Playlist: {yt_pl['title']}") + + tracks_added_set = set() + duplicate_count = 0 + error_count = 0 + + for src_pl in spotify_pls["playlists"]: + if src_pl_id is None: + if str(src_pl.get("name")) != "Liked Songs": + continue + else: + if str(src_pl.get("id")) != src_pl_id: + continue + + src_pl_name = src_pl["name"] + + print(f"== Spotify Playlist: {src_pl_name}") + + for src_track in src_pl["tracks"]: + src_album_artist = src_track["track"]["album"]["artists"][0]["name"] + src_album_name = src_track["track"]["album"]["name"] + src_track_artist = src_track["track"]["artists"][0]["name"] + src_track_name = src_track["track"]["name"] + + print(f"Spotify: {src_track_name} - {src_track_artist} - {src_album_name}") + + try: + dst_track = lookup_song(yt, src_track_name, src_track_artist, src_album_name) + except Exception as e: + print(f"ERROR: Unable to look up song on YTMusic: {e}") + error_count += 1 + continue + + print( + f" Youtube: {dst_track['title']} - {dst_track['artists'][0]['name']} - {dst_track['album']}" + ) + + if dst_track["videoId"] in tracks_added_set: + print("(DUPLICATE, this track has already been added)") + duplicate_count += 1 + tracks_added_set.add(dst_track["videoId"]) + + if not dry_run: + exception_sleep = 5 + for _ in range(10): + try: + if dst_pl_id is not None: + yt.add_playlist_items( + playlistId=dst_pl_id, + videoIds=[dst_track["videoId"]], + duplicates=False, + ) + else: + yt.rate_song(dst_track["videoId"], "LIKE") + break + except Exception as e: + print(f"ERROR: (Retrying) {e} in {exception_sleep} seconds") + time.sleep(exception_sleep) + exception_sleep *= 2 + + if track_sleep: + time.sleep(track_sleep) + + print() + print(f"Added {len(tracks_added_set)} tracks, encountered {duplicate_count} duplicates, {error_count} errors")