Example #1
0
def broadcastTopic(topicTitle):
    try:
        logger.debug(u'Start broadcast 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

    alreadyFetchedWeibos = topic.relevant_weibo.all()

    fetchedWeibos = weibo.getRelevantWeibo(topicTitle=topicTitle, count=50)['statuses']
    logger.info(u'feteched %d weibo about topic:%s' % (len(fetchedWeibos), topicTitle))
    for awb in fetchedWeibos:
        aweibo = djangodb.get_or_create_weibo(awb)
        if aweibo.comments_count < 200 and aweibo in alreadyFetchedWeibos:
            # 这条微薄已经推广过了,由于时间关系,后续的微薄理论上也应该推广过了
            continue
            break
        else:
            topic.relevant_weibo.add(aweibo)

        auser = djangodb.get_or_create_account_from_weibo(awb['user'])
        if aweibo.comments_count < 200 and auser in topic.watcher.all():
            # 该用户已经使用服务,不需要推广
            continue
        if aweibo.comments_count < 200 and len(auser.remind_history.strip()) > 0:
            # 已经推广过至少一次或正在使用服务
            continue

        topic.relevant_user.add(auser)
        # 评论推广
        _shorturl = weibo.getShortUrl(Conf.get_timeline_url(topic.id))
        reqInterval(10)
        msg = u'#%s# %s 『%s』 ^ ^' % (topicTitle, topic_news.title, _shorturl)

        try:
            weibo.postComment(weibo_id=aweibo.weibo_id, content=msg)
        except APIError, err:
            logger.warn(u'comment failed:\t%s' % (err.error))
            if err.error == u'target weibo does not exist!':
                topic.relevant_user.remove(auser)
                topic.relevant_weibo.remove(aweibo)
                aweibo.delete()
                logger.info(u'remove relevant user:%s and delete weibo:%s' % (auser, aweibo))
        except:
Example #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:
Example #3
0
            djangodb.add_task(topic=mtopic, type=u"subscribe")
        else:
            logger.debug(u"step4: topic:%s already in track" % mtopictitle)
            is_new_topic = False

        mtopic.watcher.add(muser)
        mtopic.watcher_weibo.add(mweibo)
        mtopic.save()

        # step 5: 提醒用户订阅成功
        try:
            if is_new_topic:
                shorturl = weibo.getShortUrl(Conf.site_url)
                remind_msg = u"订阅成功,我们正在整理资料,之后会将该事件的来龙去脉和最新消息推送给您! 详见:『%s』" % shorturl
            else:
                shorturl = weibo.getShortUrl(Conf.get_timeline_url(topicId=mtopic.id))
                remind_msg = u"订阅成功,您可以到『%s』获取该事件的来龙去脉,同时我们会将发展动态即时推送给您~" % shorturl
        except APIError, err:
            logger.exception(u"Get short url exception:%s" % err.error)
            remind_msg = u"订阅成功~"
        finally:
            reqInterval(31)

        try:
            sucPostCmt = weibo.postComment(mweibo.weibo_id, remind_msg)
        except APIError, err:
            sucPostCmt = False
            logger.info(u"fail to post comment for weibo:%s" % mweibo)
            if err.error == "target weibo does not exist!":
                mtopic.watcher_weibo.remove(mweibo)
                mtopic.watcher.remove(muser)