Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from basemodule import *
- import twitter
- import config
- import time
- from linklib import encode_id, decode_id
- class Twitter(BaseModule):
- def __init__(self, client):
- BaseModule.__init__(self, client)
- self.tw = twitter.Api(username=config.twitterUser, password=config.twitterPass)
- self.tw.SetSource("d00dz")
- self.lastFetchTime = time.time()-50
- self.lastFetched = None
- self.ignore = {}
- self.ignore_tags = set()
- self.audit = {}
- self.maxFetchCount = 5
- def help(self, what):
- return None
- def status(self):
- return None
- def update(self):
- if time.time() - self.lastFetchTime > 10*60:
- self.do_update()
- def do_update(self):
- self.lastFetchTime = time.time()
- try:
- tweets = self.tw.GetFriendsTimeline(since_id=self.lastFetched, count=self.maxFetchCount)
- except:
- import traceback
- traceback.print_exc()
- self._client.quit("Pau no twitter, reboot...")
- return
- self.maxFetchCount = None
- lastFetched = self.lastFetched
- tweets.reverse()
- for tw in tweets:
- if type(tw.text) is not unicode:
- text = tw.text.decode("latin1")
- else:
- text = tw.text
- if type(tw.user.screen_name) is unicode:
- name = tw.user.screen_name
- else:
- name = tw.user.screen_name.decode("latin1")
- ignore = False
- if self.ignore.has_key(name):
- if self.ignore[name] - time.time() <= 0:
- del self.ignore[name]
- else:
- ignore=True
- for t in self.ignore_tags:
- if text.find(t) >= 0:
- ignore = True
- break
- text = text.replace("<", "<").replace(">", ">")
- if not ignore:
- msg = u"[%s] %s <t%s>"%(name, text, encode_id(tw.id))
- self._client.sendMessage(config.mainChannel, msg.encode("utf8"))
- lastFetched = max(lastFetched, tw.id)
- self.lastFetched = lastFetched
- def analize(self, where, who, phrase):
- if phrase.forMe and phrase.tokens:
- if phrase.tokens[0] in ("stalk", "follow"):
- if len(phrase.tokens)==2:
- try:
- follow = phrase.tokens[1]
- self.tw.CreateFriendship(follow)
- self.audit[follow+":follow"] = who
- return ("M", "followando %s agora" % follow)
- except Exception, e:
- return ("M", "nao deu... %r"%e)
- else:
- return ("M", "%sar quem?" % phrase.tokens[0])
- elif phrase.tokens[0] in ("unfollow", "unfolloweie"):
- if len(phrase.tokens)==2:
- unfollow = phrase.tokens[1]
- try:
- self.tw.DestroyFriendship(unfollow)
- self.audit[unfollow+":unfollow"] = who
- return ("M", "%s chutado do twitter"%unfollow)
- except:
- return ("M", "acho que nao funcionou (%s nao esta followado?)"%unfollow)
- else:
- return ("M", "??????")
- elif phrase.tokens[0] in ("tweet", "twit"):
- self.tw.PostUpdate(phrase.from_token(1).decode("utf8"))
- return ("M", "twitado")
- elif phrase.tokens[0] == "twitter":
- self.do_update()
- return None
- elif phrase.tokens[0] == "twignore":
- if len(phrase.tokens) == 2:
- w = phrase.tokens[1]
- if w[0] == "#":
- self.ignore_tags.add(w)
- return ("M", "ignorando tweeets com %s"%w)
- else:
- self.ignore[w] = time.time() + 30*60
- return ("M", "ignorando %s por 1/2 hora"%w)
- return ("M", "ignorar quem/o que?")
- elif phrase.tokens[0] == "twunignore":
- if len(phrase.tokens) == 2:
- w = phrase.tokens[1]
- if w in self.ignore_tags:
- self.ignore_tags.remove(w)
- return ("M", "tweets com %s designorado"%w)
- if self.ignore.has_key(w):
- del self.ignore[w]
- return ("M", "%s designorado"%w)
- return ("M", "%s nao esta sendo ignorado"%w)
- return ("M", "designorar o que?")
- elif phrase.tokens[0] in ("reply") and len(phrase.tokens)>2 and phrase.tokens[1][0]=='t':
- try:
- twid=decode_id(phrase.tokens[1][1:])
- except:
- return ("M", "id de tweet invalid")
- text = phrase.from_token(2)
- if "@" not in text:
- tw = self.tw.GetStatus(twid)
- text = "@"+tw.user.screen_name + " " + text
- self.tw.PostUpdate(text.decode("utf8"), twid)
- return ("M", "resposta a post #%s twitada" % twid)
- return None
- def restore(self, dict):
- self.lastFetched = dict.get("lastTwit", None)
- self.audit = dict.get("twitAudit", {})
- def store(self, dict):
- dict["lastTwit"] = self.lastFetched
- dict["twitAudit"] = self.audit
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement