twitter bots: Update to use current python-twitter.
It appears that twitter has changed several of its APIs.
This commit is contained in:
parent
9aa080e663
commit
7187c2ef5e
|
@ -69,7 +69,8 @@ parser = optparse.OptionParser(r"""
|
||||||
Make sure to go the application you created and click "create my
|
Make sure to go the application you created and click "create my
|
||||||
access token" as well. Fill in the values displayed.
|
access token" as well. Fill in the values displayed.
|
||||||
|
|
||||||
Depends on: twitter-python
|
Depends on: https://github.com/bear/python-twitter version 3.1
|
||||||
|
(`pip install python-twitter`)
|
||||||
""")
|
""")
|
||||||
|
|
||||||
parser.add_option('--twitter-id',
|
parser.add_option('--twitter-id',
|
||||||
|
@ -115,7 +116,7 @@ api = twitter.Api(consumer_key=consumer_key,
|
||||||
|
|
||||||
user = api.VerifyCredentials()
|
user = api.VerifyCredentials()
|
||||||
|
|
||||||
if not user.GetId():
|
if not user.id:
|
||||||
print("Unable to log in to twitter with supplied credentials. Please double-check and try again")
|
print("Unable to log in to twitter with supplied credentials. Please double-check and try again")
|
||||||
sys.exit()
|
sys.exit()
|
||||||
|
|
||||||
|
@ -146,12 +147,12 @@ else:
|
||||||
statuses = api.GetFriendsTimeline(user=options.twitter_id, since_id=since_id)
|
statuses = api.GetFriendsTimeline(user=options.twitter_id, since_id=since_id)
|
||||||
|
|
||||||
for status in statuses[::-1][:options.limit_tweets]:
|
for status in statuses[::-1][:options.limit_tweets]:
|
||||||
composed = "%s (%s)" % (status.GetUser().GetName(), status.GetUser().GetScreenName())
|
composed = "%s (%s)" % (status.user.name, status.user.screen_name)
|
||||||
message = {
|
message = {
|
||||||
"type": "stream",
|
"type": "stream",
|
||||||
"to": [options.stream],
|
"to": [options.stream],
|
||||||
"subject": composed,
|
"subject": composed,
|
||||||
"content": status.GetText(),
|
"content": status.text,
|
||||||
}
|
}
|
||||||
|
|
||||||
ret = client.send_message(message)
|
ret = client.send_message(message)
|
||||||
|
@ -161,6 +162,6 @@ for status in statuses[::-1][:options.limit_tweets]:
|
||||||
print("Error sending message to zulip: %s" % ret['msg'])
|
print("Error sending message to zulip: %s" % ret['msg'])
|
||||||
break
|
break
|
||||||
else:
|
else:
|
||||||
since_id = status.GetId()
|
since_id = status.id
|
||||||
|
|
||||||
write_config(config, since_id, user_id)
|
write_config(config, since_id, user_id)
|
||||||
|
|
|
@ -46,10 +46,11 @@ parser = optparse.OptionParser(r"""
|
||||||
|
|
||||||
Send Twitter search results to a Zulip stream.
|
Send Twitter search results to a Zulip stream.
|
||||||
|
|
||||||
Depends on: twitter-python version 1.0 or later
|
Depends on: https://github.com/bear/python-twitter version 3.1
|
||||||
|
|
||||||
To use this script:
|
To use this script:
|
||||||
|
|
||||||
|
0. Use `pip install python-twitter` to install `python-twitter`.
|
||||||
1. Set up Twitter authentication, as described below
|
1. Set up Twitter authentication, as described below
|
||||||
2. Subscribe to the stream that will receive Twitter updates (default stream: twitter)
|
2. Subscribe to the stream that will receive Twitter updates (default stream: twitter)
|
||||||
3. Test the script by running it manually, like this:
|
3. Test the script by running it manually, like this:
|
||||||
|
@ -138,15 +139,14 @@ api = twitter.Api(consumer_key=consumer_key,
|
||||||
|
|
||||||
user = api.VerifyCredentials()
|
user = api.VerifyCredentials()
|
||||||
|
|
||||||
if not user.GetId():
|
if not user.id:
|
||||||
print("Unable to log in to Twitter with supplied credentials.\
|
print("Unable to log in to twitter with supplied credentials. Please double-check and try again")
|
||||||
Please double-check and try again.")
|
|
||||||
sys.exit()
|
sys.exit()
|
||||||
|
|
||||||
client = zulip.Client(
|
client = zulip.Client(
|
||||||
email=opts.email,
|
email=opts.zulip_email,
|
||||||
api_key=opts.api_key,
|
api_key=opts.zulip_api_key,
|
||||||
site=opts.site,
|
site=opts.zulip_site,
|
||||||
client="ZulipTwitterSearch/" + VERSION,
|
client="ZulipTwitterSearch/" + VERSION,
|
||||||
verbose=True)
|
verbose=True)
|
||||||
|
|
||||||
|
@ -155,11 +155,11 @@ statuses = api.GetSearch(search_query, since_id=since_id)
|
||||||
|
|
||||||
for status in statuses[::-1][:opts.limit_tweets]:
|
for status in statuses[::-1][:opts.limit_tweets]:
|
||||||
# https://twitter.com/eatevilpenguins/status/309995853408530432
|
# https://twitter.com/eatevilpenguins/status/309995853408530432
|
||||||
composed = "%s (%s)" % (status.GetUser().GetName(),
|
composed = "%s (%s)" % (status.user.name,
|
||||||
status.GetUser().GetScreenName())
|
status.user.screen_name)
|
||||||
url = "https://twitter.com/%s/status/%s" % (status.GetUser().GetScreenName(),
|
url = "https://twitter.com/%s/status/%s" % (status.user.screen_name,
|
||||||
status.GetId())
|
status.id)
|
||||||
content = status.GetText()
|
content = status.text
|
||||||
|
|
||||||
search_term_used = None
|
search_term_used = None
|
||||||
for term in opts.search_terms.split(","):
|
for term in opts.search_terms.split(","):
|
||||||
|
@ -186,6 +186,6 @@ for status in statuses[::-1][:opts.limit_tweets]:
|
||||||
print("Error sending message to zulip: %s" % ret['msg'])
|
print("Error sending message to zulip: %s" % ret['msg'])
|
||||||
break
|
break
|
||||||
else:
|
else:
|
||||||
since_id = status.GetId()
|
since_id = status.id
|
||||||
|
|
||||||
write_config(config, since_id)
|
write_config(config, since_id)
|
||||||
|
|
Loading…
Reference in a new issue