Search code examples
python-3.xyoutube-apivideo-thumbnails

Variable Undefined in Python Script


I wanted to download Thumbnails from a channel and I was using this Script that I found on GitHub. https://github.com/rohitnairtech/YoutubeChannelAllVideoThumbnailDownloader

So in line : pageCount +=1 it says undefined variable

Also a HTTP Bad Request When Script Runs.I was just thinking I will tackle it one by one if this Undefined variable could be sorted out first.

I have replaced the required section with my Youtube API and ChannelID. I am not a python programmer so I don't know how global variables work here or what could be a the solution to this. I read somethings on Globals but couldn't find a proper explanation of what the problem here is. enter image description here

def setURL (nextPage):
    sleep(4)
    global pageCount
    pageCount += 1 #Error Here

This code should download and store the thumbnails in a thumbnails folder that it automatically creates at root directory of script.

EDIT1: pageCount is already defined in line 55 just before calling setURL shouldn't that work ? Answer by Chetan Solves this Questions (That simple). Image 2 Code

EDIT2: ERROR Log after running the code: These are self destruct links after use.

https://file.io/rE6BdY

 https://file.io/d9fyJb

 https://file.io/XsOXMw

 https://file.io/hrWZBS

Solution

  • postCount=0 #you have to define it globally and intialize it first to use it 
    def setURL (nextPage): 
        sleep(4) 
        global pageCount 
        pageCount += 1 #Error Here