forked from sopel-irc/sopel-extras
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtwit.py
More file actions
139 lines (119 loc) · 5.7 KB
/
twit.py
File metadata and controls
139 lines (119 loc) · 5.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
"""
twitter.py - Willie Twitter Module
Copyright 2008-10, Michael Yanovich, opensource.osu.edu/~yanovich/wiki/
Copyright 2011, Edward Powell, embolalia.net
Licensed under the Eiffel Forum License 2.
http://willie.dftba.net
"""
import tweepy
import time
import re
from willie.config import ConfigurationError
def configure(config):
"""
These values are all found by signing up your bot at
[api.twitter.com](http://api.twitter.com).
| [twitter] | example | purpose |
| --------- | ------- | ------- |
| consumer_key | 09d8c7b0987cAQc7fge09 | OAuth consumer key |
| consumer_secret | LIaso6873jI8Yasdlfi76awer76yhasdfi75h6TFJgf | OAuth consumer secret |
| access_token | 564018348-Alldf7s6598d76tgsadfo9asdf56uUf65aVgdsf6 | OAuth access token |
| access_token_secret | asdfl7698596KIKJVGvJDcfcvcsfdy85hfddlku67 | OAuth access token secret |
"""
if config.option('Configure Twitter? (You will need to register on http://api.twitter.com)', False):
config.interactive_add('twitter', 'consumer_key', 'Consumer key')
config.interactive_add('twitter', 'consumer_secret', 'Consumer secret')
config.interactive_add('twitter', 'access_token', 'Access token')
config.interactive_add('twitter', 'access_token_secret', 'Access token secret')
def setup(willie):
try:
auth = tweepy.OAuthHandler(willie.config.twitter.consumer_key, willie.config.twitter.consumer_secret)
auth.set_access_token(willie.config.twitter.access_token, willie.config.twitter.access_token_secret)
api = tweepy.API(auth)
except:
raise ConfigurationError('Could not authenticate with Twitter. Are the'
' API keys configured properly?')
def format_thousands(integer):
"""Returns string of integer, with thousands separated by ','"""
return re.sub(r'(\d{3})(?=\d)', r'\1,', str(integer)[::-1])[::-1]
def gettweet(willie, trigger):
"""Show the last tweet by the given user"""
#try:
auth = tweepy.OAuthHandler(willie.config.twitter.consumer_key, willie.config.twitter.consumer_secret)
auth.set_access_token(willie.config.twitter.access_token, willie.config.twitter.access_token_secret)
api = tweepy.API(auth)
twituser = trigger.group(2)
twituser = str(twituser)
statuses = api.user_timeline(twituser)
recent = [s.text for s in statuses][0]
# #willie.say("<" + twituser + "> " + unicode(recent))
if twituser[0] != '@':
twituser = '@' + twituser
willie.say(twituser + ": " + unicode(recent))
#except:
willie.reply("You have inputted an invalid user.")
gettweet.commands = ['twit']
gettweet.priority = 'medium'
gettweet.example = '.twit aplusk'
def f_info(willie, trigger):
"""Show information about the given Twitter account"""
try:
auth = tweepy.OAuthHandler(willie.config.twitter.consumer_key, willie.config.twitter.consumer_secret)
auth.set_access_token(willie.config.twitter.access_token, willie.config.twitter.access_token_secret)
api = tweepy.API(auth)
twituser = trigger.group(2)
twituser = str(twituser)
info = api.get_user(twituser)
friendcount = format_thousands(info.friends_count)
name = info.name
id = info.id
favourites = info.favourites_count
followers = format_thousands(info.followers_count)
location = info.location
description = info.description
willie.reply("@" + str(twituser) + ": " + str(name) + ". " + "ID: " + str(id) + ". Friend Count: " + friendcount + ". Followers: " + followers + ". Favourites: " + str(favourites) + ". Location: " + str(location) + ". Description: " + str(description))
except:
willie.reply("You have inputted an invalid user.")
f_info.commands = ['twitinfo']
f_info.priority = 'medium'
f_info.example = '.twitinfo aplsuk'
def f_update(willie, trigger):
"""Tweet with Willie's account. Admin-only."""
if trigger.admin:
auth = tweepy.OAuthHandler(willie.config.twitter.consumer_key, willie.config.twitter.consumer_secret)
auth.set_access_token(willie.config.twitter.access_token, willie.config.twitter.access_token_secret)
api = tweepy.API(auth)
print api.me().name
update = str(trigger.group(2)) + " ^" + trigger.nick
if len(update) <= 140:
api.update_status(update)
willie.reply("Successfully posted to my twitter account.")
else:
toofar = len(update) - 140
willie.reply("Please shorten the length of your message by: " + str(toofar) + " characters.")
f_update.commands = ['tweet']
f_update.priority = 'medium'
f_update.example = '.tweet Hello World!'
def f_reply(willie, trigger):
auth = tweepy.OAuthHandler(willie.config.twitter.consumer_key, willie.config.twitter.consumer_secret)
auth.set_access_token(willie.config.twitter.access_token, willie.config.twitter.access_token_secret)
api = tweepy.API(auth)
incoming = str(trigger.group(2))
incoming = incoming.split()
statusid = incoming[0]
if statusid.isdigit():
update = incoming[1:]
if len(update) <= 140:
statusid = int(statusid)
#api3.PostUpdate(str(" ".join(update)), in_reply_to_status_id=10503164300)
willie.reply("Successfully posted to my twitter account.")
else:
toofar = len(update) - 140
willie.reply("Please shorten the length of your message by: " + str(toofar) + " characters.")
else:
willie.reply("Please provide a status ID.")
#f_reply.commands = ['reply']
f_reply.priority = 'medium'
f_reply.example = '.reply 892379487 I like that idea!'
if __name__ == '__main__':
print __doc__.strip()