5

I'm developing a plugin to display stats for downloads and various other information that developers might want to display on their sites about what they have developed.

I've found documentation on some of what I need and dug up some other undocumented resources I can use, but the one thing I still haven't been able to dig up is where I can retrieve the downloads today information that is shown on a plugin's stats page.

All the other stats in the history table could be parsed from the URL below, but that link provides no information on a day that is currently in progress: http://api.wordpress.org/stats/plugin/1.0/downloads.php?slug=[plugin-slug]

For those curious, the other APIs already being used are:

http://api.wordpress.org/stats/plugin/1.0/[plugin-slug] (plugin versions in use)

and

http://api.wordpress.org/stats/plugin/1.0/ (POST requests return a bunch of data about plugin)

I know there are some solutions out there that involve just scraping the HTML page for the data needed, but unless there really is no other option (which seems unlikely) I don't wish to take that approach.

As a side note, the documentation in the Codex for the WP.org API is almost non-existent. It would be amazing if someone with some knowledge of the system could fill it out some! A lot of how it works doesn't seem intuitive to me.

NOTE: This was previously posted on the wp.org forum for a week with zero responses so I thought I would try for a better result over here.

5
  • No, I don't think there is a public way to do this, there is a trac ticket here core.trac.wordpress.org/ticket/12696
    – Wyck
    Commented Feb 3, 2013 at 1:45
  • @Wyck, unless I'm missing something, that ticket isn't directly relevant to this issue. I'm looking to access today's downloads, it's talking about ordering plugins... It's also worth noting that I'm interacting with the raw API and haven't been using the plugins_api() wrapper, though I may play with that later.
    – Dan
    Commented Feb 3, 2013 at 5:23
  • @downvoter, care to enlighten me as to why you think this question deserved that?
    – Dan
    Commented Feb 3, 2013 at 23:17
  • I tried a scrapping plugin (wordpress.org/extend/plugins/wp-web-scrapper) once but there's something wrong as I can't display accents properly :(
    – Diana
    Commented Feb 7, 2013 at 17:08
  • @Dan Care to make any progress on this question?
    – kaiser
    Commented Aug 5, 2013 at 16:37

4 Answers 4

4

Late answer

A mini plugin as local API

This plugin gives you - after you filled in the slug of your repository - the downloads stats as array. The keys are the dates, the values the downloads.

<?php
/** Plugin Name: (#84254) Plugin stats API */
function wpse84254_get_download_stats()
{
    $response = wp_remote_request(
        add_query_arg(
             'slug'
            ,'YOUR-REPO-PLUGIN-SLUG'
            ,'http://wordpress.org/extend/stats/plugin-xml.php'
        )
        ,array( 'sslverify' => false )
    );
    // Check if response is valid
    if ( is_wp_error( $response ) )
        return $response->get_error_message();
    if (
        empty( $response )
        OR 200 !== wp_remote_retrieve_response_code( $response )
        OR 'OK' !== wp_remote_retrieve_response_message( $response )
    )
        return _e( 'No Stats available', 'pluginstats_textdomain' );

    $response  = wp_remote_retrieve_body( $response );
    $response  = (array) simplexml_load_string( $response )->children()->chart_data;
    $response  = (array) $response['row'];
    $dates     = (array) array_shift( $response );
    $dates     = $dates['string'];
    // Get rid of unnecessary prepended empty object
    array_shift( $dates );
    $downloads = (array) array_pop( $response )->number;
    if ( count( $dates ) !== count( $downloads ) )
        return;

    $result = array_combine(
         $dates
        ,$downloads
    );
    return array_map(
         'absint'
        ,$result
    );
}

Usage

To get only the last day + the download number:

$data = array_unshift( wpse84254_get_download_stats );
echo key( $data ).' had '.$data.' Downloads';
4

The /stats/plugin/1.0/downloads.php endpoint returns a JSON list the number of downloads for each day:

http://api.wordpress.org/stats/plugin/1.0/downloads.php?slug={slug}&limit=1&callback=someJsFunction

Replace {slug} with your plugin slug.

The &limit=1 parameter sets how many days back you want to go. The last day is presumably "today".

The &callback=someJsFunction part is optional.

Also note that in order to get the total number of downloads in a nice JSON format you can use:

http://api.wordpress.org/plugins/info/1.0/{slug}.json
3

Here is one idea:

You always have the possibility to scrape the plugin's download html page:

http://wordpress.org/extend/plugins/some-plugin-slug/stats/

while you don't have any json/xml/rss sources with the today's download counter.

The html part you are interested in has this form:

<div id="history" class="left">
<h4>History</h4>
<table>
    <tr>
        <th scope="row">Today</th>
        <td>7,390</td>
    </tr>

    <tr>
        <th scope="row">Yesterday</th>
        <td>12,262</td>
    </tr>

    <tr>
        <th scope="row">Last Week</th>
        <td>130,130</td>
    </tr>

    <tr class="last-child">
        <th scope="row">All Time</th>
        <td>13,639,901</td>
    </tr>
</table>
</div>

You could use wp_remote_get() and cache the results.

Here is a function to fetch the data, it takes a plugin_slug (like akismet) as input:

function get_todays_downloads($plugin_slug){    
    $downloads_today="?";
    $url="http://wordpress.org/extend/plugins/".$plugin_slug."/stats/";

    // fetch the html page:
    //
    $response = wp_remote_get( $url );

    if( is_wp_error( $response ) ) {
       $error_message = $response->get_error_message();
       //echo "Something went wrong:". $error_message;

       // let's show "error" if we have problems fetching the page:
       //
       $downloads_today="error";

    } else {

        // get the body of the page:
        //
        $html=$response['body'];

        // let's strip out the newlines and tabs:
        //
        $html=str_replace("\n","",$html);
        $html=str_replace("\t","",$html);

        // let's find this type of html string:
        //    Today</th><td>12</td></tr>
        //
        preg_match('/Today<\/th><td>([0-9,]+)<\/td><\/tr>/i', $html, $matches);

        // check if we got a match:
        //
        if(isset($matches[1])){
            $downloads_today=$matches[1];
        }
    }
    return $downloads_today;        
}

You can use it like this:

// let's find out how many times the Akismet plugin has been downloaded today
//
echo "Downloads today: ".get_todays_downloads('akismet');

and it will give you results like:

 Downloads today: 7,397

ps: just remember that the html of the page can change in the future ;-)

