示例#1
0
def index(mid, page=None):
    user = login_user(session)
    client = get_client(user['access_token'], user['expires_in'])

    per_page = 200
    total_page = 0
    reposts_count = 0
    source_weibo = None
    if page is None:
        source_weibo = client.get('statuses/show', id=mid)
        mongo.db.all_source_weibos.update({'id': source_weibo['id']}, source_weibo, upsert=True)

        items2mongo(resp2item_v2(source_weibo))

        reposts_count = source_weibo['reposts_count']
        total_page = int(math.ceil(reposts_count * 1.0 / per_page))
        page = total_page
    else:
        source_weibo = mongo.db.all_source_weibos.find_one({'id': mid})
        if source_weibo is None:
            return ''
        reposts_count = source_weibo['reposts_count']
        total_page = int(math.ceil(reposts_count * 1.0 / per_page))

    try:
        reposts = client.get('statuses/repost_timeline', id=mid,
                             count=200, page=page)['reposts']

        # 如果reposts为空,且是最开始访问的一页,有可能是页数多算了一页,直接将页数减一页跳转
        if reposts == [] and total_page > 1 and page == total_page:
            return redirect(url_for('graph.index', mid=mid, page=page - 1))

        items = []
        for repost in reposts:
            items.extend(resp2item_v2(repost))
        items2mongo(items)
        for item in items:
            if isinstance(item, WeiboItem) and item['id'] != source_weibo['id']:
                item = item.to_dict()
                item['source_weibo'] = source_weibo['id']
                mongo.db.all_repost_weibos.update({'id': item['id']}, item, upsert=True)
    except RuntimeError:
        pass

    reposts = list(mongo.db.all_repost_weibos.find({'source_weibo': source_weibo['id']}))
    if reposts == []:
        return ''

    page_count = total_page - page + 1 if total_page >= page else 0
    tree, tree_stats = reposts2tree(source_weibo, reposts, per_page, page_count)
    graph, max_depth, max_width = tree2graph(tree)
    tree_stats['max_depth'] = max_depth
    tree_stats['max_width'] = max_width

    # 存储转发状态
    tree_stats['id'] = mid
    tree_stats['page'] = page
    mongo.db.tree_stats.update({'id': mid, 'page': page}, tree_stats, upsert=True, w=1)
    return graph
示例#2
0
def index():
    user = login_user(session)
    q = request.args.get('q', '')
    q = q.strip('@ \r\n\t')

    client = get_client(user['access_token'], user['expires_in'])
    try:
        target_user = client.get('users/show', screen_name=q)
        mongo.db.all_visited_users.update({'id': target_user['id']}, target_user, upsert=True)
        items2mongo(resp2item_v2(target_user))

        return redirect(url_for('search.weibos_by_uid_and_page', uid=target_user['id']))
    except RuntimeError:
        flash(u'您输入的昵称不存在,请重新输入')
        return redirect(url_for('simple.index'))
示例#3
0
def index(mid, page=None):
    if page is None:
        per_page = 200
        user = login_user(session)
        client = get_client(user['access_token'], user['expires_in'])

        source_weibo = client.get('statuses/show', id=mid)
        mongo.db.all_source_weibos.update({'id': source_weibo['id']}, source_weibo, upsert=True)

        items2mongo(resp2item_v2(source_weibo))

        reposts_count = source_weibo['reposts_count']
        total_page = int(math.ceil(reposts_count * 1.0 / per_page))
        page = total_page

        return redirect(url_for('show_graph.index', mid=mid, page=page))

    screen_name = session['screen_name']
    profile_image_url = session['profile_image_url']
    return render_template('graph.html', btnuserpicvisible='inline',
                           btnloginvisible='none',
                           screen_name=screen_name, profile_image_url=profile_image_url,
                           mid=mid,
                           page=page)
示例#4
0
def index(mid, page=None):
    client = get_client()

    per_page = 200
    total_page = 0
    reposts_count = 0
    source_weibo = None
    if page is None:
        source_weibo = client.get('/showBatch', ids=mid)['statuses'][0]
        items = resp2item_search(source_weibo, 8)
        items2mongo(items, weibo_mode='REPOST')

        reposts_count = source_weibo['reposts_count']
        total_page = int(math.ceil(reposts_count * 1.0 / per_page))
        page = total_page
    else:
        source_weibo = mongo.db.master_timeline_weibo_repost.find_one({'id': mid})
        if source_weibo is None:
            return ''
        reposts_count = source_weibo['reposts_count']
        total_page = int(math.ceil(reposts_count * 1.0 / per_page))
    
    if page == 0:
        return ''
        
    # try:
    reposts = client.get('/queryReList', tweetId=mid, count=per_page, page=page)['reposts']

    # 如果reposts为空,且是最开始访问的一页,有可能是页数多算了一页,直接将页数减一页跳转
    if reposts == [] and total_page > 1 and page == total_page:
        return redirect(url_for('graph.index', mid=mid, page=page - 1))

    items = []
    for repost in reposts:
        items.extend(resp2item_search(repost, 4))
    items2mongo(items, weibo_mode='REPOST')

    for item in items:
        if isinstance(item, WeiboItem_search) and item['id'] != source_weibo['id']:
            item = item.to_dict()
            item['source_weibo'] = source_weibo['id']
            mongo.db.master_timeline_weibo_repost.update({'id': item['id']}, item, upsert=True)
    #except RuntimeError:
    #    pass
    
    reposts = list(mongo.db.master_timeline_weibo_repost.find({'source_weibo': source_weibo['id']}))
    if reposts == []:
        return ''

    page_count = total_page - page + 1 if total_page >= page else 0
    tree, tree_stats = reposts2tree(source_weibo, reposts, per_page, page_count)
    sort_results, max_depth, max_width = tree2graph(tree)
    
    tree_stats['max_depth'] = max_depth
    tree_stats['max_width'] = max_width

    # 存储转发状态
    tree_stats['id'] = mid
    tree_stats['page'] = page
    mongo.db.tree_stats.update({'id': mid, 'page': page}, tree_stats, upsert=True, w=1)
    
    return sort_results
