Skip to content

Commit 6482bc1

Browse files
authored
Merge pull request #707 from einanderson/release/3.0.3
Release 3.0.3: device-code login, Turbo/ad-free login, GQL search, HEVC setting, search & tofu fixes
2 parents 6ece95b + a41bc09 commit 6482bc1

30 files changed

Lines changed: 975 additions & 243 deletions

‎addon.xml‎

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
2-
<addon id="plugin.video.twitch" version="3.0.2" name="Twitch" provider-name="anxdpanic, A Talented Community">
2+
<addon id="plugin.video.twitch" version="3.0.3" name="Twitch" provider-name="anxdpanic, A Talented Community">
33
<requires>
44
<import addon="xbmc.python" version="3.0.1"/>
55
<import addon="script.module.requests" version="2.9.1"/>
@@ -15,8 +15,14 @@
1515
<fanart>resources/media/fanart.jpg</fanart>
1616
</assets>
1717
<news>
18-
[fix] Following Channels
19-
[lang] updated translations from Weblate
18+
[add] OAuth device-code login with automatic background token refresh
19+
[add] second device-code login for ad-free playback (Turbo/subscriber)
20+
[add] website (GQL) search backend with fuzzy/relevance ranking (+ Helix fallback)
21+
[add] optional "Supported codecs" setting (H.265/HEVC for Enhanced Broadcasting 1440p)
22+
[fix] search hanging at "Working..." after stopping playback started from search results
23+
[fix] hide emoji/symbols the skin font cannot render (tofu boxes) in titles and plots
24+
[upd] InputStream Adaptive: headers via properties, allow up to 1440p, drop sub-720p variants
25+
[rem] IRC chat integration and the obsolete browser-based token flow
2026
</news>
2127
<platform>all</platform>
2228
<source>https://github.com/anxdpanic/plugin.video.twitch</source>

‎changelog.txt‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,24 @@
1+
3.0.3
2+
[add] OAuth device-code login with automatic background token refresh (Settings -> "Login (device code)")
3+
- tokens kept in addon_data/oauth_tokens.json with atomic writes and a cross-process lock,
4+
so the rotating single-use refresh token survives Kodi's multi-process settings race
5+
- confidential client ids (no silent refresh possible) are detected once and fall back
6+
to manual login with a one-time hint
7+
[add] second device-code login for ad-free playback (Turbo/subscriber) with its own
8+
auto-refreshed token store; manual private token keeps working as fallback
9+
[add] website (GQL) search backend with fuzzy/relevance ranking, selectable via
10+
"Search method" with automatic Helix fallback
11+
[add] optional "Supported codecs" setting: request H.265 (HEVC) so Enhanced Broadcasting
12+
1440p variants are offered (requires script.module.python.twitch >= 3.0.3; silently
13+
ignored with older library versions)
14+
[fix] search hanging at "Working..." / keyboard re-prompt after stopping playback that was
15+
started from search results
16+
[fix] hide emoji/symbols the skin font cannot render (tofu boxes) in titles, plots and info
17+
[upd] InputStream Adaptive: pass headers via stream/manifest_headers properties instead of
18+
the URL pipe; allow up to 1440p (chooser_resolution_max); drop sub-720p variants from
19+
quality selection; default to Adaptive quality with InputStream Adaptive enabled
20+
[rem] IRC chat integration (script.ircchat) and the obsolete browser-based token flow
21+
122
2.6.0
223
[update] Update Twitch API usage from v5 to Helix
324
- This change will require you to generate a new oauth token from the add-on settings

