Refactoring code
This commit is contained in:
15
files/util.py
Normal file
15
files/util.py
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
import os
|
||||||
|
from typing import Any
|
||||||
|
|
||||||
|
import magic
|
||||||
|
|
||||||
|
|
||||||
|
def scan_folder(src: str):
|
||||||
|
for (root, dirs, files) in os.walk(top=src):
|
||||||
|
for file in files:
|
||||||
|
yield root + os.sep + file
|
||||||
|
|
||||||
|
|
||||||
|
def accepted_file_type(file: Any) -> bool:
|
||||||
|
mime = magic.from_file(file, mime=True)
|
||||||
|
return mime in ('audio/mpeg', 'audio/mp3')
|
||||||
32
main.py
32
main.py
@@ -3,10 +3,10 @@ import itertools
|
|||||||
import logging
|
import logging
|
||||||
import os
|
import os
|
||||||
import shutil
|
import shutil
|
||||||
|
from files import util as ioutil
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
from typing import Any
|
from typing import Any
|
||||||
|
|
||||||
import magic
|
|
||||||
from shazamio import Shazam
|
from shazamio import Shazam
|
||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
@@ -71,29 +71,23 @@ class TrackInfo:
|
|||||||
Track Number: {self.__number}"""
|
Track Number: {self.__number}"""
|
||||||
|
|
||||||
|
|
||||||
def accepted_file_type(file: Any) -> bool:
|
|
||||||
mime = magic.from_file(file, mime=True)
|
|
||||||
return mime in ('audio/mpeg', 'audio/mp3')
|
|
||||||
|
|
||||||
|
|
||||||
async def main() -> Any:
|
async def main() -> Any:
|
||||||
shazam: Shazam = Shazam()
|
shazam: Shazam = Shazam()
|
||||||
src: str = input("Which path scan?: ")
|
src: str = input("Which path scan?: ")
|
||||||
dst: str = input("Where do you want to copy files?: ")
|
dst: str = input("Where do you want to copy files?: ")
|
||||||
for (root, dirs, files) in os.walk(top=src):
|
for song_file in ioutil.scan_folder(src):
|
||||||
for file in files:
|
if ioutil.accepted_file_type(song_file):
|
||||||
song: str = root + os.sep + file
|
shazam_metadata: dict[str, Any] = await shazam.recognize(song_file)
|
||||||
if accepted_file_type(song):
|
track_info = TrackInfo(shazam_metadata)
|
||||||
shazam_metadata: dict[str, Any] = await shazam.recognize(song)
|
logging.info(f"Recognize file {song_file} as {track_info}")
|
||||||
track_info = TrackInfo(shazam_metadata)
|
album_path: str = os.path.join(dst, track_info.artist,
|
||||||
logging.info(f"Recognize file {song} as {track_info}")
|
f"{track_info.album.released} - {track_info.album.name}")
|
||||||
album_path = os.path.join(dst, track_info.artist,
|
if not os.path.exists(album_path):
|
||||||
f"{track_info.album.released} - {track_info.album.name}")
|
Path(album_path).mkdir(mode=0o755, parents=True, exist_ok=True)
|
||||||
if not os.path.exists(album_path):
|
|
||||||
Path(album_path).mkdir(mode=0o755, parents=True, exist_ok=True)
|
|
||||||
|
|
||||||
# I can copy to file now
|
# I can copy to file now
|
||||||
shutil.copy(song, os.path.join(album_path, f"{track_info.title}.mp3"))
|
shutil.copy(song_file,
|
||||||
|
os.path.join(album_path, f"{track_info.track_number} - {track_info.title}.mp3"))
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
|
|||||||
Reference in New Issue
Block a user