【程序试验】获取 B 站追番信息

获取 B 站自己的追番信息

PS:这是我之前记录的文档,终于找回来了

目标:获取自己的追番信息显示在网站首页

查询 API 和返回键

api:

GET请求:
url:
https://api.bilibili.com/x/space/bangumi/follow/list
参数说明:
#vmid -> 用户的UID
#type -> 1-追番 2-追剧
#follow_status -> 2-在看 0-全部 3-看过
#pn -> 第几页
#ps  -> 页面显示数目

返回值:

{
    "code": 0,
    "message": "0",
    "ttl": 1,
    "data": {
        "list": [
            {
                "season_id": 28546,
                "media_id": 28222624,
                "season_type": 1,
                "season_type_name": "番剧",
                "title": "爱书的下克上:为了成为图书管理员不择手段!",
                "cover": "http://i0.hdslb.com/bfs/bangumi/cdb0d208529f11602070069c372fa073eb4e4286.png",
                "total_count": -1,
                "is_finish": 0,
                "is_started": 1,
                "is_play": 1,
                "badge": "会员专享",
                "badge_type": 0,
                "rights": {},
                "stat": {
                    "follow": 282839,
                    "view": 1220872,
                    "danmaku": 21109,
                    "reply": 5330,
                    "coin": 5291
                },
                "new_ep": {
                    "id": 285221,
                    "index_show": "更新至第2话",
                    "cover": "http://i0.hdslb.com/bfs/archive/46b5d720cc59f0d90516fa454a27ba6c091b55ed.jpg",
                    "title": "2",
                    "long_title": "生活改善与石板",
                    "pub_time": "2019-10-10 00:30:00"
                },
                "square_cover": "http://i0.hdslb.com/bfs/bangumi/d993ac85365377555f08f33484f24641399015de.jpg",
                "season_status": 13,
                "season_title": "TV",
                "badge_ep": "会员",
                "media_attr": 196612,
                "season_attr": 0,
                "evaluate": "在现代日本生活的“本须丽乃”,在决定就职于自己所心愿的图书馆的那天去世了。想去读更多的书,抱着这样的念想的她,意识到的时候,已经拥有了异世界幼女清醒的身体并恢复了意识。\n故事的舞台是由拥有魔法力量的贵...",
                "areas": [
                    {
                        "id": 2,
                        "name": "日本"
                    }
                ],
                "subtitle": "",
                "first_ep": 285220,
                "can_watch": 1,
                "follow_status": 2,
                "is_new": 0,
                "progress": "看到第1话 5:44",
                "both_follow": true
            },
            {
                "season_id": 28545,
                "media_id": 28222623,
                "season_type": 1,
                "season_type_name": "番剧",
                "title": "戰×戀(僅限港澳台地區)",
                "cover": "http://i0.hdslb.com/bfs/bangumi/468f4abf7a74de83ca16dc6a4dccfeb76b4abe8b.png",
                "total_count": -1,
                "is_finish": 0,
                "is_started": 1,
                "is_play": 1,
                "badge": "会员抢先",
                "badge_type": 0,
                "rights": {},
                "stat": {
                    "follow": 29280,
                    "view": 54306,
                    "danmaku": 2199,
                    "reply": 743,
                    "coin": 548
                },
                "new_ep": {
                    "id": 285867,
                    "index_show": "更新至第2话",
                    "cover": "http://i0.hdslb.com/bfs/archive/68efde8c2c64e137efbf7a1231c05412b36dfbf6.jpg",
                    "title": "2",
                    "long_title": "束縛少女",
                    "pub_time": "2019-10-12 22:30:00"
                },
                "square_cover": "http://i0.hdslb.com/bfs/bangumi/25369a563ec855d3bb46b9cc73742e60b081a995.png",
                "season_status": 13,
                "season_title": "TV(僅港澳台)",
                "badge_ep": "会员",
                "media_attr": 196612,
                "season_attr": 0,
                "evaluate": "有人曾說「戀愛是少女的力量泉源」,9 位為了對抗攻擊人類世界的惡魔而來到現世的少女,在神界的主神「奧汀」主導下強行住進了原本獨居的高中生・亞久津拓真的家裡。拓真必須跟這些少女「親熱」才能夠提升對方的等...",
                "areas": [
                    {
                        "id": 2,
                        "name": "日本"
                    }
                ],
                "subtitle": "",
                "first_ep": 285866,
                "can_watch": 1,
                "follow_status": 2,
                "is_new": 1,
                "progress": "",
                "both_follow": true
            }
        ],
        "pn": 1,
        "ps": 15,
        "total": 18
    }
}

创建实体类

实体类

package com.tobeshrek.common.pojo.bilibili;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;

/**
 * 哔哩哔哩番剧实体类
* @ClassName: Anime 
* @author 李国豪 
* @date 2019年10月14日 下午9:14:57 
*
 */
