<commit_before><?php
/**
 * pixiv-api-php
 * Pixiv API for PHP
 *
 * @package  pixiv-api-php
 * @author   Kokororin
 * @license  MIT License
 * @version  1.5
 * @link     https://github.com/kokororin/pixiv-api-php
 */
class PixivAPI
{
    /**
     * @var string
     */
    protected $api_prefix = 'https://public-api.secure.pixiv.net';

    /**
     * @var string
     */
    protected $api_referer = 'http://spapi.pixiv.net/';

    /**
     * @var string
     */
    protected $api_useragent = 'User-Agent: PixivIOSApp/5.8.3';

    /**
     * @var string
     */
    protected $api_host = 'Host: public-api.secure.pixiv.net';

    /**
     * @var string
     */
    protected $api_authorization = 'Authorization: Bearer <KEY>';

    /**
     * @var string
     */
    protected $oauth_client_id = 'bYGKuGVw91e0NMfPGp44euvGt59s';

    /**
     * @var string
     */
    protected $oauth_client_secret = '<KEY>';

    /**
     * @var string
     */
    protected $oauth_url = 'https://oauth.secure.pixiv.net/auth/token';

    /**
     * @var string
     */
    protected $oauth_referer = 'http://www.pixiv.net/';

    /**
     * @var string
     */
    protected $access_token = '';

    /**
     * @var null
     */
    protected $authorization_response = null;

    public function __construct()
    {
        if (!in_array('curl', get_loaded_extensions())) {
            throw new Exception('You need to install cURL, see: http://curl.haxx.se/docs/install.html');
        }
    }

    public function getAccessToken()
    {
        return $this->access_token;
    }

    public function setAccessToken($access_token)
    {
        $this->access_token = $access_token;
    }

    public function getApiAuthorization()
    {
        return $this->api_authorization;
    }

    public function setApiAuthorization($api_authorization)
    {
        $this->$api_authorization = $api_authorization;
    }

    public function getAuthorizationResponse()
    {
        return $this->authorization_response;
    }

    public function setAuthorizationResponse($authorization_response)
    {
        $this->authorization_response = $authorization_response;
    }

