Welcome to mirror list, hosted at ThFree Co, Russian Federation.

cli.py « api « nextcloud_news_updater - github.com/nextcloud/news-updater.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 5bb7bd6fb17aca99c89bce0a57decd7b6ded82f6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
from subprocess import check_output

from nextcloud_news_updater.api.api import Api, Feed
from nextcloud_news_updater.api.updater import Updater, UpdateThread
from nextcloud_news_updater.common.logger import Logger
from nextcloud_news_updater.config import Config


class Cli:
    def run(self, commands):
        return check_output(commands)


class CliApi(Api):
    def __init__(self, config):
        directory = config.url
        phpini = config.phpini
        if not directory.endswith('/'):
            directory += '/'
        self.directory = directory
        base_command = ['php', '-f', self.directory + 'occ']
        if phpini is not None and phpini.strip() != '':
            base_command += ['-c', phpini]
        self.before_cleanup_command = 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 + [
            'news:updater:after-update']


class CliApiV2(CliApi):
    def __init__(self, config):
        super().__init__(config)

    def _parse_json(self, feed_json):
        feed_json = feed_json['updater']
        return [Feed(info['feedId'], info['userId']) for info in feed_json]


def create_cli_api(config):
    if config.apilevel == 'v1-2':
        return CliApi(config)
    if config.apilevel == 'v2':
        return CliApiV2(config)


class CliUpdater(Updater):
    def __init__(self, config: Config, logger: Logger, api: CliApi, cli: Cli):
        super().__init__(config, logger)
        self.cli = cli
        self.api = api

    def before_update(self):
        self.logger.info('Running before update command: %s' %
                         ' '.join(self.api.before_cleanup_command))
        self.cli.run(self.api.before_cleanup_command)

    def start_update_thread(self, feeds):
        return CliUpdateThread(feeds, self.logger, self.api, self.cli)

    def all_feeds(self):
        feeds_json = self.cli.run(self.api.all_feeds_command).strip()
        feeds_json = str(feeds_json, 'utf-8')
        self.logger.info('Running get all feeds command: %s' %
                         ' '.join(self.api.all_feeds_command))
        self.logger.info('Received these feeds to update: %s' % feeds_json)
        return self.api.parse_feed(feeds_json)

    def after_update(self):
        self.logger.info('Running after update command: %s' %
                         ' '.join(self.api.after_cleanup_command))
        self.cli.run(self.api.after_cleanup_command)


class CliUpdateThread(UpdateThread):
    def __init__(self, feeds, logger, api, cli):
        super().__init__(feeds, logger)
        self.cli = cli
        self.api = api

    def update_feed(self, feed):
        command = self.api.update_feed_command + [str(feed.feed_id),
                                                  feed.user_id]
        self.logger.info('Running update command: %s' % ' '.join(command))
        self.cli.run(command)