ncmapi

package module
v0.0.0-...-6e2e2aa Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 24, 2022 License: MIT Imports: 15 Imported by: 0

README

EN 👈

ncmapi

GitHub go.mod Go version GoDoc Go Report Card GitHub

Go版本网易云音乐API. 为什么会有这个项目? 两点, 一来是没有合适的服务器来运行NeteaseCloudMusicApi; 二来就是可以更好的整合进其他go项目.


用法

安装

$ go get -u github.com/benmooo/ncmapi

使用

package main

import (
	"fmt"
	ncmapi "github.com/benmooo/ncmapi"
)

func main() {
	api := ncmapi.Default()

	res, _ := api.Search("mota")
	fmt.Println(res)
}

配置

func main() {
	api := ncmapi.New(
		&ncmapi.NeteaseAPIConfig{
			CacheDefaultExpiration: time.Minute * 1,
			CacheCleanupInterval:   time.Minute * 2,
			PreserveCookies:        true,
		},
	)

	// ...
}

文档

大多数函数都有很好注释, 如果发现有地方不太清楚, 可以参考 这里

原理

ncmapi 主要由三部分组成

  • api: 接受APIRequest, 根据这个请求的id决定发送http请求或者从缓存中取数据.
  • client: 模拟客户端, 负责点缀APIRequest, 加密payload, 然后发送http请求, 解密response等等.
  • store: 用于缓存

贡献

欢迎PR.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var APIRoutes = map[string]string{}/* 235 elements not displayed */

This APIRoutes is generated by routes.sh

View Source
var BaseURL = url.URL{
	Scheme: "https",
	Host:   "music.163.com",
}

Functions

This section is empty.

Types

type APIClient

type APIClient interface {
	Request(areq *apitypes.APIRequest) (*apitypes.APIResponse, error)
	Req(method string, url string, data apitypes.H, opt *apitypes.RequestOption) (*apitypes.APIResponse, error)
	RequestID(areq *apitypes.APIRequest) string
	ReqID(method string, url string, data apitypes.H, opt *apitypes.RequestOption) string

	// cookies synchronization
	Cookies(u *url.URL) []*http.Cookie
	HasCookie(u *url.URL, name string) bool
}

This interface defines functions that a client model possess

type APIResponse

type APIResponse = apitypes.APIResponse

type NeteaseAPI

type NeteaseAPI struct {
	Client APIClient
	Store  Store
	Config *NeteaseAPIConfig
}

func Default

func Default() *NeteaseAPI

func New

func New(cfg *NeteaseAPIConfig) *NeteaseAPI

func (*NeteaseAPI) Album

func (api *NeteaseAPI) Album(id int) (*APIResponse, error)

说明 : 调用此接口 , 传入专辑 id, 可获得专辑内容 required 必选参数 : id: 专辑 id

func (*NeteaseAPI) AlbumSub

func (api *NeteaseAPI) AlbumSub(id, t int) (*APIResponse, error)

说明 : 调用此接口,可收藏/取消收藏专辑 required id : 专辑 id t : 1 为收藏,其他为取消收藏

func (*NeteaseAPI) AlbumSublist

func (api *NeteaseAPI) AlbumSublist(opts ...hm) (*APIResponse, error)

说明 : 调用此接口 , 可获得已收藏专辑列表 optional limit: 取出数量 , 默认为 25 offset: 偏移数量 , 用于分页 , 如 :( 页数 -1)*25, 其中 25 为 limit 的值 , 默认 为 0

func (*NeteaseAPI) ArtistSongs

func (api *NeteaseAPI) ArtistSongs(id int, opts ...apitypes.H) (*APIResponse, error)

说明 : 调用此接口,可获取歌手全部歌曲 必选参数 : required id : 歌手 id optional: order : hot ,time 按照热门或者时间排序 limit: 取出歌单数量 , 默认为 50 offset: 偏移数量 , 用于分页 , 如 :( 评论页数 -1)*50, 其中 50 为 limit 的值

func (*NeteaseAPI) ArtistSub

func (api *NeteaseAPI) ArtistSub(id, t int) (*APIResponse, error)

说明 : 调用此接口,可收藏歌手 required id : 歌手 id t:操作,1 为收藏,其他为取消收藏

