Exemplo n.º 1
0
def search():
    user = login_user(session)
    if user:
        client = Client(APP_KEY, APP_SECRET, CALLBACK_URL)
        client.set_token(user["access_token"])

        t = request.args.get('t', '')
        q = request.args.get('q', '')
        p = request.args.get('p', 1)
        u = request.args.get('u', 0)

        q = q.strip("@ \r\n\t")
        t = t.strip("@ \r\n\t")
        p = int(p)

        if t != '':
            retry = 0
            page = p
            n_page = p + 3
            t_statuses = []
            tar_screen_name = None
            tar_profile_image_url = None
            tar_location = None

            while 1:
                try:
                    if retry > 5:
                        break
                    statuses = client.get('statuses/user_timeline', uid=u, count=100, page=page)["statuses"]
                    weibo2db.statuses(statuses)
                    if tar_screen_name is None and len(statuses) > 0:
                        tar_profile_image_url = statuses[0]["user"]["profile_image_url"]
                        tar_screen_name = statuses[0]["user"]["name"]
                        tar_location = statuses[0]["user"]["location"]
                    for status in statuses:
                        if t in status["text"] or [t in status["retweeted_status"]["text"]
                                                   if "retweeted_status" in status else False][0]:
                            t_statuses.append(status)

                    if page == n_page:
                        break
                    else:
                        page += 1
                except Exception, e:
                    app.logger.error(e)
                    retry += 1

            if len(t_statuses) == 0:
                flash(u"没有搜索到相关微博,请尝试下一页或者采用其他关键词")

            statuses = t_statuses
            p = page
        else:
            try:
                target_user = client.get('users/show', screen_name=q)
            except:
                flash(u"您输入的昵称不存在,请重新输入")
                return redirect(url_for('index'))

            u = target_user["id"]
            page = p
            tar_screen_name = target_user["screen_name"]
            tar_profile_image_url = target_user["profile_image_url"]
            tar_location = target_user["location"]

            try:
                statuses = client.get('statuses/user_timeline', uid=u, count=50, page=page)["statuses"]
                weibo2db.statuses(statuses)
            except:
                flash(u"获取微博信息失败,请刷新")
                statuses = []

        for i in xrange(len(statuses)):
            weibo_url = "http://weibo.com/" \
                + str(statuses[i]["user"]["id"]) \
                + "/" + base62.mid_to_str(statuses[i]["mid"])
            statuses[i]["weibo_url"] = weibo_url

        screen_name = session["screen_name"]
        profile_image_url = session["profile_image_url"]
        return render_template('weibolist.html', btnuserpicvisible='inline',
                               btnloginvisible='none', t=t, q=q, p=int(p), u=u,
                               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)
Exemplo n.º 2
0
def status():
    user = login_user(session)
    if user is None:
        return ""

    client = Client(APP_KEY, APP_SECRET, CALLBACK_URL)
    client.set_token(user["access_token"])

    id = request.args.get('id', '')
    since_id = request.args.get('since_id', 0)

    reposts, source_weibo, since_id = load_reposts(app, weibo2db, r, client, id, since_id)
    if len(reposts) == 0:
        return ""

    #root
    tree_nodes = []
    node = source_weibo["user"]["name"]
    location = source_weibo["user"]["location"]
    datetime = source_weibo["created_at"]
    img_url = source_weibo["user"]["profile_image_url"]
    weibo_url = "http://weibo.com/" + \
        str(source_weibo["user"]["id"]) + \
        "/" + base62.mid_to_str(source_weibo["mid"])

    tree_nodes.append(Tree(node, location, datetime, int(id), img_url, weibo_url))

    for repost in reposts:
        try:
            node = repost["user"]["name"]
            wid = repost["id"]
            img_url = repost["user"]["profile_image_url"]
            location = repost["user"]["location"]
            datetime = repost['created_at']
            weibo_url = "http://weibo.com/" + \
                str(repost["user"]["id"]) + \
                "/" + base62.mid_to_str(repost["mid"])
            tree_nodes.append(Tree(node, location, datetime, wid, img_url, weibo_url))
        except:
            app.logger.error(repost)
            continue

        repost_users = re.findall(r'//@(\S+?):', repost["text"])
        if len(repost_users):
            flag = True
            for node in tree_nodes[::-1]:
                if node.node == repost_users[0]:
                    node.append_child(tree_nodes[-1])
                    flag = False
                    break

            if flag:
                tree_nodes[0].append_child(tree_nodes[-1])
        else:
            tree_nodes[0].append_child(tree_nodes[-1])

    dt, max_width = buchheim.buchheim(tree_nodes[0])

    gexf = Gexf("MOON_CLJ", "haha")
    graph = gexf.addGraph("directed", "static", "weibo graph")
    graph.addNodeAttribute("img_url", type="URI", force_id="img_url")
    graph.addNodeAttribute("name", type="string", force_id="name")
    graph.addNodeAttribute("location", type="string", force_id="location")
    graph.addNodeAttribute("datetime", type="string", force_id="datetime")
    graph.addNodeAttribute("repost_num", type="integer", force_id="repost_num")
    graph.addNodeAttribute("weibo_url", type="URI", force_id="weibo_url")

    rank = node_rank(tree_nodes[0])
    add_node_edge(dt, graph, rank, Count(), max_width=max_width)

    return etree.tostring(gexf.getXML(), pretty_print=True, encoding='utf-8', xml_declaration=True)