/Music /Pink Floyd /Wish You Were Here 01 - Shine On You Crazy Diamond (Parts I-V).m4a 02 - Welcome to the Machine.mp3 03 - Have a Cigar.mp3 /Foo Fighters /One By One /There is Nothing Left to Lose /U2 /Joshua Tree
如果一首曲子存在多个artists, plex推荐使用 Various Artists作为ArtistName, 搞不清理由,由于我下载的曲目太多是多艺术家创作的, 都放在这样的文件夹里让我感觉很乱,因此并没有遵守这个推荐规则
defsafe_name(name: str) -> str: """this function is awful and if you got a better one, just tell me """ return name.replace("/", "/").replace(":", ":").replace(">", "❯").replace( "|", " ").replace("<", "❮").replace("\\", " ").replace("?", "?").replace( '"', "'").replace("*", "*").strip().strip('.')
if __name__ == '__main__': con = sqlite3.connect(db_path) cur = con.cursor()
# Create table if not exists cur.execute('''CREATE TABLE IF NOT EXISTS musics (path text NOT NULL PRIMARY KEY)''')
for source_file in source_dir.iterdir(): if source_file.name.endswith(".ncm"): continue
source_file = source_file.absolute() cur.execute("SELECT * FROM musics WHERE path = ?", [str(source_file)]) if cur.fetchone(): # 已经出现在数据库中, 直接跳过 continue
metadata = ID3(source_file) artist = source_file.name.split('-')[0].strip().split(',')[0] try: album = safe_name(metadata.get("TALB").text[0]) except AttributeError: print(source_file, "has no album, just skipped") continue
target_file: Path = target_dir / artist / album / source_file.name
if target_file.exists(): # Insert a row of data cur.execute("INSERT INTO musics VALUES (?)", [str(source_file)]) con.commit() continue