Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Send JSON data to Perl scalar

by perl.j (Pilgrim)
on Dec 21, 2012 at 15:20 UTC ( #1009922=perlquestion: print w/ replies, xml ) Need Help??
perl.j has asked for the wisdom of the Perl Monks concerning the following question:

I currently have the following code which captures the number of followers for a particular Twitter account, and prints it to a webpage:

<html> <head> <script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.m +in.js"></script> <script type="text/javascript"> $(document).ready(function() { var twitterusername = 'ESPN'; $.getJSON('http://twitter.com/users/' + twitterusername + '.json?c +allback=?', function(data) { $('#followers').html(data.followers_count); document.write(twitterusername, "# of Followers: ", data.follo +wers_count); }); }); </script> </head> <body> <div id='followers'></div> </body> </html>

How can I take the number of followers, and store it into a Perl variable?

--perl.j

Comment on Send JSON data to Perl scalar
Download Code
Re: Send JSON data to Perl scalar
by Corion (Pope) on Dec 21, 2012 at 15:24 UTC

    I would look at the HTTP requests that this page makes and replicate them using LWP::Simple and JSON::Any.

    An alternative approach would be to use a module or program that understands Javascript and acts like a browser, for example WWW::Scripter or WWW::Mechanize::Firefox, and then read the information out of the resulting page.

      What if I don't print it to a webpage yet. Is there any way that I can just send the data directly to a Perl script?

      --perl.j

        Neither LWP::Simple nor JSON::Any "print to a web page", so I'm not sure why you think that "printing to a web page" comes into play with the approaches I suggested. Maybe you want to read the documentation of JSON::Any or JSON::XS?

Re: Send JSON data to Perl scalar
by tobyink (Abbot) on Dec 21, 2012 at 15:46 UTC

    I think what you're looking for is...

    use LWP::Simple qw(get); use JSON qw(from_json); my $username = 'ESPN'; my $data = from_json get "http://twitter.com/users/${username}.json"; print $username, " has ", $data->{followers_count}, " followers.\n";
    perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'

      This is great! My only issue here is speed, since this is going to be run about once every minute or two.

      --perl.j

        I don't see why that would be a problem. That script takes about 1.2 seconds to execute on my laptop (and this machine is pretty old and underpowered), so could comfortably execute 30 or 40 times per minute.

        Though probably if I wanted to retrieve this data more than once a minute, I'd probably just add a loop to the script so that you execute it once and it runs in the background, repeatedly retrieving the data.

        perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1009922]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (4)
As of 2014-07-26 10:10 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (175 votes), past polls