Skip to content

Commit

Permalink
Merge pull request #29 from offish/v1.5.1
Browse files Browse the repository at this point in the history
v1.5.1
  • Loading branch information
offish authored Feb 8, 2021
2 parents 652f4f4 + 66cf4da commit 955d1dd
Show file tree
Hide file tree
Showing 8 changed files with 74 additions and 182 deletions.
2 changes: 0 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,6 @@ DESCRIPTIONS = {
}
```

Counter-Strike: Global Offensive is currently not supported since folders cannot include colons in their folder name.


## Running
To run the bot, use this command. Has to be in same directory as the `requirements.txt` and `main.py` files are.
Expand Down
37 changes: 22 additions & 15 deletions main.py
Original file line number Diff line number Diff line change
@@ -1,33 +1,31 @@
from os.path import exists
from pathlib import Path
from time import sleep
from json import dump, JSONDecodeError
from glob import glob
from os import remove
import os

from twitchtube.logging import Log
from twitchtube.config import *
from twitchtube.upload import Upload
from twitchtube.utils import create_video_config, get_date
from twitchtube.utils import create_video_config, get_path
from twitchtube.clips import get_clips, download_clips
from twitchtube.video import render
from twitchtube import __name__, __version__

from opplast import Upload


log = Log()

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


while True:

for category in LIST:

path = CLIP_PATH.format(get_date(), category)
path = get_path(category)

# 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 os.path.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 @@ -54,13 +52,22 @@
dump(config, f, indent=4)

if UPLOAD_TO_YOUTUBE and RENDER_VIDEO:
upload = Upload(ROOT_PROFILE_PATH, config, SLEEP)
was_uploaded, video_id = upload.upload()
upload = Upload(ROOT_PROFILE_PATH, SLEEP, HEADLESS, DEBUG)

log.info("Trying to upload video to YouTube")

try:
was_uploaded, video_id = upload.upload(config)

if was_uploaded:
log.info(
f"{video_id} was successfully uploaded to YouTube"
)

if was_uploaded:
log.info(f"{video_id} was successfully uploaded to YouTube")
else:
log.error("Video was not successfully uploaded to YouTube")
except Exception as e:
log.error(
f"There was an error {e} when trying to upload to YouTube"
)

if DELETE_CLIPS:
# Get all the mp4 files in the path and delte them
Expand All @@ -73,7 +80,7 @@
== path + f"/{FILE_NAME}.mp4"
):
try:
remove(file)
os.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
Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
moviepy==1.0.3
colorama
selenium
opplast
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.5.0"
__version__ = "1.5.1"
32 changes: 23 additions & 9 deletions twitchtube/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,29 @@
# Path to the Firefox profile where you are logged into YouTube
ROOT_PROFILE_PATH = ""

# Selenium
# How many seconds Firefox should sleep for when uploading
SLEEP = 3

# If True Firefox will be hidden (True/False)
HEADLESS = True

# If information when uploading should be printed (True/False)
DEBUG = True


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


# Video
# Set the mode (game/channel)
MODE = "channel"
MODE = "game"

# If mode is channel put channel names e.g. ["trainwreckstv", "xqcow"]
# If mode is game put game names e.g. ["Team Fortress 2", "Just Chatting"]
LIST = ["trainwreckstv", "xqcow"]
LIST = ["Team Fortress 2", "Just Chatting"]

# If clips should be rendered into one video (True/False)
# If set to False everything else under Video will be ignored
Expand Down Expand Up @@ -102,16 +110,22 @@
# Not supported yet
CATEGORY = 20 # 20 for gaming

# Tags
# Not supported yet
TAGS = {
"Just Chatting": "just chatting, just chatting clips, just chatting twitch clips",
"Team Fortress 2": "tf2, tf2 twitch, tf2 twitch clips",
}

# Descriptions
# {} will be replaced with a list of streamer names
DESCRIPTIONS = {
"Just Chatting": "Just Chatting twitch clips \n\n{}\n",
"Team Fortress 2": "TF2 twitch clips\n\n{}\n",
}

# Thumbnails
THUMBNAILS = {
"Just Chatting": "path/to/file.jpg",
"Team Fortress 2": "path/to/file.jpg",
}

# Tags
# Not supported yet
TAGS = {
"Just Chatting": "just chatting, just chatting clips, just chatting twitch clips",
"Team Fortress 2": "tf2, tf2 twitch, tf2 twitch clips",
}
22 changes: 0 additions & 22 deletions twitchtube/constants.py

This file was deleted.

114 changes: 0 additions & 114 deletions twitchtube/upload.py

This file was deleted.

46 changes: 27 additions & 19 deletions twitchtube/utils.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from datetime import date
from re import sub
import json

from .config import *
Expand All @@ -11,25 +12,37 @@ def get_date() -> str:
return date.today().strftime("%b-%d-%Y")


def get_path(category: str) -> str:
return CLIP_PATH.format(get_date(), category.replace(":", ""))


def create_video_config(category: str, streamers: list) -> dict:
"""
Creates the video config used for uploading to YouTube
returns a dict.
"""
return {
"category": CATEGORY,
"keywords": get_tags(category),
"description": get_description(category, streamers),
"title": get_title(category),
"description": get_description(category, streamers),
"thumbnail": get_thumbnail(category),
"keywords": get_tags(category),
"category": CATEGORY,
"file": get_file(category),
}


def get_tags(category: str) -> str:
def get_title(category: str) -> str:
"""
Gets the tag for given category (if any) as a string.
Gets the title and returns it as a string.
"""
return str(TAGS.get(category))
if TITLE:
return TITLE

title = json.loads(open(f"{get_path(category)}/clips.json", "r").read())

for i in title:
# Return the first entry's title
return f"{title[i]['title']} - {category} Twitch Highlights"


def get_description(category: str, streamers: list) -> str:
Expand All @@ -47,24 +60,19 @@ def get_description(category: str, streamers: list) -> str:
return names


def get_title(category: str) -> str:
"""
Gets the title and returns it as a string.
"""
if TITLE:
return TITLE
def get_thumbnail(category: str) -> str:
return THUMBNAILS.get(category)

title = json.loads(
open(f"{CLIP_PATH.format(get_date(), category)}/clips.json", "r").read()
)

for i in title:
# Return the first entry's title
return f"{title[i]['title']} - {category} Twitch Highlights"
def get_tags(category: str) -> str:
"""
Gets the tag for given category (if any) as a string.
"""
return str(TAGS.get(category))


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"{get_path(category)}/{FILE_NAME}.mp4"

0 comments on commit 955d1dd

Please sign in to comment.