Skip to content

Commit 2ef5ad4

Browse files
committed
add backup-import userdata
1 parent 8db64f9 commit 2ef5ad4

File tree

2 files changed

+119
-8
lines changed

2 files changed

+119
-8
lines changed

addon.xml

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
22
<addon id="script.module.tvh2kodi"
33
name="Tvheadend Setup for Kodi"
4-
version="1.5"
4+
version="1.6"
55
provider-name="edit4ever">
66
<requires>
77
<import addon="xbmc.python" version="2.6.0"/>
@@ -18,9 +18,9 @@
1818

1919
* minimum version of Tvheadend is 4.2
2020

21-
v1.5
22-
- add progress bar for internal epg grab
23-
- add base tvh config parameters (set dvb scan files and channel icon paths)
21+
v1.6
22+
- add channel icons reset option
23+
- add function to backup/import Tvheadend userdata
2424

2525
</description>
2626
<disclaimer>Copyright (C) 2017 edit4ever - edit4ever@gmail.com</disclaimer>
@@ -34,6 +34,10 @@ v1.5
3434
<screenshot>resources/screenshot-03.jpg</screenshot>
3535
</assets>
3636
<news>
37+
v1.5
38+
- add progress bar for internal epg grab
39+
- add base tvh config parameters (set dvb scan files and channel icon paths)
40+
3741
v1.4
3842
- fix adapter network issue
3943
- add multiple network support to adapter

default.py

Lines changed: 111 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@
1111
import urllib2
1212
import time
1313
import ast
14-
14+
import zipfile
15+
import datetime
1516

1617
plugin = Plugin()
1718
dialog = xbmcgui.Dialog()
@@ -123,6 +124,55 @@ def find_props_dict(d, param_id, param_id2):
123124
param_val.append(param_v['val'])
124125
return (param_key, param_val)
125126

127+
def dis_or_enable_addon(addon_id, enable="true"):
128+
addon = '"%s"' % addon_id
129+
if xbmc.getCondVisibility("System.HasAddon(%s)" % addon_id) and enable == "true":
130+
return xbmc.log("### Skipped %s, reason = allready enabled" % addon_id)
131+
elif not xbmc.getCondVisibility("System.HasAddon(%s)" % addon_id) and enable == "false":
132+
return xbmc.log("### Skipped %s, reason = not installed" % addon_id)
133+
else:
134+
do_json = '{"jsonrpc":"2.0","id":1,"method":"Addons.SetAddonEnabled","params":{"addonid":%s,"enabled":%s}}' % (addon, enable)
135+
query = xbmc.executeJSONRPC(do_json)
136+
response = json.loads(query)
137+
if enable == "true":
138+
xbmc.log("### Enabled %s, response = %s" % (addon_id, response))
139+
else:
140+
xbmc.log("### Disabled %s, response = %s" % (addon_id, response))
141+
return xbmc.executebuiltin('Container.Update(%s)' % xbmc.getInfoLabel('Container.FolderPath'))
142+
143+
def zipfolder(foldername, target_dir):
144+
zipobj = zipfile.ZipFile(foldername + '.zip', 'w', zipfile.ZIP_DEFLATED)
145+
rootlen = len(target_dir) + 1
146+
for base, dirs, files in os.walk(target_dir):
147+
for file in files:
148+
fn = os.path.join(base, file)
149+
zipobj.write(fn, fn[rootlen:])
150+
151+
def zipdir(dirPath=None, zipFilePath=None, includeDirInZip=True):
152+
if not zipFilePath:
153+
zipFilePath = dirPath + ".zip"
154+
if not os.path.isdir(dirPath):
155+
raise OSError("dirPath argument must point to a directory. "
156+
"'%s' does not." % dirPath)
157+
parentDir, dirToZip = os.path.split(dirPath)
158+
def trimPath(path):
159+
archivePath = path.replace(parentDir, "", 1)
160+
if parentDir:
161+
archivePath = archivePath.replace(os.path.sep, "", 1)
162+
if not includeDirInZip:
163+
archivePath = archivePath.replace(dirToZip + os.path.sep, "", 1)
164+
return os.path.normcase(archivePath)
165+
outFile = zipfile.ZipFile(zipFilePath, "w",
166+
compression=zipfile.ZIP_DEFLATED)
167+
for (archiveDirPath, dirNames, fileNames) in os.walk(dirPath):
168+
for fileName in fileNames:
169+
filePath = os.path.join(archiveDirPath, fileName)
170+
outFile.write(filePath, trimPath(filePath))
171+
if not fileNames and not dirNames:
172+
zipInfo = zipfile.ZipInfo(trimPath(archiveDirPath) + "/")
173+
outFile.writestr(zipInfo, "")
174+
outFile.close()
175+
126176
def dvr_param_load(dvr_uuid_sel):
127177
dvr_url = 'http://' + tvh_url + ':' + tvh_port + '/api/idnode/load?uuid=' + dvr_uuid_sel
128178
dvr_load = requests.get(dvr_url).json()
@@ -1588,7 +1638,7 @@ def wizard():
15881638
else:
15891639
wizard_start()
15901640

