<commit_before><?php

/**
 * CwsShareCount
 *
 * CwsShareCount is a PHP class to get social share count for Delicious, Facebook,
 * Google+, Linkedin, Pinterest, Reddit, StumbleUpon and Twitter.
 * 
 * CwsShareCount is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or (at your option)
 * or (at your option) any later version.
 *
 * CwsShareCount is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
 * for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program. If not, see http://www.gnu.org/licenses/.
 * 
 * Related post : http://goo.gl/iKGRT
 * 
 * @package CwsShareCount
 * @author Cr@zy
 * @copyright 2013-2015, Cr@zy
 * @license GNU LESSER GENERAL PUBLIC LICENSE
 * @version 1.3
 * @link https://github.com/crazy-max/CwsShareCount
 *
 */

class CwsShareCount
{
    const SN_DELICIOUS = 'Delicious';
    const SN_FACEBOOK = 'Facebook';
    const SN_GOOGLEPLUS = 'Google+';
    const SN_LINKEDIN = 'LinkedIn';
    const SN_PINTEREST = 'Pinterest';
    const SN_REDDIT = 'Reddit';
    const SN_STUMBLEUPON = 'StumbleUpon';
    const SN_TWITTER = 'Twitter';
    
    /**
     * The last error message.
     * @var string
     */
    private $error;
    
    /**
     * The cws debug instance.
     * @var CwsDebug
     */
    private $cwsDebug;
    
    /**
     * The cws curl instance.
     * @var CwsCurl
     */
    private $cwsCurl;
    
    public function __construct(CwsDebug $cwsDebug, CwsCurl $cwsCurl)
    {
        $this->cwsDebug = $cwsDebug;
        $this->cwsCurl = $cwsCurl;
    }
    
    /**
     * Get all social share count.
     * @param string $url
     */
    public function getAll($url)
    {
        $result = array();
        
        foreach (self::getSocialNetworks() as $socialNetwork) {
            $result[$socialNetwork] = $this->getCount($url, $socialNetwork);
        }
        
        return $result;
    }
    
    /**
     * Get delicious share count.
     * @param string $url
     */
    public function getDeliciousCount($url)
    {
        return $this->getCount($url, self::SN_DELICIOUS);
    }
    
    /**
     * Get facebook share count.
     * @param string $url
     */
    public function getFacebookCount($url)
    {
        return $this->getCount($url, self::SN_FACEBOOK);
    }
    
    /**
     * Get google plus share count.
     * @param string $url
     */
    public function getGooglePlusCount($url)
    {
        return $this->getCount($url, self::SN_GOOGLEPLUS);
    }
    
    /**
     * Get linkedin share count.
     * @param string $url
     */
    public function getLinkedinCount($url)
    {
        return $this->getCount($url, self::SN_LINKEDIN);
    }
    
    /**
     * Get pinterest share count.
     * @param string $url
     */
    public function getPinterestCount($url)
    {
        return $this->getCount($url, self::SN_PINTEREST);
    }
    
    /**
     * Get reddit share count.
     * @param string $url
     */
    public function getRedditCount($url)
    {
        return $this->getCount($url, self::SN_REDDIT);
    }
    
    /**
     * Get stumbleupon share count.
     * @param string $url
     */
    public function getStumbleuponCount($url)
    {
        return $this->getCount($url, self::SN_STUMBLEUPON);
    }
    
    /**
     * Get twitter share count.
     * @param string $url
     */
    public function getTwitterCount($url)
    {
        return $this->getCount($url, self::SN_TWITTER);
    }
    
