1

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

this is my 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);
                }

             }  

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

anjel
  • 1,355
  • 4
  • 23
  • 35
  • What are you actually trying to do? What do you mean by "consecutive requests"? – Bergi Sep 16 '12 at 14:51
  • What is you `news_array`? Where did you define `user_tweets`? What is that functin `tweets` good for? – Bergi Sep 16 '12 at 14:52
  • i dont mean anything in particular. i just have an array of news tweets and i want to get the text from each of those tweets and make a request in twitter to find tweets related to that text and save it back to another array. the thing is i want make these requests without using a loop and i dont know how – anjel Sep 16 '12 at 14:57
  • What do you mean by "without a loop"? Recursion? If you want to run through that array, you need one of them (and they're very similiar) – Bergi Sep 16 '12 at 15:06
  • the problem is after i put them on a div they will appear multiple times – anjel Sep 16 '12 at 15:15
  • yes i mean without the recursion – anjel Sep 16 '12 at 15:18
  • The variables are not really declared in the scope of the `news_tweet` function, are they? – Bergi Sep 16 '12 at 16:12
  • And no, you can't use arrays without looping. – Bergi Sep 16 '12 at 16:13
  • yes the variables are declared inside the news_tweet function – anjel Sep 16 '12 at 16:52
  • So how can you try to access `full_array` and `user_tweets` from inside the `find_tweets` function? – Bergi Sep 17 '12 at 07:37

1 Answers1

1

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.

I guess re-initializing

var user_tweets = [];

inside the find_tweets function would help something.


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

Usage:

news_tweets(…).done(function callback(full_array) {
    // do something with all the objects
});
Bergi
  • 630,263
  • 148
  • 957
  • 1,375
  • i just need to access the user_tweets array outside the recursion – anjel Sep 16 '12 at 17:14
  • You don't have any recursion? – Bergi Sep 17 '12 at 07:36
  • You still have not explained what *you* actually *want* to do. – Bergi Sep 17 '12 at 10:43
  • i want to access those variables like user ,date, profile_img outside the $.getJSON callback function and store them into the user_tweets array. THIS SHOULD MAKE EVERYTHING CLEAR NOW !!!! forget about combine_arrays(news_array, user_tweets); – anjel Sep 17 '12 at 11:34
  • it seems that u know everything!but how can i get the full_array? Do i call the function with this line news_tweets(…).done(function callback(full_array) { console.log(full_array) }); – anjel Sep 17 '12 at 14:15
  • sorry for getting this stupid questions from me but my knowledge for JavaScript is not that big – anjel Sep 17 '12 at 14:16
  • This is just some advanced callback handling - you might read the docs I've linked. There are two callbacks (both executed when everything is loaded) - the first one has access to the two arrays and may combine them or do something else. The second one (independent from the function) gets as the argument what the first has returned. – Bergi Sep 17 '12 at 15:18
  • can you explain what is happening here and how to access the full array or the other ones? thanks – anjel Sep 18 '12 at 16:16
  • What exactly don't you understand, have you read the docs for the deferred thing? What do you mean by "how to access the array"? – Bergi Sep 18 '12 at 16:24
  • Bergi now i understand a bit ..can you check this code..i made a different one to see if it is working but it gives an error saying query is not defined http://jsfiddle.net/q5QY9/1/ – anjel Sep 18 '12 at 18:47
  • Could you create a fiddle that actually invokes the function (this one does not execute anything, and does not complain therefore). – Bergi Sep 18 '12 at 19:00
  • http://jsfiddle.net/LckKP/3/ Now is working ..Thanks man.thank you very much for the help!!:) – anjel Sep 18 '12 at 21:28
  • The first one does not work without that global variable, which should be avoided. The problem with my code was that jQuery didn't automatically recognize it needed to do a JSONP request on the cross domain. I added a `callback=?` parameter (and an error callback) and it works: http://jsfiddle.net/AwtkJ/2/ – Bergi Sep 19 '12 at 14:42
  • yeah but this is only outputting the news_array and not the full_array.can you see it? – anjel Sep 19 '12 at 15:22
  • Oh, right - I forgot to `apply` the `$.when` function and instead called it with an array. [Does work now](http://jsfiddle.net/AwtkJ/3/) – Bergi Sep 19 '12 at 16:07
  • still not working properly cause if you see into the news_array there is only one tweet instead there should be 3 based on the count variable we set in the first request. – anjel Sep 19 '12 at 16:36
  • and its not working on my local server.dont know why but is showing user_tweets as an empty array – anjel Sep 19 '12 at 16:49
  • Um, I get three objects in the `news_array` in that fiddle. How is your local server different from the fiddle? – Bergi Sep 19 '12 at 17:32
  • Hello @Bergi can you check this question which I posted today? http://stackoverflow.com/questions/12571093/wrong-output-from-array#comment16937422_12571093 – anjel Sep 24 '12 at 22:09