Skip to content

Commit

Permalink
Support adding playlists, changes to url extraction. Refactoring requ…
Browse files Browse the repository at this point in the history
…ired
  • Loading branch information
formatx committed Dec 5, 2021
1 parent c72f8f8 commit c8b165d
Show file tree
Hide file tree
Showing 2 changed files with 183 additions and 26 deletions.
138 changes: 138 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class

# C extensions
*.so

# Distribution / packaging
.Python
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
wheels/
share/python-wheels/
*.egg-info/
.installed.cfg
*.egg
MANIFEST

# PyInstaller
# Usually these files are written by a python script from a template
# before PyInstaller builds the exe, so as to inject date/other infos into it.
*.manifest
*.spec

# Installer logs
pip-log.txt
pip-delete-this-directory.txt

# Unit test / coverage reports
htmlcov/
.tox/
.nox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*.cover
*.py,cover
.hypothesis/
.pytest_cache/
cover/

# Translations
*.mo
*.pot

# Django stuff:
*.log
local_settings.py
db.sqlite3
db.sqlite3-journal

# Flask stuff:
instance/
.webassets-cache

# Scrapy stuff:
.scrapy

# Sphinx documentation
docs/_build/

# PyBuilder
.pybuilder/
target/

# Jupyter Notebook
.ipynb_checkpoints

# IPython
profile_default/
ipython_config.py

# pyenv
# For a library or package, you might want to ignore these files since the code is
# intended to run in multiple environments; otherwise, check them in:
# .python-version

# pipenv
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
# However, in case of collaboration, if having platform-specific dependencies or dependencies
# having no cross-platform support, pipenv may install dependencies that don't work, or not
# install all needed dependencies.
#Pipfile.lock

# PEP 582; used by e.g. github.com/David-OConnor/pyflow
__pypackages__/

# Celery stuff
celerybeat-schedule
celerybeat.pid

# SageMath parsed files
*.sage.py

# Environments
.env
.venv
env/
venv/
ENV/
env.bak/
venv.bak/

# Spyder project settings
.spyderproject
.spyproject

# Rope project settings
.ropeproject

# mkdocs documentation
/site

# mypy
.mypy_cache/
.dmypy.json
dmypy.json

# Pyre type checker
.pyre/

# pytype static type analyzer
.pytype/

# Cython debug symbols
cython_debug/
71 changes: 45 additions & 26 deletions DrDreBot.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,7 @@
'source_address': '0.0.0.0', # bind to ipv4 since ipv6 addresses cause issues sometimes
}

ffmpeg_options = {
'options': '-vn'
}


ytdl = youtube_dl.YoutubeDL(ytdl_format_options)
playlist = collections.deque()
Expand Down Expand Up @@ -69,14 +67,7 @@ async def from_url(cls, url, *, loops=None, stream=True):
loop = loops
data = await loop.run_in_executor(None, lambda: ytdl.extract_info(url, download=False))
if 'entries' in data:
# take first item from a playlist
data = data['entries'][0]

fileName = data['artist'] + ' - ' + data['title']
if not fileName:
fileName = ytdl.prepare_filename(data)
url = data['url']
time = data['duration']
data = data['entries']
#return url, fileName, time
return data

Expand All @@ -97,10 +88,11 @@ async def play(ctx,url = None):
global currentVoiceChannel
global guildTextChannel
global currentTrack


if currentTrack:
playhist.append(currentTrack)
if url == None:
if playlist:
playhist.append(currentTrack)
track = playlist.pop()
currentTrack = track
url = track.url
Expand All @@ -123,14 +115,25 @@ async def play(ctx,url = None):

async def download_song(url):
global currentTrack
fileData = await YTDLSource.from_url(url, loops=bot.loop)
fileName = fileData['artist'] + ' - ' + fileData['title']
duration = fileData['duration']
currentTrack = Track(fileName, url)
song = discord.FFmpegPCMAudio(executable="ffmpeg.exe", source=fileData['url'], before_options="-re")
playingtrack = True
await guildTextChannel.send('Playing **' + fileName + '**')
await start_playing(song, duration)
global playingtrack
fileData = await YTDLSource.from_url(url, loops=bot.loop)
if type(fileData) is list:
fileName = fileData[0]['artist'] + ' - ' + fileData[0]['title']
duration = fileData[0]['duration']
currentTrack = Track(fileName, fileData[0]['webpage_url'])
song = discord.FFmpegPCMAudio(executable="ffmpeg.exe", source=fileData[0]['url'], before_options="-re -reconnect 1 -reconnect_streamed 1 -reconnect_delay_max 5")
playingtrack = True
await guildTextChannel.send('Playing **' + fileName + '**')
await add(fileData)
await start_playing(song, duration)
else:
fileName = fileData['artist'] + ' - ' + fileData['title']
duration = fileData['duration']
currentTrack = Track(fileName, fileData['webpage_url'])
song = discord.FFmpegPCMAudio(executable="ffmpeg.exe", source=fileData['url'], before_options="-re -reconnect 1 -reconnect_streamed 1 -reconnect_delay_max 5")
playingtrack = True
await guildTextChannel.send('Playing **' + fileName + '**')
await start_playing(song, duration)
return fileData

async def start_playing(song, duration):
Expand All @@ -141,25 +144,41 @@ async def start_playing(song, duration):

@bot.command(name = 'add', help='Adds a track to the queue')
async def add(ctx, url):
global currentTrack
try:
async with ctx.typing():
fileData = await YTDLSource.from_url(url, loops=bot.loop)
playlist.appendleft(Track(fileData['artist'] + ' - ' + fileData['title'], url))
fileData = await YTDLSource.from_url(url, loops=bot.loop)

if type(fileData) is list:
currentTrack = fileData[0]
for song in fileData[1:]:
playlist.appendleft(Track(song['artist'] + ' - ' + song['title'], song['webpage_url']))

else:
playlist.appendleft(Track(fileData['artist'] + ' - ' + fileData['title'], fileData['webpage_url']))

await queue(ctx)
except:
await ctx.send("Some error occurred while accessing ytdl")

async def add(data):
for song in data[1:]:
playlist.appendleft(Track(song['artist'] + ' - ' + song['title'], song['webpage_url']))
#await queue(ctx)

@bot.command(name='queue', help = 'Prints queue and previous songs')
async def queue(ctx):
s = ""
s += "Playing : **" + currentTrack.filename + "** \n"
if playingtrack is True:
s += "Playing : **" + currentTrack.filename + "** \n"
s += "-------------In Queue-------------\n"
#Reversed for songs to upper in order: next song on toppy
for track in reversed(playlist):
s += track.filename + "\n"
s += "-------------Previous-------------\n"
for track in playhist:
s += track.filename + "\n"
if playhist is any:
for track in playhist:
s += track.filename + "\n"
await ctx.send(s)

@bot.command(name = 'next', help = '')
Expand Down

0 comments on commit c8b165d

Please sign in to comment.