Compare commits
No commits in common. "f851fdcafe2fd86be5a098b4f3936c4bfd97188b" and "5f8d8572ad59b169ec3b4bb4b201fbba1b997551" have entirely different histories.
f851fdcafe
...
5f8d8572ad
@ -572,15 +572,12 @@ class Tracks(Base):
|
|||||||
Return track with passed path, or None.
|
Return track with passed path, or None.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
try:
|
return (
|
||||||
return (
|
session.execute(
|
||||||
session.execute(
|
select(Tracks)
|
||||||
select(Tracks)
|
.where(Tracks.path == path)
|
||||||
.where(Tracks.path == path)
|
).scalar_one()
|
||||||
).scalar_one()
|
)
|
||||||
)
|
|
||||||
except NoResultFound:
|
|
||||||
return None
|
|
||||||
|
|
||||||
def rescan(self, session: Session) -> None:
|
def rescan(self, session: Session) -> None:
|
||||||
"""
|
"""
|
||||||
|
|||||||
@ -1,54 +0,0 @@
|
|||||||
#!/usr/bin/env python
|
|
||||||
#
|
|
||||||
# Script to manage renaming existing files in given directory and
|
|
||||||
# propagating that change to database. Typical usage: renaming files
|
|
||||||
# from 'title.mp3' to title - artist.mp3'
|
|
||||||
#
|
|
||||||
# Actions:
|
|
||||||
#
|
|
||||||
# - record all filenames and inode numbers
|
|
||||||
# - external: rename the files
|
|
||||||
# - update records with new filenames for each inode number
|
|
||||||
# - update external database with new paths
|
|
||||||
|
|
||||||
import os
|
|
||||||
import sqlite3
|
|
||||||
|
|
||||||
PHASE = 2
|
|
||||||
|
|
||||||
# Check file of same name exists in parent directory
|
|
||||||
source_dir = '/home/kae/tmp/Singles' # os.getcwd()
|
|
||||||
db = "/home/kae/tmp/singles.sqlite"
|
|
||||||
|
|
||||||
|
|
||||||
def main():
|
|
||||||
with sqlite3.connect(db) as connection:
|
|
||||||
cursor = connection.cursor()
|
|
||||||
if PHASE == 1:
|
|
||||||
cursor.execute(
|
|
||||||
"CREATE TABLE IF NOT EXISTS mp3s "
|
|
||||||
"(inode INTEGER, oldname TEXT, newname TEXT)"
|
|
||||||
)
|
|
||||||
|
|
||||||
for fname in os.listdir(source_dir):
|
|
||||||
fullpath = os.path.join(source_dir, fname)
|
|
||||||
inode = os.stat(fullpath).st_ino
|
|
||||||
sql = f'INSERT INTO mp3s VALUES ({inode}, "{fname}", "")'
|
|
||||||
cursor.execute(sql)
|
|
||||||
|
|
||||||
if PHASE == 2:
|
|
||||||
for fname in os.listdir(source_dir):
|
|
||||||
fullpath = os.path.join(source_dir, fname)
|
|
||||||
inode = os.stat(fullpath).st_ino
|
|
||||||
sql = (
|
|
||||||
f'UPDATE mp3s SET newname = "{fname}" WHERE inode={inode}'
|
|
||||||
)
|
|
||||||
try:
|
|
||||||
cursor.execute(sql)
|
|
||||||
except sqlite3.OperationalError:
|
|
||||||
print(f"Error with {inode} -> {fname}")
|
|
||||||
|
|
||||||
cursor.close()
|
|
||||||
|
|
||||||
|
|
||||||
main()
|
|
||||||
@ -16,7 +16,6 @@
|
|||||||
import glob
|
import glob
|
||||||
import os
|
import os
|
||||||
import shutil
|
import shutil
|
||||||
import sys
|
|
||||||
|
|
||||||
from helpers import (
|
from helpers import (
|
||||||
fade_point,
|
fade_point,
|
||||||
@ -28,14 +27,15 @@ from helpers import (
|
|||||||
|
|
||||||
from models import Tracks
|
from models import Tracks
|
||||||
from dbconfig import Session
|
from dbconfig import Session
|
||||||
from thefuzz import process # type: ignore
|
from thefuzz import process
|
||||||
from sqlalchemy.exc import IntegrityError
|
|
||||||
|
from string import ascii_lowercase as letters
|
||||||
from typing import List
|
from typing import List
|
||||||
|
|
||||||
# ###################### SETTINGS #########################
|
# ###################### SETTINGS #########################
|
||||||
process_multiple_matches = False
|
process_multiple_matches = True
|
||||||
do_processing = False
|
do_processing = True
|
||||||
process_no_matches = False
|
process_no_matches = True
|
||||||
# #########################################################
|
# #########################################################
|
||||||
|
|
||||||
|
|
||||||
@ -61,10 +61,6 @@ print(f"{source_dir=}, {parent_dir=}")
|
|||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
if 'musicmuster_prod' not in os.environ.get('MM_DB'):
|
|
||||||
response = input("Not on production database - c to continue: ")
|
|
||||||
if response != "c":
|
|
||||||
sys.exit(0)
|
|
||||||
tracks = os.listdir(parent_dir)
|
tracks = os.listdir(parent_dir)
|
||||||
for fname in os.listdir(source_dir):
|
for fname in os.listdir(source_dir):
|
||||||
parent_file = os.path.join(parent_dir, fname)
|
parent_file = os.path.join(parent_dir, fname)
|
||||||
@ -206,10 +202,7 @@ def main():
|
|||||||
def process_track(src, dst, title, artist):
|
def process_track(src, dst, title, artist):
|
||||||
|
|
||||||
new_path = os.path.join(os.path.dirname(dst), os.path.basename(src))
|
new_path = os.path.join(os.path.dirname(dst), os.path.basename(src))
|
||||||
print(
|
print(f"process_track:\n {src=}\n {new_path=}\n {dst=}\n {title=}, {artist=}\n")
|
||||||
f"process_track:\n {src=}\n {new_path=}\n "
|
|
||||||
f"{dst=}\n {title=}, {artist=}\n"
|
|
||||||
)
|
|
||||||
|
|
||||||
if not do_processing:
|
if not do_processing:
|
||||||
return
|
return
|
||||||
@ -220,24 +213,17 @@ def process_track(src, dst, title, artist):
|
|||||||
track.title = title
|
track.title = title
|
||||||
track.artist = artist
|
track.artist = artist
|
||||||
track.path = new_path
|
track.path = new_path
|
||||||
try:
|
session.commit()
|
||||||
session.commit()
|
|
||||||
except IntegrityError:
|
|
||||||
# https://jira.mariadb.org/browse/MDEV-29345 workaround
|
|
||||||
session.rollback()
|
|
||||||
track.title = title
|
|
||||||
track.artist = artist
|
|
||||||
track.path = "DUMMY"
|
|
||||||
session.commit()
|
|
||||||
track.path = new_path
|
|
||||||
session.commit()
|
|
||||||
|
|
||||||
print(f"os.unlink({dst}")
|
print(f"os.unlink({dst}")
|
||||||
print(f"shutil.move({src}, {new_path}")
|
print(f"shutil.move({src}, {new_path}")
|
||||||
|
|
||||||
os.unlink(dst)
|
os.unlink(dst)
|
||||||
shutil.move(src, new_path)
|
shutil.move(src, new_path)
|
||||||
track = Tracks.get_by_path(session, new_path)
|
try:
|
||||||
|
track = Tracks.get_by_path(session, new_path)
|
||||||
|
except:
|
||||||
|
import ipdb; ipdb.set_trace()
|
||||||
if track:
|
if track:
|
||||||
track.rescan(session)
|
track.rescan(session)
|
||||||
else:
|
else:
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user