<commit_before><?php
/* Based on original Sharrre by <NAME> 
 * by <NAME> (<EMAIL>)
 */

namespace Fuoricentrostudio\SocialShares;

if(file_exists('vendor/autoload.php')){
    require 'vendor/autoload.php';
}

class Counter {
          
    public static $stream_context = array(
        'http'=>array(
          //'proxy' => 'tcp://proxy.example.com:5100', //  
          'max_redirects' => 5,
          'user_agent' => 'Social Counter',
          'timeout' => 5,
          'verify_peer' => false,
        )
    );
        
    public static function count($provider, $url){
        
        $count = self::getCache(self::cacheKey($provider, $url));
        if(!$count){
            $count = self::update($provider, $url);
            self::setCache(self::cacheKey($provider, $url), $count);
        }
        
        return $count;
    }
    
    public static function update($provider, $url){
   
        switch($provider){
            case 'googlePlus':
                $response = self::request('https://plusone.google.com/u/0/_/+1/fastbutton?url=' . urlencode($url) . '&count=true');
                $matches = array();
                if(!empty($response) && preg_match( '/window\.__SSR = {c: ([^,]+)/', $response, $matches )){
                  $json['count'] = floatval($matches[1]);
                }
                break;

            case 'stumbleUpon':
                $response = self::request("http://www.stumbleupon.com/services/1.01/badge.getinfo?url=".urlencode($url)); 
                if (!empty($response) && ($result = json_decode($response)) && isset($result->result->views))
                {
                    $json['count'] = floatval((int)$result->result->views);
                }

            break;
        }   
        
    }
    
    public static function request($url, $stream_context=null){
                
        $context = stream_context_create($stream_context?$stream_context:self::$stream_context);
        
        return file_get_contents($url, false, $context);
    }
    
    public static function getCache($key) {
        
        if(!class_exists('phpFastCache')){
            return false;
        }
        
        $cache = phpFastCache();
         
        return $cache->get($key);
    }
    
    public static function setCache($key, $data) {
        
        if(!class_exists('phpFastCache')){
            return false;
        }
        
        $cache = phpFastCache();
         
        return $cache->set($key, $data, 60);
    }    
    
    public static function cacheKey($provider, $url){
        return $provider.'_'.md5($url);
    }
    
}<commit_msg>fixed class<commit_after><?php
/* Based on original Sharrre by <NAME> 
 * by <NAME> (<EMAIL>)
 */

namespace Fuoricentrostudio\SocialShares;

class Counter {
          
    public static $stream_context = array(
        'http'=>array(
          //'proxy' => 'tcp://proxy.example.com:5100', //  
          'max_redirects' => 5,
          'user_agent' => 'Social Counter',
          'timeout' => 5,
          'verify_peer' => false,
        )
    );
        
    public static function count($provider, $url){
        
        $count = self::getCache(self::cacheKey($provider, $url));
        if(!$count){
            $count = self::update($provider, $url);
            self::setCache(self::cacheKey($provider, $url), $count);
        }
        
        return $count;
    }
    
    public static function update($provider, $url){
   
        $count = false;
        
        switch($provider){
            case 'googlePlus':
                $response = self::request('https://plusone.google.com/u/0/_/+1/fastbutton?url=' . urlencode($url) . '&count=true');
                $matches = array();
                if(!empty($response) && preg_match( '/window\.__SSR = {c: ([^,]+)/', $response, $matches )){
                    $count = floatval($matches[1]);
                }
                break;

            case 'stumbleUpon':
                $response = self::request("http://www.stumbleupon.com/services/1.01/badge.getinfo?url=".urlencode($url)); 
                if (!empty($response) && ($result = json_decode($response)) && isset($result->result->views))
                {
                    $count = floatval($result->result->views);
                }

            break;
        }   
        
        return $count;
        
    }
    
    public static function request($url, $stream_context=null){
                
        $context = stream_context_create($stream_context?$stream_context:self::$stream_context);
        
        return file_get_contents($url, false, $context);
    }
    
    public static function getCache($key) {
        
        if(!class_exists('phpFastCache')){
            return false;
        }
        
        $cache = phpFastCache();
         
        return $cache->get($key);
    }
    
    public static function setCache($key, $data) {
        
        if(!class_exists('phpFastCache')){
            return false;
        }
        
        $cache = phpFastCache();
         
        return $cache->set($key, $data, 60);
    }    
    
    public static function cacheKey($provider, $url){
        return $provider.'_'.md5($url);
    }
    
}
{'fraction_non_alphanumeric': 0.15328467153284672, 'fraction_numerical': 0.005762581636573185, 'mean_word_length': 1.746308016877637, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 33, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6462121', 'n_tokens_mistral': 1536, 'n_tokens_neox': 1476, 'n_words': 258}