‎resources/language/resource.language.en_gb/strings.po‎

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1005,3 +1005,71 @@ msgstr ""
10051005
msgctxt "#30273"
10061006
msgid "OAuth Token is expired or invalid"
10071007
msgstr ""
1008+
1009+
msgctxt "#30300"
1010+
msgid "Login (device code)"
1011+
msgstr ""
1012+
1013+
msgctxt "#30301"
1014+
msgid "Go to [B]%s[/B] and enter this code:[CR][CR][B]%s[/B]"
1015+
msgstr ""
1016+
1017+
msgctxt "#30302"
1018+
msgid "Waiting for authorization..."
1019+
msgstr ""
1020+
1021+
msgctxt "#30303"
1022+
msgid "Login successful. The access token will now be refreshed automatically."
1023+
msgstr ""
1024+
1025+
msgctxt "#30304"
1026+
msgid "Login failed or timed out."
1027+
msgstr ""
1028+
1029+
msgctxt "#30305"
1030+
msgid "Login cancelled."
1031+
msgstr ""
1032+
1033+
msgctxt "#30306"
1034+
msgid "Device login is not available for this Client-ID.[CR]Register your own application (Public type) at dev.twitch.tv/console/apps and set its Client-ID in the add-on settings, then try again."
1035+
msgstr ""
1036+
1037+
msgctxt "#30307"
1038+
msgid "Supported codecs"
1039+
msgstr ""
1040+
1041+
msgctxt "#30308"
1042+
msgid "Twitch default"
1043+
msgstr ""
1044+
1045+
msgctxt "#30309"
1046+
msgid "H.265 (HEVC)"
1047+
msgstr ""
1048+
1049+
msgctxt "#30310"
1050+
msgid "Automatic token refresh needs a public Client-ID.[CR]Register your own application (Public type) at dev.twitch.tv/console/apps and set its Client-ID in the add-on settings."
1051+
msgstr ""
1052+
1053+
msgctxt "#30311"
1054+
msgid "Login: ad-free playback / Turbo (device code)"
1055+
msgstr ""
1056+
1057+
msgctxt "#30312"
1058+
msgid "Go to [B]%s[/B] with your [B]Turbo/subscriber account[/B] and enter this code:[CR][CR][B]%s[/B]"
1059+
msgstr ""
1060+
1061+
msgctxt "#30313"
1062+
msgid "Turbo / ad-free login successful. The token is renewed automatically. Ad-free playback only applies if the logged-in account has Turbo or is subscribed to the channel."
1063+
msgstr ""
1064+
1065+
msgctxt "#30330"
1066+
msgid "Search method"
1067+
msgstr ""
1068+
1069+
msgctxt "#30331"
1070+
msgid "Website (twitch.tv, fuzzy)"
1071+
msgstr ""
1072+
1073+
msgctxt "#30332"
1074+
msgid "Helix API"
1075+
msgstr ""

‎resources/lib/twitch_addon/addon/api.py‎

Lines changed: 39 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
import json
1313
import sys
1414

15-
from . import cache, utils
15+
from . import cache, utils, gql_search
1616
from .common import kodi, log_utils
1717
from .constants import Keys, SCOPES
1818
from .error_handling import api_error_handler
@@ -24,6 +24,13 @@
2424
from twitch.api import helix as twitch
2525
from twitch.api.parameters import Language, Boolean, VideoSort, PeriodHelix
2626

27+
try:
28+
from inspect import signature as _signature
29+
# Older library versions don't accept the supported_codecs keyword; degrade gracefully.
30+
_USHER_SUPPORTS_CODECS = 'supported_codecs' in _signature(usher.live_request).parameters
31+
except Exception:
32+
_USHER_SUPPORTS_CODECS = False
33+
2734
i18n = utils.i18n
2835

2936

@@ -38,6 +45,9 @@ class Twitch:
3845
required_scopes = SCOPES
3946

