jaimeMF / youtube-dl-api-server

A youtube-dl REST API server
https://youtube-dl-api-server.readthedocs.org/
The Unlicense
364 stars 214 forks source link

Support for user-defineable params? #31

Closed ping closed 9 years ago

ping commented 9 years ago

I've noticed a problem with using the /api/info endpoint for channels, such as https://www.youtube.com/user/Youtube/videos. The endpoint will attempt to lookup all of the videos in the channel which can take a very long time and likely timeout eventually.

This can be mitigated by defining the playlistend param, but there is no way to do this currently. I patched my local copy of app.py to support a small set of user-defined params, but I'm not sure if this a good/pythonic way of doing it.

def info():
    url = request.args['url']
    # list of user defined params
    supported_params = {'playliststart': 'int', 'playlistend': 'int', 'playlist_items': 'int', 'matchtitle': 'str', 'rejecttitle': 'str'}
    params = {}
    for p in supported_params:
        if p in request.args:
            if supported_params[p] == 'int':
                params[p] = int(request.args[p])
            else:
                params[p] = request.args[p]
    errors = (youtube_dl.utils.DownloadError, youtube_dl.utils.ExtractorError)
    try:
        result = get_videos(url, params)    # modified
        key = 'info'

    # ... <snip>

def get_videos(url, params=None):
    '''
    Get a list with a dict for every video founded
    '''
    ydl_params = {
        'format': 'best',
        'cachedir': False,
        'logger': app.logger.getChild('youtube-dl'),
    }
    # merge user params if provided
    if params:
        ydl_params.update(params)
    ydl = SimpleYDL(ydl_params)
    res = ydl.extract_info(url, download=False)
    return res
jaimeMF commented 9 years ago

I have implemented it (same idea but with some small difference in the implementation), thanks!

I haven't added matchtitle and rejectitle because they only work when the video is going to be downloaded and therefore wouldn't work here.

ping commented 9 years ago

@jaimeMF Awesome!

But can you take a look again at matchtitle and rejecttitle?

I've tested it with https://www.youtube.com/user/GoogleWebmasterHelp/videos. When defined, matchtitle=English will return only videos with titles containing the word "English" and rejecttitle=German will exclude videos titles containing "German".

Another param for possible whitelisting is playlistreverse (bool).

jaimeMF commented 9 years ago

Added in 978e6be966708d8c1bd726210be3978f5defa2d1 and 7136e1b3ff89bf4b2c76cf8382aa6efec2ed8762, I only checked with playlists, it works on users.