diff options
author | Gioele Falcetti <thegio.f@gmail.com> | 2020-10-09 20:30:09 +0300 |
---|---|---|
committer | Gioele Falcetti <thegio.f@gmail.com> | 2020-10-09 22:44:22 +0300 |
commit | 64ed3ac30b0c12d0c7a3aa4f2904a2c9bd424662 (patch) | |
tree | a76c68753098c115d9ce28fe0b52e47e52b130c6 | |
parent | 1a58286f188fbc71dc75c87bd7714c46dcdf23c2 (diff) |
Add support for Nextcloud News v15
Signed-off-by: Gioele Falcetti <thegio.f@gmail.com>
-rw-r--r-- | nextcloud_news_updater/api/cli.py | 68 | ||||
-rw-r--r-- | nextcloud_news_updater/config.py | 2 | ||||
-rw-r--r-- | nextcloud_news_updater/container.py | 4 |
3 files changed, 66 insertions, 8 deletions
diff --git a/nextcloud_news_updater/api/cli.py b/nextcloud_news_updater/api/cli.py index eece702..cc68dfd 100644 --- a/nextcloud_news_updater/api/cli.py +++ b/nextcloud_news_updater/api/cli.py @@ -21,14 +21,16 @@ class CliApi(Api): if not directory.endswith('/'): directory += '/' self.directory = directory - base_command = [config.php, '-f', self.directory + 'occ'] + self.base_command = [config.php, '-f', self.directory + 'occ'] if phpini is not None and phpini.strip() != '': - base_command += ['-c', phpini] - self.before_cleanup_command = base_command + [ + self.base_command += ['-c', phpini] + self.before_cleanup_command = self.base_command + [ 'news:updater:before-update'] - self.all_feeds_command = base_command + ['news:updater:all-feeds'] - self.update_feed_command = base_command + ['news:updater:update-feed'] - self.after_cleanup_command = base_command + [ + self.all_feeds_command = self.base_command + [ + 'news:updater:all-feeds'] + self.update_feed_command = self.base_command + [ + 'news:updater:update-feed'] + self.after_cleanup_command = self.base_command + [ 'news:updater:after-update'] @@ -40,11 +42,28 @@ class CliApiV2(CliApi): return [Feed(info['feedId'], info['userId']) for info in feeds] +class CliApiV15(CliApi): + """Cli API for Nextcloud News v15+""" + + def __init__(self, config: Config) -> None: + super().__init__(config) + self.all_feeds_command = self.base_command + ['news:feed:list'] + self.users_list_command = self.base_command + ['user:list', '--output', + 'json'] + + def _parse_feeds_json(self, feeds_json: Any, userID: str) -> List[Feed]: + if not feeds_json: + return [] + return [Feed(info['id'], userID) for info in feeds_json] + + def create_cli_api(config: Config) -> CliApi: if config.apilevel == 'v1-2': return CliApi(config) if config.apilevel == 'v2': return CliApiV2(config) + if config.apilevel == 'v15': + return CliApiV15(config) class CliUpdateThread(UpdateThread): @@ -61,6 +80,16 @@ class CliUpdateThread(UpdateThread): self.cli.run(command) +class CliUpdateThreadV15(CliUpdateThread): + """Cli Updater for Nextcloud News v15+""" + + def update_feed(self, feed: Feed) -> None: + command = self.api.update_feed_command + [feed.user_id, + str(feed.feed_id)] + self.logger.info('Running update command: %s' % ' '.join(command)) + self.cli.run(command) + + class CliUpdater(Updater): def __init__(self, config: Config, logger: Logger, api: CliApi, cli: Cli) -> None: @@ -88,3 +117,30 @@ class CliUpdater(Updater): self.logger.info('Running after update command: %s' % ' '.join(self.api.after_cleanup_command)) self.cli.run(self.api.after_cleanup_command) + + +class CliUpdaterV15(CliUpdater): + """Cli Updater for Nextcloud News v15+""" + + def start_update_thread(self, feeds: List[Feed]) -> CliUpdateThread: + return CliUpdateThreadV15(feeds, self.logger, self.api, self.cli) + + def all_feeds(self) -> List[Feed]: + self.logger.info('Running get user list command: %s' % + ' '.join(self.api.users_list_command)) + users_json = self.cli.run(self.api.users_list_command).strip() + users_json = str(users_json, 'utf-8') + users = self.api.parse_users(users_json) + + feeds_list = [] + for userID in users: + self.logger.info('Running get feeds for user "%s" command: %s' % + (userID, ' '.join(self.api.all_feeds_command))) + cmd = self.api.all_feeds_command + [userID] + feeds_json_bytes = self.cli.run(cmd).strip() + feeds_json = str(feeds_json_bytes, 'utf-8') + self.logger.info('Received these feeds to update for user %s: %s' % + (userID, feeds_json)) + feeds_list += self.api.parse_feeds(feeds_json, userID) + + return feeds_list diff --git a/nextcloud_news_updater/config.py b/nextcloud_news_updater/config.py index 092893f..588b32d 100644 --- a/nextcloud_news_updater/config.py +++ b/nextcloud_news_updater/config.py @@ -74,7 +74,7 @@ class ConfigValidator: result += ['Unknown mode: %s' % config.mode] if config.loglevel not in ['info', 'error']: result += ['Unknown loglevel: %s' % config.loglevel] - if config.apilevel not in ['v1-2', 'v2']: + if config.apilevel not in ['v1-2', 'v2', 'v15']: result += ['Unknown apilevel: %s' % config.apilevel] if config.phpini and not os.path.isabs(config.phpini): diff --git a/nextcloud_news_updater/container.py b/nextcloud_news_updater/container.py index e16be1f..c1c36d3 100644 --- a/nextcloud_news_updater/container.py +++ b/nextcloud_news_updater/container.py @@ -1,6 +1,6 @@ import sys -from nextcloud_news_updater.api.cli import CliUpdater, CliApi, \ +from nextcloud_news_updater.api.cli import CliUpdater, CliUpdaterV15, CliApi, \ create_cli_api from nextcloud_news_updater.api.updater import Updater from nextcloud_news_updater.api.web import create_web_api, WebApi, \ @@ -23,6 +23,8 @@ class Container(BaseContainer): def _create_updater(self, container: BaseContainer) -> Updater: if container.resolve(Config).is_web(): return container.resolve(WebUpdater) + elif container.resolve(Config).apilevel == 'v15': + return container.resolve(CliUpdaterV15) else: return container.resolve(CliUpdater) |