示例#1
0
logger.setLevel(logging.DEBUG)
# create file handler which logs even debug messages
fh = logging.FileHandler(u'../logs/exetask.log')
fh.setLevel(logging.DEBUG)
# create console handler with warn log level
ch = logging.StreamHandler()
ch.setLevel(logging.WARN)
# create logger output formater
formatter = logging.Formatter(u'%(asctime)s %(name)s %(levelname)s %(message)s')
fh.setFormatter(formatter)
ch.setFormatter(formatter)
logger.addHandler(fh)
logger.addHandler(ch)

# setup weibo
[access_token, expires_in] = djangodb.get_weibo_auth_info(3041970403)
if time.time() > float(expires_in):
    raise Exception(u'授权过期了,with expires_in:%s' % expires_in)
weibo = weiboAPI.weiboAPI(access_token=access_token, expires_in=expires_in, u_id=3041970403)
reqInterval(31)
logger.info(u'Sina Weibo 登录信息:\t' + weibo.getUserInfo()[u'name'])

# setup google reader
from libgreader import readerAPI
[access_token, refresh_token, access_expires] = djangodb.get_google_auth_info(u_id=1)
reader = readerAPI.readerAPI(u_id=1, access_token=access_token, \
                  refresh_token=refresh_token, expires_access=access_expires)
reqInterval(31)
logger.info(u'Google Reader 登录信息:\t' + reader.getUserInfo()[u'userName'])

