Skip to content

Commit

Permalink
Merge branch 'unstable'
Browse files Browse the repository at this point in the history
  • Loading branch information
deepjyoti30 committed Aug 27, 2023
2 parents a9251c8 + e2fe3f8 commit a76a7e6
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 11 deletions.
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
exec(open("ytmdl/__version__.py").read())

req_pkgs = [
'yt-dlp>=2022.03.08.1',
'yt-dlp>=2022.7.6',
'mutagen',
'itunespy',
'requests',
Expand Down
19 changes: 17 additions & 2 deletions ytmdl/dir.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def __replace_special_characters(passed_name: str) -> str:
return sub(r'/', '-', passed_name)


def cleanup(TRACK_INFO, index, datatype, remove_cached=True):
def cleanup(TRACK_INFO, index, datatype, remove_cached=True, filename_passed=None):
"""Move the song from temp to the song dir."""
try:
SONG = glob.glob(os.path.join(
Expand All @@ -33,6 +33,14 @@ def cleanup(TRACK_INFO, index, datatype, remove_cached=True):

SONG_NAME = os.path.basename(SONG)

# If the filename is passed, use that instead of the song
#
# NOTE that is the path is set to be a dynamic value by using
# special characters like `$` though the config then that will
# overwrite the filename_passed.
if filename_passed is not None:
SONG_NAME = filename_passed + ".{}".format(datatype)

DIR = defaults.DEFAULT.SONG_DIR
logger.debug(DIR)

Expand All @@ -49,6 +57,8 @@ def cleanup(TRACK_INFO, index, datatype, remove_cached=True):

dest_filename = os.path.join(
DIR, __replace_special_characters(SONG_NAME))

logger.debug("Final name: ", dest_filename)
shutil.move(SONG, dest_filename)

if remove_cached:
Expand Down Expand Up @@ -181,7 +191,7 @@ def make_custom_dir(DIR, TRACK_INFO):
return (base_DIR, last_element)


def dry_cleanup(current_path, passed_name):
def dry_cleanup(current_path, passed_name, filename_passed=None):
"""
Move the song from the current path to the
song dir and change the name to the passed_name.
Expand All @@ -194,6 +204,11 @@ def dry_cleanup(current_path, passed_name):
extension = os.path.basename(current_path).split(".")[-1]
logger.debug("ext: {}".format(extension))

# If the filename is passed from the CLI, we will use that
# instead of the passed name.
if filename_passed is not None:
passed_name = filename_passed

new_basename = "{}.{}".format(passed_name, extension)
DEST = defaults.DEFAULT.SONG_DIR

Expand Down
12 changes: 8 additions & 4 deletions ytmdl/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,9 @@ def arguments():
parser.add_argument("--dont-transcode", help="Don't transcode the audio after \
downloading. Applicable for OPUS format only. (Default: false)",
action="store_true")
parser.add_argument("--filename", help="Final filename after the song is ready to be used. \
This will be given priority over automatic detection unless dynamic filename \
path is set through config", default=None, metavar="NAME", type=str)

playlist_group = parser.add_argument_group("Playlist")
playlist_group.add_argument(
Expand Down Expand Up @@ -402,7 +405,7 @@ def post_processing(
stream=stream, youtube_link=link) if is_download_archive else None

# Do a dry cleanup
if dir.dry_cleanup(conv_name, song_name):
if dir.dry_cleanup(conv_name, song_name, args.filename):
logger.info("Done")
return

Expand All @@ -415,7 +418,7 @@ def post_processing(
add_song_to_archive(
stream=stream, youtube_link=link) if is_download_archive else None

if dir.dry_cleanup(conv_name, song_name):
if dir.dry_cleanup(conv_name, song_name, args.filename):
logger.info("Done")
elif not args.ignore_errors or args.on_meta_error == 'exit':
logger.critical(
Expand All @@ -434,14 +437,15 @@ def post_processing(
# If no metadata was selected, just do a dry cleanup and skip the
# song
if track_selected is None:
if dir.dry_cleanup(conv_name, song_name):
if dir.dry_cleanup(conv_name, song_name, args.filename):
logger.info("Done")
elif not args.ignore_errors or args.on_meta_error == 'exit':
logger.critical(
". Pass `--ignore-errors` or `on-meta-error` to ignore this.")
return

if dir.cleanup([track_selected], 0, passed_format, remove_cached=False):
if dir.cleanup([track_selected], 0, passed_format, remove_cached=False,
filename_passed=args.filename):
logger.info("Done")


Expand Down
1 change: 1 addition & 0 deletions ytmdl/metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ def _search_tokens(song_name, song_list):
# If there is a part like (featuring ..) or any extra data
# we should remove it as it doesn't aid the search
name = re.sub(r'\([^)]*\)', '', name)
name = re.sub(r'&', 'and', name)
name = remove_stopwords(name)
name = remove_punct(name)
name = remove_multiple_spaces(name)
Expand Down
11 changes: 8 additions & 3 deletions ytmdl/song.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
from ytmdl import prepend, defaults
from simber import Logger
from ytmdl.meta import preconfig
from ytmdl.dir import __replace_special_characters
# import traceback

logger = Logger("song")
Expand Down Expand Up @@ -227,13 +228,17 @@ def set_MP3_data(song, song_path):

data.save()

defaults.DEFAULT.SONG_NAME_TO_SAVE = song.track_name + '.mp3'
defaults.DEFAULT.SONG_NAME_TO_SAVE = __replace_special_characters(
song.track_name) + '.mp3'

# Rename the downloaded file
os.rename(SONG_PATH, os.path.join(
to_save_as = os.path.join(
defaults.DEFAULT.SONG_TEMP_DIR,
defaults.DEFAULT.SONG_NAME_TO_SAVE
))
)
logger.debug("Renaming file from: `{}` to `{}`".format(
SONG_PATH, to_save_as))
os.rename(SONG_PATH, to_save_as)

return IS_IMG_ADDED

Expand Down
7 changes: 6 additions & 1 deletion ytmdl/yt.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from urllib.parse import urlparse, parse_qs
import yt_dlp
from yt_dlp.utils import DownloadError
from re import match
from re import match, sub
from ytmdl import defaults, utility, stringutils
from downloader_cli.download import Download
import traceback
Expand Down Expand Up @@ -221,6 +221,11 @@ def search(query, bettersearch, proxy, kw=[], lim=20):
Search the passed query using the `youtube_search` module
and extract the results accordingly and return.
"""
# Remove any `plus` in the query
if '+' in query:
query = sub(r'\+\s?', '', query)
logger.debug('Query used: ', query)

# Add keywords if better search is enabled
kw = [kw_ for kw_ in kw if kw_ is not None]

Expand Down

0 comments on commit a76a7e6

Please sign in to comment.