    /**
     * 登录
     *
     * @param $user
     * @param $pwd
     */
    public function login($user, $pwd)
    {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $this->oauth_url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
        curl_setopt($ch, CURLOPT_HTTPHEADER, array(
            $this->api_authorization,
        ));
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query(array(
            'username' => $user,
            'password' => <PASSWORD>,
            'grant_type' => 'password',
            'client_id' => $this->oauth_client_id,
            'client_secret' => $this->oauth_client_secret,
        )));
        curl_setopt($ch, CURLOPT_REFERER, $this->oauth_referer);
        $result = curl_exec($ch);
        curl_close($ch);
        $object = json_decode($result);
        $this->setAuthorizationResponse($object);
        if (isset($object->has_error)) {
            throw new Exception('Login error: ' . $object->errors->system->message);
        }
        $this->setAccessToken($object->response->access_token);
        $this->setApiAuthorization('Authorization: Bearer ' . $this->access_token);
    }

    /**
     * 黑词
     *
     * @return mixed
     */
    public function bad_words()
    {
        return $this->fetch_from_url('/v1.1/bad_words.json', 'GET');
    }

    /**
     * 作品详细
     *
     * @param $illust_id
     * @return mixed
     */
    public function works($illust_id)
    {
        return $this->fetch_from_url('/v1/works/' . $illust_id . '.json', 'GET');
    }

    /**
     * 用户资料
     *
     * @param $author_id
     * @return mixed
     */
    public function users($author_id)
    {<commit_msg>add `refresh token` grant type<commit_after><?php
/**
 * pixiv-api-php
 * Pixiv API for PHP
 *
 * @package  pixiv-api-php
 * @author   Kokororin
 * @license  MIT License
 * @version  1.6
 * @link     https://github.com/kokororin/pixiv-api-php
 */
class PixivAPI
{
    /**
     * @var string
     */
    protected $api_prefix = 'https://public-api.secure.pixiv.net';

    /**
     * @var string
     */
    protected $api_referer = 'http://spapi.pixiv.net/';

    /**
     * @var string
     */
    protected $api_useragent = 'User-Agent: PixivIOSApp/5.8.3';

    /**
     * @var string
     */
    protected $api_host = 'Host: public-api.secure.pixiv.net';

    /**
     * @var string
     */
    protected $api_authorization = 'Authorization: Bearer WH<KEY>';

    /**
     * @var string
     */
    protected $oauth_client_id = 'bYGKuGVw91e0NMfPGp44euvGt59s';

    /**
     * @var string
     */
    protected $oauth_client_secret = '<KEY>';

    /**
     * @var string
     */
    protected $oauth_url = 'https://oauth.secure.pixiv.net/auth/token';

    /**
     * @var string
     */
    protected $oauth_referer = 'http://www.pixiv.net/';

    /**
     * @var null
     */
    protected $access_token = null;

    /**
     * @var null
     */
    protected $refresh_token = null;

    /**
     * @var null
     */
    protected $authorization_response = null;

    public function __construct()
    {
        if (!in_array('curl', get_loaded_extensions())) {
            throw new Exception('You need to install cURL, see: http://curl.haxx.se/docs/install.html');
        }
    }

    /**
     * 获取Access Token
     *
     * @return string
     */
    public function getAccessToken()
    {
        return $this->access_token;
    }

    /**
     * 设置Access Token
     *
     * @param $access_token
     */
    public function setAccessToken($access_token)
    {
        $this->access_token = $access_token;
    }

    /**
     * 获取Refresh Token
     *
     * @return string
     */
    public function getRefreshToken()
    {
        return $this->refresh_token;
    }

    /**
     * 设置Refresh Token
     *
     * @param $refresh_token
     */
    public function setRefreshToken($refresh_token)
    {
        $this->refresh_token = $this->refresh_token;
    }

    /**
     * 获取认证后的信息
     *
     * @return string
     */
    public function getAuthorizationResponse()
    {
        return $this->authorization_response;
    }

    /**
     * 设置认证后的信息
     *
     * @param $authorization_response
     */
    public function setAuthorizationResponse($authorization_response)
    {
        $this->authorization_response = $authorization_response;
    }

    /**
     * 登录
     *
     * @param $user
     * @param $pwd
     * @param $refresh_token
     */
    public function login($user = null, $pwd = null, $refresh_token = null)
    {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $this->oauth_url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
        curl_setopt($ch, CURLOPT_HTTPHEADER, array(
            $this->api_authorization,
        ));
        curl_setopt($ch, CURLOPT_POST, 1);
        $request = array(
            'client_id' => $this->oauth_client_id,
            'client_secret' => $this->oauth_client_secret,
        );
        if ($user != null && $pwd != null) {
            $request = array_merge($request, array(
                'username' => $user,
                'password' => $<PASSWORD>,
                'grant_type' => 'password',
            ));
        } elseif ($refresh_token != null || $this->refresh_token != null) {
            $request = array_merge($request, array(
                'grant_type' => 'refresh_token',
                'refresh_token' => $refresh_token || $this->refresh_token,
            ));
        } else {
            throw new Exception('login params error.');
        }
        curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($request));
        curl_setopt($ch, CURLOPT_REFERER, $this->oauth_referer);
        $result = curl_exec($ch);
        curl_close($ch);
        $object = json_decode($result);
        if (isset($object->has_error)) {
            throw new Exception('Login error: ' . $object->errors->system->message);
        }
        $this->setAuthorizationResponse($object->response);
        $this->setAccessToken($object->response->access_token);
        $this->setRefreshToken($object->response->refresh_token);
        $this->api_authorization = 'Authorization: Bearer ' . $this->access_token;
    }

    /**
     * 黑词
     *
     * @return mixed
     */
    public function bad_words()
    {
        return $this->fetch_from_url('/v1.1/bad_words.json', 'GET');
    }

    /**
     * 作品详细
     *
     * @param $illust_id
     * @return mixed
     */
    public function works($illust_id)
    {
        return $this->fetch_from_url('/v1/works/' . $illust_id . '.json', 'GET');
    }

    /**
     * 用户资料
     *
     * @param $author_id
     * @return mixed
     */
    public function users($author_id)
    {
{'fraction_non_alphanumeric': 0.14415714127177895, 'fraction_numerical': 0.00421706802796582, 'mean_word_length': 2.0404858299595143, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 74, 'https://': 6, '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': '2420399', 'n_tokens_mistral': 3004, 'n_tokens_neox': 2942, 'n_words': 493}