2

im using python to check if certain channel is live or not by using "https://www.youtube.com/channel/channel_id/live" by using youtube dl "is_live" I have managed to get what I want.

However, if the channel is not streaming, it takes ages to download the whole channel including all the playlist.

I want to stop the download if download time excess 5 seconds. Is there any way I can do this?

kokbab
  • 23
  • 3
  • Does this help? : https://www.mankier.com/1/youtube-dl#--socket-timeout – mama Aug 15 '21 at 21:48
  • you can add `{socket_timeout: 5}` as `ydl_opts` in the instance by `ydl = youtube_dl.YoutubeDL(ydl_opts)` where 5 is seconds – mama Aug 15 '21 at 21:53
  • @mama I tried socket timeout but it doesn't work. I guess it only work when its bringing calling the page – kokbab Aug 15 '21 at 21:54
  • I'd go with thread termination rather than a subprocess, myself, but we have answers in the knowledge base showing both approaches. – Charles Duffy Aug 15 '21 at 21:59

1 Answers1

0

You could add your code in the download function of this template:

import asyncio

async def download(url):
    await asyncio.sleep(3600)
    print(url)

async def youtbe(url):
    try:
        await asyncio.wait_for(download(url), timeout=5.0)
    except asyncio.TimeoutError:
        print(f'timeout on {url}')



async def main(urls):
    await asyncio.gather(*[youtbe(url) for url in urls])

urls = [
        'hej00',
        'hej01',
        'hej02',
        'hej03',
        'hej04',
        'hej05',
        'hej06',
        'hej07',
        'hej08',
        'hej09',
        'hej10',
        ]
asyncio.run(main(urls))

The 5 seconds timeout is defined in the wait_for function from asyncio

mama
  • 2,046
  • 1
  • 7
  • 24