Ejemplo n.º 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:
Ejemplo n.º 2
0
        else:
            postMsg = _msg + u' @' + watcher.weiboName

        logger.info(u'remind user:%s topic:%s update with msg:%s' % (watcher.weiboName, topicTitle, postMsg))
        res = {}
        try:
            res[u'status'] = _weibo.updateStatus(content=postMsg)
        except APIError, err:
            logger.warn(u'Update status failed:%t' % err.error)
        else:
            logger.info(u'Update status succeed!')
            if watcher_btw:
                watcher_btw.add_remind()
            watcher.add_remind()
            logger.info(u'add remind history for user: [%s, %s]' % (watcher.weiboName, watcher_btw))
            _status = djangodb.get_or_create_weibo(res[u'status'])
            topic.watcher_weibo.add(_status)
            logger.info(u'add watcherWeibo:%s to topic:%s' % (_status.text, topicTitle))
        finally:
            reqInterval(61)  # 间隔两次请求

    '''
    更新没有授权但发过或转发过微博的用户的状态
    '''
    for watcher in watcherWithStatusWithoutAuth:
        '''
        从watcherWithAuth中找个人评论他已有的微博提醒,找不到的话使用主帐号提醒
        '''
        if watcherWithAuth:
            _reminder = watcherWithAuth.pop()
            [access_token, expires_in] = djangodb.get_weibo_auth_info(_reminder.weiboId)
Ejemplo n.º 3
0
        mentions = weibo.getMentions(since_id=lastMentionId)
    except APIError, err:
        logger.exception(u"failed to get mentions:%s" % err.error)
        return False
    finally:
        reqInterval(31)

    mention_list = mentions[u"statuses"]
    logger.info(u"get %d mention:" % len(mention_list))

    # 提取用户@的消息并进行解读,保存话题
    # 这里拟序mention_list,先访问序号较小的微博
    for mention in reversed(mention_list):
        logger.info(u"mention:%s" % mention)
        # step 1: 提取并构造微博对象
        mweibo = djangodb.get_or_create_weibo(mention)
        if "retweeted_status" in mention:
            is_retweeted = True
            mweibo_retweeted = djangodb.get_or_create_weibo(mention[u"retweeted_status"])
            # 过滤掉用户转发自己的微薄
            # TODO 如果用户转发自己微薄重新订阅,则需要再设计
            if mweibo_retweeted.user.weiboId == mweibo.user.weiboId:
                continue
        else:
            is_retweeted = False
        logger.info(u"weibo:%s\nis_retweeted:%s" % (mweibo, is_retweeted))

        # step 2: 提取并构造用户对象
        muser = djangodb.get_or_create_account_from_weibo(mention[u"user"])
        if muser.is_robot():
            logger.warn(u"robot user:%s" % muser)