Skip to content

Commit

Permalink
Implement refreshing logic (#9)
Browse files Browse the repository at this point in the history
* Automatically refreshes at a configurable interval
* Doesn't refresh at every startup anymore, unless you have passed your interval
* Postpone to restart Simple IPTV when watching TV
* Allows to force a refresh from the settings

Co-authored-by: Dag Wieers <[email protected]>
  • Loading branch information
michaelarnauts and dagwieers committed May 12, 2020
1 parent 4848d0a commit e7d2452
Show file tree
Hide file tree
Showing 9 changed files with 203 additions and 53 deletions.
13 changes: 10 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,17 +1,24 @@
# Temporary files
*.pyc
*.pyo
.DS*
Thumbs.db
*~
.cache

# Files related to development
.pylint_rc
/.idea
/.project
/.pydevproject
/.settings
Thumbs.db
*~
.cache
/Pipfile
/Pipfile.lock

# Files related to testing
.coverage
.tox/

# IPTV Manager output
tests/userdata/playlist.m3u8
tests/userdata/epg.xml
44 changes: 40 additions & 4 deletions resources/language/resource.language.en_gb/strings.po
Original file line number Diff line number Diff line change
Expand Up @@ -11,27 +11,63 @@ msgid "Are you sure you want to setup IPTV Simple for the use with IPTV Manager?
msgstr ""

msgctxt "#30701"
msgid "The configuration of IPTV Simple is completed!"
msgid "IPTV Simple is configured for use with IPTV Manager!"
msgstr ""

msgctxt "#30702"
msgid "The configuration of IPTV Simple has failed. Please check the log files for more information."
msgstr ""

msgctxt "#30703"
msgid "Detecting IPTV add-ons..."
msgstr ""

msgctxt "#30704"
msgid "Fetching channels and guide of {addon}..."
msgstr ""

msgctxt "#30705"
msgid "Updating channels and guide..."
msgstr ""

msgctxt "#30706"
msgid "The channels and guide are updated successfully!"
msgstr ""


### SETTINGS
msgctxt "#30800"
msgid "IPTV Simple"
msgid "Channels"
msgstr ""

msgctxt "#30801"
msgid "Configuration"
msgid "Refreshing"
msgstr ""

msgctxt "#30802"
msgid "Configure IPTV Simple automatically…"
msgid "Refresh interval [I](in hours)[/I]"
msgstr ""

msgctxt "#30803"
msgid "Refresh channels and guide now…"
msgstr ""

msgctxt "#30820"
msgid "IPTV Simple"
msgstr ""

msgctxt "#30821"
msgid "Configuration"
msgstr ""

msgctxt "#30822"
msgid "Configure IPTV Simple automatically…"
msgstr ""

msgctxt "#30823"
msgid "Open IPTV Simple settings…"
msgstr ""

msgctxt "#30824"
msgid "Automatically restart IPTV Simple to refresh the data"
msgstr ""
46 changes: 41 additions & 5 deletions resources/language/resource.language.nl_nl/strings.po
Original file line number Diff line number Diff line change
Expand Up @@ -12,27 +12,63 @@ msgid "Are you sure you want to setup IPTV Simple for the use with IPTV Manager?
msgstr "Bent u zeker om IPTV Simple in te stellen om te gebruiken met IPTV Manager? De bestaande configuratie van IPTV Simple zal worden overschreven."

msgctxt "#30701"
msgid "The configuration of IPTV Simple is completed!"
msgstr "De configuratie van IPTV Simple is uitgevoerd!"
msgid "IPTV Simple is configured for use with IPTV Manager!"
msgstr "IPTV SImple is geconfigureerd om te gebruiken met IPTV Manager!"

msgctxt "#30702"
msgid "The configuration of IPTV Simple has failed. Please check the log files for more information."
msgstr "De configuratie van IPTV Simple is mislukt. Gelieve de logbestanden te bekijken voor meer informatie."

msgctxt "#30703"
msgid "Detecting IPTV add-ons..."
msgstr "Detecteren van IPTV add-ons..."

msgctxt "#30704"
msgid "Fetching channels and guide of {addon}..."
msgstr "Opvragen van kanalenlijst en gids van {addon}..."

msgctxt "#30705"
msgid "Updating channels and guide..."
msgstr "Updaten van de kanalenlijst en de gids..."

msgctxt "#30706"
msgid "The channels and guide are updated successfully!"
msgstr "De kanalenlijst en gids zijn sucessvol geüpdate."


### SETTINGS
msgctxt "#30800"
msgid "Channels"
msgstr "Kanalen"

msgctxt "#30801"
msgid "Refreshing"
msgstr "Verniewen"

msgctxt "#30802"
msgid "Refresh interval [I](in hours)[/I]"
msgstr "Vernieuwsingsinterval [I](in uren)[/I]"

msgctxt "#30803"
msgid "Refresh channels and guide now…"
msgstr "Vernieuw kanalenlijst en gids nu…"

msgctxt "#30820"
msgid "IPTV Simple"
msgstr "IPTV Simple"

msgctxt "#30801"
msgctxt "#30821"
msgid "Configuration"
msgstr "Configuratie"

msgctxt "#30802"
msgctxt "#30822"
msgid "Configure IPTV Simple automatically…"
msgstr "Configureer IPTV Simple automatisch…"

msgctxt "#30803"
msgctxt "#30823"
msgid "Open IPTV Simple settings…"
msgstr "Open de IPTV Simple instellingen…"

msgctxt "#30824"
msgid "Automatically restart IPTV Simple to refresh the data"
msgstr "Herstart IPTV Simple automatisch om de data te vernieuwen"
9 changes: 8 additions & 1 deletion resources/lib/functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import logging

from resources.lib import kodilogging, kodiutils
from resources.lib.modules.addon import Addon
from resources.lib.modules.iptvsimple import IptvSimple

kodilogging.config()
Expand All @@ -21,10 +22,16 @@ def setup_iptv_simple():
else:
kodiutils.ok_dialog(message=kodiutils.localize(30702)) # The configuration of IPTV Simple has failed!

# Open settings again
kodiutils.open_settings()


def refresh():
""" Refresh the channels and EPG """
_LOGGER.debug('TODO: refresh')
Addon.refresh(True)

# Open settings again
kodiutils.open_settings()


def run(args):
Expand Down
11 changes: 4 additions & 7 deletions resources/lib/kodiutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -197,14 +197,11 @@ def get_setting_bool(key, default=None):

def get_setting_int(key, default=None):
"""Get an add-on setting as integer"""
# ADDON.getSettingInt(key) doesn't work in Leia for settings without "number"
try:
return ADDON.getSettingInt(key)
except (AttributeError, TypeError): # On Krypton or older, or when not an integer
value = get_setting(key, default)
try:
return int(value)
except ValueError:
return default
return int(get_setting(key, default))
except ValueError: # Occurs when not an integer
return default
except RuntimeError: # Occurs when the add-on is disabled
return default

Expand Down
58 changes: 57 additions & 1 deletion resources/lib/modules/addon.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@
import logging
import os
import socket
import time

from resources.lib import kodiutils
from resources.lib.modules.iptvsimple import IptvSimple

_LOGGER = logging.getLogger(__name__)

Expand Down Expand Up @@ -44,6 +46,60 @@ def __init__(self, addon_id, addon_obj, channels_uri, epg_uri):
addon = kodiutils.get_addon(addon_id)
self.addon_path = kodiutils.addon_path(addon)

@classmethod
def refresh(cls, show_progress=False):
""" Update channels and EPG data """
channels = []
epg = dict()

if show_progress:
progress = kodiutils.progress(message=kodiutils.localize(30703)) # Detecting IPTV add-ons...
else:
progress = None

addons = cls.get_iptv_addons()

for index, addon in enumerate(addons):
_LOGGER.info('Updating IPTV data for %s...', addon.addon_id)

if progress:
progress.update(int(100 * index / len(addons)),
kodiutils.localize(30704).format(addon=kodiutils.addon_name(addon.addon_obj))) # Fetching channels and guide of {addon}...

# Fetch channels
channels.extend(addon.get_channels())
if progress and progress.iscanceled():
progress.close()
return

# Fetch EPG data
epg.update(addon.get_epg())
if progress and progress.iscanceled():
progress.close()
return

# Write files
if show_progress:
progress.update(100, kodiutils.localize(30705)) # Updating channels and guide...

IptvSimple.write_playlist(channels)
IptvSimple.write_epg(epg)

if kodiutils.get_setting_bool('iptv_simple_restart'):
if show_progress:
# Try to restart now. We will schedule it if the user is watching TV.
IptvSimple.restart(True)
else:
# Schedule a restart
IptvSimple.restart(False)

# Update last_refreshed
kodiutils.set_setting_int('last_refreshed', int(time.time()))

if show_progress:
progress.close()
kodiutils.ok_dialog(message=kodiutils.localize(30706)) # The channels and guide are updated successfully!

@staticmethod
def get_iptv_addons():
""" Find add-ons that provide IPTV channel data """
Expand Down Expand Up @@ -151,7 +207,7 @@ def _get_data_from_addon(self, uri):
# HTTP(S) path
if uri.startswith(('http://', 'https://')):
# TODO: implement requests to fetch data
return None
raise NotImplementedError

# Local path
addon = kodiutils.get_addon(self.addon_id)
Expand Down
16 changes: 14 additions & 2 deletions resources/lib/modules/iptvsimple.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@

class IptvSimple:
""" Helper class to setup IPTV Simple """

restart_required = False

def __init__(self):
""" Init """

Expand All @@ -42,6 +45,7 @@ def setup(cls):
output_dir = kodiutils.addon_profile()
playlist_path = os.path.join(output_dir, IPTV_SIMPLE_PLAYLIST)
epg_path = os.path.join(output_dir, IPTV_SIMPLE_EPG)
logo_path = '/'

addon.setSetting('m3uPathType', '0') # Local path
addon.setSetting('m3uPath', playlist_path)
Expand All @@ -50,16 +54,24 @@ def setup(cls):
addon.setSetting('epgPath', epg_path)

addon.setSetting('logoPathType', '0') # Local path
addon.setSetting('logoPath', '/')
addon.setSetting('logoPath', logo_path)

# Activate IPTV Simple
cls._activate()

return True

@classmethod
def restart(cls):
def restart(cls, force=False):
""" Restart IPTV Simple """
if not force and (kodiutils.get_cond_visibility('Pvr.IsPlayingTv') or kodiutils.get_cond_visibility('Pvr.IsPlayingRadio')):
# Don't restart when we are Playing TV or Radio
cls.restart_required = True
_LOGGER.info('Postponing restart of Simple IPTV since it is currently in use.')
return

cls.restart_required = False

cls._deactivate()
time.sleep(1)
cls._activate()
Expand Down
Loading

0 comments on commit e7d2452

Please sign in to comment.