如何在不输入递归的情况下请求 twitter api [英] how to request twitter api without entering a recursion

查看:43
本文介绍了如何在不输入递归的情况下请求 twitter api的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何人都知道如何在不使用递归的情况下根据文本查询向 twitter api 发出请求.

Anyone knows how can i make requests to twitter api based on text queries without using a recursion.

这是我的代码

        function news_tweets(query, user_id, count) {
            news_array = [];
            user_tweets = [];
            full_array = [];
            $.getJSON("https://api.twitter.com/1/statuses/user_timeline.json?include_entities=true&include_rts=false&user_id=" + user_id +
             "&count=" + count + "&callback=?",

            function (data) {
                $.each(data, function (i, item) {
                    var user = item.user.name;
                    var date = item.created_at;
                    var profile_img = item.user.profile_image_url;
                    var text = item.text;
                    var url = (item.entities.urls.length > 0 ? item.entities.urls[0].url : '');
                    news_array.push({
                        news_user: user,
                        news_date: date,
                        news_profile_img: profile_img,
                        news_text: text,
                        news_url: url
                    });
                });
                find_tweets(news_array);

            });
        }

        function find_tweets(news_array) {
            for (var i in news_array) {
                var news_text = news_array[i].news_text;
                $.getJSON("http://search.twitter.com/search.json?q=" + news_text + 
                "&rpp=10&include_entities=true&result_type=mixed&callback=?",

                function (data) {
                    $.each(data.results, function (i, item) {
                        var user = item.from_user;
                        var user_id = item.from_user_id;
                        var date = item.created_at;
                        var user_profile_img = item.profile_image_url;
                        var text = item.text;
                        var url = (item.entities.urls.length > 0 ? item.entities.urls[0].url : '');
                        user_tweets.push({
                            user: user,
                            user_id: user_id,
                            date: date,
                            user_profile_img: user_profile_img,
                            text: text
                        });
                    });
                    combine_arrays(news_array, user_tweets);
                });
            }

            function combine_arrays(news_array, user_tweets) {
                full_array = news_array.concat(user_tweets); console.log(full_array);
                }

             }  

当我使用 console.log("hello") 或尝试连接两个数组时,一切都执行了 3 次.

when i use console.log("hello") or try to connect the two arrays everything is executed three times.

推荐答案

您似乎只有 news_arrayuser_tweets 数组的一个实例.在那些上,您推送 api 查询的所有结果.然而,您从循环中对整个数组调用 combine_arrays 函数(每次搜索后都会为您提供一组新结果) - 在某些项目上运行多次.

You seem to have only one instance of the news_array and user_tweets arrays. On those, you push all the result of your api queries. Yet, you call the combine_arrays function on the whole arrays from a loop (each time after the search gave you a new set of results) - running multiple times over some of the items.

我想重新初始化

var user_tweets = [];

find_tweets 函数中会有所帮助.

inside the find_tweets function would help something.

不能访问回调外的ajax数据.相反,您需要等到所有异步请求都得到解决.我建议使用 jQuery 的延迟对象,这使得处理这些事情变得更加容易:

You can't access the ajax data outside the callback. Instead, you will need to wait until all the asynchronous requests are resolved. I recommend to use jQuery's Deferred object which makes handling such things much easier:

function news_tweets(query, user_id, count) {
    var news_array = [],
        user_tweets = [];
    return $.getJSON("https://api.twitter.com/1/statuses/user_timeline.json", {
        include_entities: "true",
        include_rts: "false",
        user_i: user_id,
        count: count
    }).then(function (data) {
        return $.when.apply(null, $.map(data, function (item) {
            news_array.push({
                news_user: item.user.name,
                news_date: item.created_at,
                news_profile_img: item.user.profile_image_url,
                news_text: item.text,
                news_url: item.entities.urls.length ? item.entities.urls[0].url : ''
            });
            return $.getJSON("http://search.twitter.com/search.json", {
                q: item.text,
                rpp: 10,
                include_entities: "true",
                result_type: "mixed"
            }).done(function (data) {
                $.each(data.results, function (i, item) {
                    user_tweets.push({
                        user: item.from_user,
                        user_id: item.from_user_id,
                        date: item.created_at,
                        user_profile_img: item.entities.urls.length ? item.entities.urls[0].url : '',
                        text: item.text
                    });
                });
            });
        }));
    }).then(function() {
        // this callback is executed [once] when all requests are done
        // and the user_tweets array is filled
        // arguments is an array of all search request results
        var full_array = news_array.concat(user_tweets);
        console.log(full_array);
        return full_array;
    })
}

用法:

news_tweets(…).done(function callback(full_array) {
    // do something with all the objects
});

这篇关于如何在不输入递归的情况下请求 twitter api的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