Skip to content

Commit

Permalink
twitter: remove requirement of two internal file and support multiple…
Browse files Browse the repository at this point in the history
… searches, users

Fixes #697.
  • Loading branch information
strifel committed Jun 10, 2021
1 parent 9ce7c52 commit d3a6231
Showing 1 changed file with 10 additions and 20 deletions.
30 changes: 10 additions & 20 deletions zulip/integrations/twitter/twitter-bot
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import zulip

VERSION = "0.9"
CONFIGFILE = os.path.expanduser("~/.zulip_twitterrc")
CONFIGFILE_INTERNAL = os.path.expanduser("~/.zulip_twitterrc_data")
INSTRUCTIONS = r"""
twitter-bot --config-file=~/.zuliprc --search="@nprnews,quantum physics"
Expand Down Expand Up @@ -43,7 +44,7 @@ that will process tweets every 5 minutes is:
== Setting up Twitter authentications ==
Run this on a personal or trusted machine, because your API key is
Run this on a personal or trusted machine, because your APIx key is
visible to local users through the command line or config file.
This bot uses OAuth to authenticate with Twitter. Please create a
Expand Down Expand Up @@ -106,10 +107,10 @@ if all([opts.search_terms, opts.twitter_name]):
parser.error("You must only specify either a search term or a username.")
if opts.search_terms:
client_type = "ZulipTwitterSearch/"
CONFIGFILE_INTERNAL = os.path.expanduser("~/.zulip_twitterrc_fetchsearch")
SINCE_ID_NAME = "since_id_search_" + opts.search_terms
elif opts.twitter_name:
client_type = "ZulipTwitter/"
CONFIGFILE_INTERNAL = os.path.expanduser("~/.zulip_twitteruserrc_fetchuser")
SINCE_ID_NAME = "since_id_name_" + opts.twitter_name
else:
parser.error("You must either specify a search term or a username.")

Expand All @@ -130,17 +131,9 @@ if not all([consumer_key, consumer_secret, access_token_key, access_token_secret
parser.error("Please provide a ~/.zulip_twitterrc")

try:
since_id = config_internal.getint("twitter", "since_id")
since_id = config_internal.getint("twitter", SINCE_ID_NAME)
except (NoOptionError, NoSectionError):
since_id = 0
try:
previous_twitter_name = config_internal.get("twitter", "twitter_name")
except (NoOptionError, NoSectionError):
previous_twitter_name = ""
try:
previous_search_terms = config_internal.get("twitter", "search_terms")
except (NoOptionError, NoSectionError):
previous_search_terms = ""

try:
import twitter
Expand All @@ -166,16 +159,15 @@ client = zulip.init_from_options(opts, client=client_type + VERSION)

if opts.search_terms:
search_query = " OR ".join(opts.search_terms.split(","))
if since_id == 0 or opts.search_terms != previous_search_terms:
# No since id yet, fetch the latest and then start monitoring from next time
# Or, a different user id is being asked for, so start from scratch
# Either way, fetch last 5 tweets to start off
if since_id == 0:
# No since id yet, fetch the latest and then start monitoring from next time.
# Fetch last 5 tweets to start off
statuses = api.GetSearch(search_query, count=5)
else:
# We have a saved last id, so insert all newer tweets into the zulip stream
statuses = api.GetSearch(search_query, since_id=since_id)
elif opts.twitter_name:
if since_id == 0 or opts.twitter_name != previous_twitter_name:
if since_id == 0:
# Same strategy as for search_terms
statuses = api.GetUserTimeline(screen_name=opts.twitter_name, count=5)
else:
Expand Down Expand Up @@ -251,8 +243,6 @@ for status in statuses[::-1][: opts.limit_tweets]:

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", "search_terms", str(opts.search_terms))
config_internal.set("twitter", "twitter_name", str(opts.twitter_name))
config_internal.set("twitter", SINCE_ID_NAME, str(since_id))

write_config(config_internal, CONFIGFILE_INTERNAL)

0 comments on commit d3a6231

Please sign in to comment.