Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add lastfm #3794

Merged
merged 7 commits into from
Feb 4, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions assets/radar-rules.js
Original file line number Diff line number Diff line change
Expand Up @@ -1040,4 +1040,27 @@
},
],
},
'last.fm': {
_name: 'Last.fm',
www: [
{
title: '用户播放记录',
docs: 'https://docs.rsshub.app/multimedia.html#last-fm',
source: ['/user/:user', '/user/:user/*'],
target: '/lastfm/recent/:user',
},
{
title: '用户 Love 记录',
docs: 'https://docs.rsshub.app/multimedia.html#last-fm',
source: ['/user/:user', '/user/:user/*'],
target: '/lastfm/loved/:user',
},
{
title: '站内 Top 榜单',
docs: 'https://docs.rsshub.app/multimedia.html#last-fm',
source: '/charts',
target: '/lastfm/top',
},
],
},
});
4 changes: 4 additions & 0 deletions docs/install/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -397,5 +397,9 @@ RSSHub 支持 `memory` 和 `redis` 两种缓存方式
- `FANFOU_USERNAME`: 饭否登录用户名、邮箱、手机号
- `FANFOU_PASSWORD`: 饭否密码

- Last.fm 全部路由: [申请地址](https://www.last.fm/api/)

- `LASTFM_API_KEY`: Last.fm API Key

- 北大未名 BBS 全站十大
- `PKUBBS_COOKIE`: BBS 注册用户登录后的 Cookie 值,获取方式:1.登录后打开论坛首页 2. 打开控制台 3. 刷新 4. 找到 <https://bbs.pku.edu.cn/v2/home.php> 请求 5. 找到请求头中的 Cookie
14 changes: 14 additions & 0 deletions docs/multimedia.md
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,20 @@ pageClass: routes

<Route author="junfengP" example="/javlibrary/userposts/siccalol" path="/javlibrary/userposts/:uid" :paramsDesc="['用户 id,即用户名称']" />

## Last.fm

### 用户播放记录

<Route author="hoilc" example="/lastfm/recent/yeFoenix" path="/lastfm/recent/:user" :paramsDesc="['Last.fm 用户名']" radar="1" />

### 用户 Love 记录

<Route author="hoilc" example="/lastfm/loved/yeFoenix" path="/lastfm/loved/:user" :paramsDesc="['Last.fm 用户名']" radar="1" />

### 站内 Top 榜单

<Route author="hoilc" example="/lastfm/top/spain" path="/lastfm/top/:country?" :paramsDesc="['国家或地区, 需要符合`ISO 3166-1`的英文全称, 可参考`https://zh.wikipedia.org/wiki/ISO_3166-1二位字母代码#正式分配代码`']" radar="1" />

## Mp4Ba

### 影视分类
Expand Down
3 changes: 3 additions & 0 deletions lib/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,9 @@ const calculateValue = () => {
username: envs.FANFOU_USERNAME,
password: envs.FANFOU_PASSWORD,
},
lastfm: {
api_key: envs.LASTFM_API_KEY,
},
pkubbs: {
cookie: envs.PKUBBS_COOKIE,
},
Expand Down
5 changes: 5 additions & 0 deletions lib/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -2187,6 +2187,11 @@ router.get('/netease/ds/:id', require('./routes/netease/ds'));
// javlibrary
router.get('/javlibrary/userposts/:uid', require('./routes/javlibrary/userposts'));

// Last.FM
router.get('/lastfm/recent/:user', require('./routes/lastfm/recent'));
router.get('/lastfm/loved/:user', require('./routes/lastfm/loved'));
router.get('/lastfm/top/:country?', require('./routes/lastfm/top'));

// piapro
router.get('/piapro/user/:pid', require('./routes/piapro/user'));
router.get('/piapro/public/:type/:tag?/:category?', require('./routes/piapro/public'));
Expand Down
27 changes: 27 additions & 0 deletions lib/routes/lastfm/loved.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
const got = require('@/utils/got');
const config = require('@/config').value;

module.exports = async (ctx) => {
if (!config.lastfm || !config.lastfm.api_key) {
throw 'Last.fm RSS is disabled due to the lack of <a href="https://docs.rsshub.app/install/#bu-fen-rss-mo-kuai-pei-zhi">relevant config</a>';
}

const user = ctx.params.user;
const api_key = config.lastfm.api_key;

const response = await got(`http://ws.audioscrobbler.com/2.0/?method=user.getlovedtracks&user=${user}&api_key=${api_key}&format=json`);
const data = response.data;
const list = data.lovedtracks.track;

ctx.state.data = {
title: `Loved Tracks by ${data.lovedtracks['@attr'].user} - Last.fm`,
link: `https://www.last.fm/user/${user}/loved`,
item: list.map((item) => ({
title: `${item.name} - ${item.artist.name}`,
author: item.artist.name,
description: `<img src="${item.image.slice(-1)[0]['#text']}" />`,
pubDate: new Date(item.date.uts * 1000),
link: item.url,
})),
};
};
27 changes: 27 additions & 0 deletions lib/routes/lastfm/recent.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
const got = require('@/utils/got');
const config = require('@/config').value;

module.exports = async (ctx) => {
if (!config.lastfm || !config.lastfm.api_key) {
throw 'Last.fm RSS is disabled due to the lack of <a href="https://docs.rsshub.app/install/#bu-fen-rss-mo-kuai-pei-zhi">relevant config</a>';
}

const user = ctx.params.user;
const api_key = config.lastfm.api_key;

const response = await got(`http://ws.audioscrobbler.com/2.0/?method=user.getrecenttracks&user=${user}&api_key=${api_key}&format=json`);
const data = response.data;
const list = data.recenttracks.track;

ctx.state.data = {
title: `Recent Tracks by ${data.recenttracks['@attr'].user} - Last.fm`,
link: `https://www.last.fm/user/${user}/library`,
item: list.map((item) => ({
title: `${item.name} - ${item.artist['#text']}`,
author: item.artist['#text'],
description: `<img src="${item.image.slice(-1)[0]['#text']}" />`,
pubDate: new Date(item.date.uts * 1000),
link: item.url,
})),
};
};
27 changes: 27 additions & 0 deletions lib/routes/lastfm/top.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
const got = require('@/utils/got');
const config = require('@/config').value;

module.exports = async (ctx) => {
if (!config.lastfm || !config.lastfm.api_key) {
throw 'Last.fm RSS is disabled due to the lack of <a href="https://docs.rsshub.app/install/#bu-fen-rss-mo-kuai-pei-zhi">relevant config</a>';
}

const country = ctx.params.country;
const api_key = config.lastfm.api_key;

const response = await got(`http://ws.audioscrobbler.com/2.0/?method=${country ? 'geo.gettoptracks' : 'chart.gettoptracks'}${country ? '&country=' + country : ''}&api_key=${api_key}&format=json`);
const data = response.data;
const list = data.tracks.track;

ctx.state.data = {
title: `Top Tracks${country ? ' in ' + data.tracks['@attr'].country : ''} - Last.fm`,
link: 'https://www.last.fm/charts#top-tracks',
item: list.map((item) => ({
title: `${item.name} - ${item.artist.name}`,
author: item.artist.name,
description: `<img src="${item.image.slice(-1)[0]['#text']}" />`,
pubDate: new Date(),
link: item.url,
})),
};
};