Advertisement
Guest User

Untitled

a guest
Jun 3rd, 2017
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.84 KB | None | 0 0
  1.  
  2.  
  3. from basemodule import *
  4. import twitter
  5. import config
  6. import time
  7. from linklib import encode_id, decode_id
  8.  
  9. class Twitter(BaseModule):
  10.     def __init__(self, client):
  11.         BaseModule.__init__(self, client)
  12.  
  13.         self.tw = twitter.Api(username=config.twitterUser, password=config.twitterPass)
  14.         self.tw.SetSource("d00dz")
  15.  
  16.         self.lastFetchTime = time.time()-50
  17.         self.lastFetched = None
  18.         self.ignore = {}
  19.         self.ignore_tags = set()
  20.         self.audit = {}
  21.         self.maxFetchCount = 5
  22.        
  23.    
  24.     def help(self, what):
  25.     return None
  26.    
  27.  
  28.     def status(self):
  29.     return None
  30.    
  31.    
  32.     def update(self):
  33.     if time.time() - self.lastFetchTime > 10*60:
  34.                 self.do_update()
  35.  
  36.     def do_update(self):
  37.         self.lastFetchTime = time.time()
  38.         try:
  39.                 tweets = self.tw.GetFriendsTimeline(since_id=self.lastFetched, count=self.maxFetchCount)
  40.         except:
  41.                 import traceback
  42.                 traceback.print_exc()
  43.                 self._client.quit("Pau no twitter, reboot...")
  44.                 return
  45.         self.maxFetchCount = None
  46.         lastFetched = self.lastFetched
  47.         tweets.reverse()
  48.         for tw in tweets:
  49.                 if type(tw.text) is not unicode:
  50.                         text = tw.text.decode("latin1")
  51.                 else:
  52.                         text = tw.text
  53.                 if type(tw.user.screen_name) is unicode:
  54.                         name = tw.user.screen_name
  55.                 else:
  56.                         name = tw.user.screen_name.decode("latin1")
  57.                 ignore = False
  58.                 if self.ignore.has_key(name):
  59.                         if self.ignore[name] - time.time() <= 0:
  60.                                 del self.ignore[name]
  61.                         else:
  62.                                 ignore=True
  63.                 for t in self.ignore_tags:
  64.                         if text.find(t) >= 0:
  65.                                 ignore = True
  66.                                 break
  67.                 text = text.replace("<", "<").replace(">", ">")
  68.                 if not ignore:                
  69.                         msg = u"[%s] %s <t%s>"%(name, text, encode_id(tw.id))
  70.                         self._client.sendMessage(config.mainChannel, msg.encode("utf8"))
  71.                 lastFetched = max(lastFetched, tw.id)
  72.         self.lastFetched = lastFetched
  73.                
  74.    
  75.  
  76.     def analize(self, where, who, phrase):
  77.     if phrase.forMe and phrase.tokens:
  78.             if phrase.tokens[0] in ("stalk", "follow"):
  79.                 if len(phrase.tokens)==2:
  80.                     try:
  81.                         follow = phrase.tokens[1]
  82.                         self.tw.CreateFriendship(follow)
  83.                         self.audit[follow+":follow"] = who
  84.                         return ("M", "followando %s agora" % follow)
  85.                     except Exception, e:
  86.                         return ("M", "nao deu... %r"%e)
  87.                 else:
  88.                         return ("M", "%sar quem?" % phrase.tokens[0])
  89.             elif phrase.tokens[0] in ("unfollow", "unfolloweie"):
  90.                 if len(phrase.tokens)==2:
  91.                         unfollow = phrase.tokens[1]
  92.                         try:
  93.                                 self.tw.DestroyFriendship(unfollow)
  94.                                 self.audit[unfollow+":unfollow"] = who
  95.                                 return ("M", "%s chutado do twitter"%unfollow)
  96.                         except:
  97.                                 return ("M", "acho que nao funcionou (%s nao esta followado?)"%unfollow)
  98.                 else:
  99.                         return ("M", "??????")
  100.             elif phrase.tokens[0] in ("tweet", "twit"):
  101.                 self.tw.PostUpdate(phrase.from_token(1).decode("utf8"))
  102.                 return ("M", "twitado")
  103.             elif phrase.tokens[0] == "twitter":
  104.                 self.do_update()
  105.                 return None
  106.             elif phrase.tokens[0] == "twignore":
  107.                 if len(phrase.tokens) == 2:
  108.                     w = phrase.tokens[1]
  109.                     if w[0] == "#":
  110.                         self.ignore_tags.add(w)
  111.                         return ("M", "ignorando tweeets com %s"%w)
  112.                     else:
  113.                         self.ignore[w] = time.time() + 30*60
  114.                         return ("M", "ignorando %s por 1/2 hora"%w)
  115.                 return ("M", "ignorar quem/o que?")
  116.             elif phrase.tokens[0] == "twunignore":
  117.                 if len(phrase.tokens) == 2:
  118.                     w = phrase.tokens[1]
  119.                     if w in self.ignore_tags:
  120.                         self.ignore_tags.remove(w)
  121.                         return ("M", "tweets com %s designorado"%w)
  122.                     if self.ignore.has_key(w):
  123.                         del self.ignore[w]
  124.                         return ("M", "%s designorado"%w)
  125.                     return ("M", "%s nao esta sendo ignorado"%w)
  126.                 return ("M", "designorar o que?")  
  127.             elif phrase.tokens[0] in ("reply") and len(phrase.tokens)>2 and phrase.tokens[1][0]=='t':
  128.                 try:
  129.                         twid=decode_id(phrase.tokens[1][1:])
  130.                 except:
  131.                         return ("M", "id de tweet invalid")
  132.                 text = phrase.from_token(2)
  133.                 if "@" not in text:
  134.                         tw = self.tw.GetStatus(twid)
  135.                         text = "@"+tw.user.screen_name + " " + text
  136.                 self.tw.PostUpdate(text.decode("utf8"), twid)
  137.                 return ("M", "resposta a post #%s twitada" % twid)
  138.         return None
  139.  
  140.  
  141.     def restore(self, dict):
  142.     self.lastFetched = dict.get("lastTwit", None)
  143.         self.audit = dict.get("twitAudit", {})
  144.    
  145.    
  146.     def store(self, dict):
  147.     dict["lastTwit"] = self.lastFetched
  148.         dict["twitAudit"] = self.audit
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement