-
-
Notifications
You must be signed in to change notification settings - Fork 377
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
strifel
wants to merge
1
commit into
zulip:main
Choose a base branch
from
strifel:twitter-697
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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.
|
||
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) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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