11
11
import urllib2
12
12
import time
13
13
import ast
14
-
14
+ import zipfile
15
+ import datetime
15
16
16
17
plugin = Plugin ()
17
18
dialog = xbmcgui .Dialog ()
@@ -123,6 +124,55 @@ def find_props_dict(d, param_id, param_id2):
123
124
param_val .append (param_v ['val' ])
124
125
return (param_key , param_val )
125
126
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
+
126
176
def dvr_param_load (dvr_uuid_sel ):
127
177
dvr_url = 'http://' + tvh_url + ':' + tvh_port + '/api/idnode/load?uuid=' + dvr_uuid_sel
128
178
dvr_load = requests .get (dvr_url ).json ()
@@ -1588,7 +1638,7 @@ def wizard():
1588
1638
else :
1589
1639
wizard_start ()
1590
1640
1591
- @plugin .route ('/yvh ' )
1641
+ @plugin .route ('/tvh ' )
1592
1642
def tvh ():
1593
1643
tvh_config_url = 'http://' + tvh_url + ':' + tvh_port + '/api/config/load'
1594
1644
tvh_config_load = requests .get (tvh_config_url ).json ()
@@ -1598,7 +1648,7 @@ def tvh():
1598
1648
ch_icon_scheme , ch_icon_scheme_key , ch_icon_scheme_val = find_param_dict (tvh_config_load , 'chiconscheme' , 'enum' )
1599
1649
picon_path = find_param (tvh_config_load , 'piconpath' )
1600
1650
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" ]
1602
1652
param_update = ""
1603
1653
sel_tvh = dialog .select ('Select a Tvh configuration parameter to edit' , list = tvh_config_info_list )
1604
1654
if sel_tvh < 0 :
@@ -1653,6 +1703,63 @@ def tvh():
1653
1703
icon_update = ',' .join (icon_update_list )
1654
1704
icon_update_url = 'http://' + tvh_url + ':' + tvh_port + '/api/idnode/save?node=[' + icon_update + ']'
1655
1705
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." )
1656
1763
if param_update != "" :
1657
1764
param_url = 'http://' + tvh_url + ':' + tvh_port + '/api/config/save?node={' + param_update + '}'
1658
1765
param_save = requests .get (param_url )
@@ -1717,7 +1824,7 @@ def index():
1717
1824
})
1718
1825
items .append (
1719
1826
{
1720
- 'label' : 'Tvh Base Configuration' ,
1827
+ 'label' : 'Tvh Base Configuration & Backup ' ,
1721
1828
'path' : plugin .url_for (u'tvh' ),
1722
1829
'thumbnail' :get_icon_path ('settings' ),
1723
1830
})
0 commit comments