如何申请Twitter的API,而无需输入递归 [英] how to request twitter api without entering a recursion

查看:104
本文介绍了如何申请Twitter的API,而无需输入递归的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何人都知道如何让我的请求基于文本查询Twitter的API,而无需使用递归。

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

这是我的code

        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(你好),或尝试将两个数组都执行三次连接。

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

推荐答案

您似乎只有一个的 news_array user_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 功能将有助于东西。

您不能回调外部访问AJAX的数据。相反,你需要等到所有的异步请求得到解决。我建议使用 jQuery的Deferred对象这使得处理这类事情要容易得多:

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天全站免登陆