From aba0de95fdcb9c849adc7777c9cd1de546eb41fd Mon Sep 17 00:00:00 2001 From: Felix Date: Wed, 9 Jun 2021 16:00:36 +0200 Subject: [PATCH] twitter: remove requirement of two internal file and support multiple searches, users --- zulip/integrations/twitter/twitter-bot | 30 +++++++++----------------- 1 file changed, 10 insertions(+), 20 deletions(-) diff --git a/zulip/integrations/twitter/twitter-bot b/zulip/integrations/twitter/twitter-bot index 15823f488..02c838969 100755 --- a/zulip/integrations/twitter/twitter-bot +++ b/zulip/integrations/twitter/twitter-bot @@ -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" @@ -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 @@ -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.") @@ -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 @@ -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: @@ -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)