Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

twitter: remove requirement of two internal file and support multiple searches and users #700

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is presumably a typo.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes it is.
I will remove the x again.

Thanks

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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was trying to think of a better way to serialize the parameters to a particular instances of the bot, but didn't come up with anything great. One idea would be to have the configuration file support having a block for each search, and use the names defined there. E.g.

[twitter_search_foo]
search_terms = ...

[twitter_search_bar]
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)