Skip to content

Commit

Permalink
Authentification par cookie / token
Browse files Browse the repository at this point in the history
  • Loading branch information
remzouille committed Nov 3, 2023
1 parent 631914a commit 0c954ad
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 14 deletions.
66 changes: 54 additions & 12 deletions resources/lib/provider_templates/orange.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,15 @@
from dataclasses import dataclass
from datetime import date, datetime, timedelta
import json
import os
import re
import xbmcvfs
from urllib.error import HTTPError
from urllib.parse import urlparse
from urllib.parse import urlparse, quote
from urllib.request import Request, urlopen

from lib.providers.provider_interface import ProviderInterface
from lib.utils import get_drm, get_global_setting, log, LogLevel, random_ua
from lib.utils import get_drm, get_global_setting, log, LogLevel, random_ua, get_addon_profile

@dataclass
class OrangeTemplate(ProviderInterface):
Expand All @@ -27,18 +30,55 @@ def __init__(
self.endpoint_programs = endpoint_programs
self.groups = groups

def _auth_urlopen(self, url: str, headers: dict = None) -> tuple:
if headers is None:
headers = {}
timestamp = datetime.timestamp(datetime.today())
filepath = os.path.join(xbmcvfs.translatePath(get_addon_profile()), 'auth')

try:
with open(filepath) as file:
auth = json.loads(file.read())
except FileNotFoundError:
auth = {'timestamp': timestamp}

for _ in range(2):
if 'cookie' in auth:
headers['cookie'] = auth['cookie']
headers['tv_token'] = auth['tv_token']
req = Request(url, headers=headers)

try:
with urlopen(req) as res:
if res.code == 200:
return res.read(), auth['cookie'], auth['tv_token']
except HTTPError as error:
if error.code in (401, 403):
log("cookie/token invalide, âge = %d" % (timestamp - auth['timestamp']), LogLevel.INFO)
else:
log("erreur %s" % error, LogLevel.INFO)
raise

req = Request("https://chaines-tv.orange.fr", headers={
'User-Agent': random_ua(),
'Host': 'chaines-tv.orange.fr',
})

with urlopen(req) as res:
cookie = res.headers['Set-Cookie'].split(";")[0]
tv_token = "Bearer %s" % re.sub('.*token:"', '', str(res.read()), 1)
tv_token = re.sub('",claims:.*', '', tv_token, 1)
auth = {'timestamp': timestamp, 'cookie': cookie, 'tv_token': tv_token}
with open(filepath, 'w') as file:
file.write(json.dumps(auth))

def get_stream_info(self, channel_id: int) -> dict:
req = Request(self.endpoint_stream_info.format(channel_id=channel_id), headers={
res, cookie, tv_token = self._auth_urlopen(self.endpoint_stream_info.format(channel_id=channel_id), headers={
'User-Agent': random_ua(),
'Host': urlparse(self.endpoint_stream_info).netloc
})

try:
with urlopen(req) as res:
stream_info = json.loads(res.read())
except HTTPError as error:
if error.code == 403:
return False
stream_info = json.loads(res)

drm = get_drm()
license_server_url = None
Expand All @@ -47,6 +87,7 @@ def get_stream_info(self, channel_id: int) -> dict:
license_server_url = system.get('laUrl')

headers = f'Content-Type=&User-Agent={random_ua()}&Host={urlparse(license_server_url).netloc}'
headers += '&Cookie=%s&tv_token=%s' % (quote(cookie), quote(tv_token))
post_data = 'R{SSM}'
response = ''

Expand All @@ -63,13 +104,14 @@ def get_stream_info(self, channel_id: int) -> dict:
return stream_info

def get_streams(self) -> list:
req = Request(self.endpoint_streams, headers={
return []

res, _, _ = self._auth_urlopen(self.endpoint_streams, headers={
'User-Agent': random_ua(),
'Host': urlparse(self.endpoint_streams).netloc
})

with urlopen(req) as res:
channels = json.loads(res.read())
channels = json.loads(res)

streams = []

Expand Down
4 changes: 2 additions & 2 deletions resources/lib/providers/fr/orange.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ class OrangeFranceProvider(OrangeTemplate):
# pylint: disable=line-too-long
def __init__(self) -> None:
super().__init__(
endpoint_stream_info = 'https://mediation-tv.orange.fr/all/live/v3/applications/PC/users/me/channels/{channel_id}/stream?terminalModel=WEB_PC',
endpoint_streams = 'https://mediation-tv.orange.fr/all/live/v3/applications/PC/channels?mco=OFR',
endpoint_stream_info = 'https://mediation-tv.orange.fr/all/api-gw/live/v3/auth/accountToken/applications/PC/channels/{channel_id}/stream?terminalModel=WEB_PC',
endpoint_streams = 'https://mediation-tv.orange.fr/all/api-gw/live/v3/auth/accountToken/applications/PC/channels?mco=OFR',
endpoint_programs = 'https://rp-ott-mediation-tv.woopic.com/api-gw/live/v3/applications/PC/programs?period={period}&mco=OFR',
groups = {
'TNT': \
Expand Down

0 comments on commit 0c954ad

Please sign in to comment.