added PoC

This commit is contained in:
Askill 2024-03-03 11:39:07 +01:00
parent 9ca2739096
commit d5399d581a
7 changed files with 108 additions and 1 deletions

1
.gitignore vendored
View File

@ -158,3 +158,4 @@ cython_debug/
# and can be added to the global gitignore or merged into this file. For a more nuclear
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
#.idea/
.vscode/settings.json

View File

@ -1,2 +1,2 @@
# YT-thumb-roller
Automatically rotate youtube thumbnails, if you're not VIP enough for youtube to gtrant you the ability
Automatically rotate youtube thumbnails, if you're not VIP enough for youtube to grant you the ability.

40
helper.py Normal file
View File

@ -0,0 +1,40 @@
import glob
import json
import os
def get_ordered_files(dir_name):
# Get list of all files only in the given directory
list_of_files = filter(os.path.isfile, glob.glob(dir_name + '/*'))
# Sort list of files based on name
list_of_files = sorted(list_of_files)
return list_of_files
def get_next_tn(dir_name):
current_path = dir_name +"/current.json"
with open(current_path, 'r') as openfile:
current_tn = json.load(openfile)
tns = get_ordered_files(dir_name)
tn_name = ""
for i, tn in enumerate(tns):
if tn == current_tn["current"]:
tn_name = tns[(i+1)%len(tns)]
break
if tn_name == current_tn["current"]:
# nothing changed
return None
elif tn_name == "":
# old thumbnail was not found in dir
tn_name = tns[0]
current_tn["current"] = tn_name
with open(current_path, "r") as outfile:
outfile.write(json.dumps(current_tn))
return tn_name

16
main.py Normal file
View File

@ -0,0 +1,16 @@
from youtube import get_authenticated_service, set_thumbnail
import argparse
if __name__ == "__main__":
parser = argparse.ArgumentParser(description='Change youtube thumbnails automatically')
parser.add_argument('video_id', metavar='video_id', type=str,
help='id of the video you want to change the thumbnail of')
parser.add_argument('tn_path', metavar='tn_path', type=str,
help='path containing thumbnails')
args = parser.parse_args()
youtube = get_authenticated_service()
video_id = args.video_id
tn_path = args.tn_path
set_thumbnail(youtube, video_id, tn_path)

2
requirements.txt Normal file
View File

@ -0,0 +1,2 @@
google-auth-oauthlib
google-api-python-client

10
secrets.json Normal file
View File

@ -0,0 +1,10 @@
{
"installed": {
"client_id": "",
"project_id": "",
"client_secret": "",
"auth_uri": "https://accounts.google.com/o/oauth2/auth",
"token_uri": "https://oauth2.googleapis.com/token",
"auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs"
}
}

38
youtube.py Normal file
View File

@ -0,0 +1,38 @@
import os
import pickle
import google_auth_oauthlib.flow
import googleapiclient.discovery
from google.auth.transport.requests import Request
from helper import get_next_tn
def get_authenticated_service():
scopes = ["https://www.googleapis.com/auth/youtube.readonly", "https://www.googleapis.com/auth/youtube.upload"]
api_service_name = "youtube"
api_version = "v3"
client_secrets_file = "secrets.json"
if os.path.exists("CREDENTIALS_PICKLE_FILE"):
with open("CREDENTIALS_PICKLE_FILE", 'rb') as f:
credentials = pickle.load(f)
else:
flow = google_auth_oauthlib.flow.InstalledAppFlow.from_client_secrets_file(
client_secrets_file, scopes)
credentials = flow.run_local_server()
if credentials.expired:
credentials.refresh(Request())
with open("CREDENTIALS_PICKLE_FILE", 'wb') as f:
pickle.dump(credentials, f)
return googleapiclient.discovery.build(api_service_name, api_version, credentials=credentials)
def set_thumbnail(youtube, video_id, thumbnail_dir):
tn_path = get_next_tn(thumbnail_dir)
if tn_path is None:
return None
thumbnail_response = youtube.thumbnails().set(
videoId=video_id,
media_body=tn_path,
).execute()
return thumbnail_response