-
Notifications
You must be signed in to change notification settings - Fork 0
/
irc.py
executable file
·200 lines (166 loc) · 7.77 KB
/
irc.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import re
import random
import urllib
import lxml.html
from twisted.internet import protocol
from twisted.words.protocols import irc
import Fe2.tools.log
from Fe2.tools.solr import Solr
import Fe2.tools.commons as commons
__version__ = "1.1"
class Bot(irc.IRCClient):
""" Prutshot a hot bot.\n
at this moment in time, i'm good at, \n
extracting title's from youtube, vinemo links.\n
following query commands are supported: \n
prutshot: img: Moon (Wikicommons image)\n
prutshot: news: find (Fuzzy solr query (local news rss))\n
Have a nice day, the Sirius Cybernetics Corporation. \n"""
log = Fe2.tools.log.create_logger(__name__) # log
prutsers = []
def _get_nickname(self):
return self.factory.nickname
nickname = property(_get_nickname)
def signedOn(self):
"""" Signed ON """
greeting = ["worlds", "trees", "birds", "ants", "cats",
"toads", "spiders", "dogs", "seas", "gulls",
"monkeys", "bees", "webz", "channelz", "you", "ppl"]
self.join(self.factory.channel)
self.log.info("Entered %s as %s." % (self.factory.channel,
self.nickname,))
self.say(self.factory.channel,
str("Hello %s!" % random.choice(greeting)))
self.sendLine('WHO %s' % self.factory.channel)
def irc_RPL_WHOREPLY(self, *nargs):
self.prutsers.append(nargs[1][2])
def joined(self, channel):
""" Joined function.
:param channel: Channel name. """
self.channel = channel
self.log.info("Joined %s." % channel)
def privmsg(self, user, channel, msg):
sentences = ["Flopop!", "No flopping on the grass.",
"If flop then make some boiled leaves.",
"Flopout!", "On the flop again.",
"You realy know how to flop it",
"I am floppiefied.", "I demand another shrubbery."]
self.log.info("%s:\t%s" % (user, msg)) # Log the message
solr = Solr('http://localhost:8983/solr/core1/', self.log)
if msg.startswith("!new news"):
result = solr.query('*:*&fl=title,url&sort=timestamp+desc')
if result.hits:
for i in range(0,3):
msg = ""
msg += result.docs[i]["title"]+" , ".encode('utf-8')
msg += result.docs[i]["url"].encode('utf-8')
self.say(self.factory.channel, str(msg.encode('ascii', 'ignore')))
if msg.startswith("!news "):
result = solr.query(urllib.quote(msg.replace('!news ', '')).strip()+'&fl=title,url&sort=timestamp+desc')
if result.hits:
msg = "Found %s hits\n" % str(result.hits).encode('utf-8')
msg += result.docs[0]["title"]+" , ".encode('utf-8')
msg += result.docs[0]["url"].encode('utf-8')
self.say(self.factory.channel, str(msg.encode('ascii', 'ignore')))
if msg.startswith(self.nickname + ":"):
if msg.lower().find('help') > -1:
self.say(self.factory.channel, self.__doc__)
if msg.lower().startswith(self.nickname + ': img:'):
image = commons.WikiImage(msg.lower().replace(self.nickname + ': img:', '').strip())
if image.result:
if channel == self.nickname:
self.log.info(user.split('!')[0])
if user.split('!')[0] in self.prutsers:
self.say(user, image.result)
else:
self.say(self.factory.channel, "extruder! " +user)
else:
self.say(self.factory.channel, image.result)
if re.search("flo*p", msg): # Flop routine
self.say(self.factory.channel, str(random.choice(sentences)))
if msg.find("http://vimeo.com/") > -1 and not channel == self.nickname: # vinemo search
try:
start = msg.find('http://vimeo.com/')
end = msg.find('http://vimeo.com/') + 25
data = lxml.html.parse(urllib.urlopen(urllib.quote(msg[start:end])))
except:
data = False
if data:
for item in (data.xpath('//head')[0].getchildren()):
if item.tag == "meta":
prop = (item.get('property', ''))
if prop == "og:description":
line = "Vimeo omschrijving:%s" % \
str(item.values()[1].encode('utf-8'))
self.say(self.factory.channel, line)
if prop == "og:title":
line = "Vimeo title: %s" % \
str(item.values()[1].encode('utf-8'))
self.say(self.factory.channel, line)
if msg.find('youtu.be') > -1:
msg = "https://www.youtube.com/watch?v=" + msg.replace("youtu.be")
if msg.find("https://www.youtube.com/watch") > -1 or \
msg.find("http://www.youtube.com/watch") > -1 and\
not channel == self.nickname:
try:
params = msg.split('?')[1].split('&')
except:
params = []
for param in params:
youtube_hash = False
if param.split('=')[0] == 'v':
youtube_hash = param.split('=')[1]
if youtube_hash:
try:
url = "https://www.youtube.com/watch?v=%s" % urllib.quote(youtube_hash)
data = lxml.html.parse(
urllib.urlopen(url)
)
for item in (data.xpath('//head')[0].getchildren()):
if item.tag == "meta":
prop = item.get('property', '')
if prop == "og:description":
self.say(self.factory.channel, "JijBuis omschrijving: " + str(item.values()[1].encode('utf-8')))
if prop == "og:title":
self.say(self.factory.channel, "JijBuis title: " + str(item.values()[1].encode('utf-8').decode('utf-8')))
except:
pass
class BotFactory(protocol.ClientFactory):
"""
This part of the Bot creates the initial connection.
"""
log = Fe2.tools.log.create_logger(__name__) # log
protocol = Bot
def __init__(self, channel, nickname='prutshot'):
""" Set nickname and channel.
:param channel: Channel to join.
:param nickname: Nickname to use, defaults to prutshot.
"""
self.channel = channel
self.nickname = nickname
def clientConnectionLost(self, connector, reason):
""" Connection Lost.
:param connector: Twisted connector to reconnect to.
:param reason: Text string containing the disconnect reason.
"""
self.log.info("Lost connection (%s), reconnecting." % (reason))
connector.connect()
def clientConnectionFailed(self, connector, reason):
""" Connection Failed.
:param connector: Twisted connector to reconnect to.
:param reason: Text string containing the disconnect reason.
"""
self.log.info("Could not connect: %s" % (reason))
def prutshot():
"""
.. function:: Fe2.tools.irc.prutsbot()
Joins #pruts as prutshot :)
"""
from twisted.internet import reactor
chan = "pruts" # Channel to join.
reactor.connectTCP('irc.freenode.net', 6667, BotFactory('#' + chan))
reactor.run()
if __name__ == "__main__":
bot = prutshot()