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

Commit

Permalink
add huya top rooms
Browse files Browse the repository at this point in the history
  • Loading branch information
Ha2ryZhang committed May 17, 2020
1 parent 18ca136 commit 8730d3e
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 3 deletions.
4 changes: 4 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@
</properties>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<!--websocket-->
<dependency>
<groupId>org.springframework.boot</groupId>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.debugers.alltv.controller;

import com.debugers.alltv.model.LiveRoom;
import com.debugers.alltv.result.Result;
import com.debugers.alltv.service.HuYaService;
import io.swagger.annotations.Api;
Expand All @@ -12,6 +13,7 @@
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Api(tags = "虎牙直播")
Expand All @@ -34,4 +36,8 @@ public Result<Map<String, String>> getRealUrl(@PathVariable(value = "roomId", re
result.put("realUrl", huYaService.getRealUrl(roomId));
return Result.success(result);
}
@GetMapping("top_rooms")
public Result<List<LiveRoom>> getTopRooms(){
return Result.success(huYaService.getTopRooms());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.debugers.alltv.result.Result;
import com.debugers.alltv.service.BilibiliService;
import com.debugers.alltv.service.DouYuService;
import com.debugers.alltv.service.HuYaService;
import io.swagger.annotations.Api;
import org.springframework.web.bind.annotation.*;

Expand All @@ -16,18 +17,22 @@
public class TopController {
private final DouYuService douYuService;
private final BilibiliService bilibiliService;
private final HuYaService huYaService;

public TopController(DouYuService douYuService, BilibiliService bilibiliService) {
public TopController(DouYuService douYuService, BilibiliService bilibiliService, HuYaService huYaService) {
this.douYuService = douYuService;
this.bilibiliService = bilibiliService;
this.huYaService = huYaService;
}
@GetMapping("live/{cid}")
public Result<List<LiveRoom>> getTopRooms(@PathVariable("cid") String cid, @RequestParam(defaultValue = "1") Integer pageNum){
List<LiveRoom> rooms = douYuService.getTopRoomsByCid(cid, 20, pageNum);
//暂时不知道怎么映射 各个平台的分类关系
if ("0".equals(cid)){
List<LiveRoom> topRooms = bilibiliService.getTopRooms(0, 10, pageNum);
List<LiveRoom> topRoomsHuya = huYaService.getTopRooms();
rooms.addAll(topRooms);
rooms.addAll(topRoomsHuya);
}
Collections.sort(rooms);
return Result.success(rooms);
Expand Down
29 changes: 28 additions & 1 deletion src/main/java/com/debugers/alltv/service/HuYaService.java
Original file line number Diff line number Diff line change
@@ -1,16 +1,28 @@
package com.debugers.alltv.service;

import com.alibaba.fastjson.JSONObject;
import com.debugers.alltv.model.LiveRoom;
import com.debugers.alltv.util.http.HttpContentType;
import com.debugers.alltv.util.http.HttpRequest;
import org.apache.commons.lang3.StringUtils;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

@Service
public class HuYaService {
private final StringRedisTemplate redisTemplate;
private static final Pattern PATTERN = Pattern.compile("(?<=hasvedio: ')(.*\\.m3u8)");

public HuYaService(StringRedisTemplate redisTemplate) {
this.redisTemplate = redisTemplate;
}

public String getRealUrl(String roomId) {
String room_url = "https://m.huya.com/" + roomId;
String response = HttpRequest.create(room_url)
Expand All @@ -27,8 +39,23 @@ public String getRealUrl(String roomId) {
return "未开播或直播间不存在";
}
if (result.startsWith("//"))
result="https:"+result;
result = "https:" + result;
// return result.replaceAll("_\\d{3,4}\\.m3u8", ".flv");
result="https://al.rtmp"+result.substring(result.indexOf(".huya.com"));
return result; //默认清晰度
}

public List<LiveRoom> getTopRooms() {
List<String> list = redisTemplate.opsForList().range("Huya", 0, 15);
List<LiveRoom> liveRooms = new ArrayList<>();
if (list != null) {
liveRooms = list.stream().map(s -> {
JSONObject jsonObject = JSONObject.parseObject(s);
String online = jsonObject.getString("online").replaceAll("万","").replaceAll(",","");
jsonObject.put("online",Double.parseDouble(online)*10000);
return jsonObject.toJavaObject(LiveRoom.class);
}).collect(Collectors.toList());
}
return liveRooms;
}
}
18 changes: 17 additions & 1 deletion src/main/resources/application.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,22 @@
spring:
redis:
host: 127.0.0.1
# Redis服务器连接端口
port: 6379
jedis:
pool:
#连接池最大连接数(使用负值表示没有限制)
max-active: 100
# 连接池中的最小空闲连接
max-idle: 10
# 连接池最大阻塞等待时间(使用负值表示没有限制)
max-wait: 100000
# 连接超时时间(毫秒)
timeout: 5000
#默认是索引为0的数据库
database: 0
jackson:
time-zone: Asia/Shanghai
date-format: yyyy-MM-dd HH:mm:ss
server:
port: 8888
port: 8888

0 comments on commit 8730d3e

Please sign in to comment.