diff --git a/addon.xml b/addon.xml index 3b99780..71f1395 100644 --- a/addon.xml +++ b/addon.xml @@ -1,7 +1,7 @@ diff --git a/resources/lib/movistar.py b/resources/lib/movistar.py index 5e8297d..8af403d 100644 --- a/resources/lib/movistar.py +++ b/resources/lib/movistar.py @@ -11,6 +11,9 @@ import io import os import time import re +import shutil +import glob + from datetime import datetime, timedelta from .endpoints import endpoints @@ -36,6 +39,7 @@ class Movistar(object): add_extra_info = True #dplayer = 'webplayer' dplayer = 'android.tv' + account_dir = 'account_1' def __init__(self, config_directory, reuse_devices=False): self.logged = False @@ -54,6 +58,27 @@ class Movistar(object): self.quality = 'HD' # or UHD + # Account dir + content = Movistar.load_file_if_exists(config_directory + 'account.txt') + if content: self.account_dir = content + LOG('account_dir: {}'.format(self.account_dir)) + + account_dir = os.path.join(config_directory, self.account_dir) + if not os.path.exists(account_dir): + os.makedirs(account_dir) + # Migrate data + if self.account_dir == 'account_1': + for ext in ['*.conf', '*.json', '*.key']: + for f in glob.glob(os.path.join(config_directory, ext)): + #LOG(f) + shutil.move(f, account_dir) + if os.path.exists(config_directory + 'cache'): + #LOG(config_directory + 'cache') + shutil.move(config_directory + 'cache', account_dir) + + self.config_dir = config_directory + config_directory = account_dir + '/' + # Cache self.cache = Cache(config_directory) if not os.path.exists(config_directory + 'cache'): @@ -1217,13 +1242,11 @@ class Movistar(object): return res def install_key_file(self, filename): - import shutil if sys.version_info[0] > 2: filename = bytes(filename, 'utf-8') shutil.copyfile(filename, self.cache.config_directory + 'auth.key') def export_key_file(self, filename): - import shutil if sys.version_info[0] > 2: filename = bytes(filename, 'utf-8') if self.account['access_token']: @@ -1443,3 +1466,50 @@ class Movistar(object): except: pass return res + + def get_accounts(self): + accounts = [] + for d in os.listdir(self.config_dir): + if os.path.isdir(os.path.join(self.config_dir, d)) and d.startswith("account_"): + account = {'id': d, 'name': d.replace('account_', 'Cuenta ')} + content = Movistar.load_file_if_exists(os.path.join(self.config_dir, d, 'account.json')) + if content: + data = json.loads(content) + login = data.get('login') + if login: + if '@' in login: + p1, p2 = login.split('@') + account['login'] = p1[:3] +'...@'+ p2 + else: + account['login'] = login[:3] + accounts.append(account) + return accounts + + def switch_account(self, name): + Movistar.save_file(self.config_dir + 'account.txt', name) + + def create_new_account(self): + accounts = self.get_accounts() + name = None + n = 1 + while True: + name = 'account_' + str(n) + if not any(account.get("id") == name for account in accounts): + break; + n += 1 + if name: + os.makedirs(self.config_dir + name) + return name + + @staticmethod + def load_file_if_exists(filename): + content = None + if os.path.exists(filename): + with io.open(filename, 'r', encoding='utf-8') as handle: + content = handle.read() + return content + + @staticmethod + def save_file(filename, content): + with io.open(filename, 'w', encoding='utf-8') as handle: + handle.write(content) diff --git a/resources/lib/plugin.py b/resources/lib/plugin.py index ee7131e..5e7750b 100644 --- a/resources/lib/plugin.py +++ b/resources/lib/plugin.py @@ -6,10 +6,13 @@ from __future__ import unicode_literals, absolute_import, division import sys import json +import io +import os.path import xbmc import xbmcgui import xbmcplugin +import xbmcaddon if sys.version_info[0] >= 3: import urllib.request as urllib2 @@ -20,9 +23,6 @@ else: from urllib import urlencode, quote_plus from urlparse import parse_qsl -import xbmcaddon -import os.path - from .log import LOG from .movistar import * from .addon import * @@ -93,7 +93,10 @@ def play(params): else: url = d['url'] - proxy = m.cache.load_file('proxy.conf') + # Read the proxy address + proxy = Movistar.load_file_if_exists(os.path.join(profile_dir, 'proxy.txt')) + LOG('proxy address: {}'.format(proxy)) + if addon.getSettingBool('manifest_modification') and proxy: url = '{}/?manifest={}'.format(proxy, url) @@ -108,7 +111,10 @@ def play(params): manifest_headers = 'User-Agent=' + useragent if stype == 'tv': - cdn_token = m.get_cdntoken() + cdn_token = m.cache.load('cdn.conf') + if not cdn_token: + cdn_token = m.get_cdntoken() + m.cache.save_file('cdn.conf', cdn_token) #LOG('cdn_token: {}'.format(cdn_token)) manifest_headers += '&x-tcdn-token=' + cdn_token @@ -178,7 +184,7 @@ def play(params): #ttml.use_language_filter = False subtype = ttml.subtitle_type() - subfolder = profile_dir + os.sep +'subtitles/' + subfolder = os.path.join(profile_dir, 'subtitles') if not os.path.exists(subfolder): os.makedirs(subfolder) @@ -186,7 +192,7 @@ def play(params): LOG('sublist: {}'.format(sublist)) subpaths = [] for sub in sublist: - filename = subfolder + sub['lang'] + filename = os.path.join(subfolder, sub['lang']) LOG('Converting {}'.format(sub['filename'])) response = m.net.session.get(sub['url'], allow_redirects=True) content = response.content @@ -618,15 +624,40 @@ def export_key(): if directory: m.export_key_file(directory + 'movistarplus.key') -def list_users(): - open_folder(addon.getLocalizedString(30160)) # Change user +def select_account(id, name): + m.switch_account(id) + open_folder(name) add_menu_option(addon.getLocalizedString(30183), get_url(action='login')) # Login with username add_menu_option(addon.getLocalizedString(30181), get_url(action='login_with_key')) # Login with key - if os.path.exists(profile_dir + 'auth.key'): + if os.path.exists(os.path.join(m.cache.config_directory, 'auth.key')): add_menu_option(addon.getLocalizedString(30184), get_url(action='export_key')) # Export key add_menu_option(addon.getLocalizedString(30150), get_url(action='logout')) # Close session close_folder() +def list_accounts(params): + LOG('list_accounts: {}'.format(params)) + + if params.get('name') == 'new_account': + m.create_new_account() + + accounts = m.get_accounts() + open_folder(addon.getLocalizedString(30160)) # Accounts + for account in accounts: + name = account['name'] + login = account.get('login') + if login: + name += ' ({})'.format(login) + if account['id'] == m.account_dir: + display_name = '[B][COLOR blue]' + name + '[/COLOR][/B]' + else: + display_name = name + add_menu_option(display_name, get_url(action='select_account', id=account['id'], name=name)) + add_menu_option(addon.getLocalizedString(30161), get_url(action='accounts', name='new_account')) # Add new + close_folder(cacheToDisc=False) + + if params.get('name') == 'new_account': + xbmc.executebuiltin('Container.Update({},replace)'.format(xbmc.getInfoLabel('Container.FolderPath'))) + def iptv(params): LOG('iptv: params: {}'.format(params)) if m.logged: @@ -702,8 +733,10 @@ def router(paramstring): login() elif params['action'] == 'export_key': export_key() - elif params['action'] == 'user': - list_users() + elif params['action'] == 'select_account': + select_account(params['id'], params['name']) + elif params['action'] == 'accounts': + list_accounts(params) elif params['action'] == 'logout': logout() elif params['action'] == 'epg': @@ -759,7 +792,7 @@ def router(paramstring): elif m.expired_access_token: show_notification(addon.getLocalizedString(30208)) - add_menu_option(addon.getLocalizedString(30160), get_url(action='user'), icon='account.png') # Accounts + add_menu_option(addon.getLocalizedString(30160), get_url(action='accounts'), icon='account.png') # Accounts add_menu_option(addon.getLocalizedString(30450), get_url(action='show_donation_dialog'), icon='qr.png') # Donation close_folder(cacheToDisc=False) diff --git a/resources/settings.xml b/resources/settings.xml index c6947b4..7071a48 100644 --- a/resources/settings.xml +++ b/resources/settings.xml @@ -1,6 +1,6 @@ - + @@ -8,8 +8,8 @@ - - + +