Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion addon.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<addon id="script.module.python.twitch" name="python-twitch for Kodi" version="1.0.0~alpha9" provider-name="A Talented Community">
<addon id="script.module.python.twitch" name="python-twitch for Kodi" version="1.0.0~beta1" provider-name="A Talented Community">
<requires>
<import addon="xbmc.python" version="2.1.0"/>
<import addon="script.module.six" version="1.9.0"/>
Expand Down
62 changes: 60 additions & 2 deletions resources/lib/twitch/api/v5/videos.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
# -*- encoding: utf-8 -*-
# https://dev.twitch.tv/docs/v5/reference/videos/

from twitch import keys
from twitch.api.parameters import BroadcastType, Period
from twitch import keys, methods
from twitch.api.parameters import BroadcastType, Period, Language
from twitch.queries import V5Query as Qry
from twitch.queries import HiddenApiQuery as HQry
from twitch.queries import UploadsQuery as UQry
from twitch.queries import query


Expand Down Expand Up @@ -38,6 +39,63 @@ def get_followed(limit=10, offset=0, broadcast_type=BroadcastType.HIGHLIGHT):
return q


# required scope: channel_editor
@query
def create(channel_id, title, description=None, game=None, language=None, tag_list=None):
q = Qry('videos/', method=methods.POST)
q.add_param(keys.CHANNEL_ID, channel_id)
q.add_param(keys.TITLE, title)
q.add_param(keys.DESCRIPTION, description)
q.add_param(keys.GAME, game)
if language is not None:
q.add_param(keys.LANGUAGE, Language.validate(language))
q.add_param(keys.TAG_LIST, tag_list)
return q


# required scope: channel_editor
@query
def update(video_id, title=None, description=None, game=None, language=None, tag_list=None):
q = Qry('videos/{video_id}', method=methods.PUT)
q.add_urlkw(keys.VIDEO_ID, video_id)
q.add_param(keys.TITLE, title)
q.add_param(keys.DESCRIPTION, description)
q.add_param(keys.GAME, game)
if language is not None:
q.add_param(keys.LANGUAGE, Language.validate(language))
q.add_param(keys.TAG_LIST, tag_list)
return q


# required scope: channel_editor
@query
def delete(video_id):
q = Qry('videos/{video_id}', method=methods.DELETE)
q.add_urlkw(keys.VIDEO_ID, video_id)
return q


# requires upload token
@query
def upload_part(video_id, part, upload_token, content_length, data):
q = UQry('upload/{video_id}', method=methods.PUT)
q.set_headers({'Content-Length': content_length, 'Content-Type': 'application/octet-stream'})
q.add_urlkw(keys.VIDEO_ID, video_id)
q.add_param(keys.PART, part)
q.add_param(keys.UPLOAD_TOKEN, upload_token)
q.add_bin(data)
return q


# requires upload token
@query
def complete_upload(video_id, upload_token):
q = UQry('upload/{video_id}/complete', method=methods.POST)
q.add_urlkw(keys.VIDEO_ID, video_id)
q.add_param(keys.UPLOAD_TOKEN, upload_token)
return q


# required scope: none
# undocumented / unsupported
@query
Expand Down
3 changes: 3 additions & 0 deletions resources/lib/twitch/keys.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
NEED_HTTPS = 'need_https'
NOTIFICATIONS = 'notifications'
OFFSET = 'offset'
PART = 'part'
PERIOD = 'period'
POSITION = 'position'
POST_ID = 'post_id'
Expand All @@ -66,6 +67,7 @@
STATUS = 'status'
STREAM_TYPE = 'stream_type'
SUMMARY = 'summary'
TAG_LIST = 'tag_list'
TARGET_ID = 'target_id'
TEAM = 'team'
TITLE = 'title'
Expand All @@ -78,6 +80,7 @@
USER_AGENT_STRING = ('Mozilla/5.0 (Windows NT 6.1; WOW64; rv:6.0) '
'Gecko/20100101 Firefox/6.0')
USER_ID = 'user_id'
UPLOAD_TOKEN = 'upload_token'
VIDEO_ID = 'video_id'
VOD = 'vod'
XBOX_HEARTBEAT = 'xbox_heartbeat'
2 changes: 1 addition & 1 deletion resources/lib/twitch/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def m3u8_wrapper(*args, **kwargs):
else:
error = re.search(_error_pattern, results)
if error:
return {'error': 'Error', 'message': error.group('message'), 'status': 0}
return {'error': 'Error', 'message': error.group('message'), 'status': 404}
return m3u8_to_list(results)

return m3u8_wrapper
Expand Down
17 changes: 16 additions & 1 deletion resources/lib/twitch/queries.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
_hidden_baseurl = 'https://api.twitch.tv/api/'
_usher_baseurl = 'https://usher.ttvnw.net/'
_clips_baseurl = 'https://clips.twitch.tv/'
_uploads_baseurl = 'https://uploads.twitch.tv/'

_v4_headers = {'ACCEPT': 'application/vnd.twitchtv.v4+json'}
_v5_headers = {'ACCEPT': 'application/vnd.twitchtv.v5+json'}
Expand Down Expand Up @@ -62,6 +63,10 @@ def add_data(self, key, value, default=None):
self._data[key] = value
return self

def add_bin(self, data):
self._data = data
return self

def add_param(self, key, value, default=None):
assert_new(self._params, key)
if value != default:
Expand All @@ -73,8 +78,12 @@ def add_urlkw(self, kw, replacement):
self._urlkws[kw] = replacement
return self

def set_headers(self, headers):
self._headers = headers
return self

def __str__(self):
return '{method} Query to {url}, params {params}, data {data}, headers {headers}'\
return '{method} Query to {url}, params {params}, data {data}, headers {headers}' \
.format(url=self.url, params=self.params, headers=self.headers, data=self.data, method=self.method)

def execute(self, f):
Expand Down Expand Up @@ -129,6 +138,12 @@ def __init__(self, path, headers={}, data={}, method=methods.GET):
self.add_path(path)


class UploadsQuery(DownloadQuery):
def __init__(self, path, headers={}, data={}, method=methods.PUT):
super(UploadsQuery, self).__init__(_uploads_baseurl, headers, data, method)
self.add_path(path)


class V4Query(ApiQuery):
def __init__(self, path, method=methods.GET):
super(V4Query, self).__init__(path, _v4_headers, method=method)
Expand Down
4 changes: 2 additions & 2 deletions resources/lib/twitch/scraper.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ def download(baseurl, parameters={}, headers={}, data={}, method=methods.GET):
headers.update({USER_AGENT: USER_AGENT_STRING})
response = requests.request(method=method, url=url, headers=headers, data=data)
content = response.content
if not content and response.status_code == 204:
content = '{"status": 204}'
if not content:
content = '{"status": %d}' % response.status_code
break
except Exception as err:
if not isinstance(err, URLError):
Expand Down