#Trying to access SpotDL via python script instead of over CLI...

18 messages · Page 1 of 1 (latest)

sour swift
#

Exactly what it says, I was wondering if there was an easy way/documented way to access spotdl from inside my python script (i want to await the end of the download before moving on, and popen doesnt let me await)

elder ermineBOT
#
spotDL Support

Please continue adding more information into this thread.
You should include the following information:

spotDL Version

Eg. 4.0.0

Operating System

Eg. Windows 22H2

The Command You Ran

Please include Spotify links.

Screenshots or Pasted Error Messages

Only send these if they are relevant.

To send pasted error messages use three backticks (```)
Here's info on how to use code blocks.

#

@sour swift

sour swift
#

Windows 11 - 23H2
spdl: 4.2.0
Error/Command - None

fading grotto
sour swift
#

tyty

elder ermineBOT
# elder ermine

Thread archived by @sour swift.
Anyone can send a message to unarchive it.

sour swift
#

Hey, sorry to reopen this, but I am now having event loop issues,

 File "D:\src\.venv\Lib\site-packages\discord\app_commands\commands.py", line 858, in _do_call
    return await self._callback(interaction, **params)  # type: ignore
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "D:\src\cmds.py", line 324, in create_playlist
    to_get = await utils.spot.get_playlist(sp_id)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "D:\src\handlers\spot.py", line 59, in get_playlist
    self.downloader.download_multiple_songs(songs)
  File "D:\src\.venv\Lib\site-packages\spotdl\download\downloader.py", line 292, in download_multiple_songs
    results = list(self.loop.run_until_complete(asyncio.gather(*tasks)))
                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Server\.pyenv\pyenv-win\versions\3.12.4\Lib\asyncio\base_events.py", line 663, in run_until_complete
    self._check_running()
  File "C:\Users\Server\.pyenv\pyenv-win\versions\3.12.4\Lib\asyncio\base_events.py", line 622, in _check_running
    raise RuntimeError('This event loop is already running')
RuntimeError: This event loop is already running

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "D:\src\.venv\Lib\site-packages\discord\app_commands\tree.py", line 1310, in _call
    await command._invoke_with_namespace(interaction, namespace)
  File "D:\src\.venv\Lib\site-packages\discord\app_commands\commands.py", line 883, in _invoke_with_namespace
    return await self._do_call(interaction, transformed_values)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "D:\src\.venv\Lib\site-packages\discord\app_commands\commands.py", line 876, in _do_call
    raise CommandInvokeError(self, e) from e
discord.app_commands.errors.CommandInvokeError: Command 'create_plex_playlist' raised an exception: RuntimeError: This event loop is already running
#

here is the code

self.downloader = Downloader(self.settings, asyncio.get_running_loop())
#

i also tried

self.downloader = Downloader(self.settings, asyncio.get_event_loop())
self.downloader = Downloader(self.settings, asyncio.new_event_loop())
fading grotto
#

Show full code fole

#

cile

#

or upload it

#

cant see how u imported

sour swift
#
class SpotDLHandler:
    """."""
    def __init__(self) -> None:
        self.head = ""
        self.client = SpotifyClient.init(
            client_id="926f36767f1b498486fb08c1ac7bfa78",
            client_secret=environ["SPOT_SEC"],
        )
        output_dir="E:/SpotTRY"
        settings = {'log_level': "WARNING", 'audio_providers': ['youtube-music'], 'lyrics_providers': ['genius'], 'playlist_numbering': False, 'scan_for_songs': False, 'm3u': None, 'output': f'{output_dir}/{{artist}}/{{album}}/{{title}}.{{output-ext}}', 'overwrite': 'skip', 'search_query': None, 'ffmpeg': 'ffmpeg', 'bitrate': 'disable', 'ffmpeg_args': None, 'format': 'mp3', 'save_file': None, 'filter_results': True, 'threads': 4, 'cookie_file': 'cookies.txt', 'restrict': None, 'print_errors': False, 'sponsor_block': False, 'preload': False, 'archive': None, 'load_config': True, 'simple_tui': False, 'fetch_albums': False, 'id3_separator': '/', 'ytm_data': False, 'add_unavailable': False, 'generate_lrc': False, 'force_update_metadata': False, 'only_verified_results': False, 'sync_without_deleting': False, 'max_filename_length': None, 'yt_dlp_args': None, 'detect_formats': None,}
        self.settings = DownloaderOptions(
            **create_settings_type(
                Namespace(config=False), dict(settings), DOWNLOADER_OPTIONS
            )
        )

        try:
            self.downloader = Downloader(self.settings, asyncio.get_running_loop())
        except RuntimeError:
            self.downloader = "n"

I do a try except inorder to see if the loop is running if its not running yet, i hold off and then use this code to set it after i KNOW the loop is running

if self.downloader == "n":
  self.downloader = Downloader(self.settings, asyncio.get_event_loop())
fading grotto
#

asked to see your imports

sour swift
# fading grotto asked to see your imports

my bad, i took that as how i am referencing it

import asyncio
import logging
import subprocess
from argparse import Namespace
from os import environ

import aiohttp
from spotdl.download.downloader import DOWNLOADER_OPTIONS, Downloader, DownloaderOptions, create_settings_type, logger
from spotdl.types.artist import Artist
from spotdl.types.playlist import Playlist
from spotdl.utils.spotify import SpotifyClient
sour swift
#

@fading grotto sorry for the ping, just wondering if you have any tips