4

I have the below code that has been used to download youtube videos. I automatically detect if it's a playlist or single video. However all the sudden it is giving the above error. What can be the problem?

import pafy
from log import *
import tkinter.filedialog
import pytube

url = input("Enter url :")

directory = tkinter.filedialog.askdirectory()


def single_url(url,directory):
    print("==================================================================================================================")
    
    video = pafy.new(url)
    print(url)
    print(video.title)

    #logs(video.title,url)
    file_object  = open(directory+"/links.log", "a")
    file_object.write(video.title +' '+ url + '\n')
    file_object.close()
    print('Rating :',video.rating,', Duration :',video.duration,', Likes :',video.likes, ', Dislikes : ', video.dislikes)
    #print(video.description)

    best = video.getbest()
    print(best.resolution, best.extension)

    best.download(quiet=False, filepath=directory+'/'+video.title+"." + best.extension)

    print("saved at :", directory, " directory")
    print("==================================================================================================================")

def playlist_func(url,directory):
    try: 
        playlist = pytube.Playlist(url)
        file_object  = open(directory+"/links.log", "a")
        file_object.write('Playlist Url :'+ url + '\n')
        file_object.close()
        print('There are {0}'.format(len(playlist.video_urls)))
        for url in playlist.video_urls:
            single_url(url,directory) 
    except:
        single_url(url,directory)
    
playlist_func(url,directory)
Bharel
  • 23,672
  • 5
  • 40
  • 80
lewis machilika
  • 819
  • 2
  • 11
  • 25
  • try updating youtube_dl sometimes youtubes changes way videos are served, causing errors like these – vinzenz Dec 14 '21 at 06:51
  • The packages are updated *Requirement already satisfied: youtube_dl in site-packages (2021.5.16)* – lewis machilika Dec 14 '21 at 06:56
  • If you have code using a third-party library that used to work and now doesn't, even though you haven't changed anything, the correct place to ask is *support for that library* (such as a Github issue tracker), not Stack Overflow. – Karl Knechtel Dec 14 '21 at 07:03

8 Answers8

5

Your issue doesn't have anything to do with your code.

Youtube does no longer have a dislike count, they simply removed it.

You just have to wait for the pafy package to be updated accordingly, or patch the package locally and remove that part by yourself.

Keep in mind there are at least 5 different pull requests open trying to fix it.

Bharel
  • 23,672
  • 5
  • 40
  • 80
4

MANUAL FIX:

You can just set the attribute _dislikes to 0 in file backend_youtube_dl.py

Line 54:

self._dislikes = 0 # self._ydl_info['dislike_count']
DAVIDQLZ
  • 51
  • 3
4

While it is true the dislike_count is removed There's the pafy cloned repo that already adjusted the changes already instead of waiting for a new release which I doubt would happen anytime soon. I've been using this one and no issues fn. Install using:

pip install git+https://github.com/Cupcakus/pafy

You don't have to do any changes at all, just remove (pip uninstall) the initial pafy with the dislike count issues before installing this one yo avoid conflicts.

2

We can manually fix it by going to

C:\Users\admin\AppData\Local\Programs\Python\Python310\lib\site-packages\pafy\backend_youtube_dl.py

and open python file in editor and comment out

self._likes = self._ydl_info['like_count']

self._dislikes = self._ydl_info['dislike_count']

these two lines at line 53 and 54 and save the file.

PS: the location of python file may differ according to your system

1

I had faced the similar issue but it is due to YouTube recent update of Dislike button. So there is nothing wrong with code. And If there is any Operating System error regarding youtube-dl occur than you need to install this in prompt

#conda install -c forge youtube-dl #pip3 install youtube-dl

  • 2
    As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Dec 25 '21 at 09:54
1

To get rid of this problem, the best way(currently) is to patch it up locally because Pafy and youtube-dl packages are not updated alongside YouTube's update(remove dislike feature).

First, check the error message. I hope you will find it like this:

Traceback (most recent call last):
  File "D:\Random Work\youtube scraping\yt_vc.py", line 5, in <module>
    result = pafy.new(url)
  File "C:\Users\username\Anaconda3\envs\wsWork\lib\site-packages\pafy\pafy.py", line 124, in new
    return Pafy(url, basic, gdata, size, callback, ydl_opts=ydl_opts)
  File "C:\Users\username\Anaconda3\envs\wsWork\lib\site-packages\pafy\backend_youtube_dl.py", line 31, in __init__
    super(YtdlPafy, self).__init__(*args, **kwargs)
  File "C:\Users\username\Anaconda3\envs\wsWork\lib\site-packages\pafy\backend_shared.py", line 97, in __init__
    self._fetch_basic()
  File "C:\Users\username\Anaconda3\envs\wsWork\lib\site-packages\pafy\backend_youtube_dl.py", line 54, in _fetch_basic
    self._dislikes = self._ydl_info['dislike_count']
KeyError: 'dislike_count'

Ignore all of them except the above one of KeyError. In my case, which is:

File "C:\Users\username\Anaconda3\envs\wsWork\lib\site-packages\pafy\backend_youtube_dl.py", line 54, in _fetch_basic
    self._dislikes = self._ydl_info['dislike_count']

Give a closer look to the file address

File "C:\Users\username\Anaconda3\envs\wsWork\lib\site-packages\pafy\backend_youtube_dl.py

and to the line number of that file line 54 which is written after the file address/path. It means, that backend_youtube_dl.py file's line 54 is occurring problem. If we can comment out or remove that line, that problem will be solved.

My file address, line number and your file address, line number can be different. Don't worry about that. Just follow the file address/path which is showing in your terminal. And open that file. Find the line and comment out or remove. Then save.

After that, I hope this error won't show again and program will run without any issue.

1

Simply you can comment out the self._dislikes = self._ydl_info['dislike_count'] line in file backend_youtube_dl.py

Amrit Raj
  • 39
  • 3
1

Remove those lines from backend_youtube_dl.py in D:\GENET\Projeler\Python\Object Detection\2\Real-Time-Object-Detection-main\.venv\Lib\site-packages\pafy\backend_youtube_dl.py

self._likes = self._ydl_info['like_count']
self._dislikes = self._ydl_info['dislike_count']
Daraan
  • 1,797
  • 13
  • 24