0

I am trying get paged json responses from Topsy (http://code.google.com/p/otterapi/) and am having problems merging the objects. I want to do this in browser as the api rate limit is per ip/user and to low to do things server side.

Here is my code. Is there a better way? Of course there is because this doesn't work. I guess I want to get this working, but also to understand if there is a safer, and/or more efficient way.

The error message I get is ...

TypeError: Result of expression 'window.holdtweetslist.prototype' [undefined] is not an object.

Thanks in advance.

Cheers Stephen

    $("#gettweets").live('click', function(event){ 

        event.preventDefault();
        getTweets('stephenbaugh');

    });



    function getTweets(name) {

        var MAX_TWEETS = 500;
        var TWEETSPERPAGE = 50;
        var BASE = 'http://otter.topsy.com/search.json?type=tweet&perpage=' + TWEETSPERPAGE + '&window=a&nohidden=0&q=@' + name + '&page=1';

        var currentpage = 1;
            alert(BASE);

        $.ajax({
            dataType: "json", 
            url: BASE,
            success: function(data) {

                window.responcesreceived = 1;
                var response=data.response;
                alert(response.total);
                window.totalweets = response.total;

                window.pagestoget = Math.ceil(window.totalweets/window.TWEETSPERPAGE);

                window.holdtweetslist = response.list;

                window.holdtweetslist.prototype.Merge = (function (ob) {var o = this;var i = 0;for (var z in ob) {if (ob.hasOwnProperty(z)) {o[z] = ob[z];}}return o;});

        //  alert(data);
            ;;  gotTweets(data);

                var loopcounter = 1;
                do
                {
                    currentpage = currentpage + 1;
                    pausecomp(1500);
                    var BASE = 'http://otter.topsy.com/search.json?type=tweet&perpage=' + TWEETSPERPAGE + '&window=a&nohidden=0&q=@' + name + '&page=' + currentpage;
alert(BASE);
                    $.ajax({dataType: "json", url: BASE, success: gotTweets(data)});
                }
                while (currentpage<pagestoget);

            }
        });
    };

    function gotTweets(data)
    {
        window.responcesreceived = window.responcesreceived + 1;
        var response = data.response;
        alert(response.total);
        window.holdtweetslist.Merge(response.list);
        window.tweetsfound = window.tweetsfound + response.total;
        if (window.responcesreceived == window.pagestoget) {
            // sendforprocessingsendtweetlist();
            alert(window.tweetsfound);
        }
    }

3 Answers 3

1

You are calling Merge as an static method, but declared it as an "instance" method (for the prototype reserved word).

Remove prototype from Merge declaration, so you'll have:

window.holdtweetslist.Merge = (function(ob)...

This will fix the javascript error.

Sign up to request clarification or add additional context in comments.

6 Comments

Thanks Edgar. I'm now getting a different error. I think I am still pretty confused between arrays, and json objects etc ... ReferenceError: Can't find variable: ARRAY
You're welcome. Mmm.. does it say "ARRAY" literally? PS: Please upvote my answer if you think it was useful. Thanks
I've up'd the answer, but it doesn't actually solve it. Yes that is the error message I get now. Cheers
Could you test it in firefox, and before loading the page press ctrl-shift-j (to open the error console), and tell me in which line the error occurs?
Jooks like it is processing the json ... [Break On This Error] ARRAY(0xf265748)({"request":{"paramete... bit.ly/epeAzx"}],"offset":0}} search...0121441 (line 1)
|
1

This is Vipul from Topsy. Would you share the literal JSON you are receiving? I want to ensure you are not receiving a broken response.

2 Comments

This is what I get if I run it in the browser.... Sorry I can't pot the responce as there aren't enough characters. But you can see it here otter.topsy.com/…
Thanks for noticing the post Vipul BTW. I think my script is wrong not the api. Cool service you have, cheers. Thanks
0

THanks to Edgar and Vipul for there help. Unfortunately they were able to answer my questions. I have managed to work out that the issue was a combination of jquery not parsing the json properly and needing to use jsonp with topsy.

Here is a little test I created that works.

Create a doc with this object on it ....

<a href="#" id="gettweets">RUN TEST</a>

You will need JQUERY

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js"></script>

And put the following in a script too. The is cycle through the required number of tweets from Topsy.

Thanks again everyone.

$("#gettweets").live('click', function(event){ 

    event.preventDefault();
    getTweets('stephenbaugh');

});


    var MAX_TWEETS = 500;
    var TWEETSPERPAGE = 50;
    var BASE = 'http://otter.topsy.com/search.json';
    var currentpage;
    var responcesreceived;
    var totalweets;
    var pagestoget;
    var totalweets;
    var TWEETSPERPAGE;
    var holdtweetslist = [];
    var requestssent;
    var responcesreceived;
    var tweetsfound;
    var nametoget;


function getTweets(name) {

    nametoget=name;
    currentpage = 1;
    responcesreceived = 0;
    pagestoget = 0;

    var BASE = 'http://otter.topsy.com/search.js?type=tweet&perpage=' + TWEETSPERPAGE + '&window=a&nohidden=0&q=@' + nametoget + '&page=1';
    $('#gettweets').html(BASE);
    $.ajax({url: BASE, 
    dataType: 'jsonp',
    success : function(data) {
            getalltweets(data);
        }
    });
};


function getalltweets(data) {

        totalweets = data.response.total;
        $('#gettweets').append('<p>'+"total tweets " + totalweets+'</p>');
        $('#gettweets').append('<p>'+"max tweets " + MAX_TWEETS+'</p>');
        if (MAX_TWEETS < totalweets) {
            totalweets = 500
        }
        $('#gettweets').append('<p>'+"new total tweets " + totalweets+'</p>');


        gotTweets(data);



        pagestoget = Math.ceil(totalweets/TWEETSPERPAGE);

        var getpagesint = self.setInterval(function() { 

            currentpage = ++currentpage;

            var BASE = 'http://otter.topsy.com/search.js?type=tweet&perpage=' + TWEETSPERPAGE + '&window=a&nohidden=0&q=@' + nametoget + '&page=' + currentpage;
            $.ajax({url: BASE, 
                dataType: 'jsonp',
                success : function(data) {
                    gotTweets(data);
                    }
            });
            if (currentpage == pagestoget) {
                $('#gettweets').append('<p>'+"finished sending " + currentpage+ ' of ' + pagestoget + '</p>');
                clearInterval(getpagesint);
            };

        }, 2000);
};


function gotTweets(data)
{

        responcesreceived = responcesreceived + 1;
        holdlist = data.response.list;

        for (x in holdlist)
        {
            holdtweetslist.push(holdlist[x]);
        }

    // var family = parents.concat(children);


        $('#gettweets').append('<p>receipt # ' + responcesreceived+' - is page : ' +data.response.page+ ' array length = ' + holdtweetslist.length +'</p>');
        //      holdtweetslist.Merge(response.list);
        tweetsfound = tweetsfound + data.response.total;
        if (responcesreceived == pagestoget) {
        // sendforprocessingsendtweetlist();
            $('#gettweets').append('<p>'+"finished receiving " + responcesreceived + ' of ' + pagestoget + '</p>');
        }

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.