qq info restfull api

This commit is contained in:
mashiro 2019-12-03 21:20:42 +08:00
parent 84d722feb5
commit 8d0bbcd56a
6 changed files with 134 additions and 47 deletions

View File

@ -0,0 +1,6 @@
_Callback(
{"error":{
"type":"",
"msg":"对不起,你输入的号码错误"
}}
);

View File

@ -12,12 +12,71 @@ add_action('rest_api_init', function () {
'methods' => 'GET', 'methods' => 'GET',
'callback' => 'cache_search_json', 'callback' => 'cache_search_json',
)); ));
register_rest_route('sakura/v1', '/qqinfo/json', array(
'methods' => 'GET',
'callback' => 'get_qq_info',
));
}); });
/**
* QQ info
*/
function get_qq_info(WP_REST_Request $request)
{
if (!check_ajax_referer('wp_rest', '_wpnonce', false)) {
$output = array(
'status' => 403,
'success' => false,
'message' => 'Unauthorized client.',
'avatar' => 'https://q2.qlogo.cn/headimg_dl?dst_uin=0&spec=100',
'name' => '',
);
} elseif ($_GET['qq']) {
$qq = $_GET['qq'];
/**
* TODO: 设置host国外服务器默认解析的不是国内IP可能无法获取数据
* 182.254.92.32 r.qzone.qq.com
* 参考https://www.php.net/manual/zh/function.file-get-contents.php#108309
*/
$get_info = file_get_contents('http://r.qzone.qq.com/fcg-bin/cgi_get_portrait.fcg?get_nick=1&uins=' . $qq);
$get_info = mb_convert_encoding($get_info, "UTF-8", "GBK");
$name = json_decode(substr($get_info, 17, -1), true);
if ($name) {
$output = array(
'status' => 200,
'success' => true,
'avatar' => 'https://q.qlogo.cn/headimg_dl?dst_uin=' . $qq . '&spec=100',
'name' => $name[$qq][6],
);
} else {
$output = array(
'status' => 404,
'success' => false,
'message' => 'QQ number not exist.',
'avatar' => 'https://q.qlogo.cn/headimg_dl?dst_uin=0&spec=100',
'name' => '',
);
}
} else {
$output = array(
'status' => 400,
'success' => false,
'message' => 'Bad Request',
'avatar' => 'https://q2.qlogo.cn/headimg_dl?dst_uin=0&spec=100',
'name' => '',
);
}
$result = new WP_REST_Response($output, $output['status']);
$result->set_headers(array('Content-Type' => 'application/json'));
return $result;
}
/** /**
* Image uploader response * Image uploader response
*/ */
function upload_image(WP_REST_Request $request) { function upload_image(WP_REST_Request $request)
{
// see: https://developer.wordpress.org/rest-api/requests/ // see: https://developer.wordpress.org/rest-api/requests/
// handle file params $file === $_FILES // handle file params $file === $_FILES
@ -29,8 +88,7 @@ function upload_image(WP_REST_Request $request) {
*/ */
// $file = $request->get_file_params(); // $file = $request->get_file_params();
if (!check_ajax_referer('wp_rest', '_wpnonce', false)) { if (!check_ajax_referer('wp_rest', '_wpnonce', false)) {
$output = array( $output = array('status' => 403,
'status' => 403,
'success' => false, 'success' => false,
'message' => 'Unauthorized client.', 'message' => 'Unauthorized client.',
'link' => "https://view.moezx.cc/images/2019/11/14/step04.md.png", 'link' => "https://view.moezx.cc/images/2019/11/14/step04.md.png",
@ -56,7 +114,7 @@ function upload_image(WP_REST_Request $request) {
break; break;
} }
$result = new WP_REST_Response($API_Request, $API_Request->status); $result = new WP_REST_Response($API_Request, $API_Request['status']);
$result->set_headers(array('Content-Type' => 'application/json')); $result->set_headers(array('Content-Type' => 'application/json'));
return $result; return $result;
} }
@ -64,13 +122,14 @@ function upload_image(WP_REST_Request $request) {
/** /**
* Chevereto upload interface * Chevereto upload interface
*/ */
function Chevereto_API($image) { function Chevereto_API($image)
$upload_url = akina_option('cheverto_url').'/api/1/upload'; {
$upload_url = akina_option('cheverto_url') . '/api/1/upload';
$args = array( $args = array(
'body' => array( 'body' => array(
'source' => base64_encode($image), 'source' => base64_encode($image),
'key' => akina_option('chevereto_api_key') 'key' => akina_option('chevereto_api_key'),
) ),
); );
$response = wp_remote_post($upload_url, $args); $response = wp_remote_post($upload_url, $args);
@ -102,16 +161,17 @@ function Chevereto_API($image) {
/** /**
* Imgur upload interface * Imgur upload interface
*/ */
function Imgur_API($image) { function Imgur_API($image)
{
$client_id = akina_option('imgur_client_id'); $client_id = akina_option('imgur_client_id');
$upload_url = akina_option('imgur_upload_image_proxy'); $upload_url = akina_option('imgur_upload_image_proxy');
$args = array( $args = array(
'headers' => array( 'headers' => array(
'Authorization' => 'Client-ID ' . $client_id 'Authorization' => 'Client-ID ' . $client_id,
), ),
'body' => array( 'body' => array(
'image' => base64_encode($image) 'image' => base64_encode($image),
) ),
); );
$response = wp_remote_post($upload_url, $args); $response = wp_remote_post($upload_url, $args);
@ -143,7 +203,8 @@ function Imgur_API($image) {
/** /**
* smms upload interface * smms upload interface
*/ */
function SMMS_API($image) { function SMMS_API($image)
{
$client_id = akina_option('smms_client_id'); $client_id = akina_option('smms_client_id');
$upload_url = "https://sm.ms/api/v2/upload"; $upload_url = "https://sm.ms/api/v2/upload";
$filename = $image['cmt_img_file']['name']; $filename = $image['cmt_img_file']['name'];
@ -170,7 +231,7 @@ function SMMS_API($image) {
$args = array( $args = array(
'headers' => $headers, 'headers' => $headers,
'body' => $fields 'body' => $fields,
); );
$response = wp_remote_post($upload_url, $args); $response = wp_remote_post($upload_url, $args);
@ -210,7 +271,8 @@ function SMMS_API($image) {
* @rest api接口路径https://sakura.2heng.xin/wp-json/sakura/v1/cache_search/json * @rest api接口路径https://sakura.2heng.xin/wp-json/sakura/v1/cache_search/json
* @可在cache_search_json()函数末尾通过设置 HTTP header 控制 json 缓存时间 * @可在cache_search_json()函数末尾通过设置 HTTP header 控制 json 缓存时间
*/ */
function cache_search_json() { function cache_search_json()
{
$vowels = array("[", "{", "]", "}", "<", ">", "\r\n", "\r", "\n", "-", "'", '"', '`', " ", ":", ";", '\\', " ", "toc"); $vowels = array("[", "{", "]", "}", "<", ">", "\r\n", "\r", "\n", "-", "'", '"', '`', " ", ":", ";", '\\', " ", "toc");
$regex = <<<EOS $regex = <<<EOS
/<\/?[a-zA-Z]+("[^"]*"|'[^']*'|[^'">])*>|begin[\S\s]*\/begin|hermit[\S\s]*\/hermit|img[\S\s]*\/img|{{.*?}}|:.*?:/m /<\/?[a-zA-Z]+("[^"]*"|'[^']*'|[^'">])*>|begin[\S\s]*\/begin|hermit[\S\s]*\/hermit|img[\S\s]*\/img|{{.*?}}|:.*?:/m
@ -256,7 +318,7 @@ EOS;
$result->set_headers( $result->set_headers(
array( array(
'Content-Type' => 'application/json', 'Content-Type' => 'application/json',
'Cache-Control' => 'max-age=3600'// json 缓存控制 'Cache-Control' => 'max-age=3600', // json 缓存控制
) )
); );

View File

@ -13,8 +13,8 @@ mashiro_option.site_name = "<?php echo akina_option('site_name', ''); ?>";
mashiro_option.author_name = "<?php echo akina_option('author_name', ''); ?>"; mashiro_option.author_name = "<?php echo akina_option('author_name', ''); ?>";
mashiro_option.template_url = "<?php echo get_template_directory_uri(); ?>"; mashiro_option.template_url = "<?php echo get_template_directory_uri(); ?>";
mashiro_option.site_url = "<?php echo site_url(); ?>"; mashiro_option.site_url = "<?php echo site_url(); ?>";
mashiro_option.qq_api_url = "https://api.2heng.xin/qqinfo/"; mashiro_option.qq_api_url = "<?php echo site_url(); ?>/wp-json/sakura/v1/qqinfo/json";
mashiro_option.qq_avatar_api_url = "https://api.2heng.xin/qqinfo/"; // mashiro_option.qq_avatar_api_url = "https://api.2heng.xin/qqinfo/";
mashiro_option.live_search = <?php if ( akina_option('live_search') ){ echo 'true'; } else { echo 'false'; } ?>; mashiro_option.live_search = <?php if ( akina_option('live_search') ){ echo 'true'; } else { echo 'false'; } ?>;
<?php if( akina_option('sakura_skin_bg' )){ $bg_arry=explode(",", akina_option('sakura_skin_bg' ));?> <?php if( akina_option('sakura_skin_bg' )){ $bg_arry=explode(",", akina_option('sakura_skin_bg' ));?>

View File

@ -875,12 +875,10 @@ function getqqinfo() {
var qq = cached.filter('#author').val(); var qq = cached.filter('#author').val();
$.ajax({ $.ajax({
type: 'get', type: 'get',
url: mashiro_option.qq_api_url + '?type=getqqnickname&qq=' + qq, url: mashiro_option.qq_api_url + '?qq=' + qq + '&_wpnonce=' + Poi.nonce,
dataType: 'jsonp', dataType: 'json',
jsonp: 'callback',
jsonpCallback: 'portraitCallBack',
success: function (data) { success: function (data) {
cached.filter('#author').val(data[qq][6]); cached.filter('#author').val(data.name);
cached.filter('#email').val($.trim(qq) + '@qq.com'); cached.filter('#email').val($.trim(qq) + '@qq.com');
if (mashiro_option.qzone_autocomplete) { if (mashiro_option.qzone_autocomplete) {
cached.filter('#url').val('https://user.qzone.qq.com/' + $.trim(qq)); cached.filter('#url').val('https://user.qzone.qq.com/' + $.trim(qq));
@ -892,12 +890,15 @@ function getqqinfo() {
$('.qq-check').css('display', 'block'); $('.qq-check').css('display', 'block');
$('.gravatar-check').css('display', 'none'); $('.gravatar-check').css('display', 'none');
} }
setCookie('user_author', data[qq][6], 30); setCookie('user_author', data.name, 30);
setCookie('user_qq', qq, 30); setCookie('user_qq', qq, 30);
setCookie('is_user_qq', 'yes', 30); setCookie('is_user_qq', 'yes', 30);
setCookie('user_qq_email', qq + '@qq.com', 30); setCookie('user_qq_email', qq + '@qq.com', 30);
setCookie('user_email', qq + '@qq.com', 30); setCookie('user_email', qq + '@qq.com', 30);
emailAddressFlag = cached.filter('#email').val(); emailAddressFlag = cached.filter('#email').val();
/***/
$('div.comment-user-avatar img').attr('src', data.avatar);
setCookie('user_avatar', data.avatar, 30);
}, },
error: function () { error: function () {
cached.filter('#qq').val(''); cached.filter('#qq').val('');
@ -907,19 +908,7 @@ function getqqinfo() {
setCookie('user_qq', '', 30); setCookie('user_qq', '', 30);
setCookie('user_email', cached.filter('#email').val(), 30); setCookie('user_email', cached.filter('#email').val(), 30);
setCookie('user_avatar', get_gravatar(cached.filter('#email').val(), 80), 30); setCookie('user_avatar', get_gravatar(cached.filter('#email').val(), 80), 30);
} /***/
});
$.ajax({
type: 'get',
url: mashiro_option.qq_avatar_api_url + '?type=getqqavatar&qq=' + qq,
dataType: 'jsonp',
jsonp: 'callback',
jsonpCallback: 'qqavatarCallBack',
success: function (data) {
$('div.comment-user-avatar img').attr('src', data[qq]);
setCookie('user_avatar', data[qq], 30);
},
error: function () {
cached.filter('#qq,#email,#url').val(''); cached.filter('#qq,#email,#url').val('');
if (!cached.filter('#qq').val()) { if (!cached.filter('#qq').val()) {
$('.qq-check').css('display', 'none'); $('.qq-check').css('display', 'none');
@ -930,6 +919,27 @@ function getqqinfo() {
} }
} }
}); });
// $.ajax({
// type: 'get',
// url: mashiro_option.qq_avatar_api_url + '?type=getqqavatar&qq=' + qq,
// dataType: 'jsonp',
// jsonp: 'callback',
// jsonpCallback: 'qqavatarCallBack',
// success: function (data) {
// $('div.comment-user-avatar img').attr('src', data[qq]);
// setCookie('user_avatar', data[qq], 30);
// },
// error: function () {
// cached.filter('#qq,#email,#url').val('');
// if (!cached.filter('#qq').val()) {
// $('.qq-check').css('display', 'none');
// $('.gravatar-check').css('display', 'block');
// setCookie('user_qq', '', 30);
// $('div.comment-user-avatar img').attr('src', get_gravatar(cached.filter('#email').val(), 80));
// setCookie('user_avatar', get_gravatar(cached.filter('#email').val(), 80), 30);
// }
// }
// });
}); });
if (getCookie('user_avatar') && getCookie('user_email') && getCookie('is_user_qq') == 'no' && !getCookie('user_qq_email')) { if (getCookie('user_avatar') && getCookie('user_email') && getCookie('is_user_qq') == 'no' && !getCookie('user_qq_email')) {
$('div.comment-user-avatar img').attr('src', getCookie('user_avatar')); $('div.comment-user-avatar img').attr('src', getCookie('user_avatar'));

View File

@ -25,27 +25,25 @@ class Single(object):
self.hash = hasher.hexdigest() self.hash = hasher.hexdigest()
self.jpeg = 'jpeg/' + self.hash + '.jpeg' self.jpeg = 'jpeg/' + self.hash + '.jpeg'
self.webp = 'webp/' + self.hash + '.webp' self.webp = 'webp/' + self.hash + '.webp'
self.th_jpeg = 'jpeg/' + self.hash + '.th.jpeg' self.jpeg_th = 'jpeg/' + self.hash + '.th.jpeg'
self.th_webp = 'webp/' + self.hash + '.th.webp' self.webp_th = 'webp/' + self.hash + '.th.webp'
def optimize(self): def optimize(self):
im = Image.open('gallary/' + self.file).convert('RGB') im = Image.open('gallary/' + self.file).convert('RGB')
im.save(self.jpeg, 'JPEG') # todo: TinyPNG API im.save(self.jpeg, 'JPEG') # todo: TinyPNG API
im.save(self.webp, 'WEBP') im.save(self.webp, 'WEBP')
def thumbnail(self):
im = Image.open('gallary/' + self.file).convert('RGB')
im.thumbnail((450, 300)) im.thumbnail((450, 300))
im.save(self.th_jpeg, 'JPEG') # todo: TinyPNG API im.save(self.jpeg_th, 'JPEG') # todo: TinyPNG API
im.save(self.th_webp, 'WEBP') im.save(self.webp_th, 'WEBP')
def manifest(self): def manifest(self):
self.mani[self.hash] = { self.mani[self.hash] = {
'source': self.file, 'source': self.file,
'jpeg': ['jpeg/' + self.hash + '.jpeg', 'jpeg/' + self.hash + '.th.jpeg'], 'jpeg': [self.jpeg, self.jpeg_th],
'webp': ['webp/' + self.hash + '.webp', 'webp/' + self.hash + '.th.webp'] 'webp': [self.webp, self.webp_th]
} }
#这个最好新建一个类
def upload_manifest(self): def upload_manifest(self):
username = input('Enter your username: ') username = input('Enter your username: ')
password = input('Enter your password: ') password = input('Enter your password: ')

11
wget-log Normal file
View File

@ -0,0 +1,11 @@
--2019-12-03 20:47:44-- https://r.qzone.qq.com/fcg-bin/cgi_get_portrait.fcg?get_nick=1
Resolving r.qzone.qq.com (r.qzone.qq.com)... 182.254.92.32
Connecting to r.qzone.qq.com (r.qzone.qq.com)|182.254.92.32|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 84 [text/html]
Saving to: cgi_get_portrait.fcg?get_nick=1
cgi_get_portrait.fcg?get_nick=1 0%[ ] 0 --.-KB/s cgi_get_portrait.fcg?get_nick=1 100%[===========================================================>] 84 --.-KB/s in 0s
2019-12-03 20:47:44 (12.0 MB/s) - cgi_get_portrait.fcg?get_nick=1 saved [84/84]