Skip to content

Commit

Permalink
🎉 support Chinese now
Browse files Browse the repository at this point in the history
  • Loading branch information
songquanpeng committed Dec 14, 2022
1 parent 8580907 commit cb9990a
Show file tree
Hide file tree
Showing 16 changed files with 318 additions and 138 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,14 @@
完整主题列表参见 [common/theme.js](./common/theme.js)
,或参见 [github-readme-stats 中的主题列表](https://github.com/anuraghazra/github-readme-stats/tree/master/themes)

### 支持的语言列表

默认语言为英文,如果想要更改为中文显示,在 url 后加入 `&lang=zh-CN` 即可,例如:
<p>
<img src="https://stats.justsong.cn/api/juejin?id=1556564194374926&lang=zh-CN" alt="掘金酱的掘金数据" width="49%" />
<img src="https://stats.justsong.cn/api/bilibili/?id=483246073&lang=zh-CN" alt="蓝天的 B 站数据" width="49%" />
</p>

## 演示

### 自定义网站监控
Expand Down
4 changes: 2 additions & 2 deletions api/bilibili.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const { cacheTime, cache } = require('../common/cache');
const { processData } = require('../common/utils');

module.exports = async (req, res) => {
const { id, theme } = req.query;
const { id, theme, lang } = req.query;
let key = 'b' + id;
let data = cache.get(key);
if (!data) {
Expand All @@ -15,5 +15,5 @@ module.exports = async (req, res) => {
processData(data);
res.setHeader('Content-Type', 'image/svg+xml');
res.setHeader('Cache-Control', `public, max-age=${cacheTime}`);
return res.send(renderBilibiliCard(data));
return res.send(renderBilibiliCard(data, lang));
};
4 changes: 2 additions & 2 deletions api/csdn.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const { cacheTime, cache } = require('../common/cache');
const { processData } = require('../common/utils');

module.exports = async (req, res) => {
const { id, theme } = req.query;
const { id, theme, lang } = req.query;
let key = 'c' + id;
let data = cache.get(key);
if (!data) {
Expand All @@ -15,5 +15,5 @@ module.exports = async (req, res) => {
processData(data);
res.setHeader('Content-Type', 'image/svg+xml');
res.setHeader('Cache-Control', `public, max-age=${cacheTime}`);
return res.send(renderCSDNCard(data));
return res.send(renderCSDNCard(data, lang));
};
4 changes: 2 additions & 2 deletions api/github.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const { cacheTime, cache } = require('../common/cache');
const { processData } = require('../common/utils');

module.exports = async (req, res) => {
const { username, theme } = req.query;
const { username, theme, lang } = req.query;
let key = 'g' + username;
let data = cache.get(key);
if (!data) {
Expand All @@ -15,5 +15,5 @@ module.exports = async (req, res) => {
processData(data);
res.setHeader('Content-Type', 'image/svg+xml');
res.setHeader('Cache-Control', `public, max-age=${cacheTime}`);
return res.send(renderGitHubCard(data));
return res.send(renderGitHubCard(data, lang));
};
4 changes: 2 additions & 2 deletions api/juejin.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const { cacheTime, cache } = require('../common/cache');
const { processData } = require('../common/utils');

module.exports = async (req, res) => {
const { id, theme } = req.query;
const { id, theme, lang } = req.query;
let key = 'j' + id;
let data = cache.get(key);
if (!data) {
Expand All @@ -15,5 +15,5 @@ module.exports = async (req, res) => {
processData(data);
res.setHeader('Content-Type', 'image/svg+xml');
res.setHeader('Cache-Control', `public, max-age=${cacheTime}`);
return res.send(renderJuejinCard(data));
return res.send(renderJuejinCard(data, lang));
};
4 changes: 2 additions & 2 deletions api/leetcode.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const renderLeetCodeCard = require('../render/leetcode');
const { cache, cacheTime } = require('../common/cache');

module.exports = async (req, res) => {
const { username, cn, theme, cn_username } = req.query;
const { username, cn, theme, cn_username, lang } = req.query;
let data = null;
if (!cn_username) {
if (cn) {
Expand Down Expand Up @@ -44,5 +44,5 @@ module.exports = async (req, res) => {
data.theme = theme;
res.setHeader('Content-Type', 'image/svg+xml');
res.setHeader('Cache-Control', `public, max-age=${cacheTime}`);
return res.send(renderLeetCodeCard(data));
return res.send(renderLeetCodeCard(data, lang));
};
4 changes: 2 additions & 2 deletions api/nowcoder.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const { cache, cacheTime } = require('../common/cache');
const { processData } = require('../common/utils');

module.exports = async (req, res) => {
const { id, theme } = req.query;
const { id, theme, lang } = req.query;
let key = 'n' + id;
let data = cache.get(key);
if (!data) {
Expand All @@ -15,5 +15,5 @@ module.exports = async (req, res) => {
processData(data);
res.setHeader('Content-Type', 'image/svg+xml');
res.setHeader('Cache-Control', `public, max-age=${cacheTime}`);
return res.send(renderNowCoderCard(data));
return res.send(renderNowCoderCard(data, lang));
};
4 changes: 2 additions & 2 deletions api/zhihu.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const { cache, cacheTime } = require('../common/cache');
const { processData } = require('../common/utils');

module.exports = async (req, res) => {
const { username, theme } = req.query;
const { username, theme, lang } = req.query;
let key = 'z' + username;
let data = cache.get(key);
if (!data) {
Expand All @@ -15,5 +15,5 @@ module.exports = async (req, res) => {
processData(data);
res.setHeader('Content-Type', 'image/svg+xml');
res.setHeader('Cache-Control', `public, max-age=${cacheTime}`);
return res.send(renderZhihuCard(data));
return res.send(renderZhihuCard(data, lang));
};
6 changes: 6 additions & 0 deletions common/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,14 @@ const processData = (data) => {
}
};

const isEndsWithASCII = (str) => {
if (str.length === 0) return false;
return str.charCodeAt(str.length - 1) <= 127;
};

module.exports = {
mobileConfig,
desktopConfig,
processData,
isEndsWithASCII,
};
52 changes: 38 additions & 14 deletions render/bilibili.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,49 @@
const { constructItem, render } = require('./common');
const { isEndsWithASCII } = require('../common/utils');

function renderBilibiliCard(data) {
function renderBilibiliCard(data, lang) {
let { name, description, following, fans, likes, level, views, theme } = data;
let lengthLimit = 14;
if (description.length > lengthLimit) {
description = description.substr(0, lengthLimit);
description += '...';
}
let items = [
constructItem(94, 44, `${name}&apos;s Bilibili Stats`, 'title', 18),
constructItem(55, 84, `Followers`, 'label', 13.5),
constructItem(203, 84, `Video Views`, 'label', 13.5),
constructItem(55, 119, `Upvotes`, 'label', 13.5),
constructItem(203, 119, `Bilibili Level`, 'label', 13.5),
constructItem(55, 154, `Signature`, 'label', 13.5),
constructItem(126, 84, `${fans}`, 'value', 15),
constructItem(126, 119, `${likes}`, 'value', 15),
constructItem(289, 84, `${views}`, 'value', 15),
constructItem(289, 119, `LV${level}`, 'value', 15),
constructItem(126, 154, `${description}`, 'value', 13, '', 'YuGothicUI-Regular, Yu Gothic UI')
];
let items = [];
switch (lang) {
case 'zh-CN':
if (isEndsWithASCII(name)) {
name += ' ';
}
items = [
constructItem(94, 44, `${name}的 B 站数据`, 'title', 18),
constructItem(55, 84, `关注数`, 'label', 13.5),
constructItem(203, 84, `播放量`, 'label', 13.5),
constructItem(55, 119, `点赞数`, 'label', 13.5),
constructItem(203, 119, `等级`, 'label', 13.5),
constructItem(55, 154, `签名`, 'label', 13.5),
constructItem(126, 84, `${fans}`, 'value', 15),
constructItem(126, 119, `${likes}`, 'value', 15),
constructItem(289, 84, `${views}`, 'value', 15),
constructItem(289, 119, `LV${level}`, 'value', 15),
constructItem(126, 154, `${description}`, 'value', 13),
];
break;
default:
items = [
constructItem(94, 44, `${name}&apos;s Bilibili Stats`, 'title', 18),
constructItem(55, 84, `Followers`, 'label', 13.5),
constructItem(203, 84, `Video Views`, 'label', 13.5),
constructItem(55, 119, `Upvotes`, 'label', 13.5),
constructItem(203, 119, `Bilibili Level`, 'label', 13.5),
constructItem(55, 154, `Signature`, 'label', 13.5),
constructItem(126, 84, `${fans}`, 'value', 15),
constructItem(126, 119, `${likes}`, 'value', 15),
constructItem(289, 84, `${views}`, 'value', 15),
constructItem(289, 119, `LV${level}`, 'value', 15),
constructItem(126, 154, `${description}`, 'value', 13),
];
break;
}
return render(items, theme);
}

Expand Down
59 changes: 42 additions & 17 deletions render/csdn.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,48 @@
const { render, constructItem } = require('./common');
const { isEndsWithASCII } = require('../common/utils');

function renderCSDNCard(data) {
function renderCSDNCard(data, lang) {
let { name, articles, fans, likes, replies, views, credit, theme } = data;

let items = [
constructItem(97, 44, `${name}'s CSDN Stats`, 'title', 18),
constructItem(63, 83, `Articles`, 'label', 13.5),
constructItem(63, 120, `Fans`, 'label', 13.5),
constructItem(63, 157, `Likes`, 'label', 13.5),
constructItem(220, 83, `Replies`, 'label', 13.5),
constructItem(220, 120, `Views`, 'label', 13.5),
constructItem(220, 157, `Credit`, 'label', 13.5),
constructItem(134, 83, `${articles}`, 'value', 15),
constructItem(134, 120, `${fans}`, 'value', 15),
constructItem(134, 157, `${likes}`, 'value', 15),
constructItem(295, 83, `${replies}`, 'value', 15),
constructItem(295, 120, `${views}`, 'value', 15),
constructItem(295, 157, `${credit}`, 'value', 15)
];
let items = [];
switch (lang) {
case 'zh-CN':
if (isEndsWithASCII(name)) {
name += ' ';
}
items = [
constructItem(97, 44, `${name}的 CSDN 数据`, 'title', 18),
constructItem(63, 83, `原创`, 'label', 13.5),
constructItem(63, 120, `粉丝`, 'label', 13.5),
constructItem(63, 157, `获赞`, 'label', 13.5),
constructItem(220, 83, `评论`, 'label', 13.5),
constructItem(220, 120, `访问`, 'label', 13.5),
constructItem(220, 157, `积分`, 'label', 13.5),
constructItem(134, 83, `${articles}`, 'value', 15),
constructItem(134, 120, `${fans}`, 'value', 15),
constructItem(134, 157, `${likes}`, 'value', 15),
constructItem(295, 83, `${replies}`, 'value', 15),
constructItem(295, 120, `${views}`, 'value', 15),
constructItem(295, 157, `${credit}`, 'value', 15),
];
break;
default:
items = [
constructItem(97, 44, `${name}'s CSDN Stats`, 'title', 18),
constructItem(63, 83, `Articles`, 'label', 13.5),
constructItem(63, 120, `Fans`, 'label', 13.5),
constructItem(63, 157, `Likes`, 'label', 13.5),
constructItem(220, 83, `Replies`, 'label', 13.5),
constructItem(220, 120, `Views`, 'label', 13.5),
constructItem(220, 157, `Credit`, 'label', 13.5),
constructItem(134, 83, `${articles}`, 'value', 15),
constructItem(134, 120, `${fans}`, 'value', 15),
constructItem(134, 157, `${likes}`, 'value', 15),
constructItem(295, 83, `${replies}`, 'value', 15),
constructItem(295, 120, `${views}`, 'value', 15),
constructItem(295, 157, `${credit}`, 'value', 15),
];
break;
}
return render(items, theme);
}

Expand Down
62 changes: 44 additions & 18 deletions render/github.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
const { constructItem, render } = require('./common');
const { isEndsWithASCII } = require('../common/utils');

function renderGitHubCard(data) {
const {
function renderGitHubCard(data, lang) {
let {
name,
followers,
stars,
Expand All @@ -11,24 +12,49 @@ function renderGitHubCard(data) {
following,
theme,
issues,
gists
gists,
} = data;

let items = [
constructItem(97, 44, `${name}&apos;s GitHub Stats`, 'title', 18),
constructItem(63, 83, `Stars`, 'label', 13.5),
constructItem(63, 120, `Repos`, 'label', 13.5),
constructItem(63, 157, `Gists`, 'label', 13.5),
constructItem(220, 83, `Followers`, 'label', 13.5),
constructItem(220, 120, `Following`, 'label', 13.5),
constructItem(220, 157, `Forked`, 'label', 13.5),
constructItem(134, 83, `${stars}`, 'value', 15),
constructItem(134, 120, `${repos}`, 'value', 15),
constructItem(134, 157, `${gists}`, 'value', 15),
constructItem(295, 83, `${followers}`, 'value', 15),
constructItem(295, 120, `${following}`, 'value', 15),
constructItem(295, 157, `${forks}`, 'value', 15)
];
let items = [];
switch (lang) {
case 'zh-CN':
if (isEndsWithASCII(name)) {
name += ' ';
}
items = [
constructItem(97, 44, `${name}的 GitHub 数据`, 'title', 18),
constructItem(63, 83, `星标总数`, 'label', 13.5),
constructItem(63, 120, `代码仓库`, 'label', 13.5),
constructItem(63, 157, `代码片段`, 'label', 13.5),
constructItem(220, 83, `粉丝总数`, 'label', 13.5),
constructItem(220, 120, `追踪数量`, 'label', 13.5),
constructItem(220, 157, `复刻数量`, 'label', 13.5),
constructItem(134, 83, `${stars}`, 'value', 15),
constructItem(134, 120, `${repos}`, 'value', 15),
constructItem(134, 157, `${gists}`, 'value', 15),
constructItem(295, 83, `${followers}`, 'value', 15),
constructItem(295, 120, `${following}`, 'value', 15),
constructItem(295, 157, `${forks}`, 'value', 15),
];
break;
default:
items = [
constructItem(97, 44, `${name}&apos;s GitHub Stats`, 'title', 18),
constructItem(63, 83, `Stars`, 'label', 13.5),
constructItem(63, 120, `Repos`, 'label', 13.5),
constructItem(63, 157, `Gists`, 'label', 13.5),
constructItem(220, 83, `Followers`, 'label', 13.5),
constructItem(220, 120, `Following`, 'label', 13.5),
constructItem(220, 157, `Forked`, 'label', 13.5),
constructItem(134, 83, `${stars}`, 'value', 15),
constructItem(134, 120, `${repos}`, 'value', 15),
constructItem(134, 157, `${gists}`, 'value', 15),
constructItem(295, 83, `${followers}`, 'value', 15),
constructItem(295, 120, `${following}`, 'value', 15),
constructItem(295, 157, `${forks}`, 'value', 15),
];
break;
}
return render(items, theme);
}

Expand Down
Loading

0 comments on commit cb9990a

Please sign in to comment.