SitePoint Sponsor

User Tag List

Results 1 to 14 of 14
  1. #1
    SitePoint Guru
    Join Date
    Jun 2009
    Posts
    823
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)

    Twitter Search API -- two questions......



    hi,

    I have two questions about Twitter Search API

    I've been working on this Twitter Search app, and I'm grateful for all the help I've gotten for it here, especially from fretburner....

    now I have two questions:

    1) I'm printing just five properties (text, date, user screen name, user location, and tweet url (which I composed with help from fb..;-)
    but sometimes none of the values for these properties contain the term that was searched for... I looked it up, there is apparently no way to tell Twitter Search API to search only in status "text"; that's really too bad.....;-o

    so in JavaScript before I print a record, I check to see if the value for "text" property contains the term that was searhed for, if it doesn't then I don't print it... but I have noticed that for some reason this sometimes leaves out even entries where the term does occur in "text" property.. (I would have to parse the JSON in PHP to filter this on the back-end... yikes...)

    2) I can't get JS to fulfill a conditional for when no results are returned... sometimes I search for something very unusual, and ask for only 2 results, to test...

    first I did this..

    Code:
    if (!response)  {
    		console.log('no results');
    	} else {
    			.........
    	}
    then I realized that even if no search results are returned, it still sends metadata... but "statuses" is empty.. so I did:

    Code:
    if (tweet.statuses == null )  {
    		console.log('no results -- ');
    	} else {
    		parse & print data.......
    	}
    doesn't print, even if results are empty... also tried this...

    Code:
    if (!tweet.statuses )  {  ..... }
    also didn't work....


    man, amazing how complicated a simple search app can be....

    thank you.....




  2. #2
    Community Advisor bronze trophy
    fretburner's Avatar
    Join Date
    Apr 2013
    Location
    Brazil
    Posts
    1,414
    Mentioned
    45 Post(s)
    Tagged
    12 Thread(s)
    Hey maya,

    1. Could you post the code you're using to try to filter the tweets?

    2. You can check for empty results by checking the length of the statuses array - if there are no results, it'll be 0:
    Code JavaScript:
    $.getJSON('search.php', {terms: terms}, function(response) {
        if (response.statuses.length === 0) {
            console.log('No results found');
        } else {
            // ...
        }
    }

  3. #3
    SitePoint Guru
    Join Date
    Jun 2009
    Posts
    823
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    hi fretburner...

    code you posted doesn't work... :-(

    entire code is now here
    mayacove.com/dev/tw/twitter_search.zip
    (need to rename js files...)

    both my filtering of tweets and testing for whether results are empty are not working
    (both indicated with comments...)

    also, fb, a general JS question:
    I have JS in a separate .js file now (lots of code by now...;-)
    but I don't understand why this whole thing doesn't work if I put the call to the .js file in header like I always do... I just don't get this...
    why does it only work if call to .js file is in <body>? I find this slightly disturbing...;-) I have never done anything which only works if call to .js file is in <body> instead of in <head>...

    thank you very much for your help, fb....

  4. #4
    SitePoint Guru
    Join Date
    Jun 2009
    Posts
    823
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)

    just posted again... had forgotten one .js file.. you would have gotten an error...
    (found this neat little jQuery plugin for highlighting the terms searched.... very cool....;-)

  5. #5
    Community Advisor bronze trophy
    fretburner's Avatar
    Join Date
    Apr 2013
    Location
    Brazil
    Posts
    1,414
    Mentioned
    45 Post(s)
    Tagged
    12 Thread(s)
    Quote Originally Posted by maya90 View Post
    code you posted doesn't work... :-(
    You need to check the length of the statuses array before you try to loop over it, as I showed in my example. If the search doesn't return any results (i.e. the status array is empty) then none of the code inside the loop will be run.

    Quote Originally Posted by maya90 View Post
    both my filtering of tweets and testing for whether results are empty are not working
    Twitter returns the tweets that match one or more of the terms you sort for, so you'll get better results if you try to match individual words rather than the whole phrase:

    Code JavaScript:
    // Split the search terms into separate words
    terms = terms.split(' ');
     
    $.each(response.statuses, function(index, tweet) {
        var containsTerms = false;
     
        // Loop over the terms, and check if any of them appear in the tweet text
        $.each(terms, function(index, term){
            if (tweet.text.indexOf(term) > -1) containsTerms = true;
        });
     
        // If any of the terms are present..
        if (containsTerms) {
            // ..display the tweet
        }
    }

    I've saved the whole thing as a fiddle, here: http://jsfiddle.net/8BBsq/, so you can see it in context.

    Quote Originally Posted by maya90 View Post
    also, fb, a general JS question:
    I have JS in a separate .js file now (lots of code by now...;-)
    but I don't understand why this whole thing doesn't work if I put the call to the .js file in header like I always do... I just don't get this...
    why does it only work if call to .js file is in <body>? I find this slightly disturbing...;-) I have never done anything which only works if call to .js file is in <body> instead of in <head>...
    When you link JS files within the <head> tags the scripts are loaded and executed before the page content has finished loading, so any elements you try to manipulate or attach handlers to won't exist yet. For this reason, people often wrap their scripts in a jQuery function which only runs once the page has finished loading:
    Code JavaScript:
    $(function(){ 
        // Your code
    });
    But it's better just to include your JS files just before the closing </body> tag, as this ensures the page content is loaded first, doesn't require jQuery, and doesn't block the rest of the page content from loading while they load (as scripts linked in the <head> section do).

  6. #6
    SitePoint Guru
    Join Date
    Jun 2009
    Posts
    823
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)

    thank you so much, fb!!

    both your solutions worked.... (yes of course testing for whether array is empty is best done before you loop through the array...;-)

    regarding the placing of the JS code,

    you didn't wrap the code in this $(function(){, but in (function() { ... it's different, no? ;-)
    at any rate I wrapped my code in $(document).ready(function() { (what I usu. do) and placed the call to it in <head> and it works fine.....
    (just like you say, wrapping the code in $(function(){ should do it, right? (it's short for $(document).ready(function(), yes?)

    thanks again, fb......

  7. #7
    SitePoint Guru
    Join Date
    Jun 2009
    Posts
    823
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)

    hi fretburner,

    so am trying to add another feature, which of course means I'm running into more problems....:-(

    am adding a dropdown so user can choose how many entries....
    very simple thing with a <select> dropdown

    Code:
    $countR = filter_input(INPUT_GET, 'count', FILTER_SANITIZE_ENCODED);
    Code:
        $tweets = $twitter->get("search/tweets.json?q=$terms&result_type=recent&count=$countR&include_entities=true");
    (wanted to highlight $countR variable here....;-) why can't you change font-color in this forum????)

    this always prints only ONE record, and in my JS code, where I print response.statuses.length to see how many entries are printed, it always prints "15"
    but if I hardcode number of entries, like this,

    Code:
        $tweets = $twitter->get("search/tweets.json?q=$terms&result_type=recent&count=4&include_entities=true");
    in PHP then everything works fine....


    thank you...

  8. #8
    SitePoint Guru
    Join Date
    Jun 2009
    Posts
    823
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)

    I just realized that number of returns does not match

    response.statuses.length

    i.e., if I request 8 records, (by hard-coding it in PHP), and it returns less records than that, it still prints "8 records"

    why does it do that????

    if it only prints 4 records (because presumably it only FOUND 4 records) why does it always print the no. of records I request in PHP in query string instead of actual no. of records it found?

    thank you....


    PS: I think this has to do with fact I'm only printing tweets that contain searched-for term...

    but still: it prints "15 entries" every single time I put param from request for 'count' param in that PHP url....

    and I mean every single time...

    (again "15 entries" comes form response.statuses.length
    if finds exactly 15 tweets every single time, when the number of results requested is dynamic?
    even if it prints only one tweet, response.statuses.length prints "15"....)


    so there's another problem here.... (options are show 25, 45, or 65 tweets...)



  9. #9
    Community Advisor bronze trophy
    fretburner's Avatar
    Join Date
    Apr 2013
    Location
    Brazil
    Posts
    1,414
    Mentioned
    45 Post(s)
    Tagged
    12 Thread(s)
    At I guess, I'd say that API is returning eight tweets, but only four are displayed because of the code we added to filter out tweets where the text doesn't contain the search terms.

  10. #10
    SitePoint Guru
    Join Date
    Jun 2009
    Posts
    823
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    thank you fretburner.... yes I realized that after I posted my question.. but I think there's another problem..

    pls see my amended post above..

    and again, thank you very much...

  11. #11
    Community Advisor bronze trophy
    fretburner's Avatar
    Join Date
    Apr 2013
    Location
    Brazil
    Posts
    1,414
    Mentioned
    45 Post(s)
    Tagged
    12 Thread(s)
    Can you post a link to your updated code please? Would be good to see everything in context

  12. #12
    SitePoint Guru
    Join Date
    Jun 2009
    Posts
    823
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    actually I think I need to forget letting the user pick how many entries.... the no. of records printed will never match requested count.....

    (Twitter API refusing to add a feature to search only in tweet text is throwing a huge monkey-wrench into my thingie here... too bad.....)

    I don't know how many records to print before my web page starts slowing down and not performing......


    I confess this thing was for a test for a job, that I got about three weeks ago.. they wanted to me to write a Twitter Search app in ONE HOUR.... lol...

    so obviously I didn't get the job....;-) but I used the occasion to finally start learning Twitter API, which I had wanted to do for a long time...
    (and after I'm done with this one want to do a classic twitter feed... have little "widgets" with twitter feeds from my favorite accounts, just as an exercise... (main thing I will need to do for that is find out how to auto-refresh....)


    my code is here, http://www.mayacove.com/dev/tw/twitter_search.zip
    (if there's no way to match requested no. of records with actual records printed I think I need to forget about that one...
    even if there is a way to count how many records I'm actually printing, it will never match requested no. of records....)

    once again, fb, thank you very very much for your help...

  13. #13
    Community Advisor bronze trophy
    fretburner's Avatar
    Join Date
    Apr 2013
    Location
    Brazil
    Posts
    1,414
    Mentioned
    45 Post(s)
    Tagged
    12 Thread(s)
    You just need to make a small change to your JS to ensure that the count gets passed to the PHP script:

    Code JavaScript:
    $.getJSON('search_results.php', {terms: terms, count: count}, function(response) {

    As you say though, because you're filtering the tweets there's no way to ensure that you actually end up with the requested number of tweets to display.
    Last edited by fretburner; Sep 8, 2013 at 05:30. Reason: Added code highlighting

  14. #14
    SitePoint Guru
    Join Date
    Jun 2009
    Posts
    823
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    thanks again Fretburner!!!

    :-))


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •