Skip to content

Commit

Permalink
Added proxy server for fixing hls streams in inputstream adaptive (#193)
Browse files Browse the repository at this point in the history
Removed proxy and m3u8 fix due to update of inputstream adaptive
  • Loading branch information
quarckster authored Jul 19, 2021
1 parent a657fe2 commit ce1c422
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 52 deletions.
10 changes: 5 additions & 5 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
exclude: tests/expected_results.py|tests/data
repos:
- repo: https://github.com/asottile/reorder_python_imports
rev: v2.4.0
rev: v2.5.0
hooks:
- id: reorder-python-imports
language_version: python36
args:
- --application-directories=.:src:tests
- repo: https://github.com/psf/black
rev: 20.8b1
rev: 21.6b0
hooks:
- id: black
language_version: python36
args: [--safe, --quiet, --line-length, "100"]
require_serial: true
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v3.4.0
rev: v4.0.1
hooks:
- id: trailing-whitespace
language_version: python36
Expand All @@ -24,15 +24,15 @@ repos:
- id: debug-statements
language_version: python36
- repo: https://gitlab.com/pycqa/flake8
rev: "3.8.4"
rev: "3.9.2"
hooks:
- id: flake8
language_version: python36
args:
- --max-line-length=100
- --ignore=W503,E203
- repo: https://github.com/asottile/pyupgrade
rev: v2.10.0
rev: v2.20.0
hooks:
- id: pyupgrade
language_version: python36
30 changes: 10 additions & 20 deletions src/resources/lib/modeling.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
# -*- coding: utf-8 -*-
import re
import sys
import urllib
from collections import namedtuple

import xbmcgui

from resources.lib.utils import cached_property
from resources.lib.utils import fix_m3u8
from resources.lib.utils import notice


try:
import inputstreamhelper
except ImportError:
inputstreamhelper = None


Response = namedtuple("Response", ["items", "pagination"])


Expand All @@ -36,7 +36,7 @@ def __init__(self, plugin):

def get(self, endpoint, data=None, exclude_anime=False):
if exclude_anime:
resp = self._get_anime_exluded(endpoint, data=data)
resp = self._get_anime_excluded(endpoint, data=data)
else:
resp = self.plugin.client(endpoint).get(data=data)
items = [self.instantiate(item=item, index=i) for i, item in enumerate(resp["items"], 1)]
Expand Down Expand Up @@ -85,7 +85,7 @@ def get_playable(self, item, season_index=None, index=None):
else:
return item

def _get_anime_exluded(self, endpoint, data=None, collection=None):
def _get_anime_excluded(self, endpoint, data=None, collection=None):
# init items collection
collection = collection or {"items": []}

Expand All @@ -101,7 +101,7 @@ def _get_anime_exluded(self, endpoint, data=None, collection=None):

# filter items list from anime items
non_anime_items = list(
[x for x in new_items[start_from:] if all(i["id"] != 25 for i in x["genres"])]
x for x in new_items[start_from:] if all(i["id"] != 25 for i in x["genres"])
)

# if not enough items continue with next API page
Expand All @@ -110,9 +110,9 @@ def _get_anime_exluded(self, endpoint, data=None, collection=None):

if int(pagination["current"]) + 1 < int(pagination["total"]):
data.update({"page": pagination["current"] + 1, "start_from": 0})
collection = self._get_anime_exluded(endpoint, data, collection)
collection = self._get_anime_excluded(endpoint, data, collection)
else:
# exlude extra items from filtered items
# exclude extra items from filtered items
count_items_to_extend = page_size - len(collection["items"])
items = non_anime_items[:count_items_to_extend]
last_item_id = items[-1]["id"]
Expand Down Expand Up @@ -218,7 +218,8 @@ def __repr__(self):
class PlayableItem(ItemEntity):
isdir = False

def get_media_url(self):
@property
def media_url(self):
quality = self.plugin.settings.video_quality
stream_type = self.plugin.settings.stream_type
ask_quality = self.plugin.settings.ask_quality
Expand Down Expand Up @@ -253,13 +254,6 @@ def alphanum_key(key):
# if there is no such quality then return a link with the highest available quality
return files[natural_sort(list(files.keys()))[-1]][stream_type]

@property
def media_url(self):
url = self.get_media_url()
if urllib.parse.urlsplit(url).path.endswith("m3u8"):
return fix_m3u8(url, self.plugin.logger)
return url

@property
def list_item(self):
li = super(PlayableItem, self).list_item
Expand All @@ -275,11 +269,7 @@ def resume_time(self):

@property
def hls_properties(self):
if (
"hls" in self.plugin.settings.stream_type
and self.plugin.settings.inputstream_adaptive_enabled == "true"
and inputstreamhelper
):
if self.plugin.is_hls_enabled:
helper = inputstreamhelper.Helper("hls")
if not helper.check_inputstream():
notice("HLS поток не поддерживается")
Expand Down
10 changes: 0 additions & 10 deletions src/resources/lib/player.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
# -*- coding: utf-8 -*-
import json
import pathlib
import time

import xbmc
import xbmcgui
import xbmcvfs


class Player(xbmc.Player):
Expand Down Expand Up @@ -57,11 +55,6 @@ def _base_data(self):
data = {"id": item_id, "video": video_number}
return data

def delete_temp_playlist(self):
if pathlib.Path(self.list_item.getPath()).exists():
xbmcvfs.delete(self.list_item.getPath())
self.plugin.logger.info(f"deleted {self.list_item.getPath()}")

def onPlayBackStarted(self):
self.plugin.logger.info("playback started")
self.plugin.clear_window_property()
Expand All @@ -82,7 +75,6 @@ def onPlayBackStopped(self):
self.is_playing = False
data = self._base_data
self.plugin.logger.info("playback stopped")
self.delete_temp_playlist()
if self.should_make_resume_point:
data["time"] = self.marktime
self.plugin.logger.info("sending resume point")
Expand All @@ -101,7 +93,6 @@ def onPlayBackStopped(self):
def onPlayBackEnded(self):
self.is_playing = False
self.plugin.logger.info("playback ended")
self.delete_temp_playlist()
if int(self.list_item.getProperty("playcount")) < 1:
data = self._base_data
data["status"] = 1
Expand All @@ -111,4 +102,3 @@ def onPlayBackEnded(self):
def onPlaybackError(self):
self.plugin.logger.error("playback error")
self.is_playing = False
self.delete_temp_playlist()
14 changes: 14 additions & 0 deletions src/resources/lib/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@
from resources.lib.settings import Settings


try:
import inputstreamhelper
except ImportError:
inputstreamhelper = None


MainMenuItem = namedtuple("MainMenuItem", ["title", "url", "icon", "is_dir", "is_displayed"])


Expand Down Expand Up @@ -244,3 +250,11 @@ def get_window_property(self, item_id):
if item:
item._plugin = self
return item

@property
def is_hls_enabled(self):
return (
"hls" in self.settings.stream_type
and self.settings.inputstream_adaptive_enabled == "true"
and inputstreamhelper
)
17 changes: 0 additions & 17 deletions src/resources/lib/utils.py
Original file line number Diff line number Diff line change
@@ -1,27 +1,10 @@
import uuid

import m3u8
import xbmcgui
import xbmcvfs


def notice(message, heading="", time=4000):
xbmcgui.Dialog().notification(heading, message, time=time)


def fix_m3u8(uri, logger):
master_playlist = m3u8.load(uri)
for playlist in master_playlist.playlists:
playlist.uri = playlist.absolute_uri
for media in master_playlist.media:
media.uri = media.absolute_uri
path = xbmcvfs.translatePath(f"special://temp/{uuid.uuid4()}.m3u8")
with xbmcvfs.File(path, "w") as f:
result = f.write(master_playlist.dumps())
logger.info(f"temporary hls playlist {path} {'was' if result else 'was not'} created")
return path


class cached_property(object): # noqa
"""A property that is only computed once per instance and then replaces itself with an ordinary
attribute. Deleting the attribute resets the property.
Expand Down

0 comments on commit ce1c422

Please sign in to comment.