2013-03-02 21:26:28 +01:00
# ex:ts=4:sw=4:sts=4:et
# -*- tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*-
2014-06-07 20:43:40 +02:00
import copy
2019-08-25 00:40:39 +02:00
import json
2018-11-18 12:47:19 +01:00
import logging
2019-08-25 00:40:39 +02:00
import re
2020-10-10 13:30:44 +02:00
from html import unescape
2021-10-30 12:05:08 +02:00
from urllib . parse import urljoin
2013-02-12 19:43:37 +01:00
2015-09-06 14:19:10 +02:00
from svtplay_dl . error import ServiceError
2019-08-25 00:40:39 +02:00
from svtplay_dl . fetcher . hls import hlsparse
from svtplay_dl . service import OpenGraphThumbMixin
from svtplay_dl . service import Service
2014-08-31 01:20:36 +02:00
from svtplay_dl . subtitle import subtitle
2021-11-13 14:31:47 +01:00
from svtplay_dl . utils . http import download_thumbnails
2013-02-12 19:43:37 +01:00
2015-09-15 20:10:32 +02:00
2014-01-19 14:26:48 +01:00
class Urplay ( Service , OpenGraphThumbMixin ) :
2019-08-25 00:27:31 +02:00
supported_domains = [ " urplay.se " , " ur.se " , " betaplay.ur.se " , " urskola.se " ]
2013-01-17 00:21:47 +01:00
2015-12-26 11:46:14 +01:00
def get ( self ) :
2021-02-28 23:14:34 +01:00
urldata = self . get_urldata ( )
2021-10-30 12:05:08 +02:00
match = re . search ( r " __NEXT_DATA__ \" type= \" application \ /json \" >( { .+})< \ /script> " , urldata )
2014-01-03 12:15:21 +01:00
if not match :
2021-10-30 12:05:08 +02:00
yield ServiceError ( " Can ' t find video info. " )
return
2014-12-22 17:41:40 +01:00
2020-10-10 13:30:44 +02:00
data = unescape ( match . group ( 1 ) )
2013-03-03 10:58:37 +01:00
jsondata = json . loads ( data )
2016-11-30 23:21:42 +01:00
2020-10-10 13:30:44 +02:00
res = self . http . get ( " https://streaming-loadbalancer.ur.se/loadbalancer.json " )
loadbalancer = res . json ( ) [ " redirect " ]
2021-10-30 12:05:08 +02:00
jsondata = jsondata [ " props " ] [ " pageProps " ] [ " program " ]
2020-10-10 13:30:44 +02:00
2021-10-30 12:05:08 +02:00
self . outputfilename ( jsondata , urldata )
2021-02-28 23:14:34 +01:00
2021-10-30 12:05:08 +02:00
for streaminfo in jsondata [ " streamingInfo " ] . keys ( ) :
stream = jsondata [ " streamingInfo " ] [ streaminfo ]
2021-02-28 23:14:34 +01:00
2020-10-21 09:06:05 +02:00
if streaminfo == " raw " :
2020-12-29 14:08:31 +01:00
if " sd " in stream :
url = " https:// {} / {} playlist.m3u8 " . format ( loadbalancer , stream [ " sd " ] [ " location " ] )
2021-05-16 02:22:37 +02:00
yield from hlsparse ( self . config , self . http . request ( " get " , url ) , url , output = self . output )
2020-12-29 14:08:31 +01:00
if " hd " in stream :
url = " https:// {} / {} playlist.m3u8 " . format ( loadbalancer , stream [ " hd " ] [ " location " ] )
2021-05-16 02:22:37 +02:00
yield from hlsparse ( self . config , self . http . request ( " get " , url ) , url , output = self . output )
2021-05-12 21:00:00 +02:00
if not ( self . config . get ( " get_all_subtitles " ) ) and streaminfo == " sweComplete " :
2021-02-16 23:57:16 +01:00
yield subtitle ( copy . copy ( self . config ) , " wrst " , stream [ " tt " ] [ " location " ] . replace ( " .tt " , " .vtt " ) , output = self . output )
2020-10-10 13:30:44 +02:00
if self . config . get ( " get_all_subtitles " ) and " tt " in stream :
label = stream [ " tt " ] [ " language " ]
if stream [ " tt " ] [ " scope " ] != " complete " :
label = " {} - {} " . format ( label , stream [ " tt " ] [ " scope " ] )
2021-02-16 23:57:16 +01:00
yield subtitle ( copy . copy ( self . config ) , " wrst " , stream [ " tt " ] [ " location " ] . replace ( " .tt " , " .vtt " ) , label , output = copy . copy ( self . output ) )
2014-01-11 23:02:47 +01:00
2018-05-13 13:06:45 +02:00
def find_all_episodes ( self , config ) :
2016-12-06 22:42:54 +01:00
episodes = [ ]
2017-09-16 17:36:37 +02:00
2021-10-30 12:05:08 +02:00
match = re . search ( r " __NEXT_DATA__ \" type= \" application \ /json \" >( { .+})< \ /script> " , self . get_urldata ( ) )
2020-10-10 13:30:44 +02:00
if not match :
2021-10-30 12:05:08 +02:00
logging . error ( " Can ' t find video info. " )
return
2020-10-10 13:30:44 +02:00
data = unescape ( match . group ( 1 ) )
jsondata = json . loads ( data )
2021-10-30 12:05:08 +02:00
seasons = jsondata [ " props " ] [ " pageProps " ] [ " superSeriesSeasons " ]
if seasons :
for season in seasons :
res = self . http . get ( f ' https://urplay.se/api/v1/series?id= { season [ " id " ] } ' )
for episode in res . json ( ) [ " programs " ] :
episodes . append ( urljoin ( " https://urplay.se " , episode [ " link " ] ) )
else :
for episode in jsondata [ " props " ] [ " pageProps " ] [ " accessibleEpisodes " ] :
episodes . append ( urljoin ( " https://urplay.se " , episode [ " link " ] ) )
2014-12-21 13:45:44 +01:00
episodes_new = [ ]
n = 0
for i in episodes :
2018-05-13 13:06:45 +02:00
if n == config . get ( " all_last " ) :
2014-12-21 13:45:44 +01:00
break
2015-09-06 22:41:49 +02:00
if i not in episodes_new :
episodes_new . append ( i )
2014-12-21 13:45:44 +01:00
n + = 1
2015-03-07 10:43:21 +01:00
return episodes_new
2021-02-28 23:14:34 +01:00
def outputfilename ( self , data , urldata ) :
if " seriesTitle " in data :
self . output [ " title " ] = data [ " seriesTitle " ]
self . output [ " title_nice " ] = data [ " seriesTitle " ]
if " episodeNumber " in data and data [ " episodeNumber " ] :
self . output [ " episode " ] = str ( data [ " episodeNumber " ] )
if " title " in data :
2021-04-15 19:50:56 +02:00
if self . output [ " title " ] is None :
self . output [ " title " ] = data [ " title " ]
else :
self . output [ " episodename " ] = data [ " title " ]
2021-02-28 23:14:34 +01:00
if " id " in data and data [ " id " ] :
self . output [ " id " ] = str ( data [ " id " ] )
2021-11-13 14:31:47 +01:00
self . output [ " episodethumbnailurl " ] = data [ " image " ] [ " 1280x720 " ]
2021-02-28 23:14:34 +01:00
# Try to match Season info from HTML (not available in json, it seems), e.g.: <button class="SeasonsDropdown-module__seasonButton___25Uyt" type="button"><span>Säsong 6</span>
2021-10-30 12:05:08 +02:00
seasonmatch = re . search ( r " data-testid= \" season-name-label \" >S.song ( \ d+)...< \ /span " , urldata )
2021-02-28 23:14:34 +01:00
if seasonmatch :
self . output [ " season " ] = seasonmatch . group ( 1 )
else :
self . output [ " season " ] = " 1 " # No season info - probably show without seasons
2021-11-13 14:31:47 +01:00
def get_thumbnail ( self , options ) :
download_thumbnails ( self . output , options , [ ( False , self . output [ " episodethumbnailurl " ] ) ] )