func (*NeteaseAPI) ArtistSublist

func (api *NeteaseAPI) ArtistSublist(opts ...hm) (*APIResponse, error)

说明 : 调用此接口,可获取收藏的歌手列表

func (*NeteaseAPI) ArtistTopSong

func (api *NeteaseAPI) ArtistTopSong(id int) (*APIResponse, error)

说明 : 调用此接口,可获取歌手热门50首歌曲 required id : 歌手 id

func (*NeteaseAPI) CheckMusic

func (api *NeteaseAPI) CheckMusic(id int, opts ...hm) (*APIResponse, error)

说明: 调用此接口,传入歌曲 id, 可获取音乐是否可用,返回 { success: true, message: 'ok' } 或者 { success: false, message: '亲爱的,暂无版权' } requried 必选参数 : id : 歌曲 id optional 可选参数 : br: 码率,默认设置了 999000 即最大码率,如果要 320k 则可设置为 320000,其他类推

func (*NeteaseAPI) CloudSearch

func (api *NeteaseAPI) CloudSearch(keyword string, opts ...hm) (*APIResponse, error)

func (*NeteaseAPI) Comment

func (api *NeteaseAPI) Comment(t int, commentType apitypes.ResourceTypeCode, id int, opts ...apitypes.H) (*APIResponse, error)

说明 : 调用此接口,可发送评论或者删除评论

New Comment required t:1 发送, 2 回复 type: 数字,资源类型,对应歌曲,mv,专辑,歌单,电台,视频对应以下类型 id:对应资源 id content :要发送的内容 commentId :回复的评论id (回复评论时必填)

Delete Comment required t:0 删除 type: 数字,资源类型,对应歌曲,mv,专辑,歌单,电台,视频对应以下类型 id:对应资源 id content :内容 id,可通过 /comment/mv 等接口获取

func (*NeteaseAPI) CommentCreate

func (api *NeteaseAPI) CommentCreate(rid int, rt apitypes.ResourceTypeCode, cmt string) (*APIResponse, error)

required rid: resource id rt: resource type cmt: comment body

func (*NeteaseAPI) CommentDel

func (api *NeteaseAPI) CommentDel(rid int, rt apitypes.ResourceTypeCode, cmtid int) (*APIResponse, error)

required rid: resource id rt: resource type cmtid: comment id

func (*NeteaseAPI) CommentHot

func (api *NeteaseAPI) CommentHot(id int, t apitypes.ResourceTypeCode, opts ...apitypes.H) (*APIResponse, error)

说明 : 调用此接口 , 传入 type, 资源 id 可获得对应资源热门评论 ( 不需要登录 ) required id : 资源 id type: 数字 , 资源类型 , 对应歌曲 , mv, 专辑 , 歌单 , 电台, 视频对应以下类型 0: 歌曲 1: mv 2: 歌单 3: 专辑 4: 电台 5: 视频

optional 可选参数 : limit: 取出评论数量 , 默认为 20 offset: 偏移数量 , 用于分页 , 如 :( 评论页数 -1)*20, 其中 20 为 limit 的值 before: 分页参数,取上一页最后一项的 time 获取下一页数据(获取超过5000条评论的时候需要用到)

func (*NeteaseAPI) CommentNew

func (api *NeteaseAPI) CommentNew(id int, t apitypes.ResourceTypeCode, opts ...hm) (*APIResponse, error)

新版评论接口 说明 : 调用此接口 , 传入资源类型和资源id,以及排序方式,可获取对应资源的评论

required id : 资源 id, 如歌曲 id,mv id type: 数字 , 资源类型 , 对应歌曲 , mv, 专辑 , 歌单 , 电台, 视频对应以下类型

optional pageNo:分页参数,第N页,默认为1 pageSize:分页参数,每页多少条数据,默认20 sortType: 排序方式,1:按推荐排序,2:按热度排序,3:按时间排序 cursor: 当sortType为3时且页数不是第一页时需传入,值为上一条数据的time

func (*NeteaseAPI) CommentRe

func (api *NeteaseAPI) CommentRe(rid int, rt apitypes.ResourceTypeCode, reid int, cmt string) (*APIResponse, error)

