alexmercerind/youtube-search-python

How to run asyns downloads?

Closed this issue · 1 comments

I'm trying run this code for download other search pages:

import asyncio
from youtubesearchpython.__future__ import VideosSearch
search = VideosSearch('NoCopyrightSounds')

result = await search.next()
print(result['result'])

result = await search.next()
print(result['result'])

result = await search.next()
print(result['result'])

result = await search.next()
print(result['result'])

But getting this error:

  File "/usr/lib/python3.6/runpy.py", line 193, in _run_module_as_main
    "__main__", mod_spec)
  File "/usr/lib/python3.6/runpy.py", line 85, in _run_code
    exec(code, run_globals)
  File "/home/pc/.vscode/extensions/ms-python.python-2021.2.636928669/pythonFiles/lib/python/debugpy/__main__.py", line 45, in <module>
    cli.main()
  File "/home/pc/.vscode/extensions/ms-python.python-2021.2.636928669/pythonFiles/lib/python/debugpy/../debugpy/server/cli.py", line 444, in main
    run()
  File "/home/pc/.vscode/extensions/ms-python.python-2021.2.636928669/pythonFiles/lib/python/debugpy/../debugpy/server/cli.py", line 285, in run_file
    runpy.run_path(target_as_str, run_name=compat.force_str("__main__"))
  File "/usr/lib/python3.6/runpy.py", line 261, in run_path
    code, fname = _get_code_from_file(run_name, path_name)
  File "/usr/lib/python3.6/runpy.py", line 236, in _get_code_from_file
    code = compile(f.read(), fname, 'exec')
  File "/home/pc/Projects/DatasetFetch/new_scripts.py", line 116
    videosResult = await videosSearch.next()
                                    ^
SyntaxError: invalid syntax

What I'm doing not right?

Hi there @kirill505 !

You must call an async method within another async method along with await keyword.

import asyncio
from youtubesearchpython.__future__ import VideosSearch
search = VideosSearch('NoCopyrightSounds')

async def main():
    result = await search.next()
    print(result['result'])

    result = await search.next()
    print(result['result'])

    result = await search.next()
    print(result['result'])

    result = await search.next()
    print(result['result'])

asyncio.run(main())

Just execute your code like this.

Hope this works out.
Feel free to give further feedback.