1591-
@plugin.route('/yvh')
1641+
@plugin.route('/tvh')
15921642
def tvh():
15931643
tvh_config_url = 'http://' + tvh_url + ':' + tvh_port + '/api/config/load'
15941644
tvh_config_load = requests.get(tvh_config_url).json()
@@ -1598,7 +1648,7 @@ def tvh():
15981648
ch_icon_scheme, ch_icon_scheme_key, ch_icon_scheme_val = find_param_dict(tvh_config_load, 'chiconscheme', 'enum')
15991649
picon_path = find_param(tvh_config_load, 'piconpath')
16001650
picon_scheme, picon_scheme_key, picon_scheme_val = find_param_dict(tvh_config_load, 'piconscheme', 'enum')
1601-
tvh_config_info_list = ["DVB scan path: " + str(dvb_scan_path), "Prefer picon: " + str(prefer_picon), "Channel icon path: " + str(ch_icon_path), "Channel icon scheme: " + str(ch_icon_scheme), "Picon path: " + str(picon_path), "Picon scheme: " + str(picon_scheme), "RESET ALL CHANNEL ICONS"]
1651+
tvh_config_info_list = ["DVB scan path: " + str(dvb_scan_path), "Prefer picon: " + str(prefer_picon), "Channel icon path: " + str(ch_icon_path), "Channel icon scheme: " + str(ch_icon_scheme), "Picon path: " + str(picon_path), "Picon scheme: " + str(picon_scheme), "RESET ALL CHANNEL ICONS", "BACKUP TVHEADEND USERDATA", "IMPORT TVHEADEND USERDATA"]
16021652
param_update = ""
16031653
sel_tvh = dialog.select('Select a Tvh configuration parameter to edit', list=tvh_config_info_list)
16041654
if sel_tvh < 0:
@@ -1653,6 +1703,63 @@ def tvh():
16531703
icon_update = ','.join(icon_update_list)
16541704
icon_update_url = 'http://' + tvh_url + ':' + tvh_port + '/api/idnode/save?node=[' + icon_update + ']'
16551705
icon_update_save = requests.get(icon_update_url)
1706+
if sel_tvh == 7:
1707+
dialog.ok("Tvheadend Userdata Backup", "The Tvheadend service will be stopped to start the backup.", "The Tvheadend client may show a connection error during the process.")
1708+
try:
1709+
tvh_json_url = 'http://' + tvh_url + ':8080/jsonrpc?request={"jsonrpc":"2.0","id":1,"method":"Addons.SetAddonEnabled","params":{"addonid":"service.tvheadend42","enabled":false}}'
1710+
tvh_json_load = requests.get(tvh_json_url).json()
1711+
tvh_stop = tvh_json_load['result']
1712+
except:
1713+
dialog.ok("Tvheadend Service Still Running!", "Unable to stop the Tvheadend service.", "Unable to complete backup.")
1714+
return
1715+
if tvh_stop == "OK":
1716+
if tvh_url == "127.0.0.1":
1717+
tvh_addon = xbmcaddon.Addon(id='service.tvheadend42')
1718+
tvh_userdata_path = xbmc.translatePath(tvh_addon.getAddonInfo('profile'))
1719+
else:
1720+
tvh_userdata_path = '//' + tvh_url + '/userdata/addon_data/service.tvheadend42'
1721+
output_path = dialog.browse(3, "Where would you like to save the Tvheadend Backup file?", "files")
1722+
output_name = output_path + "service.tvheadend42-backup-" + str(datetime.datetime.today())
1723+
if dialog.yesno('Backup Tvheadend Userdata to Zip File', 'Zip file will be created in the following location:', str(output_path), 'Select YES to create backup.'):
1724+
zipfolder(output_name, tvh_userdata_path)
1725+
dialog.ok("Tvheadend Userdata Backup Complete", "Tvheadend userdata has been backed up.", "Tvheadend service will be restarted.")
1726+
try:
1727+
tvh_json_url = 'http://' + tvh_url + ':8080/jsonrpc?request={"jsonrpc":"2.0","id":1,"method":"Addons.SetAddonEnabled","params":{"addonid":"service.tvheadend42","enabled":true}}'
1728+
tvh_json_load = requests.get(tvh_json_url).json()
1729+
tvh_stop = tvh_json_load['result']
1730+
except:
1731+
dialog.ok("Unable to Restart Tvheadend Service!", "Unable to restart the Tvheadend service.", "Please enable the service in Kodi addons.")
1732+
else:
1733+
dialog.ok("Tvheadend Service Still Running!", "Unable to stop the Tvheadend service.", "Unable to complete backup.")
1734+
if sel_tvh == 8:
1735+
dialog.ok("Tvheadend Userdata Import", "The Tvheadend service will be stopped to start the import.", "The Tvheadend client may show a connection error during the process.")
1736+
try:
1737+
tvh_json_url = 'http://' + tvh_url + ':8080/jsonrpc?request={"jsonrpc":"2.0","id":1,"method":"Addons.SetAddonEnabled","params":{"addonid":"service.tvheadend42","enabled":false}}'
1738+
tvh_json_load = requests.get(tvh_json_url).json()
1739+
tvh_stop = tvh_json_load['result']
1740+
except:
1741+
dialog.ok("Tvheadend Service Still Running!", "Unable to stop the Tvheadend service.", "Unable to complete the import.")
1742+
return
1743+
if tvh_stop == "OK":
1744+
if tvh_url == "127.0.0.1":
1745+
tvh_addon = xbmcaddon.Addon(id='service.tvheadend42')
1746+
tvh_userdata_path = xbmc.translatePath(tvh_addon.getAddonInfo('profile'))
1747+
else:
1748+
tvh_userdata_path = '//' + tvh_url + '/userdata/addon_data/service.tvheadend42'
1749+
zipfile_path = dialog.browse(1, "Select your Tvheadend userdata backup zip file?", "files", ".zip")
1750+
if dialog.yesno('Import Tvheadend Userdata from Zip File', 'Your current Tvheadend userdata will be overwritten.', '', 'Select YES to start import.'):
1751+
tvh_zip = zipfile.ZipFile(zipfile_path)
1752+
tvh_zip.extractall(tvh_userdata_path)
1753+
tvh_zip.close()
1754+
dialog.ok("Tvheadend Userdata Import Complete", "Tvheadend userdata has been imported.", "Tvheadend service will be restarted.")
1755+
try:
1756+
tvh_json_url = 'http://' + tvh_url + ':8080/jsonrpc?request={"jsonrpc":"2.0","id":1,"method":"Addons.SetAddonEnabled","params":{"addonid":"service.tvheadend42","enabled":true}}'
1757+
tvh_json_load = requests.get(tvh_json_url).json()
1758+
tvh_stop = tvh_json_load['result']
1759+
except:
1760+
dialog.ok("Unable to Restart Tvheadend Service!", "Unable to restart the Tvheadend service.", "Please enable the service in Kodi addons.")
1761+
else:
1762+
dialog.ok("Tvheadend Service Still Running!", "Unable to stop the Tvheadend service.", "Unable to complete backup.")
16561763
if param_update != "":
16571764
param_url = 'http://' + tvh_url + ':' + tvh_port + '/api/config/save?node={' + param_update + '}'
16581765
param_save = requests.get(param_url)
@@ -1717,7 +1824,7 @@ def index():
17171824
})
17181825
items.append(
17191826
{
1720-
'label': 'Tvh Base Configuration',
1827+
'label': 'Tvh Base Configuration & Backup',
17211828
'path': plugin.url_for(u'tvh'),
17221829
'thumbnail':get_icon_path('settings'),
17231830
})

0 commit comments

Comments
 (0)