Skip to content

Commit 0c92b6c

Browse files
committed
feat: add support for filter spoken languages
Closes #84
1 parent c1859de commit 0c92b6c

8 files changed

Lines changed: 1674 additions & 739 deletions

File tree

README.md

Lines changed: 71 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,13 @@
4343
- [Trending Repositories](#trending-repositories)
4444
- [Trending Developers](#trending-developers)
4545
- [List Languages](#list-languages)
46+
- [List Spoken Languages](#list-spoken-languages)
4647
- [NPM Package](#npm-package)
4748
- [Install](#install)
4849
- [Usage](#usage)
4950
- [API](#api)
5051
- [languages](#languages)
52+
- [spokenLanguages](#spokenlanguages)
5153
- [fetchRepositories(params)](#fetchrepositoriesparams)
5254
- [fetchDevelopers(params)](#fetchdevelopersparams)
5355
- [Contributors](#contributors)
@@ -76,6 +78,7 @@
7678
- **[GitNews](https://git.news)**: Trending repositories from GitHub, HackerNews & Reddit (Mobile & Desktop).
7779
- **[mini-github](https://github.com/kezhenxu94/mini-github)**: GitHub WeChat Mini Program (可能是全网功能最全的 GitHub 微信小程序).
7880
- **[StarGit](https://github.com/theArgex/stargit2.0)** ([Website](https://stargit.xyz)): Find trending repositories from GitHub. Built with Angular.
81+
- **[GitTouch](https://github.com/pd4d10/git-touch)** ([App Store](https://itunes.apple.com/us/app/gittouch/id1452042346), [Google Play](https://play.google.com/store/apps/details?id=io.github.pd4d10.gittouch)): Open source GitHub App built with Flutter.
7982

8083
## Backers
8184

@@ -109,6 +112,7 @@ https://github-trending-api.now.sh/repositories?language=javascript&since=weekly
109112

110113
- `language`: **optional**, list trending repositories of certain programming languages, possible values are listed [here](./src/languages.json).
111114
- `since`: **optional**, default to `daily`, possible values: `daily`, `weekly` and `monthly`.
115+
- `spoken_language_code`: **optional**, list trending repositories of certain spoken languages (e.g English, Chinese), possible values are listed [here](./src/spoken-languages.json).
112116

113117
**Response:**
114118

@@ -176,8 +180,6 @@ https://github-trending-api.now.sh/developers?language=javascript&since=weekly
176180
177181
### List Languages
178182

179-
Receive popular languages and all languages.
180-
181183
**URL Endpoint:**
182184

183185
https://github-trending-api.now.sh/languages
@@ -187,24 +189,53 @@ https://github-trending-api.now.sh/languages
187189
```json
188190
[
189191
{
190-
"id": "1c-enterprise",
192+
"urlParam": "1c-enterprise",
191193
"name": "1C Enterprise"
192194
},
193195
{
194-
"id": "abap",
196+
"urlParam": "abap",
195197
"name": "ABAP"
196198
},
197199
{
198-
"id": "abnf",
200+
"urlParam": "abnf",
199201
"name": "ABNF"
200202
},
201203
{
202-
"id": "actionscript",
204+
"urlParam": "actionscript",
203205
"name": "ActionScript"
204206
}
205207
]
206208
```
207209

210+
### List Spoken Languages
211+
212+
**URL Endpoint:**
213+
214+
https://github-trending-api.now.sh/spoken_languages
215+
216+
**Response:**
217+
218+
```json
219+
[
220+
{
221+
"urlParam": "ab",
222+
"name": "Abkhazian"
223+
},
224+
{
225+
"urlParam": "aa",
226+
"name": "Afar"
227+
},
228+
{
229+
"urlParam": "af",
230+
"name": "Afrikaans"
231+
},
232+
{
233+
"urlParam": "ak",
234+
"name": "Akan"
235+
}
236+
]
237+
```
238+
208239
## NPM Package
209240

210241
You could also use the API as a NPM package.
@@ -220,6 +251,7 @@ $ npm install --save @huchenme/github-trending
220251
```js
221252
import {
222253
languages,
254+
spokenLanguages,
223255
fetchRepositories,
224256
fetchDevelopers,
225257
} from '@huchenme/github-trending';
@@ -233,6 +265,7 @@ fetchDevelopers({ language: 'javascript' }).then(developers => {
233265
});
234266

235267
console.log(languages);
268+
console.log(spokenLanguages);
236269
```
237270

238271
### API
@@ -244,32 +277,58 @@ List all languages
244277
```js
245278
[
246279
{
247-
id: '1c-enterprise',
280+
urlParam: '1c-enterprise',
248281
name: '1C Enterprise',
249282
},
250283
{
251-
id: 'abap',
284+
urlParam: 'abap',
252285
name: 'ABAP',
253286
},
254287
{
255-
id: 'abnf',
288+
urlParam: 'abnf',
256289
name: 'ABNF',
257290
},
258291
{
259-
id: 'actionscript',
292+
urlParam: 'actionscript',
260293
name: 'ActionScript',
261294
},
262295
];
263296
```
264297

298+
#### spokenLanguages
299+
300+
List all spoken languages
301+
302+
```js
303+
[
304+
{
305+
urlParam: 'ab',
306+
name: 'Abkhazian',
307+
},
308+
{
309+
urlParam: 'aa',
310+
name: 'Afar',
311+
},
312+
{
313+
urlParam: 'af',
314+
name: 'Afrikaans',
315+
},
316+
{
317+
urlParam: 'ak',
318+
name: 'Akan',
319+
},
320+
];
321+
```
322+
265323
#### fetchRepositories(params)
266324

267325
Receive an array of trending repositories.
268326

269327
**params**:
270328

271-
- `language`: possible values are the the ones from `fetchAllLanguages()` or [just find here](./src/languages.ts).
329+
- `language`: possible values are the the ones from `languages` or [just find here](./src/languages.json).
272330
- `since`: `daily`, `weekly` or `monthly`, default to `daily`.
331+
- `spokenLanguageCode`: possible values are the the ones from `spokenLanguages` or [just find here](./src/spoken-languages.json).
273332

274333
```js
275334
[
@@ -303,7 +362,7 @@ Receive an array of trending developers.
303362

304363
**params**:
305364

306-
- `language`: possible values are the the ones from `fetchAllLanguages()` or [just find here](languages.json).
365+
- `language`: possible values are the the ones from `languages` or [just find here](languages.json).
307366
- `since`: `daily`, `weekly` or `monthly`, default to `daily`.
308367

309368
```js

apiary.apib

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,13 @@ HOST: https://github-trending-api.now.sh/
55

66
GitHub Trending API is a simple API to fetch trending repositories and developers.
77

8-
## Repositories [/repositories{?language,since}]
8+
## Repositories [/repositories{?language,since,spoken_language_code}]
99

1010
+ Parameters
1111
+ language (string, optional) - Programming language
1212
+ since (string, optional) - Trending period: `daily`, `weekly`, `monthly`
1313
+ Default: `daily`
14+
+ spoken_language_code (string, optional) - Spoken language
1415

1516
### List Trending Repositories [GET]
1617

@@ -23,6 +24,8 @@ GitHub Trending API is a simple API to fetch trending repositories and developer
2324
"avatar": "https://github.com/xingshaocheng.png",
2425
"url": "https://github.com/xingshaocheng/architect-awesome",
2526
"description": "后端架构师技术图谱",
27+
"language": "Go",
28+
"languageColor": "#3572A5",
2629
"stars": 7333,
2730
"forks": 1546,
2831
"currentPeriodStars": 1528,
@@ -59,6 +62,8 @@ GitHub Trending API is a simple API to fetch trending repositories and developer
5962
"avatar": "https://github.com/davideuler.png",
6063
"url": "https://github.com/davideuler/architecture.of.internet-product",
6164
"description": "互联网公司技术架构,微信/淘宝/腾讯/阿里/美团点评/百度/微博/Google/Facebook/Amazon/eBay的架构,欢迎PR补充",
65+
"language": "Go",
66+
"languageColor": "#3572A5",
6267
"stars": 2763,
6368
"forks": 416,
6469
"currentPeriodStars": 1427,
@@ -178,3 +183,28 @@ GitHub Trending API is a simple API to fetch trending repositories and developer
178183
"name": "ActionScript"
179184
}
180185
]
186+
187+
## Spoken Languages Collection [/spoken_languages]
188+
189+
### List Spoken Languages [GET]
190+
191+
+ Response 200 (application/json)
192+
193+
[
194+
{
195+
"urlParam": "ab",
196+
"name": "Abkhazian"
197+
},
198+
{
199+
"urlParam": "aa",
200+
"name": "Afar"
201+
},
202+
{
203+
"urlParam": "af",
204+
"name": "Afrikaans"
205+
},
206+
{
207+
"urlParam": "ak",
208+
"name": "Akan"
209+
},
210+
]

package.json

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
},
2727
"homepage": "https://github.com/huchenme/github-trending-api#readme",
2828
"dependencies": {
29-
"@babel/runtime": "^7.7.2",
29+
"@babel/runtime": "^7.7.7",
3030
"cheerio": "^1.0.0-rc.3",
3131
"cors": "^2.8.5",
3232
"express": "^4.17.1",
@@ -37,31 +37,30 @@
3737
"opencollective-postinstall": "^2.0.2"
3838
},
3939
"devDependencies": {
40-
"@babel/core": "^7.7.2",
41-
"@babel/node": "^7.7.0",
42-
"@babel/plugin-proposal-class-properties": "^7.7.0",
43-
"@babel/plugin-proposal-object-rest-spread": "^7.6.2",
44-
"@babel/plugin-transform-runtime": "^7.6.2",
45-
"@babel/preset-env": "^7.7.1",
46-
"@babel/preset-typescript": "^7.7.2",
40+
"@babel/core": "^7.7.7",
41+
"@babel/node": "^7.7.7",
42+
"@babel/plugin-proposal-class-properties": "^7.7.4",
43+
"@babel/plugin-proposal-object-rest-spread": "^7.7.7",
44+
"@babel/plugin-transform-runtime": "^7.7.6",
45+
"@babel/preset-env": "^7.7.7",
4746
"babel-jest": "^24.9.0",
4847
"babel-plugin-lodash": "^3.3.4",
4948
"doctoc": "^1.4.0",
50-
"eslint": "^6.6.0",
51-
"eslint-config-kentcdodds": "14.6.1",
52-
"husky": "^3.0.9",
49+
"eslint": "^6.8.0",
50+
"eslint-config-kentcdodds": "14.7.0",
51+
"husky": "^3.1.0",
5352
"jest": "^24.9.0",
54-
"lint-staged": "^9.4.2",
55-
"nodemon": "^1.19.4",
53+
"lint-staged": "^9.5.0",
54+
"nodemon": "^2.0.2",
5655
"npm-run-all": "^4.1.5",
5756
"prettier": "^1.19.1",
5857
"rimraf": "^3.0.0",
59-
"rollup": "^1.26.4",
58+
"rollup": "^1.27.14",
6059
"rollup-plugin-babel": "^4.3.3",
6160
"rollup-plugin-json": "^4.0.0",
6261
"rollup-plugin-node-resolve": "^5.2.0",
63-
"rollup-plugin-terser": "^5.1.2",
64-
"semantic-release": "^15.13.30",
62+
"rollup-plugin-terser": "^5.1.3",
63+
"semantic-release": "^15.14.0",
6564
"travis-deploy-once": "^5.0.11"
6665
},
6766
"scripts": {

src/fetch.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,9 @@ function removeDefaultAvatarSize(src) {
1919
export async function fetchRepositories({
2020
language = '',
2121
since = 'daily',
22+
spokenLanguage = '',
2223
} = {}) {
23-
const url = `${GITHUB_URL}/trending/${language}?since=${since}`;
24+
const url = `${GITHUB_URL}/trending/${language}?since=${since}&spoken_language_code=${spokenLanguage}`;
2425
const data = await fetch(url);
2526
const $ = cheerio.load(await data.text());
2627
return (

src/index.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
import fetch from 'node-fetch';
2-
import { sample, sampleSize } from 'lodash';
2+
import { sample, sampleSize, snakeCase } from 'lodash';
33
import languages from './languages.json';
4+
import spokenLanguages from './spoken-languages.json';
45

56
const SERVER_URL = 'https://github-trending-api.now.sh';
67

78
function buildUrl(baseUrl, params = {}) {
89
const queryString = Object.keys(params)
910
.filter(key => params[key])
10-
.map(key => `${key}=${params[key]}`)
11+
.map(key => `${snakeCase(key)}=${params[key]}`)
1112
.join('&');
1213

1314
return queryString === '' ? baseUrl : `${baseUrl}?${queryString}`;
@@ -47,4 +48,4 @@ export async function fetchRandomRepositories(size = 1, params) {
4748
return sampleSize(json, size);
4849
}
4950

50-
export { languages };
51+
export { languages, spokenLanguages };

src/server.js

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import cacheControl from 'express-cache-controller';
55
import cors from 'cors';
66

77
import languages from './languages.json';
8+
import spokenLanguages from './spoken-languages.json';
9+
810
import { fetchRepositories, fetchDevelopers } from './fetch';
911

1012
const PORT = process.env.PORT || 8080;
@@ -24,6 +26,13 @@ app.get('/languages', (req, res) => {
2426
res.json(languages);
2527
});
2628

29+
app.get('/spoken_languages', (req, res) => {
30+
res.cacheControl = {
31+
maxAge: 60 * 60 * 24,
32+
};
33+
res.json(spokenLanguages);
34+
});
35+
2736
app.get('(/|/repositories)', async (req, res) => {
2837
try {
2938
const parsedUrl = url.parse(req.originalUrl);
@@ -36,9 +45,9 @@ app.get('(/|/repositories)', async (req, res) => {
3645
}
3746
}
3847

39-
const { language, since } = params;
48+
const { language, since, spoken_language_code: spokenLanguage } = params;
4049
const cacheKey = `repositories::${language || 'nolang'}::${since ||
41-
'daily'}`;
50+
'daily'}::${spokenLanguage || 'nolang'}}`;
4251
const cacheKeyPerm = `perm::${cacheKey}`;
4352
const cached = cache.get(cacheKey);
4453
const cachedPerm = cache.get(cacheKeyPerm);
@@ -50,7 +59,7 @@ app.get('(/|/repositories)', async (req, res) => {
5059
if (Boolean(cached) && cached.length > 0) {
5160
return res.json(cached);
5261
}
53-
const data = await fetchRepositories({ language, since });
62+
const data = await fetchRepositories({ language, since, spokenLanguage });
5463
if (data && data.length > 0) {
5564
cache.put(cacheKey, data, 1000 * 3600); // Store for a hour
5665
cache.put(cacheKeyPerm, data);

0 commit comments

Comments
 (0)