From 26a774503b8a192dd93b8ec13779e27e838c8df5 Mon Sep 17 00:00:00 2001 From: derAnfaenger Date: Wed, 23 Aug 2017 18:30:37 +0200 Subject: [PATCH] twitter bots: Write internal config to separate file. Tweaked by tabbott to remove duplicate write_config lines. Fixes #88. --- zulip/integrations/twitter/twitter-bot | 20 ++++++++++++------- zulip/integrations/twitter/twitter-search-bot | 18 ++++++++++------- 2 files changed, 24 insertions(+), 14 deletions(-) diff --git a/zulip/integrations/twitter/twitter-bot b/zulip/integrations/twitter/twitter-bot index 7d0cf36..016dbd1 100755 --- a/zulip/integrations/twitter/twitter-bot +++ b/zulip/integrations/twitter/twitter-bot @@ -32,12 +32,11 @@ from six.moves.configparser import ConfigParser, NoSectionError, NoOptionError import zulip VERSION = "0.9" CONFIGFILE = os.path.expanduser("~/.zulip_twitterrc") +CONFIGFILE_INTERNAL = os.path.expanduser("~/.zulip_twitterrc_internal") -def write_config(config, since_id, user): +def write_config(config, configfile_path): # type: (ConfigParser, int, int) -> None - config.set('twitter', 'since_id', str(since_id)) - config.set('twitter', 'user_id', str(user)) - with open(CONFIGFILE, 'w') as configfile: + with open(configfile_path, 'w') as configfile: config.write(configfile) parser = zulip.add_default_arguments(argparse.ArgumentParser(r""" @@ -92,6 +91,8 @@ if not options.twitter_id: try: config = ConfigParser() config.read(CONFIGFILE) + config_internal = ConfigParser() + config_internal.read(CONFIGFILE_INTERNAL) consumer_key = config.get('twitter', 'consumer_key') consumer_secret = config.get('twitter', 'consumer_secret') @@ -122,12 +123,12 @@ if not user.id: try: since_id = config.getint('twitter', 'since_id') -except NoOptionError: +except (NoOptionError, NoSectionError): since_id = -1 try: user_id = config.get('twitter', 'user_id') -except NoOptionError: +except (NoOptionError, NoSectionError): user_id = options.twitter_id client = zulip.Client( @@ -164,4 +165,9 @@ for status in statuses[::-1][:options.limit_tweets]: else: since_id = status.id -write_config(config, since_id, user_id) +if 'twitter' not in config_internal.sections(): + config_internal.add_section('twitter') +config_internal.set('twitter', 'since_id', str(since_id)) +config_internal.set('twitter', 'user_id', str(user)) + +write_config(config_internal, CONFIGFILE_INTERNAL) diff --git a/zulip/integrations/twitter/twitter-search-bot b/zulip/integrations/twitter/twitter-search-bot index 793943e..b94ed79 100755 --- a/zulip/integrations/twitter/twitter-search-bot +++ b/zulip/integrations/twitter/twitter-search-bot @@ -32,13 +32,11 @@ from six.moves.configparser import ConfigParser, NoSectionError, NoOptionError import zulip VERSION = "0.9" CONFIGFILE = os.path.expanduser("~/.zulip_twitterrc") +CONFIGFILE_INTERNAL = os.path.expanduser("~/.zulip_twitterrc_internal") -def write_config(config, since_id): +def write_config(config, configfile_path): # type: (ConfigParser, int) -> None - if 'search' not in config.sections(): - config.add_section('search') - config.set('search', 'since_id', str(since_id)) - with open(CONFIGFILE, 'w') as configfile: + with open(configfile_path, 'w') as configfile: config.write(configfile) parser = zulip.add_default_arguments(argparse.ArgumentParser(r""" @@ -113,6 +111,8 @@ if not opts.search_terms: try: config = ConfigParser() config.read(CONFIGFILE) + config_internal = ConfigParser() + config_internal.read(CONFIGFILE_INTERNAL) consumer_key = config.get('twitter', 'consumer_key') consumer_secret = config.get('twitter', 'consumer_secret') @@ -125,7 +125,7 @@ if not (consumer_key and consumer_secret and access_token_key and access_token_s parser.error("Please provide a ~/.zulip_twitterrc") try: - since_id = config.getint('search', 'since_id') + since_id = config_internal.getint('search', 'since_id') except (NoOptionError, NoSectionError): since_id = 0 @@ -190,4 +190,8 @@ for status in statuses[::-1][:opts.limit_tweets]: else: since_id = status.id -write_config(config, since_id) +if 'search' not in config_internal.sections(): + config_internal.add_section('search') +config_internal.set('search', 'since_id', str(since_id)) + +write_config(config_internal, CONFIGFILE_INTERNAL)