Skip to content

Commit

Permalink
update google api
Browse files Browse the repository at this point in the history
  • Loading branch information
nang-dev committed Apr 1, 2021
1 parent 870e3a5 commit 4251cb7
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 49 deletions.
4 changes: 1 addition & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,7 @@ Code to run a fully automated youtube that can scrape content, edit a compilatio

4. Get setup and create a Project with the Youtube API: https://developers.google.com/youtube/v3/quickstart/python
Be sure to follow it carefully, as it won't work if you don't do this part right.
Download your OATH file as "secret.json" in your folder.

5. Run `python3 setup_google.py` and follow the web page that opens up.
Download your OATH file as "googleAPI.json" in your project folder.

6. Create an instagram account and follow accounts you want to scrape from

Expand Down
61 changes: 37 additions & 24 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@
import datetime
import os
import shutil
import google_auth_oauthlib.flow #pip install google-auth-oauthlib
import googleapiclient.discovery #pip install google-api-python-client
import googleapiclient.errors

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


# FILL THESE OUT
IG_USERNAME = ""
Expand All @@ -14,9 +20,9 @@
INTRO_VID = '' # SET AS '' IF YOU DONT HAVE ONE
OUTRO_VID = ''
TOTAL_VID_LENGTH = 13*60
MAX_CLIP_LENGTH = 18
MIN_CLIP_LENGTH = 4
DAILY_SCHEDULED_TIME = "19:05"
MAX_CLIP_LENGTH = 19
MIN_CLIP_LENGTH = 5
DAILY_SCHEDULED_TIME = "20:00"

num_to_month = {
1: "Jan",
Expand All @@ -33,13 +39,24 @@
12: "Dec"
}

# Setup Google API
os.environ["OAUTHLIB_INSECURE_TRANSPORT"] = "1"
api_service_name = "youtube"
api_version = "v3"
client_secrets_file = "googleAPI.json"
flow = google_auth_oauthlib.flow.InstalledAppFlow.from_client_secrets_file(
client_secrets_file, scopes)
credentials = flow.run_console()
googleAPI = googleapiclient.discovery.build(
api_service_name, api_version, credentials=credentials)

def routine():
now = datetime.datetime.now()
print(now.year, now.month, now.day, now.hour, now.minute, now.second)
title = "TEST V" + str(now.month) + "." + str(now.day)
videoDirectory = "/tmp/Memes_" + num_to_month[now.month].upper() + "_" + str(now.year) + "_V" + str(now.day) + "/"
title = "TRY NOT TO LAUGH (BEST Dank video memes) V1"
videoDirectory = "./Memes_" + num_to_month[now.month].upper() + "_" + str(now.year) + "_V" + str(now.day) + "/"
outputFile = "./" + num_to_month[now.month].upper() + "_" + str(now.year) + "_v" + str(now.day) + ".mp4"
metadataFile = "./metadata-" + num_to_month[now.month].upper() + "_" + str(now.year) + "_v" + str(now.day) + ".txt"
#metadataFile = "./metadata-" + num_to_month[now.month].upper() + "_" + str(now.year) + "_v" + str(now.day) + ".txt"
description = ""
print(outputFile)

Expand All @@ -54,17 +71,12 @@ def routine():
days=1)
print("Scraped Videos!")

f = open(metadataFile, "w")
f.write(title + "\n\n")
description = "Enjoy the memes :) \n\n" \
"like and subscribe to @Chewy for more \n\n" \
"The memes in the compilation are reposts from various private instagram meme accounts.\n" \
"this episode's were from: \n"
f.write(description)