required rid: resource id rt: resource type reid: the comment id of reply to cmt: comment body

func (*NeteaseAPI) DailySignin

func (api *NeteaseAPI) DailySignin(opts ...apitypes.H) (*APIResponse, error)

说明 : 调用此接口 , 传入签到类型 ( 可不传 , 默认安卓端签到 ), 可签到 ( 需要登录 ), 其中安卓端签到可获得 3 点经验 , web/PC 端签到可获得 2 点经验

optional 可选参数 : type: 签到类型 , 默认 0, 其中 0 为安卓端签到 ,1 为 web/PC 签到

func (*NeteaseAPI) FmTrash

func (api *NeteaseAPI) FmTrash(id int, opts ...hm) (*APIResponse, error)

说明 : 调用此接口 , 传入音乐 id, 可把该音乐从私人 FM 中移除至垃圾桶

required id: 歌曲 id

func (*NeteaseAPI) GetComment

func (api *NeteaseAPI) GetComment(id int, t apitypes.ResourceTypeCode, pageSize, pageNo, sortType, cursor int, showInner bool) (*APIResponse, error)

The function above is a reedition of https://github.com/Binaryify/NeteaseCloudMusicApi/module/comment_new.js which looks terrible, so here comes the alternative

说明 : 调用此接口 , 传入资源类型和资源id,以及排序方式,可获取对应资源的评论

required id : 资源 id, 如歌曲 id,mv id type: 数字 , 资源类型 , 对应歌曲 , mv, 专辑 , 歌单 , 电台, 视频对应以下类型

optional pageNo:分页参数,第N页,默认为1 pageSize:分页参数,每页多少条数据,默认20 sortType: 排序方式,1:按推荐排序,2:按热度排序,3:按时间排序 cursor: 当sortType为3时且页数不是第一页时需传入,值为上一条数据的time

func (*NeteaseAPI) HasCache

func (api *NeteaseAPI) HasCache(id string) bool

func (*NeteaseAPI) Like

func (api *NeteaseAPI) Like(id int, opts ...hm) (*APIResponse, error)

说明 : 调用此接口 , 传入音乐 id, 可喜欢该音乐

required 必选参数 : id: 歌曲 id

optional 可选参数 : like: 布尔值 , 默认为 true 即喜欢 , 若传 false, 则取消喜欢

func (*NeteaseAPI) LikeList

func (api *NeteaseAPI) LikeList(uid int) (*APIResponse, error)

说明 : 调用此接口 , 传入用户 id, 可获取已喜欢音乐id列表(id数组)

required 必选参数 : uid: 用户 id

func (*NeteaseAPI) LoginPhone

func (api *NeteaseAPI) LoginPhone(phone string, password string) (*APIResponse, error)

必选参数 : phone: 手机号码 password: 密码

可选参数 : countrycode: 国家码,用于国外手机号登录,例如美国传入:1 md5_password: md5加密后的密码,传入后 password 将失效

func (*NeteaseAPI) LoginRefresh

func (api *NeteaseAPI) LoginRefresh() (*APIResponse, error)

说明 : 调用此接口 , 可刷新登录状态

func (*NeteaseAPI) LoginStatus

func (api *NeteaseAPI) LoginStatus() (*APIResponse, error)

说明 : 调用此接口,可获取登录状态

func (*NeteaseAPI) Logout

func (api *NeteaseAPI) Logout() (*APIResponse, error)

说明 : 调用此接口 , 可退出登录

func (*NeteaseAPI) Lyric

func (api *NeteaseAPI) Lyric(id int) (*APIResponse, error)

说明 : 调用此接口 , 传入音乐 id 可获得对应音乐的歌词 ( 不需要登录 )

required 必选参数 : id: 音乐 id

func (*NeteaseAPI) PersonalFm

func (api *NeteaseAPI) PersonalFm() (*APIResponse, error)

说明 : 私人 FM( 需要登录 )

func (*NeteaseAPI) PlaylistCreate

func (api *NeteaseAPI) PlaylistCreate(name string, opts ...apitypes.H) (*APIResponse, error)

说明 : 调用此接口 , 传入歌单名字可新建歌单

required 必选参数 : name : 歌单名