4047
def __init__(self):
48+
# Silently refresh the Helix OAuth token if it is (near) expired (Device Code Flow).
49+
utils.ensure_valid_token()
50+
self.access_token = utils.get_oauth_token(token_only=True, required=False)
4151
self.queries.CLIENT_ID = self.client_id
4252
self.queries.CLIENT_SECRET = self.client_secret
4353
self.queries.OAUTH_TOKEN = self.access_token
@@ -72,7 +82,7 @@ def valid_token(self, client_id, token, scopes): # client_id, token used for un
7282
'[CR]'.join([
7383
i18n('missing_scopes') % missing_scopes,
7484
i18n('get_new_oauth_token') %
75-
(i18n('settings'), i18n('login'), i18n('get_oauth_token'))
85+
(i18n('settings'), i18n('login'), i18n('device_login'))
7686
])
7787
)
7888
log_utils.log('Error: Current OAuth token is missing required scopes |%s|' % missing_scopes,
@@ -100,7 +110,7 @@ def valid_token(self, client_id, token, scopes): # client_id, token used for un
100110
'[CR]'.join([
101111
i18n('client_id_mismatch'),
102112
i18n('get_new_oauth_token') %
103-
(i18n('settings'), i18n('login'), i18n('get_oauth_token'))
113+
(i18n('settings'), i18n('login'), i18n('device_login'))
104114
])
105115
)
106116
return False
@@ -230,20 +240,32 @@ def get_game_streams(self, game_id=None, language=Language.ALL, after='MA==', be
230240
@api_error_handler
231241
@cache.cache_method(cache_limit=cache.limit)
232242
def get_channel_search(self, search_query, after='MA==', first=20):
243+
if utils.use_gql_search():
244+
gql = gql_search.search(search_query, 'channels')
245+
if gql is not None:
246+
return gql # GQL ok -> use it; else fall back to Helix below
233247
results = self.api.search.get_channels(search_query=search_query, after=after, first=first,
234248
live_only=Boolean.FALSE)
235249
return self.error_check(results)
236250

237251
@api_error_handler
238252
@cache.cache_method(cache_limit=cache.limit)
239253
def get_stream_search(self, search_query, after='MA==', first=20):
254+
if utils.use_gql_search():
255+
gql = gql_search.search(search_query, 'streams')
256+
if gql is not None:
257+
return gql
240258
results = self.api.search.get_channels(search_query=search_query, after=after, first=first,
241259
live_only=Boolean.TRUE)
242260
return self.error_check(results)
243261

244262
@api_error_handler
245263
@cache.cache_method(cache_limit=cache.limit)
246264
def get_game_search(self, search_query, after='MA==', first=20):
265+
if utils.use_gql_search():
266+
gql = gql_search.search(search_query, 'games')
267+
if gql is not None:
268+
return gql
247269
results = self.api.search.get_categories(search_query=search_query, after=after, first=first)
248270
return self.error_check(results)
249271

@@ -329,10 +351,17 @@ def get_followed_streams(self, user_id, after='MA==', first=20):
329351
results = self.error_check(results)
330352
return results
331353

354+
@staticmethod
355+
def _codec_kwargs():
356+
codecs = utils.get_supported_codecs()
357+
if codecs and _USHER_SUPPORTS_CODECS:
358+
return {'supported_codecs': codecs}
359+
return {}
360+
332361
@api_error_handler
333362
@cache.cache_method(cache_limit=cache.limit)
334363
def get_vod(self, video_id):
335-
results = self.usher.video(video_id, headers=self.get_private_credential_headers())
364+
results = self.usher.video(video_id, headers=self.get_private_credential_headers(), **self._codec_kwargs())
336365
return self.error_check(results, private=True)
337366

338367
@api_error_handler
@@ -343,25 +372,25 @@ def get_clip(self, slug):
343372
@api_error_handler
344373
@cache.cache_method(cache_limit=cache.limit)
345374
def get_live(self, name):
346-
results = self.usher.live(name, headers=self.get_private_credential_headers())
375+
results = self.usher.live(name, headers=self.get_private_credential_headers(), **self._codec_kwargs())
347376
return self.error_check(results, private=True)
348377

349378
@api_error_handler
350379
@cache.cache_method(cache_limit=cache.limit)
351380
def live_request(self, name):
352381
if not utils.inputstream_adpative_supports('EXT-X-DISCONTINUITY'):
353-
results = self.usher.live_request(name, platform='ps4', headers=self.get_private_credential_headers())
382+
results = self.usher.live_request(name, platform='ps4', headers=self.get_private_credential_headers(), **self._codec_kwargs())
354383
else:
355-
results = self.usher.live_request(name, headers=self.get_private_credential_headers())
384+
results = self.usher.live_request(name, headers=self.get_private_credential_headers(), **self._codec_kwargs())
356385
return self.error_check(results, private=True)
357386

358387
@api_error_handler
359388
@cache.cache_method(cache_limit=cache.limit)
360389
def video_request(self, video_id):
361390
if not utils.inputstream_adpative_supports('EXT-X-DISCONTINUITY'):
362-
results = self.usher.video_request(video_id, platform='ps4', headers=self.get_private_credential_headers())
391+
results = self.usher.video_request(video_id, platform='ps4', headers=self.get_private_credential_headers(), **self._codec_kwargs())
363392
else:
364-
results = self.usher.video_request(video_id, headers=self.get_private_credential_headers())
393+
results = self.usher.video_request(video_id, headers=self.get_private_credential_headers(), **self._codec_kwargs())
365394
return self.error_check(results, private=True)
366395

367396
@staticmethod
@@ -377,7 +406,7 @@ def error_check(results, private=False):
377406
if not private:
378407
_ = kodi.Dialog().ok(
379408
i18n('oauth_heading'),
380-
i18n('oauth_message') % (i18n('settings'), i18n('login'), i18n('get_oauth_token'))
409+
i18n('oauth_message') % (i18n('settings'), i18n('login'), i18n('device_login'))
381410
)
382411
else:
383412
_ = kodi.Dialog().ok(

‎resources/lib/twitch_addon/addon/common/kodi.py‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -171,11 +171,11 @@ class KodiVersion(object):
171171
if ('result' in _json_query) and ('name' in _json_query['result']):
172172
application = decode_utf8(_json_query['result']['name'])
173173
version = decode_utf8(xbmc.getInfoLabel('System.BuildVersion'))
174-
match = re.search('([0-9]+)\.([0-9]+)', version)
174+
match = re.search(r'([0-9]+)\.([0-9]+)', version)
175175
if match: major, minor = match.groups()
176-
match = re.search('-([a-zA-Z]+)([0-9]*)', version)
176+
match = re.search(r'-([a-zA-Z]+)([0-9]*)', version)
177177
if match: tag, tag_version = match.groups()
178-
match = re.search('\w+:(\w+-\w+)', version)
178+
match = re.search(r'\w+:(\w+-\w+)', version)
179179
if match: revision = match.group(1)
180180

181181
try:

‎resources/lib/twitch_addon/addon/constants.py‎

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,16 +35,14 @@ def __enum(**enums):
3535
CHANNELVIDEOLIST='channel_video_list',
3636
GAMESTREAMS='game_streams',
3737
RESETCACHE='reset_cache',
38-
INSTALLIRCCHAT='install_ircchat',
38+
DEVICELOGINPRIVATE='device_login_private',
39+
REVOKEPRIVATETOKEN='revoke_private_token',
3940
PLAY='play',
40-
TOKENURL='get_token_url',
41+
DEVICELOGIN='device_login',
4142
EDITFOLLOW='edit_user_follows',
4243
EDITBLOCK='edit_user_blocks',
43-
EDITBLACKLIST='edit_blacklist',
4444
EDITQUALITIES='edit_qualities',
4545
CLEARLIST='clear_list',
46-
COLLECTIONS='collections',
47-
COLLECTIONVIDEOLIST='collection_video_list',
4846
BROWSE='browse',
4947
STREAMLIST='stream_list',
5048
CLIPSLIST='clips_list',

‎resources/lib/twitch_addon/addon/converter.py‎

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
from . import menu_items
1515
from .common import kodi
1616
from .constants import Keys, Images, MODES, ADAPTIVE_SOURCE_TEMPLATE
17-
from .utils import the_art, TitleBuilder, i18n, get_oauth_token, get_vodcast_color, use_inputstream_adaptive, get_thumbnail_size, get_refresh_stamp, to_string, get_private_oauth_token, convert_duration
17+
from .utils import the_art, TitleBuilder, i18n, get_oauth_token, get_vodcast_color, use_inputstream_adaptive, get_thumbnail_size, get_refresh_stamp, to_string, get_private_oauth_token, convert_duration, filter_qualities, strip_tofu
1818

1919

2020
class PlaylistConverter(object):
@@ -403,6 +403,12 @@ def _format_key(key, headings, info):
403403
value = item_template.format(head=val_heading, info=val_info)
404404
return value
405405

406+
@staticmethod
407+
def _clean_info(info):
408+
# Strip emoji/symbols Kodi's skin font cannot render from the free-text fields
409+
# (title/description) -- affects the text below the thumb (tagline) + info dialog (plot).
410+
return {key: strip_tofu(value) for key, value in info.items()}
411+
406412
def get_plot_for_search(self, search, include_title=True):
407413
headings = {Keys.GAME: i18n('game'),
408414
Keys.BROADCASTER_LANGUAGE: i18n('language')}
@@ -421,7 +427,7 @@ def get_plot_for_search(self, search, include_title=True):
421427
plot = plot_template.format(title=title, game=self._format_key(Keys.GAME, headings, info),
422428
broadcaster_language=self._format_key(Keys.BROADCASTER_LANGUAGE, headings, info))
423429

424-
return {u'plot': plot, u'plotoutline': plot, u'tagline': _title.rstrip('\r\n')}
430+
return self._clean_info({u'plot': plot, u'plotoutline': plot, u'tagline': _title.rstrip('\r\n')})
425431

426432
def get_plot_for_stream(self, stream, include_title=True):
427433
headings = {Keys.GAME: i18n('game'),
@@ -446,7 +452,7 @@ def get_plot_for_stream(self, stream, include_title=True):
446452
broadcaster_language=self._format_key(Keys.BROADCASTER_LANGUAGE, headings, info),
447453
mature=self._format_key(Keys.MATURE, headings, info))
448454

449-
return {u'plot': plot, u'plotoutline': plot, u'tagline': _title.rstrip('\r\n')}
455+
return self._clean_info({u'plot': plot, u'plotoutline': plot, u'tagline': _title.rstrip('\r\n')})
450456

451457
def get_plot_for_channel(self, channel):
452458
headings = {Keys.VIEWS: i18n('views'),
@@ -468,7 +474,7 @@ def get_plot_for_channel(self, channel):
468474
broadcaster_type=broadcaster_type + '\r\n',
469475
date=date)
470476

471-
return {u'plot': plot, u'plotoutline': plot, u'tagline': title.rstrip('\r\n')}
477+
return self._clean_info({u'plot': plot, u'plotoutline': plot, u'tagline': title.rstrip('\r\n')})
472478

473479
def get_plot_for_clip(self, clip, include_title=True):
474480
headings = {Keys.VIEWS: i18n('views'),
@@ -495,7 +501,7 @@ def get_plot_for_clip(self, clip, include_title=True):
495501
curator=self._format_key(Keys.CURATOR, headings, info),
496502
date=date)
497503

498-
return {u'plot': plot, u'plotoutline': plot, u'tagline': _title.rstrip('\r\n')}
504+
return self._clean_info({u'plot': plot, u'plotoutline': plot, u'tagline': _title.rstrip('\r\n')})
499505

500506
def get_plot_for_video(self, video, include_title=True):
501507
headings = {Keys.VIEWS: i18n('views'),
@@ -519,12 +525,13 @@ def get_plot_for_video(self, video, include_title=True):
519525
if video.get(Keys.DESCRIPTION) else title,
520526
date=date)
521527

522-
return {u'plot': plot, u'plotoutline': plot, u'tagline': _title.rstrip('\r\n')}
528+
return self._clean_info({u'plot': plot, u'plotoutline': plot, u'tagline': _title.rstrip('\r\n')})
523529

524530
def get_video_for_quality(self, videos, ask=True, quality=None, clip=False):
525531
use_ia = use_inputstream_adaptive()
526532
if use_ia and not any(v['name'] == 'Adaptive' for v in videos) and not clip:
527533
videos.append(ADAPTIVE_SOURCE_TEMPLATE)
534+
videos = filter_qualities(videos) # drop sub-720p variants (keep Source/720p+/audio_only/Adaptive)
528535
if ask is True:
529536
return self.select_video_for_quality(videos)
530537
else:
@@ -574,12 +581,15 @@ def get_video_for_quality(self, videos, ask=True, quality=None, clip=False):
574581
bwidth = int(video['bandwidth'])
575582
if bwidth <= bandwidth_value:
576583
bandwidths.append(bwidth)
577-
best_match = max(bandwidths)
578-
try:
579-
index = next(idx for idx, video in enumerate(videos) if int(video['bandwidth']) == best_match)
580-
return videos[index]
581-
except:
582-
pass
584+
# may be empty: with sub-720p variants filtered out, every remaining
585+
# variant can exceed the configured limit -> fall through to the dialog
586+
if bandwidths:
587+
best_match = max(bandwidths)
588+
try:
589+
index = next(idx for idx, video in enumerate(videos) if int(video['bandwidth']) == best_match)
590+
return videos[index]
591+
except:
592+
pass
583593
return self.select_video_for_quality(videos)
584594

585595
@staticmethod

0 commit comments

Comments
 (0)