Skip to content
This repository has been archived by the owner on Jun 21, 2023. It is now read-only.

Commit

Permalink
优化部分细节
Browse files Browse the repository at this point in the history
  • Loading branch information
Ha2ryZhang committed May 12, 2020
1 parent ad8f34e commit cb01e4b
Show file tree
Hide file tree
Showing 11 changed files with 124 additions and 28 deletions.
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@

整合了 swagger2 本地运行访问 http://localhost:8888/swagger-ui.html 即可查看api文档

**最近没怎么更新,主要时间写app去了 最下面附上个截图**

#### New Feature

* 已经开始 app开发了 采用 flutter 所以兼容 android 和 ios
* 新增触手直播
* 新增获取bilibili直播间弹幕(websocket 具体食用方法在下面)

Expand Down Expand Up @@ -107,3 +108,6 @@ docker run --rm -it -d --name alltv -p 8888:8888 harryzhang6/alltv
- [ ] 可视化web界面

- [ ] 增加其他平台获取弹幕(websocket)

### AllTV app(努力中)别忘了点star哦
![testwebsocket](./images/alltv.jpeg)
Binary file added images/alltv.jpeg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package com.debugers.alltv.controller;

import com.alibaba.fastjson.JSONArray;
import com.debugers.alltv.model.dto.DouYuDTO;
import com.debugers.alltv.enumType.DouYuOpenApi;
import com.debugers.alltv.exception.RoomNotFondException;
import com.debugers.alltv.model.dto.DouYuDTO;
import com.debugers.alltv.result.CodeMsg;
import com.debugers.alltv.result.Result;
import com.debugers.alltv.service.DouYuService;
Expand Down Expand Up @@ -77,7 +77,7 @@ public Result<JSONArray> getTopRooms(@PathVariable(value = "cateId", required =
.setContentType(HttpContentType.FORM).get();
if (404 == response.getCode())
throw new RoomNotFondException(CodeMsg.PAGE_ERROR);

JSONArray data = response.getBodyJson().getJSONArray("data");
return Result.success(response.getBodyJson().getJSONArray("data"));

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public HuYaController(HuYaService huYaService) {
@ApiImplicitParams({
@ApiImplicitParam(name = "roomId", value = "房间号", required = true, dataType = "String"),
})

@GetMapping("real_url/{roomId}")
public Result<Map<String, String>> getRealUrl(@PathVariable(value = "roomId", required = true) String roomId){
Map<String, String> result = new HashMap<>();
Expand Down
25 changes: 25 additions & 0 deletions src/main/java/com/debugers/alltv/controller/TopController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.debugers.alltv.controller;

import com.debugers.alltv.model.LiveRoom;
import com.debugers.alltv.result.Result;
import com.debugers.alltv.service.DouYuService;
import io.swagger.annotations.Api;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "综合各个平台")
@RestController
@RequestMapping("api/top")
public class TopController {
private final DouYuService douYuService;

public TopController(DouYuService douYuService) {
this.douYuService = douYuService;
}
@GetMapping("live/{cid}")
public Result<List<LiveRoom>> getTopRooms(@PathVariable("cid") String cid, @RequestParam(defaultValue = "0") Integer pageNum){
List<LiveRoom> rooms = douYuService.getTopRoomsByCid(cid, 10, pageNum);
return Result.success(rooms);
}
}
29 changes: 29 additions & 0 deletions src/main/java/com/debugers/alltv/enumType/BilibiliOpenApi.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.debugers.alltv.enumType;

/**
* bilibili官方api
* @author harryzhang
* https://api.live.bilibili.com/room/v1/RoomRecommend/biliIndexRecList
*/
public enum BilibiliOpenApi {
//统一后面+roomId
SERVER_CONFIG("https://api.live.bilibili.com/room/v1/Danmu/getConf?id="),
ROOM_INIT("https://api.live.bilibili.com/room/v1/Room/room_init?id="),
PLAY_URL("https://api.live.bilibili.com/room/v1/Room/playUrl?cid=")
;

private String url;

BilibiliOpenApi(String url) {
this.url = url;
}

public String getValue() {
return url;
}

@Override
public String toString() {
return getValue();
}
}
3 changes: 2 additions & 1 deletion src/main/java/com/debugers/alltv/enumType/DouYuOpenApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
public enum DouYuOpenApi {
//统一后面+roomId
ROOM_INFO("http://open.douyucdn.cn/api/RoomApi/room/"),
TOP_ROOM("http://open.douyucdn.cn/api/RoomApi/live/"),
SIMPLE_TOP_ROOM("http://open.douyucdn.cn/api/RoomApi/live/"),
TOP_ROOM("http://api.douyutv.com/api/v1/live/")
;

private String url;
Expand Down
21 changes: 21 additions & 0 deletions src/main/java/com/debugers/alltv/model/LiveRoom.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.debugers.alltv.model;

import lombok.Data;

import java.util.Date;

@Data
public class LiveRoom {
private String roomId;
private String com;//哪个平台
private String cateId;
private String roomThumb;
private String cateName;//分类名
private String roomName;
private Integer roomStatus; //1 开播 2 未开播
private Date startTime;
private String ownerName;
private String avatar;
private Long online; //热度
private String realUrl; //真是直播地址
}
27 changes: 12 additions & 15 deletions src/main/java/com/debugers/alltv/model/dto/DouYuDTO.java
Original file line number Diff line number Diff line change
@@ -1,34 +1,31 @@
package com.debugers.alltv.model.dto;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import com.alibaba.fastjson.annotation.JSONField;
import lombok.Data;

import java.util.Date;

@Data
@ApiModel(description = "斗鱼房间信息")
public class DouYuDTO {
@ApiModelProperty("房间id")
@JSONField(name = "room_id")
private String roomId;
@ApiModelProperty(value = "直播分类id",example = "1")
@JSONField(name = "cate_id")
private String cateId;
@ApiModelProperty("房间预览缩略图")
@JSONField(name = "room_src")
private String roomThumb;
@ApiModelProperty("分类名")
@JSONField(name = "game_name")
private String cateName;//分类名
@ApiModelProperty("房间名")
@JSONField(name = "room_name")
private String roomName;
@ApiModelProperty(value = "开播状态",notes = "1 开播 2 未开播")
@JSONField(name = "show_status")
private Integer roomStatus; //1 开播 2 未开播
@ApiModelProperty("本次开播时间,如果没开播则是上一次开播时间")
@JSONField(name = "show_time")
private Date startTime;
@ApiModelProperty("主播名")
@JSONField(name = "nickname")
private String ownerName;
@ApiModelProperty("主播头像")
@JSONField(name = "avatar")
private String avatar;
@ApiModelProperty("斗鱼热度")
private Long online; //斗鱼叫热度
@ApiModelProperty("真实直播源地址")
@JSONField(name = "online")
private Long online; //热度
private String realUrl; //真是直播地址
}
7 changes: 4 additions & 3 deletions src/main/java/com/debugers/alltv/service/BilibiliService.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.debugers.alltv.enumType.BilibiliOpenApi;
import com.debugers.alltv.model.BilibiliServerConfig;
import com.debugers.alltv.util.http.HttpRequest;
import org.springframework.stereotype.Service;

@Service
public class BilibiliService {
public JSONObject getRealRoomId(String rid) {
String room_url = "https://api.live.bilibili.com/room/v1/Room/room_init?id=" + rid;
String room_url = BilibiliOpenApi.ROOM_INIT + rid;
JSONObject response = HttpRequest.create(room_url).get().getBodyJson();
JSONObject data = response.getJSONObject("data");
if (data == null) {
Expand All @@ -24,7 +25,7 @@ public JSONObject getRealRoomId(String rid) {
}

public BilibiliServerConfig getRoomConfig(String rid) {
String configUrl = "https://api.live.bilibili.com/room/v1/Danmu/getConf?id=" + rid;
String configUrl = BilibiliOpenApi.SERVER_CONFIG + rid;
JSONObject response = HttpRequest.create(configUrl).get().getBodyJson();
return response.getObject("data",BilibiliServerConfig.class);
}
Expand All @@ -38,7 +39,7 @@ public String getRealUrl(String rid) {
if (!roomInfo.getBoolean("live_status")) {
return "未开播";
}
String room_url = "https://api.live.bilibili.com/room/v1/Room/playUrl?cid=" + roomInfo.getLongValue("room_id") + "&platform=h5&otype=json&quality=4";
String room_url = BilibiliOpenApi.PLAY_URL.toString() + roomInfo.getLongValue("room_id") + "&platform=h5&otype=json&quality=4";
JSONObject response = HttpRequest.create(room_url).get().getBodyJson();
JSONArray durl = response.getJSONObject("data").getJSONArray("durl");
if (durl != null) {
Expand Down
29 changes: 23 additions & 6 deletions src/main/java/com/debugers/alltv/service/DouYuService.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
package com.debugers.alltv.service;

import com.alibaba.fastjson.JSONObject;
import com.debugers.alltv.model.dto.DouYuDTO;
import com.debugers.alltv.enumType.DouYuOpenApi;
import com.debugers.alltv.exception.RoomNotFondException;
import com.debugers.alltv.model.LiveRoom;
import com.debugers.alltv.model.dto.DouYuDTO;
import com.debugers.alltv.result.CodeMsg;
import com.debugers.alltv.util.MD5Util;
import com.debugers.alltv.util.http.HttpContentType;
import com.debugers.alltv.util.http.HttpRequest;
import com.debugers.alltv.util.http.HttpResponse;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.script.ScriptEngine;
Expand All @@ -18,8 +20,10 @@
import java.security.NoSuchAlgorithmException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

/**
* 斗鱼直播
Expand Down Expand Up @@ -200,6 +204,24 @@ public DouYuDTO getRoomInfo(String roomId) {
return getDTO(response.getBodyJson());
}

public List<LiveRoom> getTopRoomsByCid(String cid, Integer pageSize, Integer pageNum) {
HttpResponse response = HttpRequest.create(DouYuOpenApi.TOP_ROOM + cid)
.appendParameter("offset", pageNum)
.appendParameter("limit", pageSize)
.setContentType(HttpContentType.FORM).get();
if (404 == response.getCode())
throw new RoomNotFondException(CodeMsg.PAGE_ERROR);
List<DouYuDTO> data = response.getBodyJson().getJSONArray("data").toJavaList(DouYuDTO.class);
return data.stream().map(this::convertToLiveRoom).collect(Collectors.toList());
}

private LiveRoom convertToLiveRoom(DouYuDTO douYuDTO) {
LiveRoom liveRoom = new LiveRoom();
BeanUtils.copyProperties(douYuDTO, liveRoom);
liveRoom.setCom("douyu");
return liveRoom;
}

private DouYuDTO getDTO(JSONObject jsonObject) {
JSONObject data = jsonObject.getJSONObject("data");
DouYuDTO douYuDTO = new DouYuDTO();
Expand All @@ -222,9 +244,4 @@ private DouYuDTO getDTO(JSONObject jsonObject) {
return douYuDTO;
}

public static void main(String[] args) {
DouYuService douYuService = new DouYuService();
DouYuDTO roomInfo = douYuService.getRoomInfo("5");
System.out.println(roomInfo);
}
}

0 comments on commit cb01e4b

Please sign in to comment.