diff --git a/README.md b/README.md index 37fd8df..5a1cd74 100644 --- a/README.md +++ b/README.md @@ -8,9 +8,13 @@ [![Donate Steam](https://img.shields.io/badge/donate-steam-green.svg)](https://steamcommunity.com/tradeoffer/new/?partner=293059984&token=0-l_idZR) [![Donate PayPal](https://img.shields.io/badge/donate-paypal-blue.svg)](https://www.paypal.me/0ffish) - Automatically make video compilations of the most viewed Twitch clips and upload them to YouTube using Python 3. +## Example +![Screenshot](https://user-images.githubusercontent.com/30203217/103347433-4e5a7400-4a97-11eb-833a-0f5d59b0cd7e.png) + +[Here](https://www.youtube.com/channel/UCd0wttXr03lIcTLv38U5d-w) is an example of how the videos look like on YouTube. Majority of these videos are made using +this repo. Only a couple of titles and thumbnails have been changed. ## Installation Download the repo as ZIP and unzip it somewhere accessible. @@ -111,10 +115,6 @@ To run the script run this command (must be in the correct folder). python main.py ``` -## Example -[Here](https://www.youtube.com/channel/UCd0wttXr03lIcTLv38U5d-w) is an example of how the videos look like on YouTube. Majority of these videos are made using -this repo. Only a couple of titles and thumbnails have been changed. - ## Note I've only tested this script using Python 3.7.3, but should work with later versions. diff --git a/twitchtube/__init__.py b/twitchtube/__init__.py index 15c6031..d1d8b67 100644 --- a/twitchtube/__init__.py +++ b/twitchtube/__init__.py @@ -2,4 +2,4 @@ __title__ = 'twitchtube' __author__ = 'offish' __license__ = 'MIT' -__version__ = '1.3.1' +__version__ = '1.3.2' diff --git a/twitchtube/api.py b/twitchtube/api.py new file mode 100644 index 0000000..b0d5a01 --- /dev/null +++ b/twitchtube/api.py @@ -0,0 +1,44 @@ +import json + +from .config import OAUTH_TOKEN, CLIENT_ID + +import requests + + +local = locals() + + +def request(endpoint: str, headers: dict, params: dict) -> dict: + return requests.get( + 'https://api.twitch.tv/' + endpoint, + headers = headers, + params = params + ) + + +def data(slug: str) -> dict: + return request( + 'helix/clips', + { + 'Authorization': f'Bearer {OAUTH_TOKEN}', + 'Client-Id': CLIENT_ID + }, + {'id': slug} + ) + + +def top_clips(headers: dict, params: dict) -> dict: + return request( + 'kraken/clips/top', + headers, + params + ) + + +def get(name: str, **args) -> dict: + response = local[name](**args) + + try: + return response.json() + except SyntaxError: + return response diff --git a/twitchtube/clips.py b/twitchtube/clips.py index 1e38cae..9177e67 100644 --- a/twitchtube/clips.py +++ b/twitchtube/clips.py @@ -5,6 +5,7 @@ from .config import CLIENT_ID, OAUTH_TOKEN, PARAMS, HEADERS from .logging import Log +from .api import get import requests @@ -17,16 +18,7 @@ def get_data(slug: str) -> dict: Gets the data from a given slug, returns a JSON respone from the Helix API endpoint """ - response = requests.get( - 'https://api.twitch.tv/helix/clips', - headers={ - 'Authorization': 'Bearer ' + OAUTH_TOKEN, - 'Client-Id': CLIENT_ID - }, - params={ - 'id': slug - } - ).json() + response = get('data', slug=slug) try: return response['data'][0] @@ -55,7 +47,7 @@ def get_clip_data(slug: str) -> tuple: return mp4_url, title - raise TypeError(f'Twitch didn\'t send what we wanted as response (could not find \'data\' in response). Response from /helix/ API endpoint:\n{clip_info}') + raise TypeError(f'We didn\'t receieve what we wanted. /helix/clips endpoint gave:\n{clip_info}') def get_progress(count, block_size, total_size) -> None: @@ -103,11 +95,7 @@ def get_clips(game: str, path: str) -> dict: PARAMS['game'] = game - response = requests.get( - 'https://api.twitch.tv/kraken/clips/top', - headers = HEADERS, - params = PARAMS - ).json() + response = get('top_clips', headers=HEADERS, params=PARAMS) if 'clips' in response: diff --git a/twitchtube/logging.py b/twitchtube/logging.py index 1362908..e3c81fa 100644 --- a/twitchtube/logging.py +++ b/twitchtube/logging.py @@ -12,6 +12,7 @@ def log(color: int, sort: str, text: str) -> None: Used for colored printing, does not return anything. """ time = datetime.now().time().strftime('%H:%M:%S') + text.encode(encoding='UTF-8', errors='ignore') print(f'{f.GREEN}twitchtube {f.WHITE}| {time} - {color + sort}{f.WHITE}: {text}{f.WHITE}')