# Step 2: Make Compilation
print("Making Compilation...")
description += makeCompilation(path = videoDirectory,
makeCompilation(path = videoDirectory,
introName = INTRO_VID,
outroName = OUTRO_VID,
totalVidLength = TOTAL_VID_LENGTH,
Expand All @@ -75,14 +87,13 @@ def routine():

description += "\n\nCopyright Disclaimer, Under Section 107 of the Copyright Act 1976, allowance is made for 'fair use' for purposes such as criticism, comment, news reporting, teaching, scholarship, and research. Fair use is a use permitted by copyright statute that might otherwise be infringing. Non-profit, educational or personal use tips the balance in favor of fair use.\n\n"
description += "#memes #dankmemes #compilation #funny #funnyvideos \n\n"
f.write(description + "\n\n")
f.close()

# Step 3: Upload to Youtube
print("Uploading to Youtube...")
uploadYtvid(VIDEO_FILE_NAME=outputFile,
title=title,
description=description)
description=description,
googleAPI=googleAPI)
print("Uploaded To Youtube!")

# Step 4: Cleanup
Expand All @@ -93,22 +104,24 @@ def routine():
# File outputFile
try:
os.remove(outputFile)
except OSError as e: ## if faile,d, report it back to the user ##
except OSError as e: ## if failed, report it back to the user ##
print ("Error: %s - %s." % (e.filename, e.strerror))
print("Removed temp files!")

def attemptRoutine():
try:
routine()
except OSError as err:
print("Routine Failed on " + "OS error: {0}".format(err))
time.sleep(60*60)
routine()
while(1):
try:
routine()
break
except OSError as err:
print("Routine Failed on " + "OS error: {0}".format(err))
time.sleep(60*60)

attemptRoutine()
#attemptRoutine()
schedule.every().day.at(DAILY_SCHEDULED_TIME).do(attemptRoutine)


while True:
schedule.run_pending()
schedule.run_pending()
time.sleep(60) # wait one min

10 changes: 1 addition & 9 deletions make_compilation.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ def makeCompilation(path = "./",
print(fileName)
filePath = os.path.join(path, fileName)
# Destination path
destination = os.path.join("./Duplicates/", fileName)
clip = VideoFileClip(filePath)
clip = clip.resize(width=1920)
clip = clip.resize(height=1080)
Expand All @@ -59,14 +58,7 @@ def makeCompilation(path = "./",
totalLength += duration

print("Total Length: " + str(totalLength))
for length in seenLengths:
lst = seenLengths[length]
if len(lst) > 1:
for fileName in lst:
filePath = os.path.join(path, fileName)
# Destination path
destination = os.path.join("./Duplicates/", fileName)
shutil.copy(filePath, destination)

random.shuffle(allVideos)

duration = 0
Expand Down
17 changes: 4 additions & 13 deletions upload_ytvid.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,11 @@
import datetime
from Google import Create_Service
from googleapiclient.http import MediaFileUpload

CLIENT_SECRET_FILE = 'secret.json'
API_NAME = 'youtube'
API_VERSION = 'v3'
SCOPES = ['https://www.googleapis.com/auth/youtube.upload']

# Note: Lots of Credit To Jie Jenn

service = Create_Service(CLIENT_SECRET_FILE, API_NAME, API_VERSION, SCOPES)

def uploadYtvid(VIDEO_FILE_NAME='',
title='Intro Video!',
description=':) ',
tags=[],):
tags=[],
googleAPI=None):

now = datetime.datetime.now()
upload_date_time = datetime.datetime(now.year, now.month, now.day, now.hour, now.minute, int(now.second)).isoformat() + '.000Z'
Expand All @@ -35,14 +26,14 @@ def uploadYtvid(VIDEO_FILE_NAME='',

mediaFile = MediaFileUpload(VIDEO_FILE_NAME, chunksize=-1, resumable=True)

response_upload = service.videos().insert(
response_upload = googleAPI.videos().insert(
part='snippet,status',
body=request_body,
media_body=mediaFile
).execute()

"""
service.thumbnails().set(
googleAPI.thumbnails().set(
videoId=response_upload.get('id'),
media_body=MediaFileUpload('thumbnail.png')
).execute()
Expand Down

0 comments on commit 4251cb7

Please sign in to comment.