示例#5
0
def weibos_by_uid_and_page(uid, page=1):
    user = login_user(session)
    q = request.args.get('q', '')
    q = q.strip('@ \r\n\t')
    auto_redirect = request.args.get('auto_redirect')
    if auto_redirect:
        auto_redirect = int(auto_redirect)

    target_user = mongo.db.all_visited_users.find_one({'id': uid})
    tar_screen_name = target_user['screen_name']
    tar_profile_image_url = target_user['profile_image_url']
    tar_location = target_user['location']

    if q and auto_redirect and auto_redirect > 3:
        statuses = []
    else:
        try:
            client = get_client(user['access_token'], user['expires_in'])
            statuses = client.get('statuses/user_timeline', uid=uid, count=50, page=page)['statuses']
            items = []
            for status in statuses:
                items.extend(resp2item_v2(status))
            items2mongo(items)
            if q:
                temp_statuses = []
                for status in statuses:
                    if ('text' in status and q in status['text']) or ('retweeted_status' in status and q in status['retweeted_status']['text']):
                        temp_statuses.append(status)
                statuses = temp_statuses
                if statuses == []:
                    page += 1
                    if auto_redirect:
                        auto_redirect += 1
                    else:
                        auto_redirect = 1
                    return redirect(url_for('search.weibos_by_uid_and_page', uid=uid,
                                    page=page) + '?q=%s&auto_redirect=%s' % (q, auto_redirect))
        except RuntimeError:
            flash(u'获取微博信息失败,请刷新')
            statuses = []

    if statuses == []:
        flash(u'没有搜索到相关微博,请尝试下一页或者采用其他关键词')

    for i in xrange(len(statuses)):
        weibo_url = base62.weiboinfo2url(statuses[i]['user']['id'], statuses[i]['mid'])
        statuses[i]['weibo_url'] = weibo_url

    screen_name = session['screen_name']
    profile_image_url = session['profile_image_url']

    has_prev = True if page > 1 else False
    has_next = True  # 默认始终有下一页
    page_url = lambda page: url_for('search.weibos_by_uid_and_page', uid=uid,
                                    page=page)

    return render_template('weibolist.html', btnuserpicvisible='inline',
                           btnloginvisible='none',
                           screen_name=screen_name, profile_image_url=profile_image_url,
                           tar_screen_name=tar_screen_name,
                           tar_profile_image_url=tar_profile_image_url,
                           tar_location=tar_location,
                           statuses=statuses,
                           page=page,
                           has_prev=has_prev,
                           has_next=has_next,
                           page_url=page_url
                           )
示例#6
0
def index(mid, page=None):
    client = get_client()

    per_page = 200
    total_page = 0
    reposts_count = 0
    source_weibo = None
    if page is None:
        source_weibo = client.get('/showBatch', ids=mid)['statuses'][0]
        items = resp2item_search(source_weibo, 8)
        items2mongo(items, weibo_mode='REPOST')

        reposts_count = source_weibo['reposts_count']
        total_page = int(math.ceil(reposts_count * 1.0 / per_page))
        page = total_page
    else:
        source_weibo = mongo.db.master_timeline_weibo_repost.find_one(
            {'id': mid})
        if source_weibo is None:
            return ''
        reposts_count = source_weibo['reposts_count']
        total_page = int(math.ceil(reposts_count * 1.0 / per_page))

    if page == 0:
        return ''

    # try:
    reposts = client.get('/queryReList',
                         tweetId=mid,
                         count=per_page,
                         page=page)['reposts']

    # 如果reposts为空,且是最开始访问的一页,有可能是页数多算了一页,直接将页数减一页跳转
    if reposts == [] and total_page > 1 and page == total_page:
        return redirect(url_for('graph.index', mid=mid, page=page - 1))

    items = []
    for repost in reposts:
        items.extend(resp2item_search(repost, 4))
    items2mongo(items, weibo_mode='REPOST')

    for item in items:
        if isinstance(item,
                      WeiboItem_search) and item['id'] != source_weibo['id']:
            item = item.to_dict()
            item['source_weibo'] = source_weibo['id']
            mongo.db.master_timeline_weibo_repost.update({'id': item['id']},
                                                         item,
                                                         upsert=True)
    #except RuntimeError:
    #    pass

    reposts = list(
        mongo.db.master_timeline_weibo_repost.find(
            {'source_weibo': source_weibo['id']}))
    if reposts == []:
        return ''

    page_count = total_page - page + 1 if total_page >= page else 0
    tree, tree_stats = reposts2tree(source_weibo, reposts, per_page,
                                    page_count)
    sort_results, max_depth, max_width = tree2graph(tree)

    tree_stats['max_depth'] = max_depth
    tree_stats['max_width'] = max_width

    # 存储转发状态
    tree_stats['id'] = mid
    tree_stats['page'] = page
    mongo.db.tree_stats.update({
        'id': mid,
        'page': page
    },
                               tree_stats,
                               upsert=True,
                               w=1)

    return sort_results