@JsonIgnoreProperties(ignoreUnknown = true)
public class Anime {
	/**
	 * 番剧ID,如28546
	 * 番剧详细信息为https://www.bilibili.com/bangumi/play/ss28546/
	 */
	private Long season_id;
	/**
	 * 番剧标题
	 */
	private String title;
	/**
	 * 番剧首页图片
	 */
	private String cover;
	/**
	 * 番剧简介
	 */
	private String evaluate;
	/**
	 * 观看进度
	 */
	private String progress;
	
	
	public Long getSeason_id() {
		return season_id;
	}
	public void setSeason_id(Long season_id) {
		this.season_id = season_id;
	}
	public String getTitle() {
		return title;
	}
	public void setTitle(String title) {
		this.title = title;
	}
	public String getEvaluate() {
		return evaluate;
	}
	public void setEvaluate(String evaluate) {
		this.evaluate = evaluate;
	}
	public String getProgress() {
		return progress;
	}
	public void setProgress(String progress) {
		this.progress = progress;
	}
	public String getCover() {
		return cover;
	}
	public void setCover(String cover) {
		this.cover = cover;
	}
	
	
}


@JsonIgnoreProperties(ignoreUnknown = true)
public class AnimeData {
	private List<Anime> list;
	private Integer pn;
	private Integer ps;
	private Long total;
	public List<Anime> getList() {
		return list;
	}
	public void setList(List<Anime> list) {
		this.list = list;
	}
	public Integer getPn() {
		return pn;
	}
	public void setPn(Integer pn) {
		this.pn = pn;
	}
	public Integer getPs() {
		return ps;
	}
	public void setPs(Integer ps) {
		this.ps = ps;
	}
	public Long getTotal() {
		return total;
	}
	public void setTotal(Long total) {
		this.total = total;
	}
	
}

@JsonIgnoreProperties(ignoreUnknown = true)
public class BliBliResult {
	
	private Integer code;
	private String message;
	private Integer ttl;
	private AnimeData data;
	public Integer getCode() {
		return code;
	}
	public void setCode(Integer code) {
		this.code = code;
	}
	public String getMessage() {
		return message;
	}
	public void setMessage(String message) {
		this.message = message;
	}
	public Integer getTtl() {
		return ttl;
	}
	public void setTtl(Integer ttl) {
		this.ttl = ttl;
	}
	public AnimeData getData() {
		return data;
	}
	public void setData(AnimeData data) {
		this.data = data;
	}
	
	
}

注意:

@JsonIgnoreProperties(ignoreUnknown = true) 

这个必须加,作用是忽略 JSON 中实体类没有字段的转换,类注解,加在实体类上就 OK 了,不加 JSON 转实体类时会报错

测试实体类

String result = HttpClientUtil.doGet("https://api.bilibili.com/x/space/bangumi/follow/list?type=1&follow_status=2&pn=1&ps=15&vmid=118744944&ts=1571057868836");
		if(result!=null && !result.equals("")){
			System.out.println("取到数据了");
		}
		BliBliResult bilibiliResult = JsonUtils.jsonToPojo(result, BliBliResult.class);
		System.out.println("到此结束");

用 debuge 模式查看 JSON 转实体类会不会报错,没有报错并取到值了证明 OK

编写服务层 RESTFul

不用编写代码,客户层直接调用

编写客户层代码

@Controller
@RequestMapping("/bilibili")
public class BiliBiliController {
	
	@Autowired
	private BiliBiliService bilibiliService;
	
	@RequestMapping("/getanime")
	public String getAnimeTest(HttpServletRequest request){
		BliBliResult bliBliResult = bilibiliService.getAnimeList();
		request.setAttribute("animeList", bliBliResult.getData().getList());
		return "test/test";
	}
	
}
@Service
public class BiliBiliServiceImpl implements BiliBiliService {
	
	@Value("${BILIBILI_ANIME_METTING_URL}")
	private String BILIBILI_ANIME_METTING_URL;
	@Override
	public BliBliResult getAnimeList() {
		String result = HttpClientUtil.doGet(BILIBILI_ANIME_METTING_URL);
		BliBliResult bilibiliResult = JsonUtils.jsonToPojo(result, BliBliResult.class);
		return bilibiliResult;
	}

}
<c:forEach items="${animeList}" var="anime">
	  	<div class="col-xs-6 col-md-3">
	    <a href="https://www.bilibili.com/bangumi/play/ss${anime.season_id}" class="thumbnail">
	    	<img src="${anime.cover}" alt="图片未找到">
	    	<font size="6">${anime.title}</font><br>
	    	${anime.evaluate}<br>
	    	${anime.progress}
	    </a>
		</div>
	</c:forEach>

解决 img 标签访问图片 403

在 meta 标签中加入如下解决

<meta name="referrer" content="no-referrer" />

我很开心 23333

  • 二次元
    9 引用 • 30 回帖 • 1 关注
  • 哔哩哔哩
    17 引用 • 132 回帖 • 2 关注
  • API

    应用程序编程接口(Application Programming Interface)是一些预先定义的函数,目的是提供应用程序与开发人员基于某软件或硬件得以访问一组例程的能力,而又无需访问源码,或理解内部工作机制的细节。

    66 引用 • 377 回帖

赞助商 我要投放

回帖
请输入回帖内容 ...