Skip to content

Commit

Permalink
Merge pull request #23 from offish/v1.4.1
Browse files Browse the repository at this point in the history
v1.4.1
  • Loading branch information
offish authored Jan 23, 2021
2 parents cd3fb86 + e679d92 commit 95d9927
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 16 deletions.
15 changes: 11 additions & 4 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,13 @@
from twitchtube.utils import create_video_config, get_date
from twitchtube.clips import get_clips, download_clips
from twitchtube.video import render
from twitchtube import __name__, __version__


log = Log()

log.info(f"Running {__name__} at v{__version__}")


while True:

Expand All @@ -24,7 +27,7 @@

# Here we check if we've made a video for today
# by checking if the rendered file exists.
if not exists(path + f"\\{FILE_NAME}.mp4"):
if not exists(path + f"/{FILE_NAME}.mp4"):

# We want to retry because Twitch often gives a
# 500 Internal Server Error when trying to get clips
Expand All @@ -47,7 +50,7 @@
render(path)

if SAVE_TO_FILE:
with open(path + f"\\{SAVE_FILE_NAME}.json", "w") as f:
with open(path + f"/{SAVE_FILE_NAME}.json", "w") as f:
dump(config, f, indent=4)

if UPLOAD_TO_YOUTUBE and RENDER_VIDEO:
Expand All @@ -62,9 +65,13 @@
files = glob(f"{path}/*.mp4")

for file in files:
if not file == path + f"\\{FILE_NAME}.mp4":
if (
not file.replace("\\", "/")
== path + f"/{FILE_NAME}.mp4"
):
try:
remove(file)
log.info(f"Deleted {file.replace(path, '')}")
# Sometimes a clip is "still being used" giving
# us an exception that would else crash the program
except PermissionError as e:
Expand All @@ -75,7 +82,7 @@
else:
# Response was most likely an Internal Server Error and we retry
log.info(
f"There was an error or timeout on Twitch's end, retrying... {i + 1}/{RETRIES}"
f"There was an error Twitch's end or no clips were found. If no error has been thrown please check your PARAMS option in config.py, retrying... {i + 1}/{RETRIES}"
)

else:
Expand Down
2 changes: 1 addition & 1 deletion twitchtube/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
__title__ = "twitchtube"
__author__ = "offish"
__license__ = "MIT"
__version__ = "1.4.0"
__version__ = "1.4.1"
9 changes: 4 additions & 5 deletions twitchtube/clips.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
import urllib.request
import re

from .config import CLIENT_ID, OAUTH_TOKEN, MODE, PARAMS, HEADERS
from .logging import Log
from .config import *
from .api import get

import requests
Expand Down Expand Up @@ -40,11 +40,10 @@ def get_clip_data(slug: str) -> tuple:
# All to get what we need to return
# the mp4_url and title of the clip
thumb_url = clip_info["thumbnail_url"]
title = clip_info["title"]
slice_point = thumb_url.index("-preview-")
mp4_url = thumb_url[:slice_point] + ".mp4"

return mp4_url, title
return mp4_url, clip_info["title"]

raise TypeError(
f"We didn't receieve what we wanted. /helix/clips endpoint gave:\n{clip_info}"
Expand Down Expand Up @@ -76,12 +75,12 @@ def download_clip(clip: str, basepath: str) -> None:
mp4_url, clip_title = get_clip_data(slug)
# Remove special characters so we can save the video
regex = re.compile("[^a-zA-Z0-9_]")
clip_title = clip_title.replace(" ", "_")
clip_title = clip_title.replace(" ", "_") if CLIP_TITLE == "title" else slug
out_filename = regex.sub("", clip_title) + ".mp4"
output_path = basepath + "/" + out_filename

log.info(f"Downloading clip with slug: {slug}.")
log.info(f'Saving "{clip_title}" as "{out_filename}".')
log.info(f"Saving '{clip_title}' as '{out_filename}'.")
# Download the clip with given mp4_url
urllib.request.urlretrieve(mp4_url, output_path, reporthook=get_progress)
log.info(f"{slug} has been downloaded.")
Expand Down
7 changes: 5 additions & 2 deletions twitchtube/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@


# Paths
PATH = str(pathlib.Path().absolute())
CLIP_PATH = PATH + "\\clips\\{}\\{}"
PATH = str(pathlib.Path().absolute()).replace("\\", "/")
CLIP_PATH = PATH + "/clips/{}/{}"


# Video
Expand Down Expand Up @@ -45,6 +45,9 @@
# Name of the rendered video
FILE_NAME = "rendered"

# Name of downloaded clip (slug/title)
CLIP_TITLE = "slug"

# Enable (True/False)
# Resize (True/False) read RESIZE_CLIPS
# Path to video file (str)
Expand Down
2 changes: 1 addition & 1 deletion twitchtube/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,4 @@ def get_file(category: str) -> str:
"""
Gets the path/file given category and returns it as a string.
"""
return f"{CLIP_PATH.format(get_date(), category)}\\{FILE_NAME}.mp4"
return f"{CLIP_PATH.format(get_date(), category)}/{FILE_NAME}.mp4"
12 changes: 9 additions & 3 deletions twitchtube/video.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import os

from twitchtube.config import *
from twitchtube.logging import Log
from twitchtube.config import *

from moviepy.editor import VideoFileClip, concatenate_videoclips


log = Log()


Expand Down Expand Up @@ -62,14 +63,19 @@ def render(path: str) -> None:

final = concatenate_videoclips(video, method="compose")
final.write_videofile(
f"{path}\\{FILE_NAME}.mp4",
f"{path}/{FILE_NAME}.mp4",
fps=FRAMES,
temp_audiofile=f"{path}\\temp-audio.m4a",
temp_audiofile=f"{path}/temp-audio.m4a",
remove_temp=True,
codec="libx264",
audio_codec="aac",
)

for clip in video:
clip.close()

final.close()

print() # New line for cleaner logging
log.info("Video is done rendering!\n")

Expand Down

0 comments on commit 95d9927

Please sign in to comment.