Skip to content

Commit

Permalink
Merge pull request #50 from offish/v1.6.2
Browse files Browse the repository at this point in the history
v1.6.2
  • Loading branch information
offish authored May 2, 2021
2 parents 45aec60 + ee9ac3c commit fb466c4
Show file tree
Hide file tree
Showing 8 changed files with 68 additions and 37 deletions.
32 changes: 22 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,28 @@ Automatically make video compilations of the most viewed Twitch clips, and uploa
## Usage
Example of making a video through terminal
```python
python main.py "g Just Chatting, c xQcOW, c Trainwreckstv" --duration 10.5 --resolution 1080 1920 --title "Top Just Chatting, xQc and Trainwrecks Twitch Clips Today" --tags "xqc, trainwrecks, twitch clips, xqc twitch, trainwrecks twitch"
python main.py --data "g Just Chatting, c xQcOW, c Trainwreckstv" --client_id "1hq8ektpki36w5kn37mluioungyqjo" --oauth_token "9f5einm9qtp0bj4m9l1ykevpwdn98o" --duration 10.5 --resolution 1080 1920 --title "Top Just Chatting, xQc and Trainwrecks Twitch Clips Today" --tags "xqc, trainwrecks, twitch clips, xqc twitch, trainwrecks twitch"
```
`g` indicates game, `c` indicates channel. You can also use `channel` and or `game`.
`g` indicates game, `c` indicates channel. You can also use `channel` and `game`.

The only parameter required is `data`. **Every other parameter that is not specified, will default to an assigned value in [`config.py`](twitchtube/config.py).**
**Every parameter that is not specified, will default to an assigned value in [`config.py`](twitchtube/config.py).**

Example of minium required input:
Example of running with only default values:

```text
python main.py
```

[`config.py`](twitchtube/config.py):
```python
python main.py "channel maskenissen"
DATA = ["channel maskenissen"]

CLIENT_ID = "1hq8ektpki36w5kn37mluioungyqjo" # Twitch Client ID
OAUTH_TOKEN = "9f5einm9qtp0bj4m9l1ykevpwdn98o" # Twitch OAuth Token
PERIOD = "day" # day, week, month or all
LANGUAGE = "en" # en, es, th etc.
LIMIT = 100 # 1-100
...
```

You can also run the bot from a Python file like [`example.py`](example.py)
Expand All @@ -33,13 +45,13 @@ from twitchtube.video import make_video

