Acalaysia
Acalaysia

Reputation: 21

Obtaining the title and url after using "ytsearch:string" with youtube-dl in python

I have been working on a Discord music bot and it is almost complete. I have two more things to figure out (which have the same idea behind them).

I am using youtube_dl to download the videos and I recently found out that there is a way to search for and grab the top search result from youtube and play that, allowing a user on my discord channel to play a song by typing in a few words they remember. The problem comes when I attempt to get the title of the youtube video that is going to be played when searching for the top result.

Below I have included some code that simply shows what I mean, as well as what I am currently working with (if there is a better way to perform the task I am trying to figure out feel free to show me the way).

import youtube_dl

ydl_opts = {
    'quiet': True,
    'skip_download': True,
    'forcetitle': True,
    'forceurl': True,
}

# Extracts information using the "ytsearch:string" method
with youtube_dl.YoutubeDL(ydl_opts) as ydl:
    infoSearched = ydl.extract_info("ytsearch:eraser f64")

# Print the available keys
print('\n keys available: \n')
for i in infoSearched:
    print(i)

# Output the information that has potential to be what I am looking for
for i in infoSearched:
    if i is 'webpage_url' or i is 'webpage_url_basename':
        print('\n {}    {}' .format(infoSearched[str(i)], i))

for i in range(2):
    print("\n")

# Extracts information using the actual URL method
with youtube_dl.YoutubeDL(ydl_opts) as ydl:
    infoUrlGiven = ydl.extract_info("https://www.youtube.com/watch?v=pb2fwx4O_Ks")

# Outputs the correct information I am looking for
for i in infoUrlGiven:
    if i is 'webpage_url' or i is 'title':
        print('\n {}    {}' .format(infoUrlGiven[str(i)], i))

Output of previous code

I am trying to get the title of the video and the actual YouTube URL being used. I am able to easily obtain that information when it comes to a user inputting an actual URL themselves (as shown above), however I am unsure of how to extract the information when the video was looked up using the "ytsearch:string" method.

the "'forcetitle': True" option outputs the title I want to the console so I know it is in there somewhere, I just need to find a way to extract it. As for the webpage_URL output, I am unsure of whether or not I can actually obtain that this way since all that the 'webpage_url' outputs in the case of searching for the first result is returning the search ("ytsearch:eraser f64").

Also I included the option "'forceurl': True" in order to be able to say that forceurl is not outputting the url I am looking for. I am specifically looking for the youtube page's url so that I can automatically add the song to an autoplaylist (unsure of whether I am going to do this in the end anyway due to the possibility of duplicates due to different search results).

Please suggest.

Upvotes: 2

Views: 5434

Answers (1)

usr-local-bin
usr-local-bin

Reputation: 43

I think that when you perform a extract_info with ytsearch:, it returns a different dictionary of information than if you were to perform extract_info with a url for the string.

When performing ytsearch:, you would do this:

infosearched['entries'][0]['webpage_url']

Or for the title of the video:

infosearched['entries'][0]['title']

Instead of:

infoSearched[str(i)]

I hope that helps.

Upvotes: 1

Related Questions