Skip to content

Commit

Permalink
feat: add support for filter spoken languages
Browse files Browse the repository at this point in the history
Closes #84
  • Loading branch information
huchenme committed Dec 29, 2019
1 parent c1859de commit 0c92b6c
Show file tree
Hide file tree
Showing 8 changed files with 1,674 additions and 739 deletions.
83 changes: 71 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,13 @@
- [Trending Repositories](#trending-repositories)
- [Trending Developers](#trending-developers)
- [List Languages](#list-languages)
- [List Spoken Languages](#list-spoken-languages)
- [NPM Package](#npm-package)
- [Install](#install)
- [Usage](#usage)
- [API](#api)
- [languages](#languages)
- [spokenLanguages](#spokenlanguages)
- [fetchRepositories(params)](#fetchrepositoriesparams)
- [fetchDevelopers(params)](#fetchdevelopersparams)
- [Contributors](#contributors)
Expand Down Expand Up @@ -76,6 +78,7 @@
- **[GitNews](https://git.news)**: Trending repositories from GitHub, HackerNews & Reddit (Mobile & Desktop).
- **[mini-github](https://github.com/kezhenxu94/mini-github)**: GitHub WeChat Mini Program (可能是全网功能最全的 GitHub 微信小程序).
- **[StarGit](https://github.com/theArgex/stargit2.0)** ([Website](https://stargit.xyz)): Find trending repositories from GitHub. Built with Angular.
- **[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.

## Backers

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

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

**Response:**

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

Receive popular languages and all languages.

**URL Endpoint:**

https://github-trending-api.now.sh/languages
Expand All @@ -187,24 +189,53 @@ https://github-trending-api.now.sh/languages
```json
[
{
"id": "1c-enterprise",
"urlParam": "1c-enterprise",
"name": "1C Enterprise"
},
{
"id": "abap",
"urlParam": "abap",
"name": "ABAP"
},
{
"id": "abnf",
"urlParam": "abnf",
"name": "ABNF"
},
{
"id": "actionscript",
"urlParam": "actionscript",
"name": "ActionScript"
}
]
```

### List Spoken Languages

**URL Endpoint:**

https://github-trending-api.now.sh/spoken_languages

**Response:**

```json
[
{
"urlParam": "ab",
"name": "Abkhazian"
},
{
"urlParam": "aa",
"name": "Afar"
},
{
"urlParam": "af",
"name": "Afrikaans"
},
{
"urlParam": "ak",
"name": "Akan"
}
]
```

## NPM Package

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

console.log(languages);
console.log(spokenLanguages);
```

### API
Expand All @@ -244,32 +277,58 @@ List all languages
```js
[
{
id: '1c-enterprise',
urlParam: '1c-enterprise',
name: '1C Enterprise',
},
{
id: 'abap',
urlParam: 'abap',
name: 'ABAP',
},
{
id: 'abnf',
urlParam: 'abnf',
name: 'ABNF',
},
{
id: 'actionscript',
urlParam: 'actionscript',
name: 'ActionScript',
},
];
```

#### spokenLanguages

List all spoken languages

```js
[
{
urlParam: 'ab',
name: 'Abkhazian',
},
{
urlParam: 'aa',
name: 'Afar',
},
{
urlParam: 'af',
name: 'Afrikaans',
},
{
urlParam: 'ak',
name: 'Akan',
},
];
```

#### fetchRepositories(params)

Receive an array of trending repositories.

**params**:

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

```js
[
Expand Down Expand Up @@ -303,7 +362,7 @@ Receive an array of trending developers.

**params**:

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

```js
Expand Down
32 changes: 31 additions & 1 deletion apiary.apib
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@ HOST: https://github-trending-api.now.sh/

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

## Repositories [/repositories{?language,since}]
## Repositories [/repositories{?language,since,spoken_language_code}]

+ Parameters
+ language (string, optional) - Programming language
+ since (string, optional) - Trending period: `daily`, `weekly`, `monthly`
+ Default: `daily`
+ spoken_language_code (string, optional) - Spoken language

### List Trending Repositories [GET]

Expand All @@ -23,6 +24,8 @@ GitHub Trending API is a simple API to fetch trending repositories and developer
"avatar": "https://github.com/xingshaocheng.png",
"url": "https://github.com/xingshaocheng/architect-awesome",
"description": "后端架构师技术图谱",
"language": "Go",
"languageColor": "#3572A5",
"stars": 7333,
"forks": 1546,
"currentPeriodStars": 1528,
Expand Down Expand Up @@ -59,6 +62,8 @@ GitHub Trending API is a simple API to fetch trending repositories and developer
"avatar": "https://github.com/davideuler.png",
"url": "https://github.com/davideuler/architecture.of.internet-product",
"description": "互联网公司技术架构,微信/淘宝/腾讯/阿里/美团点评/百度/微博/Google/Facebook/Amazon/eBay的架构,欢迎PR补充",
"language": "Go",
"languageColor": "#3572A5",
"stars": 2763,
"forks": 416,
"currentPeriodStars": 1427,
Expand Down Expand Up @@ -178,3 +183,28 @@ GitHub Trending API is a simple API to fetch trending repositories and developer
"name": "ActionScript"
}
]

## Spoken Languages Collection [/spoken_languages]

### List Spoken Languages [GET]

+ Response 200 (application/json)

[
{
"urlParam": "ab",
"name": "Abkhazian"
},
{
"urlParam": "aa",
"name": "Afar"
},
{
"urlParam": "af",
"name": "Afrikaans"
},
{
"urlParam": "ak",
"name": "Akan"
},
]
31 changes: 15 additions & 16 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
},
"homepage": "https://github.com/huchenme/github-trending-api#readme",
"dependencies": {
"@babel/runtime": "^7.7.2",
"@babel/runtime": "^7.7.7",
"cheerio": "^1.0.0-rc.3",
"cors": "^2.8.5",
"express": "^4.17.1",
Expand All @@ -37,31 +37,30 @@
"opencollective-postinstall": "^2.0.2"
},
"devDependencies": {
"@babel/core": "^7.7.2",
"@babel/node": "^7.7.0",
"@babel/plugin-proposal-class-properties": "^7.7.0",
"@babel/plugin-proposal-object-rest-spread": "^7.6.2",
"@babel/plugin-transform-runtime": "^7.6.2",
"@babel/preset-env": "^7.7.1",
"@babel/preset-typescript": "^7.7.2",
"@babel/core": "^7.7.7",
"@babel/node": "^7.7.7",
"@babel/plugin-proposal-class-properties": "^7.7.4",
"@babel/plugin-proposal-object-rest-spread": "^7.7.7",
"@babel/plugin-transform-runtime": "^7.7.6",
"@babel/preset-env": "^7.7.7",
"babel-jest": "^24.9.0",
"babel-plugin-lodash": "^3.3.4",
"doctoc": "^1.4.0",
"eslint": "^6.6.0",
"eslint-config-kentcdodds": "14.6.1",
"husky": "^3.0.9",
"eslint": "^6.8.0",
"eslint-config-kentcdodds": "14.7.0",
"husky": "^3.1.0",
"jest": "^24.9.0",
"lint-staged": "^9.4.2",
"nodemon": "^1.19.4",
"lint-staged": "^9.5.0",
"nodemon": "^2.0.2",
"npm-run-all": "^4.1.5",
"prettier": "^1.19.1",
"rimraf": "^3.0.0",
"rollup": "^1.26.4",
"rollup": "^1.27.14",
"rollup-plugin-babel": "^4.3.3",
"rollup-plugin-json": "^4.0.0",
"rollup-plugin-node-resolve": "^5.2.0",
"rollup-plugin-terser": "^5.1.2",
"semantic-release": "^15.13.30",
"rollup-plugin-terser": "^5.1.3",
"semantic-release": "^15.14.0",
"travis-deploy-once": "^5.0.11"
},
"scripts": {
Expand Down
3 changes: 2 additions & 1 deletion src/fetch.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@ function removeDefaultAvatarSize(src) {
export async function fetchRepositories({
language = '',
since = 'daily',
spokenLanguage = '',
} = {}) {
const url = `${GITHUB_URL}/trending/${language}?since=${since}`;
const url = `${GITHUB_URL}/trending/${language}?since=${since}&spoken_language_code=${spokenLanguage}`;
const data = await fetch(url);
const $ = cheerio.load(await data.text());
return (
Expand Down
7 changes: 4 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import fetch from 'node-fetch';
import { sample, sampleSize } from 'lodash';
import { sample, sampleSize, snakeCase } from 'lodash';
import languages from './languages.json';
import spokenLanguages from './spoken-languages.json';

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

function buildUrl(baseUrl, params = {}) {
const queryString = Object.keys(params)
.filter(key => params[key])
.map(key => `${key}=${params[key]}`)
.map(key => `${snakeCase(key)}=${params[key]}`)
.join('&');

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

export { languages };
export { languages, spokenLanguages };
15 changes: 12 additions & 3 deletions src/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import cacheControl from 'express-cache-controller';
import cors from 'cors';

import languages from './languages.json';
import spokenLanguages from './spoken-languages.json';

import { fetchRepositories, fetchDevelopers } from './fetch';

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

app.get('/spoken_languages', (req, res) => {
res.cacheControl = {
maxAge: 60 * 60 * 24,
};
res.json(spokenLanguages);
});

app.get('(/|/repositories)', async (req, res) => {
try {
const parsedUrl = url.parse(req.originalUrl);
Expand All @@ -36,9 +45,9 @@ app.get('(/|/repositories)', async (req, res) => {
}
}

const { language, since } = params;
const { language, since, spoken_language_code: spokenLanguage } = params;
const cacheKey = `repositories::${language || 'nolang'}::${since ||
'daily'}`;
'daily'}::${spokenLanguage || 'nolang'}}`;
const cacheKeyPerm = `perm::${cacheKey}`;
const cached = cache.get(cacheKey);
const cachedPerm = cache.get(cacheKeyPerm);
Expand All @@ -50,7 +59,7 @@ app.get('(/|/repositories)', async (req, res) => {
if (Boolean(cached) && cached.length > 0) {
return res.json(cached);
}
const data = await fetchRepositories({ language, since });
const data = await fetchRepositories({ language, since, spokenLanguage });
if (data && data.length > 0) {
cache.put(cacheKey, data, 1000 * 3600); // Store for a hour
cache.put(cacheKeyPerm, data);
Expand Down
Loading

0 comments on commit 0c92b6c

Please sign in to comment.