Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bugfix: songs being dropped when adding them to the queue #56

Merged
merged 7 commits into from
Nov 28, 2021
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@

import discord
import jokeapi
import pafy
import youtubesearchpython
import pafy_fixed.pafy_fixed as pafy


class BotDispatcher(discord.Client):
Expand Down Expand Up @@ -429,7 +429,11 @@ async def create_or_get_voice_client(self, message):

def pafy_search(self, youtube_link_or_id):
"""Search for youtube link with pafy"""
return pafy.new(youtube_link_or_id)
media = pafy.new(youtube_link_or_id)
if media.dislikes == 0:
logging.info("Ignoring dislike count in new media")

return media

def youtube_search(self, search_str):
"""Search for search_str on youtube"""
Expand Down
Empty file added pafy_fixed/__init__.py
Empty file.
60 changes: 60 additions & 0 deletions pafy_fixed/backend_youtube_dl_fixed.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
# pylint: skip-file
# fmt: off

import sys
import time
import logging
import os
import subprocess

if sys.version_info[:2] >= (3, 0):
# pylint: disable=E0611,F0401,I0011
uni = str
else:
uni = unicode

import youtube_dl

import pafy.g as g
from pafy.backend_shared import BasePafy, BaseStream, remux, get_status_string, get_size_done

from pafy.backend_youtube_dl import YtdlPafy

class YtdlPafyFixed(YtdlPafy):
"""
Modified version of pafy.backend_youtube_dl.YtdlPafy
"""
def __init__(self, *args, **kwargs):
super(YtdlPafyFixed, self).__init__(*args, **kwargs)

def _fetch_basic(self):
""" Fetch basic data and streams. """
if self._have_basic:
return

with youtube_dl.YoutubeDL(self._ydl_opts) as ydl:
try:
self._ydl_info = ydl.extract_info(self.videoid, download=False)
# Turn into an IOError since that is what pafy previously raised
except youtube_dl.utils.DownloadError as e:
raise IOError(str(e).replace('YouTube said', 'Youtube says'))

if self.callback:
self.callback("Fetched video info")

self._title = self._ydl_info['title']
self._author = self._ydl_info['uploader']
self._rating = self._ydl_info['average_rating']
self._length = self._ydl_info['duration']
self._viewcount = self._ydl_info['view_count']
self._likes = self._ydl_info['like_count']
michael-je marked this conversation as resolved.
Show resolved Hide resolved
# added a default value for dislike_count
self._dislikes = self._ydl_info.get('dislike_count', 0)
morgaesis marked this conversation as resolved.
Show resolved Hide resolved
self._username = self._ydl_info['uploader_id']
self._category = self._ydl_info['categories'][0] if self._ydl_info['categories'] else ''
self._bestthumb = self._ydl_info['thumbnails'][0]['url']
self._bigthumb = g.urls['bigthumb'] % self.videoid
self._bigthumbhd = g.urls['bigthumbhd'] % self.videoid
self.expiry = time.time() + g.lifespan

self._have_basic = True
21 changes: 21 additions & 0 deletions pafy_fixed/pafy_fixed.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# pylint: skip-file
# fmt: off

from pafy.pafy import *

Pafy = None

def new(url, basic=True, gdata=False, size=False,
callback=None, ydl_opts=None):
"""
Modified version of pafy.new()
"""
global Pafy
if Pafy is None:
if backend == "internal":
from pafy.backend_internal import InternPafy as Pafy
else:
# changed this line
from pafy_fixed.backend_youtube_dl_fixed import YtdlPafyFixed as Pafy

return Pafy(url, basic, gdata, size, callback, ydl_opts=ydl_opts)