示例#1
0
文件: twitter.py 项目: buzzworkers/tl
def twitter_out(username, txt, event=None):
    """ post a message on twitter. """
    if event and event.chan:
        taglist = event.chan.data.taglist
        if taglist:
           for tag in taglist:
               txt += " %s" % tag
    url = parseurl(txt)
    if url:
        tiny = get_tinyurl(url)
        if tiny:
            tinyurl = tiny[0]  
            if tinyurl: txt = txt.replace(url, tinyurl)
    if len(txt) > 140: logging.error("size of twitter message > 140 chars: %s" % txt) ; return
    token, api = get_token(username)
    if token and api:
        status = api.update_status(txt)
        logging.warn("posted 1 tweet (%s chars) for %s" % (len(txt), username))
    else: logging.error("no token or API available")
    return status
示例#2
0
文件: bot.py 项目: buzzworkers/tl
 def _raw(self, txt, event=None):
     """ post a message on twitter. """
     try:
         if event and event.chan:
             taglist = event.chan.data.taglist
             if taglist:
                for tag in taglist:
                    txt += " %s" % tag
         url = parseurl(txt)
         tiny = get_tinyurl(url)
         if tiny:
             tinyurl = tiny[0]
             txt = txt.replace(url, tinyurl)
         if len(txt) > 140: logging.error("size of twitter message > 140 chars: %s" % txt) ; return
         token = get_token(self.users, username)
         if not token: raise tweepy.TweepError("Can't get twitter token")
         twitter = twitterapi(key, secret, token)
         status = twitter.update_status(txt)
         logging.warn("posted 1 tweet (%s chars) for %s" % (len(txt), username))
         return status
     except tweepy.TweepError as ex: logging.error("twitter - error: %s" % str(ex))
示例#3
0
文件: hubbub.py 项目: buzzworkers/tl
 def makeresponse(self, name, res, channel, sep=" .. "):
     """ loop over result to make a response. """
     item = self.byname(name)
     result = "[%s] - " % name 
     try: itemslist = item.itemslists.data[jsonstring([name, channel])]
     except KeyError: 
         item = self.byname(name)
         if item == None: return "no %s rss item" % name
         else:
             item.itemslists.data[jsonstring([name, channel])] = ['title', 'link']
             item.itemslists.save()
     for j in res:
         if item.markup.get(jsonstring([name, channel]), 'skipmerge') and 'Merge branch' in j['title']: continue
         resultstr = ""
         for i in item.itemslists.data[jsonstring([name, channel])]:
             try:
                 ii = getattr(j, i)
                 if not ii: continue
                 ii = str(ii)
                 if ii.startswith('http://'):
                     if item.markup.get(jsonstring([name, channel]), 'tinyurl'):
                         try:
                             tinyurl = get_tinyurl(ii)
                             logging.debug('rss - tinyurl is: %s' % str(tinyurl))
                             if not tinyurl: resultstr += "%s - " % ii
                             else: resultstr += "%s - " % tinyurl[0]
                         except Exception as ex:
                             handle_exception()
                             resultstr += "%s - " % item
                     else: resultstr += "%s - " % ii
                 else: resultstr += "%s - " % ii.strip()
             except (KeyError, AttributeError) as ex:
                 logging.info('%s - %s' % (name, str(ex)))
                 continue
         resultstr = resultstr[:-3]
         if resultstr: result += "%s %s " % (resultstr, sep)
     return result[:-(len(sep)+2)]