backend_youtube_dl.py", line 54, in _fetch_basic self._dislikes = self._ydl_info['dislike_count'] KeyError: 'dislike_count'
Asked Answered
W

8

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)
Watchword answered 14/12, 2021 at 6:49 Comment(3)
try updating youtube_dl sometimes youtubes changes way videos are served, causing errors like theseFatso
The packages are updated Requirement already satisfied: youtube_dl in site-packages (2021.5.16)Watchword
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.Cosmotron
P
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.

Polad answered 14/12, 2021 at 6:56 Comment(4)
What can be the solutionWatchword
@lewismachilika either wait for the package to be updated, send them a pull request, or patch it up locally by yourself.Polad
I have patched it locallyWatchword
How did you patch it locally? Can you share that as an answer?Gouveia
S
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']
Skardol answered 15/3, 2022 at 19:57 Comment(0)
H
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.

Hominoid answered 28/5, 2022 at 17:39 Comment(1)
Yup, this works for meDeflagrate
L
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

Lowborn answered 1/6, 2022 at 9:36 Comment(0)
D
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

Disconsolate answered 25/12, 2021 at 9:46 Comment(1)
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.Sawicki
S
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.

Snood answered 18/7, 2022 at 17:12 Comment(0)
S
1

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

Sunroom answered 24/11, 2022 at 7:3 Comment(0)
D
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']
Disgorge answered 13/1, 2023 at 8:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.