Skip to content

Commit

Permalink
[PL - TVP] Add replay
Browse files Browse the repository at this point in the history
  • Loading branch information
thomas-ernest authored and joaopa00 committed Oct 7, 2023
1 parent e7337d4 commit 6dca663
Show file tree
Hide file tree
Showing 3 changed files with 137 additions and 11 deletions.
116 changes: 107 additions & 9 deletions resources/lib/channels/pl/tvp.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,10 @@
import json
import urlquick

from codequick import Resolver, Script
from resources.lib import web_utils
from codequick import Listitem, Resolver, Route, Script
from resources.lib import resolver_proxy, web_util


# TO DO
# Add Replay
from resources.lib.menu_utils import item_post_treatment

# Live
LIVE_MAIN_URL = 'http://tvpstream.vod.tvp.pl/'
Expand Down Expand Up @@ -68,10 +66,110 @@
'tvpalfa': 51656487
}

URL_REPLAY = {
'catalog': 'https://vod.tvp.pl/api/products/vods',
'serial': 'https://vod.tvp.pl/api/products/vods/serials/%s/seasons/%s/episodes',
# 'info': 'https://vod.tvp.pl/api/products/vods/{content_id}?ln={ln}&platform={pla}',
'content': 'https://vod.tvp.pl/api/products/%s/videos/playlist',
}

# ANDROID_TV, APPLE_TV, BROWSER, ANDROID, IOS
PLATFORM = 'SMART_TV'
LANG = 'pl'
PAGE_SIZE = 100

GENERIC_HEADERS = {'User-Agent': web_utils.get_random_ua()}

Check failure on line 81 in resources/lib/channels/pl/tvp.py

View workflow job for this annotation

GitHub Actions / Flake8

resources/lib/channels/pl/tvp.py#L81

Undefined name 'web_utils' (F821)

# TO DO
# Add more replays

def fetch_programs():

Check failure on line 86 in resources/lib/channels/pl/tvp.py

View workflow job for this annotation

GitHub Actions / Flake8

resources/lib/channels/pl/tvp.py#L86

Expected 2 blank lines, found 1 (E302)
fetch_more_content = True
next_itm = 0
tvp_items = []
while fetch_more_content:
fetch_more_content = False
params = {
'firstResult': next_itm,
'maxResults': PAGE_SIZE,
'mainCategoryId[]': 24,
'sort': 'createdAt',
'order': 'desc',
'ln': LANG,
'platform': PLATFORM
}
resp = urlquick.get(URL_REPLAY['catalog'], params=params, headers=GENERIC_HEADERS, max_age=-1)
if resp.status_code != 200:
break
resp_json = resp.json()
tvp_items.extend(resp_json['items'])
next_itm = resp_json['meta']['firstResult'] + resp_json['meta']['maxResults']
if next_itm <= resp_json['meta']['totalCount']:
fetch_more_content = True
return tvp_items


@Route.register
def list_programs(plugin, item_id, **kwargs):
tvp_items = fetch_programs()
if tvp_items is None or len(tvp_items) == 0:
plugin.notify(plugin.localize(30891), plugin.localize(30718), Script.NOTIFY_ERROR)
return

for tvp_item in tvp_items:
item = Listitem()
item.label = tvp_item['title']
serial_image_url = 'https:' + tvp_item['images']['16x9'][0]['url']
item.art['thumb'] = serial_image_url
# item.art['fanart'] = 'resources/channels/pl/tvpvod_fanart.jpg'

serial_id = int(tvp_item['id'])
item.set_callback(list_episodes, serial_id=serial_id, season_id=serial_id + 1, serial_image_url=serial_image_url)
item_post_treatment(item)
yield item


@Route.register
def list_episodes(plugin, serial_id, season_id, serial_image_url):
url = URL_REPLAY['serial'] % (serial_id, season_id)
params = {
'ln': LANG,
'platform': PLATFORM
}
resp = urlquick.get(url, params=params, headers=GENERIC_HEADERS, max_age=-1)
for tvp_episode in resp.json():
item = Listitem()
item.label = tvp_episode['title']
item.art['thumb'] = 'https:' + tvp_episode['images']['16x9'][0]['url']
item.art['fanart'] = serial_image_url

episode_id = int(tvp_episode['id'])
item.set_callback(play_episode, episode_id=episode_id)
item_post_treatment(item, is_playable=True)
yield item


@Resolver.register
def play_episode(plugin, episode_id):
content_url = URL_REPLAY['content'] % episode_id
params = {
'platform': PLATFORM,
'videoType': 'MOVIE'
}
content = None
content = urlquick.get(content_url, params=params, headers=GENERIC_HEADERS, max_age=-1, )
if content.status_code != 200:
plugin.notify(plugin.localize(30891), plugin.localize(30718), Script.NOTIFY_ERROR)
return False

content_json = content.json() if content is not None else {}
# HLS vs DASH
m3u8_url = content_json['sources']['HLS'][0]['src']

return resolver_proxy.get_stream_with_quality(plugin, m3u8_url)


def _get_channels():
def get_channels():
"""
Extract the listing of channels from TVP page as a list of JSON elments.
None if HTTP request fails or infomation moved to another place in the HTML page.
Expand All @@ -96,7 +194,7 @@ def _get_channels():
return None


def _get_channel_id(item_id, **kwargs):
def get_channel_id(item_id, **kwargs):
"""
Get the id of the channel in TVP page from id internal to catchuptvandmore plugin.
TVP3 is a channel shared for every regions.
Expand Down Expand Up @@ -154,12 +252,12 @@ def _get_live_stream_url(live_id):

@Resolver.register
def get_live_url(plugin, item_id, **kwargs):
channels = _get_channels()
channels = get_channels()
if channels is None:
plugin.notify('INFO', plugin.localize(30716))
return False

live_id = _get_live_id(channels, _get_channel_id(item_id, **kwargs))
live_id = _get_live_id(channels, get_channel_id(item_id, **kwargs))
if live_id is None:
# Stream is not available - channel not found on scrapped page
plugin.notify('INFO', plugin.localize(30716))
Expand Down
28 changes: 28 additions & 0 deletions resources/lib/skeletons/pl_replay.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# -*- coding: utf-8 -*-
# Copyright: (c) 2016, SylvainCecchetto
# GNU General Public License v2.0+ (see LICENSE.txt or https://www.gnu.org/licenses/gpl-2.0.txt)

# This file is part of Catch-up TV & More

from __future__ import unicode_literals

# The following dictionaries describe
# the addon's tree architecture.
# * Key: item id
# * Value: item infos
# - route (folder)/resolver (playable URL): Callback function to run once this item is selected
# - thumb: Item thumb path relative to "media" folder
# - fanart: Item fanart path relative to "media" folder

root = 'replay'

menu = {
'tvp-vod': {
'route': '/resources/lib/channels/pl/tvp:list_programs',
'label': 'TVP VOD',
'thumb': 'channels/pl/tvpvod.png',
'fanart': 'channels/pl/tvpvod_fanart.jpg',
'enabled': True,
'order': 1
},
}
4 changes: 2 additions & 2 deletions resources/lib/skeletons/replay.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@
'pl_replay': {
'route': '/resources/lib/main:generic_menu',
'label': 30058,
'thumb': 'channels/us.png',
'enabled': False,
'thumb': 'channels/pl.png',
'enabled': True,
'order': 6
},
'es_replay': {
Expand Down

0 comments on commit 6dca663

Please sign in to comment.