Search code examples
pythonyoutubeyoutube-apiyoutube-data-api

How can i upload whole folder to Youtube with Youtube api


I need to upload nearly two hundered videos to my channel. I tried to upload them with yotube api but it gives error. I tried to make a request for each video with a for loop. Without for loop it can upload only one video but i wanna upload whole folder without entering OAuth key several times

Error:

  File "C:\Python36\lib\ssl.py", line 972, in sendall
    v = self.send(byte_view[count:])
  File "C:\Python36\lib\ssl.py", line 941, in send
    return self._sslobj.write(data)
  File "C:\Python36\lib\ssl.py", line 642, in write
    return self._sslobj.write(data)
socket.timeout: The write operation timed out
import os
import google_auth_oauthlib.flow
import googleapiclient.discovery
import googleapiclient.errors
from googleapiclient.http import MediaFileUpload

scopes = ["https://www.googleapis.com/auth/youtube.upload"]

def main():
    Song_list=os.listdir('C:\\Users\\erkan\\Desktop\\müzik ve kutusu\\hazir')

    os.environ["OAUTHLIB_INSECURE_TRANSPORT"] = "1"
    api_service_name = "youtube"
    api_version = "v3"
    client_secrets_file = "client_secret.json"

    flow = google_auth_oauthlib.flow.InstalledAppFlow.from_client_secrets_file(
        client_secrets_file, scopes)
    credentials = flow.run_console()
    youtube = googleapiclient.discovery.build(
        api_service_name, api_version, credentials=credentials)

    for song in Song_list: ######################## the For Loop ################
        song_name = song.replace('.mp4', '')
        request = youtube.videos().insert(
            part="snippet,status",
            body={
                "snippet": {
                    "categoryId": "10",
                    "description": "Description of uploaded video.",
                    "title": song_name,
                    "tags": ["müzik", "ve", "kutusu", song_name]
                },
                "status": {
                    "privacyStatus": "private"
                }
            },

            media_body=MediaFileUpload('C:\\Users\\erkan\\Desktop\\müzik ve kutusu\\hazir\\' + song)
        )
        response = request.execute()
        print(response)
        print(song + " uploaded")
if __name__ == "__main__":
    main()

Solution

  • i solved it with changing

    os.environ["OAUTHLIB_INSECURE_TRANSPORT"] = "0"
    

    and

    media_body=MediaFileUpload('C:\\Users\\erkan\\Desktop\\müzik ve kutusu\\hazir\\' + song, chunksize=-1, resumable=True)
    

    but idk which one of them is solved my problem ¯\_(ツ)_/¯