Browse Source

Merge pull request #27 from doko-desuka/master

Update CleanupService.py
pull/30/head
I-A-C 1 year ago
parent
commit
48cc91f84a
No account linked to committer's email address
1 changed files with 39 additions and 25 deletions
  1. 39
    25
      CleanupService.py

+ 39
- 25
CleanupService.py View File

@@ -1,46 +1,60 @@
# -*- coding: utf-8 -*-
# CleanupService.py
# LambdaScrapers Cleanup Service

import os
import re

import xbmc
import xbmcvfs
import xbmcaddon

from lib.lambdascrapers import getAllHosters

'''
Temporary service to TRY to make some file changes, and then remove itself.
Temporary service to TRY to make some file changes, and then prevent itself from running again.
'''

ADDON = xbmcaddon.Addon()

# 1) Do the actual file changes you need:

profileFolder = xbmc.translatePath(ADDON.getAddonInfo('profile')).decode('utf-8')
# 1) Do the actual housekeeping changes.
try:
oldFilePath = os.path.join(profileFolder, 'settings.xml')
if xbmcvfs.exists(oldFilePath):
xbmcvfs.delete(oldFilePath)
profileFolderPath = xbmc.translatePath(ADDON.getAddonInfo('profile')).decode('utf-8')
settingsPath = os.path.join(profileFolderPath, 'settings.xml')

# We rewrite the user settings file while ignoring all obsolete providers.
if xbmcvfs.exists(settingsPath):
with open(settingsPath, 'r+') as settingsFile:
# Parse an XML tree from the settings file.
from xml.etree import ElementTree
tree = ElementTree.fromstring(settingsFile.read())
currentProviders = set(getAllHosters())
if len(currentProviders) > 0:
# Traverse the tree backwards so we can safely remove elements on the go.
for element in reversed(tree):
id = element.get('id')
if id and id.startswith('provider.') and id.split('.', 1)[1] not in currentProviders:
tree.remove(element)
# Dump the cleaned up XML tree back to the file.
settingsFile.seek(0)
settingsFile.write(ElementTree.tostring(tree))
settingsFile.truncate()
except:
pass

# 2) Remove itself from the add-on folder, and overwrite 'addon.xml' to remove the extension point
# that ran this service at Kodi startups.s

addonRootFolder = xbmc.translatePath(ADDON.getAddonInfo('path')).decode('utf-8')
SERVICE_FILENAME = 'CleanupService.py'

# 2) Disable the service in the 'addon.xml' file.
try:
serviceScriptPath = os.path.join(addonRootFolder, SERVICE_FILENAME)
xbmcvfs.delete(serviceScriptPath)

addonXMLPath = os.path.join(addonRootFolder, 'addon.xml')
with open(addonXMLPath, 'r+') as xmlFile:
originalLines = xmlFile.readlines()
xmlFile.seek(0)
for line in originalLines:
if SERVICE_FILENAME not in line: # Ignore the line with your service entry, accept all others.
xmlFile.write(line)
xmlFile.truncate()
# Now 'addon.xml' doesn't have the service extension point anymore.
addonFolderPath = xbmc.translatePath(ADDON.getAddonInfo('path')).decode('utf-8')
addonXMLPath = os.path.join(addonFolderPath, 'addon.xml')
# Disabling is done by commenting out the XML line with the service extension so it doesn't run anymore.
with open(addonXMLPath, 'r+') as addonXMLFile:
xmlText = addonXMLFile.read()
serviceFilename = 'CleanupService\.py'
pattern = r'(<\s*?extension.*?' + serviceFilename + '.*?>)'
updatedXML = re.sub(pattern, r'<!--\1-->', xmlText, count=1, flags=re.IGNORECASE)
addonXMLFile.seek(0)
addonXMLFile.write(updatedXML)
addonXMLFile.truncate()
except:
pass

Loading…
Cancel
Save