Created
March 29, 2025 01:05
-
-
Save theandrewbailey/2cc3989f01209165f03769850560ab4f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env python3 | |
| # -*- coding: utf-8 -*- | |
| from xml.dom import minidom | |
| from os import getenv, listdir | |
| from os.path import exists,basename,normpath,splitext | |
| import psycopg2,pathlib,datetime | |
| from glob import glob | |
| # directory where your playlists are | |
| outDir = "/srv/funkwhale/data/music/" | |
| # database credentials | |
| host="localhost" | |
| dbname="funkwhale" | |
| user="funkwhale" | |
| password="YOUR FUNKWHALE DATABASE PASSWORD" | |
| # funkwhale IDs | |
| userID="1" | |
| libraryID=1 | |
| privacy="instance" | |
| fileprefix="file://"+outDir | |
| extensionOverride=None#".flac" | |
| connection=psycopg2.connect(f"dbname='{dbname}' user='{user}' host='{host}' password='{password}'") | |
| try: | |
| for playlist in glob(outDir+"*.m3u*"): | |
| fstat=pathlib.Path(playlist).stat() | |
| with open(playlist, "r", encoding = "utf-8-sig") as inFile: | |
| playlistID=None | |
| playlistName=basename(normpath(playlist)) | |
| index=0 | |
| with connection.cursor() as c: | |
| c.execute("select id from playlists_playlist where name = %s",(playlistName,)) | |
| playlistID=c.fetchone() | |
| if playlistID is None: | |
| c.execute("insert into playlists_playlist (name,user_id,privacy_level,creation_date,modification_date) values (%s, %s, %s, %s, %s) returning id", | |
| (playlistName,userID,privacy,datetime.datetime.fromtimestamp(fstat.st_ctime),datetime.datetime.fromtimestamp(fstat.st_mtime))) | |
| playlistID=c.fetchone() | |
| else: | |
| c.execute("delete from playlists_playlisttrack where playlist_id = %s", (playlistID,)) | |
| for line in inFile: | |
| if line[0] != '#': | |
| trackfile=line.replace("\\","/").strip() | |
| #source=fileprefix+(trackfile if extensionOverride is None else splitext(trackfile)[0]+extensionOverride) | |
| with connection.cursor() as c: | |
| c.execute("select track_id from music_upload where source like %s and library_id = %s",(fileprefix+trackfile+"%",libraryID)) | |
| #rows=c.fetchall() | |
| result=c.fetchone() | |
| if result is not None and result[0] is not None: | |
| c.execute("insert into playlists_playlisttrack (playlist_id, track_id, creation_date, index) values (%s, %s, now(), %s)", | |
| (playlistID, result[0], index)) | |
| #print("inserted "+trackfile+" into "+playlistName) | |
| index+=1 | |
| else: | |
| print(trackfile+" not found.") | |
| connection.commit() | |
| finally: | |
| connection.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment