예제 #1
0
파일: ytmdl.py 프로젝트: danieldeb/ytmdl
def main():
    """Run on program call."""
    args = arguments()
    song_name = args.SONG_NAME

    # Check if --setup is passed
    if args.setup:
        setupConfig.make_config()
        exit(0)

    # After this part song name is required
    if song_name is None:
        prepend.PREPEND(2)
        print("Please pass a song name. This is necessary",
              "to search in itunes.")
        exit(1)

    if not args.nolocal:
        # Search for the song locally
        if not cache.main(song_name):
            exit(0)

    is_quiet = args.quiet
    url = args.url

    # If the url is passed then get the data
    if url is not None:
        data = []
        # Get video data from youtube
        temp_data = yt.scan_video(yt.get_href(url))
        data.append(temp_data)

        # link to dw the song
        link = url

        # In this case choice will be 0
        choice = 0
    else:
        if is_quiet:
            prepend.PREPEND(1)
            print('Quiet is enabled')

        prepend.PREPEND(1)
        print('Searching Youtube for ', end='')
        print(Fore.LIGHTYELLOW_EX, end='')
        print(song_name, end='')
        print(Style.RESET_ALL)

        data, urls = yt.search(song_name)

        if len(data) > 1 and not is_quiet:
            # Ask for a choice
            choice = song.getChoice(data, 'mp3')
        else:
            choice = 0

        link = 'https://youtube.com{}'.format(urls[int(choice)])

    # Declare a var to store the name of the yt video
    yt_title = data[choice]['title']

    prepend.PREPEND(1)
    print('Downloading ', end='')
    print(Fore.LIGHTMAGENTA_EX, end='')
    print(yt_title, end=' ')
    print(Style.RESET_ALL, end='')
    print('in', end=' ')
    print(Fore.LIGHTYELLOW_EX, end='')
    print(defaults.DEFAULT.SONG_QUALITY + 'kbps', end='')
    print(Style.RESET_ALL)
    path = yt.dw(link, yt_title)

    if not path:
        prepend.PREPEND(2)
        print('Something went wrong while downloading!\a')
        sys.exit(0)
    else:
        prepend.PREPEND(1)
        print('Downloaded!')

    prepend.PREPEND(1)
    print('Converting to mp3...')

    conv_name = utility.convert_to_mp3(path)

    if not conv_name:
        prepend.PREPEND(2)
        print('Something went wrong while converting!\a')

    prepend.PREPEND(1)
    print('Getting song data...')

    # TRACK_INFO = song.getData(song_name)
    TRACK_INFO = metadata.SEARCH_SONG(song_name)

    # declare a variable to store the option
    option = 0

    if TRACK_INFO is False:
        # prepend.PREPEND(2)
        # print('Data \a')
        # exit(0)
        pass
    elif len(TRACK_INFO) == 0:
        prepend.PREPEND(2)
        print('No data was found!\a')
        sys.exit(0)
    else:
        prepend.PREPEND(1)
        print('Setting data...')

        option = song.setData(TRACK_INFO, is_quiet, conv_name)

        if type(option) is not int:
            prepend.PREPEND(2)
            print('Something went wrong while writing data!\a')
            sys.exit(0)

    # Get the directory where song is moved

    DIR = dir.cleanup(TRACK_INFO, option)
    prepend.PREPEND(1)
    print('Moving to {}...'.format(DIR))

    if not DIR:
        prepend.PREPEND(2)
        print('Something went wrong while moving!\a')
        sys.exit(0)
    else:
        prepend.PREPEND(1)
        print('Done')
예제 #2
0
파일: core.py 프로젝트: deepjyoti30/ytmdl
def search(song_name, args) -> Union[str, str]:
    """Search the song on YouTube, ask the user for an input and accordingly
    return a selected song.

    The song can be extracted either from an URL or the name. If the name is
    present, we will search on YouTube for the name, ask the user for a choice
    and accordingly return the choice.

    If the URL is present, we will extract the data from the URL and return it
    accordingly.
    """
    # Declare some default values
    YOUTUBE_LINK_BASE = "https://youtube.com{}"
    PASSED_CHOICE = args.choice
    URL = args.url
    IS_QUIET = args.quiet

    if URL is None:
        if IS_QUIET:
            logger.info('Quiet is enabled')

        logger.info('Searching Youtube for {}{}{}'.format(
            Fore.LIGHTYELLOW_EX, song_name, Style.RESET_ALL))

        data = yt.search(song_name,
                         not args.disable_metaadd,
                         args.proxy,
                         kw=[args.artist, args.album])

        # Handle the exception if urls has len 0
        if len(data) == 0:
            logger.critical(
                "No song found. Please try again with a different keyword.")

        if not IS_QUIET:
            # Ask for a choice
            choice = song.getChoice(data, 'mp3')
        elif PASSED_CHOICE is not None and PASSED_CHOICE <= len(data):
            choice = PASSED_CHOICE - 1
            logger.info("Using {} as choice".format(PASSED_CHOICE))
        else:
            # Extract the verified music result if valid
            choice = song.get_default(data) - 1

        # Check if choice if -2. If it is that, then we need to stop executing
        # of the current song and gracefully exit.
        if choice == -2:
            return False, False

        return (YOUTUBE_LINK_BASE.format(data[choice]["href"]),
                data[choice]["title"])

    # If the url is passed then get the data
    data = []

    # Strip unwanted stuff from the URL
    URL = stringutils.srtip_unwanted_words_from_url(URL)

    # Get video data from youtube
    temp_data = yt.scan_video(yt.get_href(URL), args.proxy)

    # Sometimes the temp_data may be returned as unauthorized, skip that
    if type(temp_data) is str and temp_data.lower() == "Unauthorized".lower():
        if args.ignore_errors:
            logger.warning("{}: is unauthorized".format(URL))
            return
        else:
            logger.critical("{}: is unauthorized".format(URL))

    data.append(temp_data)

    # In this case choice will be 0
    return URL, data[0]["title"]