Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python code that updates YouTube thumbnails every 10 minutes, fails after about an hour - do I need to use refresh tokens?

So, earlier today, I started a YouTube experiment that involves updating the thumbnail image every 10 minutes. So, I based my Python code off of a code sample from the Google Developers page on youtube.thumbnails.set(): https://developers.google.com/youtube/v3/docs/thumbnails/set

My code looks like this:

def doCredentialStuff():
    global youtube
    global flow
    os.environ["OAUTHLIB_INSECURE_TRANSPORT"] = "1"

    # Get credentials and create an API client
    flow = google_auth_oauthlib.flow.InstalledAppFlow.from_client_secrets_file(
        client_secrets_file, scopes)
    credentials = flow.run_console(access_type='offline')
    youtube = googleapiclient.discovery.build(
        api_service_name, api_version, credentials=credentials)

def updateYTthumb(filename):
    try:
        request = youtube.thumbnails().set(
            videoId=VIDEO_ID_UPLOAD,media_body=MediaFileUpload(filename))
        response = request.execute()
        print(response)
    except Exception as exc:
        print("ERROR!!!! I got this exception: "+str(exc)+" at time "+str(time.time()))

For my purposes, I want to upload thumbnails every 10 minutes. So, I run doCredentials() only once, at the start of running the code. Then, every 10 minutes, I re-run updateYTthumb(), where it actually executes the request. (This part)

It works perfectly for anywhere from 2 to 12 iterations! (20 - 120 minutes). However, if I let it sit for long enough, it always eventually returns this error:

 [WinError 10053] An established connection was aborted by the software in your host machine

I'm not sure how to fix this:

  • Should I get a refresh token somewhere in doCredentialStuff(), and then use it during updateYTthumb()?
like image 295
Cary Huang Avatar asked Dec 02 '25 10:12

Cary Huang


1 Answers

The error is saying that something on your computer interrupted the connection, here are a few things you can try:

  • Updating your google-api-python-client
  • Disabling your antivirus
  • Disabling your firewall
like image 61
Alex Avatar answered Dec 03 '25 23:12

Alex



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!