-
Notifications
You must be signed in to change notification settings - Fork 0
/
Code_youtube_upload.py
104 lines (89 loc) · 3.27 KB
/
Code_youtube_upload.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
import os
import time
import xlrd
import random
import httplib2
import http.client
import googleapiclient
import google_auth_oauthlib
import googleapiclient.errors
import google_auth_oauthlib.flow
import googleapiclient.discovery
from googleapiclient.errors import HttpError
from googleapiclient.http import MediaFileUpload
scopes = ["https://www.googleapis.com/auth/youtube.upload"]
class upload:
def __init__(self):
self.video1_file_path = ""
self.title_of_video = ""
self.video_description = ""
self.video_tags = ""
self.category_id = int
self.privacy_status = ""
self.file_path_thumbnail = ""
self.channel_id = ""
self.file_name = ""
print("ENTER PATH OF FILE")
self.path = input()
self.open_excel_file(1)
def open_excel_file(self,start):
loc = str(self.path)
self.start=start
wb = xlrd.open_workbook(loc)
sheet = wb.sheet_by_index(0)
column = sheet.ncols
rows = sheet.nrows
print(rows,column)
youtube = self.authorization()
for i in range(start,rows):
for j in range(0,column):
self.video1_file_path = sheet.cell_value(i,0)
self.title_of_video = sheet.cell_value(i,1)
self.video_description = sheet.cell_value(i,2)
self.video_tags = sheet.cell_value(i,3)
self.category_id =int(sheet.cell_value(i,4))
self.privacy_status = sheet.cell_value(i,5)
self.file_path_thumbnail = sheet.cell_value(i,6)
self.channel_id = sheet.cell_value(i,7)
self.file_name = sheet.cell_value(i,8)
#print(sheet.cell_value(i,j))
self.initialize_upload(youtube)
if i<rows:
open_excel_file(i)
else :
print('uploaded')
def authorization(self) :
# Disable OAuthlib's HTTPS verification when running locally.
# *DO NOT* leave this option enabled in production.
os.environ["OAUTHLIB_INSECURE_TRANSPORT"] = "1"
api_service_name = "youtube"
api_version = "v3"
client_secrets_file = r"C:\Users\Devhuti\Desktop\client_credentials3.json"
# 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()
return googleapiclient.discovery.build(
api_service_name, api_version, credentials=credentials)
def initialize_upload(self,youtube):
self.body=dict(
snippet = dict(
title=self.title_of_video,
description=self.video_description,
tags=self.video_tags,
categoryId=self.category_id,
file = self.video1_file_path,
channelid = self.channel_id,
),
status=dict(
privacyStatus=self.privacy_status
),
fileDetails=dict(
fileName = self.file_name)
)
self.insert_request = youtube.videos().insert(part=",".join(self.body.keys()),
body=self.body,
media_body=MediaFileUpload(self.video1_file_path,chunksize=-1, resumable=True))
#media_body.
self.insert_request.execute()
ud = upload()