4
  • Thanks, @birgire, but I explicitly state in the question that I don't want a page scraping solution. Obviously I can hack one together if needed (that isn't difficult), but like you pointed out yourself, it is subject to changing without notice.
    – Dan
    Commented Feb 3, 2013 at 19:46
  • 1
    ahh ok, missed that comment ;-)
    – birgire
    Commented Feb 3, 2013 at 20:08
  • if your plugin is younger than 2 years old, you could subtract the historic data from the total download count to get the today's value ;-) see for example the historic data for Akismet: api.wordpress.org/stats/plugin/1.0/…?
    – birgire
    Commented Feb 3, 2013 at 20:10
  • Yeah, I can look into that as a solution. I'll probably accept your solution since you're the only one to approach answering the question, but I want to give it some time just in case anything else emerges.
    – Dan
    Commented Feb 3, 2013 at 20:20
3

The API does not seem to provide this info. The two datasets you can grab for plugins are:

http://api.wordpress.org/plugins/info/1.0/[plugin name]
This will return total downloads, not per day.

http://api.wordpress.org/stats/plugin/1.0/[plugin name]
This will return the versions

You will have to scrape the HTML.

4
  • So your answer is to duplicate what I included in the question?
    – Dan
    Commented Feb 3, 2013 at 19:47
  • 1
    No, your question did not mention the http://api.wordpress.org/plugins/info/1.0/ , even so the answer to your question is , no.
    – Wyck
    Commented Feb 3, 2013 at 19:53
  • The information available through that resource is a duplicate of what is available here: http://api.wordpress.org/stats/plugin/1.0/...
    – Dan
    Commented Feb 3, 2013 at 20:15
  • And you're right, you didn't duplicate my question. I included an additional resource in my question (daily downloads).
    – Dan
    Commented Feb 3, 2013 at 20:25

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.