From 8d0bbcd56aa649183fab0616ae7ca0df6f45e664 Mon Sep 17 00:00:00 2001 From: mashiro Date: Tue, 3 Dec 2019 21:20:42 +0800 Subject: [PATCH] qq info restfull api --- cgi_get_portrait.fcg?get_nick=1 | 6 +++ inc/api.php | 96 +++++++++++++++++++++++++++------ inc/swicher.php | 4 +- js/sakura-app.js | 48 ++++++++++------- manifest/manifest.py | 16 +++--- wget-log | 11 ++++ 6 files changed, 134 insertions(+), 47 deletions(-) create mode 100644 cgi_get_portrait.fcg?get_nick=1 create mode 100644 wget-log diff --git a/cgi_get_portrait.fcg?get_nick=1 b/cgi_get_portrait.fcg?get_nick=1 new file mode 100644 index 0000000..71ec479 --- /dev/null +++ b/cgi_get_portrait.fcg?get_nick=1 @@ -0,0 +1,6 @@ +_Callback( +{"error":{ +"type":"", +"msg":"对不起,你输入的号码错误" +}} +); diff --git a/inc/api.php b/inc/api.php index 9cb0ca8..c30e21f 100644 --- a/inc/api.php +++ b/inc/api.php @@ -12,12 +12,71 @@ add_action('rest_api_init', function () { 'methods' => 'GET', '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 */ -function upload_image(WP_REST_Request $request) { +function upload_image(WP_REST_Request $request) +{ // see: https://developer.wordpress.org/rest-api/requests/ // handle file params $file === $_FILES @@ -29,8 +88,7 @@ function upload_image(WP_REST_Request $request) { */ // $file = $request->get_file_params(); if (!check_ajax_referer('wp_rest', '_wpnonce', false)) { - $output = array( - 'status' => 403, + $output = array('status' => 403, 'success' => false, 'message' => 'Unauthorized client.', 'link' => "https://view.moezx.cc/images/2019/11/14/step04.md.png", @@ -56,7 +114,7 @@ function upload_image(WP_REST_Request $request) { 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')); return $result; } @@ -64,13 +122,14 @@ function upload_image(WP_REST_Request $request) { /** * Chevereto upload interface */ -function Chevereto_API($image) { - $upload_url = akina_option('cheverto_url').'/api/1/upload'; +function Chevereto_API($image) +{ + $upload_url = akina_option('cheverto_url') . '/api/1/upload'; $args = array( 'body' => array( 'source' => base64_encode($image), - 'key' => akina_option('chevereto_api_key') - ) + 'key' => akina_option('chevereto_api_key'), + ), ); $response = wp_remote_post($upload_url, $args); @@ -102,16 +161,17 @@ function Chevereto_API($image) { /** * Imgur upload interface */ -function Imgur_API($image) { +function Imgur_API($image) +{ $client_id = akina_option('imgur_client_id'); $upload_url = akina_option('imgur_upload_image_proxy'); $args = array( 'headers' => array( - 'Authorization' => 'Client-ID ' . $client_id + 'Authorization' => 'Client-ID ' . $client_id, ), 'body' => array( - 'image' => base64_encode($image) - ) + 'image' => base64_encode($image), + ), ); $response = wp_remote_post($upload_url, $args); @@ -143,7 +203,8 @@ function Imgur_API($image) { /** * smms upload interface */ -function SMMS_API($image) { +function SMMS_API($image) +{ $client_id = akina_option('smms_client_id'); $upload_url = "https://sm.ms/api/v2/upload"; $filename = $image['cmt_img_file']['name']; @@ -170,7 +231,7 @@ function SMMS_API($image) { $args = array( 'headers' => $headers, - 'body' => $fields + 'body' => $fields, ); $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 * @可在cache_search_json()函数末尾通过设置 HTTP header 控制 json 缓存时间 */ -function cache_search_json() { +function cache_search_json() +{ $vowels = array("[", "{", "]", "}", "<", ">", "\r\n", "\r", "\n", "-", "'", '"', '`', " ", ":", ";", '\\', " ", "toc"); $regex = <<])*>|begin[\S\s]*\/begin|hermit[\S\s]*\/hermit|img[\S\s]*\/img|{{.*?}}|:.*?:/m @@ -218,7 +280,7 @@ EOS; $posts = new WP_Query('posts_per_page=-1&post_status=publish&post_type=post'); while ($posts->have_posts()): $posts->the_post(); - $output .= '{"type":"post","link":"' . get_post_permalink() . '","title":' . json_encode(get_the_title()) . ',"comments":"' . get_comments_number('0', '1', '%') . '","text":' . json_encode(str_replace($vowels, " ", preg_replace($regex, ' ', get_the_content()))) . '},'; + $output .= '{"type":"post","link":"' . get_post_permalink() . '","title":' . json_encode(get_the_title()) . ',"comments":"' . get_comments_number('0', '1', '%') . '","text":' . json_encode(str_replace($vowels, " ", preg_replace($regex, ' ', get_the_content()))) . '},'; endwhile; wp_reset_postdata(); @@ -256,7 +318,7 @@ EOS; $result->set_headers( array( 'Content-Type' => 'application/json', - 'Cache-Control' => 'max-age=3600'// json 缓存控制 + 'Cache-Control' => 'max-age=3600', // json 缓存控制 ) ); diff --git a/inc/swicher.php b/inc/swicher.php index f2ec4d0..6df3a98 100644 --- a/inc/swicher.php +++ b/inc/swicher.php @@ -13,8 +13,8 @@ mashiro_option.site_name = ""; mashiro_option.author_name = ""; mashiro_option.template_url = ""; mashiro_option.site_url = ""; -mashiro_option.qq_api_url = "https://api.2heng.xin/qqinfo/"; -mashiro_option.qq_avatar_api_url = "https://api.2heng.xin/qqinfo/"; +mashiro_option.qq_api_url = "/wp-json/sakura/v1/qqinfo/json"; +// mashiro_option.qq_avatar_api_url = "https://api.2heng.xin/qqinfo/"; mashiro_option.live_search = ; diff --git a/js/sakura-app.js b/js/sakura-app.js index c96d6b6..e7fd170 100644 --- a/js/sakura-app.js +++ b/js/sakura-app.js @@ -875,12 +875,10 @@ function getqqinfo() { var qq = cached.filter('#author').val(); $.ajax({ type: 'get', - url: mashiro_option.qq_api_url + '?type=getqqnickname&qq=' + qq, - dataType: 'jsonp', - jsonp: 'callback', - jsonpCallback: 'portraitCallBack', + url: mashiro_option.qq_api_url + '?qq=' + qq + '&_wpnonce=' + Poi.nonce, + dataType: 'json', success: function (data) { - cached.filter('#author').val(data[qq][6]); + cached.filter('#author').val(data.name); cached.filter('#email').val($.trim(qq) + '@qq.com'); if (mashiro_option.qzone_autocomplete) { cached.filter('#url').val('https://user.qzone.qq.com/' + $.trim(qq)); @@ -892,12 +890,15 @@ function getqqinfo() { $('.qq-check').css('display', 'block'); $('.gravatar-check').css('display', 'none'); } - setCookie('user_author', data[qq][6], 30); + setCookie('user_author', data.name, 30); setCookie('user_qq', qq, 30); setCookie('is_user_qq', 'yes', 30); setCookie('user_qq_email', qq + '@qq.com', 30); setCookie('user_email', qq + '@qq.com', 30); emailAddressFlag = cached.filter('#email').val(); + /***/ + $('div.comment-user-avatar img').attr('src', data.avatar); + setCookie('user_avatar', data.avatar, 30); }, error: function () { cached.filter('#qq').val(''); @@ -907,19 +908,7 @@ function getqqinfo() { setCookie('user_qq', '', 30); setCookie('user_email', cached.filter('#email').val(), 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(''); if (!cached.filter('#qq').val()) { $('.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')) { $('div.comment-user-avatar img').attr('src', getCookie('user_avatar')); diff --git a/manifest/manifest.py b/manifest/manifest.py index d5e53c7..7fefea2 100644 --- a/manifest/manifest.py +++ b/manifest/manifest.py @@ -25,27 +25,25 @@ class Single(object): self.hash = hasher.hexdigest() self.jpeg = 'jpeg/' + self.hash + '.jpeg' self.webp = 'webp/' + self.hash + '.webp' - self.th_jpeg = 'jpeg/' + self.hash + '.th.jpeg' - self.th_webp = 'webp/' + self.hash + '.th.webp' + self.jpeg_th = 'jpeg/' + self.hash + '.th.jpeg' + self.webp_th = 'webp/' + self.hash + '.th.webp' def optimize(self): im = Image.open('gallary/' + self.file).convert('RGB') im.save(self.jpeg, 'JPEG') # todo: TinyPNG API im.save(self.webp, 'WEBP') - - def thumbnail(self): - im = Image.open('gallary/' + self.file).convert('RGB') im.thumbnail((450, 300)) - im.save(self.th_jpeg, 'JPEG') # todo: TinyPNG API - im.save(self.th_webp, 'WEBP') + im.save(self.jpeg_th, 'JPEG') # todo: TinyPNG API + im.save(self.webp_th, 'WEBP') def manifest(self): self.mani[self.hash] = { 'source': self.file, - 'jpeg': ['jpeg/' + self.hash + '.jpeg', 'jpeg/' + self.hash + '.th.jpeg'], - 'webp': ['webp/' + self.hash + '.webp', 'webp/' + self.hash + '.th.webp'] + 'jpeg': [self.jpeg, self.jpeg_th], + 'webp': [self.webp, self.webp_th] } + #½һ def upload_manifest(self): username = input('Enter your username: ') password = input('Enter your password: ') diff --git a/wget-log b/wget-log new file mode 100644 index 0000000..a901b97 --- /dev/null +++ b/wget-log @@ -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] +