-
Notifications
You must be signed in to change notification settings - Fork 1
/
App.py
130 lines (106 loc) · 3.8 KB
/
App.py
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
from tweepy import API
from tweepy import Stream
from tweepy import OAuthHandler
from tweepy.streaming import StreamListener
import os
import sys
import simplejson as json
import configparser as ConfigParser
import urllib3
class listener(StreamListener):
def __init__(self, target):
self.target = target
def on_data(self, data):
# Load the json data
data = json.loads(data)
# Get the bot owner's information
user = data.get('user')
# Get the user name and the message of the tweet
name = user.get('screen_name')
message = data.get('text')
# Make sure the tweeter is the one we want
if name != self.target.screen_name:
return True
# Send the payload to the mattermost server
payload = json.dumps(self.create_payload(message))
http.request('POST', hook, headers={'Content-Type':'application/json'}, body=payload)
return True
def on_error(self, status):
print(status)
def create_payload(self, message):
screen = self.target.screen_name
# Format payload
name = self.target.name
icon = 'http://icons.iconarchive.com/icons/sicons/basic-round-social/512/twitter-icon.png'
profile = '@' + screen
url = 'https://twitter.com/' + screen
# Format the message with markdown, if possible
msg = ''
for word in message.split():
if word[0] == '@':
msg += self.format_link(word)
else:
msg += word
msg += ' '
# Create the payload itself
return {
'username' : bot,
'icon_url' : avatar,
'attachments' : [{
'color' : '#FF8000',
'author_name': name,
'author_icon': icon,
'title' : profile,
'title_link' : url,
'fields': [{
'short' : False,
'value' : msg
}]
}]
}
def format_link(self, name):
# Find the range of the name
for i in range(1, len(name)):
if not ((name[i].isalnum()) or (name[i] == '_')):
break
# Fix the range if the last letter is valid
if (name[i].isalnum()) or (name[i] == '_'):
i += 1
return '[@' + name[1:i] + '](https://twitter.com/' + name[1:i] + ')' + name[i:]
if __name__ == '__main__':
# Make sure the config file was given
if (len(sys.argv) != 2):
print("You must only specify the path to the configuration file.")
exit(1)
# Make sure the file path is valid
path = sys.argv[1]
if (not os.path.isfile(path)):
print("The specified config file `" + path + "` doesn't exist.")
exit(1)
# Load the configuration file
Config = ConfigParser.ConfigParser()
Config.read(path)
# Parse the configuration
try:
c_key = Config.get('twitter', 'Ckey')
c_secret = Config.get('twitter', 'CSecret')
a_token = Config.get('twitter', 'AToken')
a_secret = Config.get('twitter', 'ASecret')
user = Config.get('twitter', 'User')
bot = Config.get('mattermost', 'Name')
avatar = Config.get('mattermost', 'AvatarURL')
hook = Config.get('mattermost', 'Hook')
except ConfigParser.NoOptionError as exception:
print(exception)
exit(1)
# Get an http manager
http = urllib3.PoolManager()
# Create Twitter OAuth tokens
auth = OAuthHandler(c_key, c_secret)
auth.set_access_token(a_token, a_secret)
# Get the user object
api = API(auth)
target = api.get_user(user)
# Create Twitter stream
twitterStream = Stream(auth, listener(target))
twitterStream.filter(follow=[user])