    /**
     * Get social share count.
     * @param string $url
     * @param string $socialNetwork
     */
    private function getCount($url, $socialNetwork)
    {
        if (!in_array($socialNetwork, self::getSocialNetworks())) {
            $this->error = 'Social network is not valid...';
            $this->cwsDebug->error($this->error);
            exit();
        } elseif (!$this->isValidUrl($url)) {
            $this->error = 'URL is not valid...';
            $this->cwsDebug->error($this->error);
            exit();
        }
        
        $this->cwsDebug->titleH2('get ' . $socialNetwork . ' count');
        $this->cwsDebug->labelValue('URL', $url);
        
        $this->cwsCurl->reset();
        $this->cwsCurl->addOption(CURLOPT_HTTPHEADER, array('Content-type:application/json'));
        
        $apiUrl = null;
        switch ($socialNetwork) {
            case self::SN_DELICIOUS:
                $apiUrl = 'http://feeds.delicious.com/v2/json/urlinfo/data?url=' . urlencode($url);
                break;
            case self::SN_FACEBOOK:
                $apiUrl = 'http://api.ak.facebook.com/restserver.php?v=1.0&method=links.getStats&urls=' . urlencode($url) . '&format=json';
                break;
            case self::SN_GOOGLEPLUS:
                $apiUrl = 'https://clients6.google.com/rpc?key=<KEY>';
                $this->cwsCurl->setPostMethod();
                $this->cwsCurl->addOption(CURLOPT_POSTFIELDS, '[{"method":"pos.plusones.get","id":"p","params":{"nolog":true,"id":"' . $url . '","source":"widget","userId":"@viewer","groupId":"@self"},"jsonrpc":"2.0","key":"p","apiVersion":"v1"}]');
                break;
            case self::SN_LINKEDIN:
                $apiUrl = 'http://www.linkedin.com/countserv/count/share?url=' . urlencode($url);<commit_msg>updateurl for facebook api<commit_after><?php

/**
 * CwsShareCount
 *
 * CwsShareCount is a PHP class to get social share count for Delicious, Facebook,
 * Google+, Linkedin, Pinterest, Reddit, StumbleUpon and Twitter.
 * 
 * CwsShareCount is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or (at your option)
 * or (at your option) any later version.
 *
 * CwsShareCount is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
 * for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program. If not, see http://www.gnu.org/licenses/.
 * 
 * Related post : http://goo.gl/iKGRT
 * 
 * @package CwsShareCount
 * @author Cr@zy
 * @copyright 2013-2015, Cr@zy
 * @license GNU LESSER GENERAL PUBLIC LICENSE
 * @version 1.3
 * @link https://github.com/crazy-max/CwsShareCount
 *
 */

class CwsShareCount
{
    const SN_DELICIOUS = 'Delicious';
    const SN_FACEBOOK = 'Facebook';
    const SN_GOOGLEPLUS = 'Google+';
    const SN_LINKEDIN = 'LinkedIn';
    const SN_PINTEREST = 'Pinterest';
    const SN_REDDIT = 'Reddit';
    const SN_STUMBLEUPON = 'StumbleUpon';
    const SN_TWITTER = 'Twitter';
    
    /**
     * The last error message.
     * @var string
     */
    private $error;
    
    /**
     * The cws debug instance.
     * @var CwsDebug
     */
    private $cwsDebug;
    
    /**
     * The cws curl instance.
     * @var CwsCurl
     */
    private $cwsCurl;
    
    public function __construct(CwsDebug $cwsDebug, CwsCurl $cwsCurl)
    {
        $this->cwsDebug = $cwsDebug;
        $this->cwsCurl = $cwsCurl;
    }
    
    /**
     * Get all social share count.
     * @param string $url
     */
    public function getAll($url)
    {
        $result = array();
        
        foreach (self::getSocialNetworks() as $socialNetwork) {
            $result[$socialNetwork] = $this->getCount($url, $socialNetwork);
        }
        
        return $result;
    }
    
    /**
     * Get delicious share count.
     * @param string $url
     */
    public function getDeliciousCount($url)
    {
        return $this->getCount($url, self::SN_DELICIOUS);
    }
    
    /**
     * Get facebook share count.
     * @param string $url
     */
    public function getFacebookCount($url)
    {
        return $this->getCount($url, self::SN_FACEBOOK);
    }
    
    /**
     * Get google plus share count.
     * @param string $url
     */
    public function getGooglePlusCount($url)
    {
        return $this->getCount($url, self::SN_GOOGLEPLUS);
    }
    
    /**
     * Get linkedin share count.
     * @param string $url
     */
    public function getLinkedinCount($url)
    {
        return $this->getCount($url, self::SN_LINKEDIN);
    }
    
    /**
     * Get pinterest share count.
     * @param string $url
     */
    public function getPinterestCount($url)
    {
        return $this->getCount($url, self::SN_PINTEREST);
    }
    
    /**
     * Get reddit share count.
     * @param string $url
     */
    public function getRedditCount($url)
    {
        return $this->getCount($url, self::SN_REDDIT);
    }
    
    /**
     * Get stumbleupon share count.
     * @param string $url
     */
    public function getStumbleuponCount($url)
    {
        return $this->getCount($url, self::SN_STUMBLEUPON);
    }
    
    /**
     * Get twitter share count.
     * @param string $url
     */
    public function getTwitterCount($url)
    {
        return $this->getCount($url, self::SN_TWITTER);
    }
    
    /**
     * Get social share count.
     * @param string $url
     * @param string $socialNetwork
     */
    private function getCount($url, $socialNetwork)
    {
        if (!in_array($socialNetwork, self::getSocialNetworks())) {
            $this->error = 'Social network is not valid...';
            $this->cwsDebug->error($this->error);
            exit();
        } elseif (!$this->isValidUrl($url)) {
            $this->error = 'URL is not valid...';
            $this->cwsDebug->error($this->error);
            exit();
        }
        
        $this->cwsDebug->titleH2('get ' . $socialNetwork . ' count');
        $this->cwsDebug->labelValue('URL', $url);
        
        $this->cwsCurl->reset();
        $this->cwsCurl->addOption(CURLOPT_HTTPHEADER, array('Content-type:application/json'));
        
        $apiUrl = null;
        switch ($socialNetwork) {
            case self::SN_DELICIOUS:
                $apiUrl = 'http://feeds.delicious.com/v2/json/urlinfo/data?url=' . urlencode($url);
                break;
            case self::SN_FACEBOOK:
                $apiUrl = 'http://api.facebook.com/method/links.getStats?urls=' . urlencode($url) . '&format=json';
                break;
            case self::SN_GOOGLEPLUS:
                $apiUrl = 'https://clients6.google.com/rpc?key=<KEY>';
                $this->cwsCurl->setPostMethod();
                $this->cwsCurl->addOption(CURLOPT_POSTFIELDS, '[{"method":"pos.plusones.get","id":"p","params":{"nolog":true,"id":"' . $url . '","source":"widget","userId":"@viewer","groupId":"@self"},"jsonrpc":"2.0","key":"p","apiVersion":"v1"}]');
                break;
            case self::SN_LINKEDIN:
                $apiUrl = 'http://www.linkedin.com/countserv/count/share?url=' . urlencode($url);
{'fraction_non_alphanumeric': 0.1366037060245278, 'fraction_numerical': 0.003222630024169725, 'mean_word_length': 2.236384704519119, 'pattern_counts': {'":': 22, '<': 7, '<?xml version=': 0, '>': 69, 'https://': 4, 'lorem ipsum': 0, 'www.': 4, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5968392', 'n_tokens_mistral': 3535, 'n_tokens_neox': 3449, 'n_words': 816}