Example #1
0
function readers($num = 10, $size = 32, $prefix = 'ys_')
{
    $options = Typecho_Widget::widget('Widget_Options');
    //系统参数
    if (!$options->duoshuo) {
        return false;
    }
    $n = 1;
    //数量开关
    $reader = '';
    //读者墙列表
    $limit = 200;
    //统计条数,越大越详细,但是却会越慢
    $author = explode(',', $options->author);
    $url = 'http://' . $options->duoshuo . '.duoshuo.com/api/posts/list.json?limit=' . $limit;
    //API地址
    $period = 3600 * 24 * 30;
    // 時段: 30 天, 單位: 秒
    $path = __TYPECHO_ROOT_DIR__ . '/usr/uploads/cache/';
    //缓存路径
    $file = $path . $prefix . 'readers.php';
    if (time() - @filemtime($file) > $period || !file_exists($file)) {
        //统计读者墙
        $list = file_get_contents($url);
        $list = json_decode($list, true);
        $list = $list['parentPosts'];
        foreach ($list as $k => $v) {
            if (isset($v['author']) && !in_array($v['author']['name'], $authors)) {
                $usr_arr[] = $v['author']['name'];
                $usr_info[$v['author']['name']]['url'] = $v['author']['url'];
                $usr_info[$v['author']['name']]['avatar_url'] = empty($v['author']['avatar_url']) ? 'http://static.duoshuo.com/images/noavatar_default.png' : $v['author']['avatar_url'];
            }
        }
        $usr = array_count_values($usr_arr);
        arsort($usr);
        //排序
        foreach ($usr as $k => $v) {
            if ($n > $num) {
                break;
            }
            $reader .= '<li><a href="' . $usr_info[$k]['url'] . '" target="_blank" rel="nofollow" title="' . $k . '[共' . $v . '条评论]"><img src="' . $usr_info[$k]['avatar_url'] . '" style="width:' . $size . 'px"></a></li>' . "\n\r";
            $n++;
        }
        SaveCache($file, $reader);
    } else {
        $reader = LoadCache($file);
    }
    echo $reader;
}
Example #2
0
function SetCache($szUrl, $results)
{
    if (!is_numeric($results)) {
        $results = 1;
    }
    if ($results == 0) {
        $results = 1;
    }
    $md5 = md5($szUrl);
    $GLOBALS["CACHE_URLS"][$md5] = intval($results);
    if (count($GLOBALS["CACHE_URLS"]) > $GLOBALS["CacheSizeItems"]) {
        unset($GLOBALS["CACHE_URLS"]);
        @file_put_contents($GLOBALS["CACHE_FILE"], serialize($GLOBALS["CACHE_URLS"]));
    }
    $GLOBALS["CACHE_URLS_COUNT"]++;
    if ($GLOBALS["CACHE_URLS_COUNT"] > 500) {
        SaveCache();
        ReadCache();
        $GLOBALS["CACHE_URLS_COUNT"] = 0;
    }
}