Skip to content

Commit

Permalink
Merge pull request #17 from offish/v1.3.2
Browse files Browse the repository at this point in the history
v1.3.2
  • Loading branch information
offish authored Dec 30, 2020
2 parents 141ad56 + 964d763 commit 9fc41ce
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 22 deletions.
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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.

Expand Down
2 changes: 1 addition & 1 deletion twitchtube/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
__title__ = 'twitchtube'
__author__ = 'offish'
__license__ = 'MIT'
__version__ = '1.3.1'
__version__ = '1.3.2'
44 changes: 44 additions & 0 deletions twitchtube/api.py
Original file line number Diff line number Diff line change
@@ -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
20 changes: 4 additions & 16 deletions twitchtube/clips.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

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

import requests

Expand All @@ -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]
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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:

Expand Down
1 change: 1 addition & 0 deletions twitchtube/logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}')


Expand Down

0 comments on commit 9fc41ce

Please sign in to comment.