示例#2
0
def remindUserTopicUpdates(topicTitle):
    try:
        logger.debug(u'Start remind user for topic:%s' % topicTitle)
        topic = djangodb.Topic.objects.get(title=topicTitle)
        if topic.alive():
            topic_news = topic.news_set.all()[0]
        else:
            logger.warn(u'topic:%s is already dead, add unsubscribe task!' % topicTitle)
            djangodb.add_task(topic=topic, type=u'unsubscribe')
            return False
    except:
        logger.exception(u'topic or news may not exist in database')
        return False

    topicWatchers = topic.watcher.all()
    topicWatcherWeibo = topic.watcher_weibo.all()


    '''
    得到订阅该话题的所有用户,分两类,已经授权的和没有授权的
    '''
    watcherWithAuth = set()
    watcherWithoutAuth = set()
    for watcher in topicWatchers:
        if watcher.has_oauth():
            watcherWithAuth.add(watcher)
        else:
            watcherWithoutAuth.add(watcher)

    '''
    筛选出其中需要提醒的用户,分为四类:
    
    watcherWithoutStatusAndAuth
    watcherWithStatusAndAuth
    watcherWithStatusWithoutAuth
    watcherWithoutStatusWithAuth
    '''
    watcherWithStatusAndAuth = set()
    watcherWithStatusWithoutAuth = set()
    for watcherWeibo in topicWatcherWeibo:
        watcher = watcherWeibo.user
        if not watcher.to_remind():
            # 去掉不需要提醒的用户
            continue
        watcher.original_weibo = watcherWeibo  # 人工添加的字段
        if watcher in watcherWithAuth:
            watcherWithStatusAndAuth.add(watcher)
        elif watcher in watcherWithoutAuth:
            watcherWithStatusWithoutAuth.add(watcher)

    # 去掉不需要提醒的用户
    watcherWithoutStatusAndAuth = set([watcher for watcher in (watcherWithoutAuth - watcherWithStatusWithoutAuth) if watcher.to_remind()])
    watcherWithoutStatusWithAuth = set([watcher for watcher in (watcherWithAuth - watcherWithStatusAndAuth) if watcher.to_remind()])

    _shorturl = weibo.getShortUrl(Conf.get_timeline_url(topic.id))
    _msg = u'#%s#有新进展:%s 『%s』' % (topicTitle, topic_news.title, _shorturl)
    reqInterval(61)

    logger.debug(u'topicWatcherWeibo:%s' % topicWatcherWeibo)
    logger.debug(u'topicWatchers:%s' % topicWatchers)
    logger.debug(u'watcherWithAuth:%s' % watcherWithAuth)
    logger.debug(u'watcherWithoutAuth:%s' % watcherWithoutAuth)
    logger.debug(u'posgMsg:%s' % _msg)

    _user_reminded = []

    '''
    先更新有授权同时也发过微博的用户的状态
    '''
    for watcher in watcherWithStatusAndAuth:
        '''
        自己转发或评论自己的原始微博提醒自己,如果允许提醒他人的话,顺带提醒~
        '''
        [access_token, expires_in] = djangodb.get_weibo_auth_info(watcher.weiboId)
        _weibo = weiboAPI.weiboAPI(access_token=access_token, \
                                   expires_in=expires_in, \
                                   u_id=watcher.weiboId)
        watcher_btw = None
        if watcher.allow_remind_others and watcherWithStatusWithoutAuth:
            watcher_btw = watcherWithStatusWithoutAuth.pop()
            postMsg = _msg + u' @' + watcher_btw.weiboName + u' 顺便提醒你一下~'
        else:
            postMsg = _msg + u' @' + watcher.weiboName

        logger.info(u'remind user:%s \ntopic:%s \nupdate with msg:%s\noriginalWeibo:%s' % \
                     (watcher.weiboName, topicTitle, postMsg, watcher.original_weibo))
        res = {}
        try:
            if watcher.repost_remind:
                res[u'type'] = u'repost status'
                res[u'status'] = _weibo.repostStatus(weibo_id=watcher.original_weibo.weibo_id, content=postMsg)
            else:
                res[u'type'] = u'comment status'
                res[u'status'] = _weibo.postComment(weibo_id=watcher.original_weibo.weibo_id, content=postMsg)
        except APIError, err:
            logger.warn(u'%s failed:\t%s' % (res[u'type'], err.error))
            if err.error == u'target weibo does not exist!':
                topic.watcher_weibo.remove(watcher.original_weibo)
                topic.watcher.remove(watcher)
                logger.info(u'remove watcher:%s and delete watcherWeibo:%s' % (watcher, watcher.original_weibo))
                watcher.original_weibo.delete()
            else:
                logger.exception(u'')
        else:
            logger.info(u'%s Succeed!' % res[u'type'])
            if watcher_btw:
                watcher_btw.add_remind()
            watcher.add_remind()
            logger.info(u'added remind history for user: [%s, %s]' % (watcher.weiboName, watcher_btw))
        finally:
示例#3
0
logger.setLevel(logging.DEBUG)
# create file handler which logs even debug messages
fh = logging.FileHandler(u"../logs/checkweibo.log")
fh.setLevel(logging.DEBUG)
# create console handler with warn log level
ch = logging.StreamHandler()
ch.setLevel(logging.WARN)
# create logger output formater
formatter = logging.Formatter(u"%(asctime)s %(name)s %(levelname)s %(message)s")
fh.setFormatter(formatter)
ch.setFormatter(formatter)
logger.addHandler(fh)
logger.addHandler(ch)

# setup weibo
[access_token, expires_in] = djangodb.get_weibo_auth_info(3041970403)
if time.time() > float(expires_in):
    raise Exception(u"授权过期了,with expires_in:%s" % str(expires_in))
weibo = weiboAPI.weiboAPI(access_token=access_token, expires_in=expires_in, u_id=3041970403)
reqInterval(31)
logger.info(u"Sina Weibo 登录信息:%s" % weibo.getUserInfo()[u"name"])


def fetchUserMention():
    """
    获取微博上用户的订阅话题,之后更新订阅
    用户订阅话题的方式可以发布包含:@狗狗追踪 *订阅或取消订阅* #话题内容#的微博;或者转发上述微博

    TODO:
    1. mentions只获取了前50条
    """