-
Notifications
You must be signed in to change notification settings - Fork 0
/
engine.py
77 lines (59 loc) · 2.23 KB
/
engine.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
# -*- coding: utf-8 -*-
from time import time
from random import randint
import twitter
# API keys
from keys import consumer_key, consumer_secret, access_token_key, \
access_token_secret
def clean(msg):
""" Removes special characters from the message.
@param msg: message to be cleansed (muaha)
@type msg: string
"""
return msg.replace('<', '<').replace('>', '>')
class Echo(str):
pass
class Engine(object):
MaxCmdsPerSec = 10
DefaultProbability = 10
def __init__(self):
self.api = twitter.Api(consumer_key=consumer_key,
consumer_secret=consumer_secret,
access_token_key=access_token_key,
access_token_secret=access_token_secret)
self.count = 0
self.probability = Engine.DefaultProbability
self.last = time()
def twit(self, message):
""" Tweets a message and increases our counter.
"""
self.count += 1
try:
status = self.api.PostUpdate(message)
message = 'Twitted (' + status.text + ')'
if self.count > 4:
message += ' (delicious spam: http://www.twitter.com/_lolwtf)'
self.count = 0
except Exception, e:
message = 'Failed to twit! :-( (%s)' % str(e)
return clean(message)
def process(self, cmd):
""" Processes a command
"""
if '!twit' in cmd:
return Echo(self.twit(cmd.split('!twit')[1]))
elif '!url' in cmd and time() > self.last + Engine.MaxCmdsPerSec:
self.last = time()
return Echo('Url: http://twitter.com/_lolwtf')
elif '!chances' in cmd:
try:
self.probability = int(cmd.split('!chances')[1].replace('%', ''))
ans = Echo('Probability set to %d%%' % self.probability)
self.probability = int(100.0/float(self.probability))
print self.probability
return ans
except Exception, e:
print e
return Echo('Failed to set probability. Example: !chances 50%.')
elif randint(1, self.probability) == 1:
return Echo(self.twit(cmd))