Skip to main content
added 114 characters in body
Source Link
birgire
  • 67.8k
  • 7
  • 119
  • 251

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

function get_todays_downloads($url$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;        
}
// let's find out how many times the Akismet plugin has been downloaded today
//
$url="http://wordpress.org/extend/plugins/akismet/stats/";
echo "Downloads today: ".get_todays_downloads($url'akismet');

Here is a function to fetch the data:

function get_todays_downloads($url){    
    $downloads_today="?";
    $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;        
}
// let's find out how many times the Akismet plugin has been downloaded today
//
$url="http://wordpress.org/extend/plugins/akismet/stats/";
echo "Downloads today: ".get_todays_downloads($url);

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;        
}
// let's find out how many times the Akismet plugin has been downloaded today
//
echo "Downloads today: ".get_todays_downloads('akismet');
added 30 characters in body
Source Link
birgire
  • 67.8k
  • 7
  • 119
  • 251
function get_todays_downloads($url){    
    $downloads_today="?";
    $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;        
}
// plugin'slet's statsfind url:out how many times the Akismet plugin has been downloaded today
//
$url="http://wordpress.org/extend/plugins/akismet/stats/";
echo "Downloads today: ".get_todays_downloads($url);

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

function get_todays_downloads($url){    
    $downloads_today="?";
    $response = wp_remote_get( $url );
    if( is_wp_error( $response ) ) {
       $error_message = $response->get_error_message();
       echo "Something went wrong:". $error_message;
    } 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;        
}
// plugin's stats url:
$url="http://wordpress.org/extend/plugins/akismet/stats/";
echo "Downloads today: ".get_todays_downloads($url);

Just remember that the html of the page can change in the future ;-)

function get_todays_downloads($url){    
    $downloads_today="?";
    $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;        
}
// let's find out how many times the Akismet plugin has been downloaded today
//
$url="http://wordpress.org/extend/plugins/akismet/stats/";
echo "Downloads today: ".get_todays_downloads($url);

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

Source Link
birgire
  • 67.8k
  • 7
  • 119
  • 251

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:

function get_todays_downloads($url){    
    $downloads_today="?";
    $response = wp_remote_get( $url );
    if( is_wp_error( $response ) ) {
       $error_message = $response->get_error_message();
       echo "Something went wrong:". $error_message;
    } 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:

// plugin's stats url:
$url="http://wordpress.org/extend/plugins/akismet/stats/";
echo "Downloads today: ".get_todays_downloads($url);

and it will give you results like:

 Downloads today: 7,397

Just remember that the html of the page can change in the future ;-)