while True:
make_video(
["channel xQcOW", "game Just Chatting"],
data=["channel xQcOW", "game Just Chatting"],
client_id="1hq8ektpki36w5kn37mluioungyqjo", # example client id (fake)
oauth_token="9f5einm9qtp0bj4m9l1ykevpwdn98o", # example token (fake)
video_length=10.5,
resolution=(1080, 1920),
video_length=10.5, # minutes as float
resolution=(1080, 1920), # height x width
frames=60,
period="day",
period="day", # most viewed clips today
)
sleep(24 * 60 * 60) # make a video daily
```
Expand Down Expand Up @@ -108,7 +120,7 @@ These are all the parameters `make_video` takes
```python
def make_video(
# required
data: list,
data: list = DATA,
# other
path: str = get_path(),
check_version: bool = CHECK_VERSION,
Expand Down
2 changes: 1 addition & 1 deletion example.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@


make_video(
["c xQcOW", "game Just Chatting"],
data=["c xQcOW", "game Just Chatting"],
client_id="1hq8ektpki36w5kn37mluioungyqjo", # example client id (fake)
oauth_token="9f5einm9qtp0bj4m9l1ykevpwdn98o", # example token (fake)
video_length=10.5,
Expand Down
2 changes: 1 addition & 1 deletion main.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def str_to_bool(value):
raise argparse.ArgumentTypeError("Boolean value expected.")


parser.add_argument("data", type=str, help="")
parser.add_argument("--data", type=str, help="")
parser.add_argument("--path", type=str, help="")
parser.add_argument("--check_version", type=str_to_bool, help="")
parser.add_argument("--client_id", type=str, help="")
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.6.1"
__version__ = "1.6.2"
41 changes: 23 additions & 18 deletions twitchtube/clips.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import urllib.request
import re

from .exceptions import WrongKrakenResponse
from .logging import Log
from .api import get

Expand Down Expand Up @@ -93,13 +92,14 @@ def get_clips(
period: str,
language: str,
limit: int,
) -> (dict, list):
) -> (dict, list, list):
"""
Gets the top clips for given game, returns JSON response
from the Kraken API endpoint.
"""
data = {}
new_ids = []
new_titles = []

headers = {"Accept": "application/vnd.twitchtv.v5+json", "Client-ID": client_id}

Expand Down Expand Up @@ -128,28 +128,33 @@ def get_clips(
)

else:
raise WrongKrakenResponse(f"Could not find 'clips' in response. {response}")
log.warn(
f'Did not find "clips" in response {response} for {category} {name}, period: {period} language: {language}'
)

for clip in response["clips"]:
duration = clip["duration"]
if "clips" in response:
for clip in response["clips"]:
duration = clip["duration"]

if seconds <= 0.0:
break
if seconds <= 0.0:
break

tracking_id = clip["tracking_id"]
tracking_id = clip["tracking_id"]

if not tracking_id in ids:
data[clip["tracking_id"]] = {
"url": "https://clips.twitch.tv/" + clip["slug"],
"title": clip["title"],
"display_name": clip["broadcaster"]["display_name"],
"duration": duration,
}
new_ids.append(tracking_id)
if not tracking_id in ids:
data[clip["tracking_id"]] = {
"url": "https://clips.twitch.tv/" + clip["slug"],
"title": clip["title"],
"display_name": clip["broadcaster"]["display_name"],
"duration": duration,
}
new_ids.append(tracking_id)
new_titles.append(clip["title"])
seconds -= duration

seconds -= duration
return (data, new_ids, new_titles)

return (data, new_ids)
return ({}, [], [])


def download_clips(data: dict, path: str, oauth_token: str, client_id: str) -> list:
Expand Down
4 changes: 3 additions & 1 deletion twitchtube/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
True # see if youre running the latest version of twitchtube and opplast
)

DATA = ["c xQcOW", "c Trainwreckstv", "g Just Chatting"]

# twitch
CLIENT_ID = "" # Twitch Client ID
OAUTH_TOKEN = "" # Twitch OAuth Token
Expand Down Expand Up @@ -55,7 +57,7 @@


# youtube
TITLE = "" # youtube title
TITLE = "" # youtube title, leave empty for the first clip's title
DESCRIPTION = (
"Streamers in this video:\n" # youtube description, streamers will be added
)
Expand Down
2 changes: 1 addition & 1 deletion twitchtube/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ class VideoPathAlreadyExists(Exception):
pass


class WrongKrakenResponse(Exception):
class NoClipsFound(Exception):
pass
20 changes: 16 additions & 4 deletions twitchtube/video.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,13 @@
from moviepy.editor import VideoFileClip, concatenate_videoclips
from opplast import Upload, __version__ as opplast_version


log = Log()

# add language as param
def make_video(
# required
data: list,
data: list = DATA,
# other
path: str = get_path(),
check_version: bool = CHECK_VERSION,
Expand Down Expand Up @@ -87,6 +88,7 @@ def make_video(
except Exception as e:
print(e)

titles = []
clips = []
names = []
ids = []
Expand Down Expand Up @@ -117,7 +119,7 @@ def make_video(
)

# so we dont add the same clip twice
new_clips, new_ids = get_clips(
new_clips, new_ids, new_titles = get_clips(
category,
name,
path,
Expand All @@ -130,8 +132,13 @@ def make_video(
limit,
)

ids += new_ids
clips.append(new_clips)
if new_clips:
ids += new_ids
titles += new_titles
clips.append(new_clips)

if not clips:
raise NoClipsFound("Did not find any clips")

Path(path).mkdir(parents=True, exist_ok=True)

Expand All @@ -143,6 +150,9 @@ def make_video(
# remove duplicate names
names = list(dict.fromkeys(names))

if not title:
title = titles[0]

config = create_video_config(
path, file_name, title, description, thumbnail, tags, names
)
Expand Down Expand Up @@ -196,6 +206,8 @@ def make_video(
except Exception as e:
log.clip(f"Could not delete {file} because {e}")

log.info("Done!")


def get_clip_paths(path: str) -> list:
"""
Expand Down

0 comments on commit fb466c4

Please sign in to comment.