optional privacy : 是否设置为隐私歌单,默认否,传'10'则设置成隐私歌单 type : 歌单类型,默认'NORMAL',传 'VIDEO'则为视频歌单

func (*NeteaseAPI) PlaylistDetail

func (api *NeteaseAPI) PlaylistDetail(id int, opts ...hm) (*APIResponse, error)

说明 : 歌单能看到歌单名字, 但看不到具体歌单内容 , 调用此接口 , 传入歌单 id, 可以获取对应歌单内的所有的音乐(未登录状态只能获取不完整的歌单,登录后是完整的), 但是返回的trackIds是完整的,tracks 则是不完整的, 可拿全部 trackIds 请求一次 song/detail 接口获取所有歌曲的详情 (https://github.com/Binaryify/NeteaseCloudMusicApi/issues/452)

required 必选参数 : id : 歌单 id

optional 可选参数 : s : 歌单最近的 s 个收藏者,默认为8

func (*NeteaseAPI) PlaylistTracks

func (api *NeteaseAPI) PlaylistTracks(op string, pid int, tracks []int) (*APIResponse, error)

说明 : 调用此接口 , 可以添加歌曲到歌单或者从歌单删除某首歌曲 ( 需要登录 )

required op: 从歌单增加单曲为 add, 删除为 del pid: 歌单 id tracks: 歌曲 id,可多个,用逗号隔开

func (*NeteaseAPI) PlaylistUpdate

func (api *NeteaseAPI) PlaylistUpdate(id int, name, desc string, tags []string) (*APIResponse, error)

说明 : 登录后调用此接口,可以更新用户歌单

required id:歌单id name:歌单名字 desc:歌单描述 tags:歌单tag ,多个用 `;` 隔开,只能用官方规定标签

func (*NeteaseAPI) RecommendResource

func (api *NeteaseAPI) RecommendResource() (*APIResponse, error)

说明 : 调用此接口 , 可获得每日推荐歌单 ( 需要登录 )

func (*NeteaseAPI) RecommendSongs

func (api *NeteaseAPI) RecommendSongs() (*APIResponse, error)

说明 : 调用此接口 , 可获得每日推荐歌曲 ( 需要登录 )

func (*NeteaseAPI) RelatedPlaylist

func (api *NeteaseAPI) RelatedPlaylist(id int) (*APIResponse, error)

说明 : 调用此接口,传入歌单 id 可获取相关歌单

required 必选参数 : id : 歌单 id

func (*NeteaseAPI) Req

func (api *NeteaseAPI) Req(method string, url string, data apitypes.H, opt *apitypes.RequestOption) (*apitypes.APIResponse, error)

func (*NeteaseAPI) Request

func (api *NeteaseAPI) Request(areq *apitypes.APIRequest) (*apitypes.APIResponse, error)

func (*NeteaseAPI) Scrobble

func (api *NeteaseAPI) Scrobble(id, sourceid int, opts ...apitypes.H) (*APIResponse, error)

说明 : 调用此接口 , 传入音乐 id, 来源 id,歌曲时间 time,更新听歌排行数据

requried 必选参数 : id: 歌曲 id sourceid: 歌单或专辑 id

optional 可选参数 : time: 歌曲播放时间,单位为秒

func (*NeteaseAPI) Search

func (api *NeteaseAPI) Search(keyword string, opts ...hm) (*APIResponse, error)

说明 : 调用此接口 , 传入搜索关键词可以搜索该音乐 / 专辑 / 歌手 / 歌单 / 用户 , 关键词可以多个 , 以空格隔开 , 如 " 周杰伦 搁浅 "( 不需要登录 ), 搜索获取的 mp3url 不能直接用 , 可通过 /song/url 接口传入歌曲 id 获取具体的播放链接

required 必选参数 : keywords : 关键词

optional 可选参数 : limit : 返回数量 , 默认为 30 offset : 偏移数量,用于分页 , 如 : 如 :( 页数 -1)*30, 其中 30 为 limit 的值 , 默认为 0 type: 搜索类型;默认为 1 即单曲 , 取值意义 : 1: 单曲, 10: 专辑, 100: 歌手, 1000: 歌单, 1002: 用户, 1004: MV, 1006: 歌词, 1009: 电台, 1014: 视频, 1018:综合

func (*NeteaseAPI) SearchAlbum

func (api *NeteaseAPI) SearchAlbum(keyword string, opts ...hm) (*apitypes.SearchAlbumResult, error)

func (*NeteaseAPI) SearchArtist

func (api *NeteaseAPI) SearchArtist(keyword string, opts ...hm) (*apitypes.SearchArtistResult, error)

func (*NeteaseAPI) SearchCollection

func (api *NeteaseAPI) SearchCollection(keyword string, opts ...hm) (*apitypes.SearchCollectionResult, error)

func (*NeteaseAPI) SearchDefault

func (api *NeteaseAPI) SearchDefault() (*APIResponse, error)

说明 : 调用此接口 , 可获取默认搜索关键词

func (*NeteaseAPI) SearchHot

func (api *NeteaseAPI) SearchHot() (*APIResponse, error)

说明 : 调用此接口,可获取热门搜索列表(简略)

func (*NeteaseAPI) SearchHotDetail

func (api *NeteaseAPI) SearchHotDetail() (*APIResponse, error)

说明 : 调用此接口,可获取热门搜索列表

func (*NeteaseAPI) SearchSong

func (api *NeteaseAPI) SearchSong(keyword string, opts ...hm) (*apitypes.SearchSongResult, error)

func (*NeteaseAPI) SearchSuggest

func (api *NeteaseAPI) SearchSuggest(keywords string, opts ...apitypes.H) (*APIResponse, error)

说明 : 调用此接口 , 传入搜索关键词可获得搜索建议 , 搜索结果同时包含单曲 , 歌手 , 歌单 ,mv 信息

required 必选参数 : keywords : 关键词

optional 可选参数 : type : 如果传 'mobile' 则返回移动端数据

func (*NeteaseAPI) SimiArtist

func (api *NeteaseAPI) SimiArtist(id int) (*APIResponse, error)

说明 : 调用此接口 , 传入歌手 id, 可获得相似歌手

requried 必选参数 : id: 歌手 id

func (*NeteaseAPI) SimiPlaylist

func (api *NeteaseAPI) SimiPlaylist(songId int, opts ...hm) (*APIResponse, error)

说明 : 调用此接口 , 传入歌曲 id, 可获得相似歌单

required 必选参数 : id: 歌曲 id

func (*NeteaseAPI) SimiSong

func (api *NeteaseAPI) SimiSong(songId int, opts ...hm) (*APIResponse, error)

说明 : 调用此接口 , 传入歌曲 id, 可获得相似歌曲

required 必选参数 : id: 歌曲 id

func (*NeteaseAPI) SongDetail

func (api *NeteaseAPI) SongDetail(id int, ids ...int) (*APIResponse, error)

说明 : 调用此接口 , 传入音乐 id(支持多个 id, 用 , 隔开), 可获得歌曲详情

requried 必选参数 : ids: 音乐 id, 如 ids=347230

func (*NeteaseAPI) SongUrl

func (api *NeteaseAPI) SongUrl(id int, ids ...int) (*APIResponse, error)

说明 : 使用歌单详情接口后 , 能得到的音乐的 id, 但不能得到的音乐 url, 调用此接口, 传入的音乐 id( 可多个 , 用逗号隔开 ), 可以获取对应的音乐的 url,未登录状态或者非会员返回试听片段(返回字段包含被截取的正常歌曲的开始时间和结束时间)

required 必选参数 : id : 音乐 id

optional 可选参数 : br: 码率,默认设置了 999000 即最大码率,如果要 320k 则可设置为 320000,其他类推

func (*NeteaseAPI) UserAccount

func (api *NeteaseAPI) UserAccount() (*APIResponse, error)

说明 : 登录后调用此接口 ,可获取用户账号信息

func (*NeteaseAPI) UserCloud

func (api *NeteaseAPI) UserCloud(opts ...hm) (*APIResponse, error)

说明 : 登录后调用此接口 , 可获取云盘数据 , 获取的数据没有对应 url, 需要再调用一 次 /song/url 获取 url

optional 可选参数 : limit : 返回数量 , 默认为 200 offset : 偏移数量,用于分页 , 如 :( 页数 -1)*200, 其中 200 为 limit 的值 , 默认为 0

func (*NeteaseAPI) UserCloudDetail

func (api *NeteaseAPI) UserCloudDetail(ids []int) (*APIResponse, error)

requried 必选参数 : id: 歌曲id,可多个,用逗号隔开

func (*NeteaseAPI) UserCommentHistory

func (api *NeteaseAPI) UserCommentHistory(uid int, opts ...hm) (*APIResponse, error)

说明 : 登录后调用此接口 , 传入用户 id, 可以获取用户历史评论

requried 必选参数 : uid : 用户 id

optional 可选参数 : limit : 返回数量 , 默认为 10 time: 上一条数据的time,第一页不需要传,默认为0

func (*NeteaseAPI) UserDetail

func (api *NeteaseAPI) UserDetail(uid int) (*APIResponse, error)

说明 : 登录后调用此接口 , 传入用户 id, 可以获取用户详情

required 必选参数 : uid : 用户 id

func (*NeteaseAPI) UserDj

func (api *NeteaseAPI) UserDj(uid int, opts ...hm) (*APIResponse, error)

说明 : 登录后调用此接口 , 传入用户 id, 可以获取用户电台

required 必选参数 : uid : 用户 id

func (*NeteaseAPI) UserEvent

func (api *NeteaseAPI) UserEvent(uid int, opts ...hm) (*APIResponse, error)

func (*NeteaseAPI) UserLevel

func (api *NeteaseAPI) UserLevel() (*APIResponse, error)

说明 : 登录后调用此接口 , 可以获取用户等级信息,包含当前登录天数,听歌次数,下一等级需要的登录天数和听歌次数,当前等级进度 对应 https://music.163.com/#/user/level

func (*NeteaseAPI) UserPlaylist

func (api *NeteaseAPI) UserPlaylist(uid int, opts ...hm) (*APIResponse, error)

说明 : 登录后调用此接口 , 传入用户 id, 可以获取用户歌单

required 必选参数 : uid : 用户 id

optional 可选参数 : limit : 返回数量 , 默认为 30 offset : 偏移数量,用于分页 , 如 :( 页数 -1)*30, 其中 30 为 limit 的值 , 默认为 0

func (*NeteaseAPI) UserRecord

func (api *NeteaseAPI) UserRecord(uid int, opts ...apitypes.H) (*APIResponse, error)

说明 : 登录后调用此接口 , 传入用户 id, 可获取用户播放记录

requred 必选参数 : uid : 用户 id

optional 可选参数 : type : type=1 时只返回 weekData, type=0 时返回 allData

func (*NeteaseAPI) UserSubcount

func (api *NeteaseAPI) UserSubcount() (*APIResponse, error)

说明 : 登录后调用此接口 , 可以获取用户信息 获取用户信息 , 歌单,收藏,mv, dj 数量

type NeteaseAPIConfig

type NeteaseAPIConfig struct {
	DisableCache           bool
	CacheDefaultExpiration time.Duration
	CacheCleanupInterval   time.Duration

	PreserveCookies bool
	LogHttpRequest  bool
	LogHttpResponse bool
}

func DefaultNeteaseAPIConfig

func DefaultNeteaseAPIConfig() *NeteaseAPIConfig

type Store

type Store interface {
	// Add an item to the cache only if an item doesn't already exist, or if the existing item has expired. Returns an error otherwise.
	Add(k string, v interface{}, d time.Duration) error
	// Delete an item from the cache. Does nothing if the key is not in the cache.
	Delete(k string)
	// Delete all expired items from the cache.
	DeleteExpired()
	// Delete all items from the cache.
	Flush()
	//Get an item from the cache
	Get(k string) (interface{}, bool)
	//Add an item to the cache, replacing any existing item. If the duration is 0 (DefaultExpiration), the cache's default expiration time is used.
	Set(k string, v interface{}, d time.Duration)
	SetDefault(k string, v interface{})
	// Returns the number of items in the cache. This may include items that have expired, but have not yet been cleaned up.
	ItemCount() int
}

Directories

Path Synopsis
this package is a basic implementation of APIClient
this package is a basic implementation of APIClient
package store implements the neteaseapi store interface
package store implements the neteaseapi store interface

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL