twitter bots: Write internal config to separate file.

Tweaked by tabbott to remove duplicate write_config lines.

Fixes #88.
This commit is contained in:
derAnfaenger 2017-08-23 18:30:37 +02:00 committed by Tim Abbott
parent 6c47eda92b
commit 26a774503b
2 changed files with 24 additions and 14 deletions

View file

@ -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)

View file

@ -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)