twitter bots: Write internal config to separate file.
Tweaked by tabbott to remove duplicate write_config lines. Fixes #88.
This commit is contained in:
parent
6c47eda92b
commit
26a774503b
|
@ -32,12 +32,11 @@ from six.moves.configparser import ConfigParser, NoSectionError, NoOptionError
|
||||||
import zulip
|
import zulip
|
||||||
VERSION = "0.9"
|
VERSION = "0.9"
|
||||||
CONFIGFILE = os.path.expanduser("~/.zulip_twitterrc")
|
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
|
# type: (ConfigParser, int, int) -> None
|
||||||
config.set('twitter', 'since_id', str(since_id))
|
with open(configfile_path, 'w') as configfile:
|
||||||
config.set('twitter', 'user_id', str(user))
|
|
||||||
with open(CONFIGFILE, 'w') as configfile:
|
|
||||||
config.write(configfile)
|
config.write(configfile)
|
||||||
|
|
||||||
parser = zulip.add_default_arguments(argparse.ArgumentParser(r"""
|
parser = zulip.add_default_arguments(argparse.ArgumentParser(r"""
|
||||||
|
@ -92,6 +91,8 @@ if not options.twitter_id:
|
||||||
try:
|
try:
|
||||||
config = ConfigParser()
|
config = ConfigParser()
|
||||||
config.read(CONFIGFILE)
|
config.read(CONFIGFILE)
|
||||||
|
config_internal = ConfigParser()
|
||||||
|
config_internal.read(CONFIGFILE_INTERNAL)
|
||||||
|
|
||||||
consumer_key = config.get('twitter', 'consumer_key')
|
consumer_key = config.get('twitter', 'consumer_key')
|
||||||
consumer_secret = config.get('twitter', 'consumer_secret')
|
consumer_secret = config.get('twitter', 'consumer_secret')
|
||||||
|
@ -122,12 +123,12 @@ if not user.id:
|
||||||
|
|
||||||
try:
|
try:
|
||||||
since_id = config.getint('twitter', 'since_id')
|
since_id = config.getint('twitter', 'since_id')
|
||||||
except NoOptionError:
|
except (NoOptionError, NoSectionError):
|
||||||
since_id = -1
|
since_id = -1
|
||||||
|
|
||||||
try:
|
try:
|
||||||
user_id = config.get('twitter', 'user_id')
|
user_id = config.get('twitter', 'user_id')
|
||||||
except NoOptionError:
|
except (NoOptionError, NoSectionError):
|
||||||
user_id = options.twitter_id
|
user_id = options.twitter_id
|
||||||
|
|
||||||
client = zulip.Client(
|
client = zulip.Client(
|
||||||
|
@ -164,4 +165,9 @@ for status in statuses[::-1][:options.limit_tweets]:
|
||||||
else:
|
else:
|
||||||
since_id = status.id
|
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)
|
||||||
|
|
|
@ -32,13 +32,11 @@ from six.moves.configparser import ConfigParser, NoSectionError, NoOptionError
|
||||||
import zulip
|
import zulip
|
||||||
VERSION = "0.9"
|
VERSION = "0.9"
|
||||||
CONFIGFILE = os.path.expanduser("~/.zulip_twitterrc")
|
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
|
# type: (ConfigParser, int) -> None
|
||||||
if 'search' not in config.sections():
|
with open(configfile_path, 'w') as configfile:
|
||||||
config.add_section('search')
|
|
||||||
config.set('search', 'since_id', str(since_id))
|
|
||||||
with open(CONFIGFILE, 'w') as configfile:
|
|
||||||
config.write(configfile)
|
config.write(configfile)
|
||||||
|
|
||||||
parser = zulip.add_default_arguments(argparse.ArgumentParser(r"""
|
parser = zulip.add_default_arguments(argparse.ArgumentParser(r"""
|
||||||
|
@ -113,6 +111,8 @@ if not opts.search_terms:
|
||||||
try:
|
try:
|
||||||
config = ConfigParser()
|
config = ConfigParser()
|
||||||
config.read(CONFIGFILE)
|
config.read(CONFIGFILE)
|
||||||
|
config_internal = ConfigParser()
|
||||||
|
config_internal.read(CONFIGFILE_INTERNAL)
|
||||||
|
|
||||||
consumer_key = config.get('twitter', 'consumer_key')
|
consumer_key = config.get('twitter', 'consumer_key')
|
||||||
consumer_secret = config.get('twitter', 'consumer_secret')
|
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")
|
parser.error("Please provide a ~/.zulip_twitterrc")
|
||||||
|
|
||||||
try:
|
try:
|
||||||
since_id = config.getint('search', 'since_id')
|
since_id = config_internal.getint('search', 'since_id')
|
||||||
except (NoOptionError, NoSectionError):
|
except (NoOptionError, NoSectionError):
|
||||||
since_id = 0
|
since_id = 0
|
||||||
|
|
||||||
|
@ -190,4 +190,8 @@ for status in statuses[::-1][:opts.limit_tweets]:
|
||||||
else:
|
else:
|
||||||
since_id = status.id
|
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)
|
||||||
|
|
Loading…
Reference in a new issue