簡體   English   中英

如何在不輸入遞歸的情況下請求Twitter API

[英]how to request twitter api without entering a recursion

任何人都知道如何在不使用遞歸的情況下基於文本查詢向Twitter API發出請求。

這是我的代碼

        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”)或嘗試連接兩個數組時,一切都會執行三次。

您似乎只有news_arrayuser_tweets數組的一個實例。 在這些上,您可以推送所有api查詢的結果。 但是,您會從一個循環中對整個數組調用combine_arrays函數(每次搜索后給您一組新的結果)-在某些項上運行多次。

我想重新初始化

var user_tweets = [];

find_tweets函數內部會有所幫助。


您不能在回調外部訪問ajax數據。 相反,您將需要等待,直到所有異步請求都得到解決。 我建議使用jQuery的Deferred對象 ,它使處理這些事情變得更加容易:

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
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM