in reply to Running JavaScript from within Perl

Can you offer guidance

Did you know that (or even consider whether) WordPress has an API? Not only that but there is already a whole range of modules on CPAN which use it. Perhaps the ability to retrieve the follower count is available via that API and will save you all this scaping and javascripting and whatnot.

  • Comment on Re: Running JavaScript from within Perl (or just use the API)

Replies are listed 'Best First'.
Re^2: Running JavaScript from within Perl (or just use the API)
by anautismobserver (Sexton) on Sep 13, 2019 at 19:38 UTC

    I tried following the A Beginners’s Guide to the WordPress REST API tutorial

    It didn't work for my own (free) WordPress account, but when I used the "the-art-of-autism.com" (a premium account on which I have admin privileges) in place of "yourdomain.com" I was able to follow the tutorial successfully.

    However, none of the Routes or Endpoints seem to give me what I want, which is the number of followers for an arbitrary WordPress account on which I don't have admin privileges. I'm encouraged by the REST API Handbook Reference page stating "The REST API provides public data accessible to any client anonymously, as well as private data only available after authentication."

    I can't find any way to determine the number of followers, or what public data is accessible anonymously. Can you help with either of those? Thanks.

      It seems that the URL to use is

      https://developer.wordpress.com/docs/api/1.1/get/sites/$site/stats/fol +lowers/

      ... but you need to be authenticated:

      curl "https://public-api.wordpress.com/rest/v1.1/sites/the-art-of-auti +sm.com/stats/followers" {"error":"unauthorized","message":"user cannot view stats"}

      So, you will either have to get permission by the respective sites or you will have to continue scraping the websites.

Re^2: Running JavaScript from within Perl (or just use the API)
by anautismobserver (Sexton) on Sep 20, 2019 at 20:35 UTC
    (Updated and clarified) The following endpoint:

    https://public-api.wordpress.com/rest/v1/read/feed/?url=the-art-of-aut +ism.com
    contains a "feed" url:

    https://public-api.wordpress.com/rest/v1/read/feed/34259929

    that I want to read.

    The following code (based on this JSON Tutorial) gives an error "Use of uninitialized value $feedurl in print".

    use strict; use warnings; use Mojo::UserAgent; my $url = 'https://public-api.wordpress.com/rest/v1/read/feed/?url=the-art-of-au +tism.com'; my $ua = Mojo::UserAgent->new; my $feedurl = $ua->get( $url )->result->json->{'feeds.meta.links.feed' +}; print $feedurl;

    Pleae tell me what I'm doing wrong. Thanks.

      What happened when you tried to adapt one of the previous examples you've been given?

        I was previously given the example:

        my $subscribers = $ua->get( $url )->result->json->{subscribers_count};

        which I modified (based on this JSON Tutorial) to:

        my $feedurl = $ua->get($url)->result->json->{feeds.meta.links.feed};

        which gave error message:

        Bareword "feeds" not allowed while "strict subs" in use

        I'll keep exploring but would appreciate any hints.

        marto, with regards to your reply on Sep 21, 2019 at 16:31 UTC (which I couldn't figure out how to reply to directly): the code worked (and I'll use it) but gave a warning of "Use of uninitialized value in concatenation (.) or string" at the following line:

        print "$feed->{meta}{links}{feed}\n";

        Thanks for solving the problem, albeit with a confusing warning.

      The following code works to assign $subscribers to subscribers_count, but gives an error "Use of uninitialized value $feedurl in print" for the assignment of $feedurl.

      use strict; use warnings; use Mojo::UserAgent; my $url = 'https://public-api.wordpress.com/rest/v1/read/feed/34259929'; my $ua = Mojo::UserAgent->new; my $subscribers = $ua->get($url)->result->json->{subscribers_count}; print "Number of subscribers: $subscribers\n"; my $feedurl = $ua->get( $url )->result->json->{'meta.links.self'}; print $feedurl;

      Pleae tell me what I'm doing wrong. Thanks.

        There is no element named "meta.links.self" in the returned JSON, so when you try to set $feedurl to that it is unititialised.

        Also, you call $ua->get($url)->result->json twice in the space of 3 lines. DRY.

        use strict; use warnings; use Mojo::UserAgent; use Data::Dumper; my $url = 'https://public-api.wordpress.com/rest/v1/read/feed/34259929 +'; my $ua = Mojo::UserAgent->new; my $json = $ua->get($url)->res->json; print Dumper ($json); my $subscribers = $json->{subscribers_count}; print "\nNumber of subscribers: $subscribers\n"; my $feedurl = $json->{meta}{links}{self}; print "URL is $feedurl\n";

        This works for me. My version of Mojo::UserAgent (Mojolicious 5.77) does not have a result method. YMMV.