|
|
@@ -0,0 +1,254 @@
|
|
|
+package com.cnd3b.utility.polyv;
|
|
|
+
|
|
|
+import com.alibaba.fastjson.JSON;
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
+import junit.framework.Assert;
|
|
|
+import net.polyv.common.v1.exception.PloyvSdkException;
|
|
|
+import net.polyv.live.v1.config.LiveGlobalConfig;
|
|
|
+import net.polyv.live.v1.constant.LiveConstant;
|
|
|
+import net.polyv.live.v1.entity.account.LiveCreateCategoryRequest;
|
|
|
+import net.polyv.live.v1.entity.account.LiveCreateCategoryResponse;
|
|
|
+import net.polyv.live.v1.entity.account.LiveListCategoryRequest;
|
|
|
+import net.polyv.live.v1.entity.account.LiveListCategoryResponse;
|
|
|
+import net.polyv.live.v1.entity.channel.operate.*;
|
|
|
+import net.polyv.live.v1.entity.web.auth.LiveChannelAuthExternalRequest;
|
|
|
+import net.polyv.live.v1.entity.web.auth.LiveChannelAuthExternalResponse;
|
|
|
+import net.polyv.live.v1.service.account.impl.LiveAccountServiceImpl;
|
|
|
+import net.polyv.live.v1.service.channel.impl.LiveChannelOperateServiceImpl;
|
|
|
+import net.polyv.live.v1.service.web.impl.LiveWebAuthServiceImpl;
|
|
|
+import net.polyv.live.v1.util.LiveSignUtil;
|
|
|
+
|
|
|
+import java.io.IOException;
|
|
|
+import java.security.NoSuchAlgorithmException;
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.Calendar;
|
|
|
+import java.util.List;
|
|
|
+
|
|
|
+public class Polyv {
|
|
|
+ String appid = "g3mt33nllk";
|
|
|
+ String userid = "8c1720a694";
|
|
|
+ String appSecret = "97363458f676438280c22b1d05185031";
|
|
|
+
|
|
|
+ public Polyv() {
|
|
|
+ /**
|
|
|
+ * 全局设置
|
|
|
+ */
|
|
|
+ LiveGlobalConfig.init(appid, userid, appSecret);
|
|
|
+ }
|
|
|
+
|
|
|
+ public static void main(String[] args) {
|
|
|
+// Polyv polyv = new Polyv();
|
|
|
+// polyv.listCategory();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 生成授权播放地址
|
|
|
+ *
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public String getSignUrl(String channelId, String secretKey, long userid) {
|
|
|
+ String ts = String.valueOf(System.currentTimeMillis());
|
|
|
+ String url = "https://live.polyv.cn/watch/" + channelId;
|
|
|
+ String signText = secretKey + userid + secretKey + ts;
|
|
|
+ try {
|
|
|
+ String sign = LiveSignUtil.md5Hex(signText);
|
|
|
+ url += "?userid=" + userid + "&ts=" + ts + "&sign=" + sign;
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ return url;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 创建频道
|
|
|
+ *
|
|
|
+ * @throws Exception
|
|
|
+ * @throws NoSuchAlgorithmException
|
|
|
+ */
|
|
|
+ public LiveChannelResponse createChannel(String channelName, String channelPasswd, int categoryId) {
|
|
|
+ LiveChannelRequest liveChannelRequest = new LiveChannelRequest();
|
|
|
+ LiveChannelResponse liveChannelResponse = null;
|
|
|
+ try {
|
|
|
+ liveChannelRequest.setName(channelName);//频道名称
|
|
|
+ liveChannelRequest.setChannelPasswd(channelPasswd);//频道密码,长度不能超过16位
|
|
|
+ liveChannelRequest.setAutoPlay(LiveConstant.AutoPlay.AOTU_PLAY.getFlag());//是否自动播放标识,0:关闭自动播放;1:开启,默认取值 1
|
|
|
+ //liveChannelRequest.setPlayerColor("#666666");//播放器控制栏颜色,默认:#666666
|
|
|
+ //liveChannelRequest.setScene(LiveConstant.SceneType.ALONE.getDesc());//直播场景:alone 活动拍摄; ppt 三分屏; topclass 大班课; seminar 研讨会; 默认:alone
|
|
|
+ liveChannelRequest.setCategoryId(categoryId);//分类ID ,新建频道的所属分类,如果不提交,则为默认分类(分类ID可通过“获取直播分类”接口得到)
|
|
|
+ //liveChannelRequest.setMaxViewer(100);//频道的最大在线人数观看限制的人数
|
|
|
+ //liveChannelRequest.setWatchLayout(LiveConstant.WatchLayout.PPT.getFlag());//三分屏频道的观看布局,ppt:文档为主;video:视频为主;不设置会使用账号的通用设置
|
|
|
+ //liveChannelRequest.setLinkMicLimit(-1);//连麦人数,-1=<取值范围<=账号级的连麦人数,-1:表示使用账号默认的连麦人数,最大16人(注:账号级连麦人数需通知平台管理员设置才生效)
|
|
|
+ //liveChannelRequest.setPureRtcEnabled(LiveConstant.Flag.NO.getFlag());//是否为无延时直播,Y 表示开启,默认为N
|
|
|
+ //liveChannelRequest.setReceive(LiveConstant.Flag.YES.getFlag());//是否为接收转播频道,Y表示是,不填或者填其他值为发起转播频道(注:需要开启频道转播功能该参数才生效)
|
|
|
+ //liveChannelRequest.setReceiveChannelIds("");//接收转播频道号,多个频道号用半角逗号,隔开,如果receive参数值为Y时,此参数无效(注:需要开启频道转播功能该参数才生效)
|
|
|
+ liveChannelResponse = new LiveChannelOperateServiceImpl().createChannel(liveChannelRequest);
|
|
|
+ Assert.assertNotNull(liveChannelResponse);
|
|
|
+ if (liveChannelResponse != null) {
|
|
|
+ System.out.println(JSON.toJSONString(liveChannelResponse));
|
|
|
+ //to do something ......
|
|
|
+ // log.debug("频道创建成功{}", JSON.toJSONString(liveChannelResponse));
|
|
|
+ //log.debug("网页开播地址:https://live.polyv.net/web-start/login?channelId={} , 登录密码: {}",
|
|
|
+ liveChannelResponse.getChannelId();
|
|
|
+ liveChannelRequest.getChannelPasswd();
|
|
|
+ //log.debug("网页观看地址:https://live.polyv.cn/watch/{} ", liveChannelResponse.getChannelId());
|
|
|
+ System.out.println("网页开播地址:https://live.polyv.net/web-start/login?channelId=" + liveChannelResponse.getChannelId());
|
|
|
+ System.out.println("网页开播登陆密码:" + liveChannelRequest.getChannelPasswd());
|
|
|
+ System.out.println("网页观看地址:https://live.polyv.cn/watch/" + liveChannelResponse.getChannelId());
|
|
|
+ }
|
|
|
+ return liveChannelResponse;
|
|
|
+ } catch (PloyvSdkException e) {
|
|
|
+ //参数校验不合格 或者 请求服务器端500错误,错误信息见PloyvSdkException.getMessage(),B
|
|
|
+ //log.error(e.getMessage(), e);
|
|
|
+ // 异常返回做B端异常的业务逻辑,记录log 或者 上报到ETL 或者回滚事务
|
|
|
+ throw e;
|
|
|
+ } catch (Exception e) {
|
|
|
+ // log.error("SDK调用异常", e);
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 设置频道外部授权
|
|
|
+ *
|
|
|
+ * @throws Exception
|
|
|
+ * @throws NoSuchAlgorithmException
|
|
|
+ */
|
|
|
+ public String updateChannelAuthExternal(String channelId) {
|
|
|
+ LiveChannelAuthExternalRequest liveChannelAuthExternalRequest = new LiveChannelAuthExternalRequest();
|
|
|
+ LiveChannelAuthExternalResponse liveChannelAuthExternalResponse;
|
|
|
+ try {
|
|
|
+ liveChannelAuthExternalRequest.setChannelId(channelId);
|
|
|
+ liveChannelAuthExternalRequest.setExternalUri("http://123.60.111.36:8080/bwj/rest/webclientrest/polyvAuth");
|
|
|
+ liveChannelAuthExternalResponse = new LiveWebAuthServiceImpl().updateChannelAuthExternal(
|
|
|
+ liveChannelAuthExternalRequest);
|
|
|
+ Assert.assertNotNull(liveChannelAuthExternalResponse);
|
|
|
+ if (liveChannelAuthExternalResponse != null && !liveChannelAuthExternalResponse.getChannelAuthExternals().isEmpty()) {
|
|
|
+ return liveChannelAuthExternalResponse.getChannelAuthExternals().get(0).getSecretKey();
|
|
|
+ //to do something ......
|
|
|
+ //log.debug("测试通过接口设置外部授权成功,{}", JSON.toJSONString(liveChannelAuthExternalResponse));
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ //参数校验不合格 或者 请求服务器端500错误,错误信息见PloyvSdkException.getMessage()
|
|
|
+ //log.error(e.getMessage(), e);
|
|
|
+ // 异常返回做B端异常的业务逻辑,记录log 或者 上报到ETL 或者回滚事务
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 频道删除
|
|
|
+ *
|
|
|
+ * @throws Exception
|
|
|
+ * @throws NoSuchAlgorithmException
|
|
|
+ */
|
|
|
+ public boolean deleteChannel(String channelId) {
|
|
|
+ LiveDeleteChannelRequest liveDeleteChannelRequest = new LiveDeleteChannelRequest();
|
|
|
+ Boolean liveDeleteChannelResponse;
|
|
|
+ try {
|
|
|
+ liveDeleteChannelRequest.setChannelId(channelId);
|
|
|
+ liveDeleteChannelResponse = new LiveChannelOperateServiceImpl().deleteChannel(liveDeleteChannelRequest);
|
|
|
+ Assert.assertNotNull(liveDeleteChannelResponse);
|
|
|
+ return liveDeleteChannelResponse;
|
|
|
+ } catch (PloyvSdkException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ public JSONObject testGetChannelInfo() throws Exception, NoSuchAlgorithmException {
|
|
|
+ LiveChannelInfoRequest liveChannelInfoRequest = new LiveChannelInfoRequest();
|
|
|
+ LiveChannelInfoResponse liveChannelInfoResponse;
|
|
|
+ try {
|
|
|
+ //准备测试数据
|
|
|
+ String channelId = "2636984";
|
|
|
+ liveChannelInfoRequest.setChannelId(channelId);
|
|
|
+ liveChannelInfoResponse = new LiveChannelOperateServiceImpl().getChannelInfo(liveChannelInfoRequest);
|
|
|
+ Assert.assertNotNull(liveChannelInfoResponse);
|
|
|
+ return (JSONObject) JSON.toJSON(liveChannelInfoResponse);
|
|
|
+ } catch (PloyvSdkException | IOException | NoSuchAlgorithmException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ return new JSONObject();
|
|
|
+ }
|
|
|
+
|
|
|
+ public JSONObject testGetChannelBasicInfo() throws Exception, NoSuchAlgorithmException {
|
|
|
+ LiveChannelBasicInfoRequest liveChannelBasicInfoRequest = new LiveChannelBasicInfoRequest();
|
|
|
+ LiveChannelBasicInfoResponse liveChannelBasicInfoResponse;
|
|
|
+ try {
|
|
|
+ //准备测试数据
|
|
|
+ String channelId = "2636984";
|
|
|
+ liveChannelBasicInfoRequest.setChannelId(channelId);
|
|
|
+ liveChannelBasicInfoResponse = new LiveChannelOperateServiceImpl().getChannelBasicInfo(
|
|
|
+ liveChannelBasicInfoRequest);
|
|
|
+ Assert.assertNotNull(liveChannelBasicInfoResponse);
|
|
|
+ return (JSONObject) JSON.toJSON(liveChannelBasicInfoResponse);
|
|
|
+ } catch (PloyvSdkException | IOException | NoSuchAlgorithmException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ return new JSONObject();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 查询频道直播截图
|
|
|
+ *
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public String getChannelCapture() {
|
|
|
+ LiveChannelCaptureRequest liveChannelCaptureRequest = new LiveChannelCaptureRequest();
|
|
|
+ String liveChannelCaptureResponse;
|
|
|
+ try {
|
|
|
+ //准备测试数据
|
|
|
+ String channelId = "2636984";
|
|
|
+ liveChannelCaptureRequest.setChannelId(channelId);
|
|
|
+ liveChannelCaptureResponse = new LiveChannelOperateServiceImpl().getChannelCapture(
|
|
|
+ liveChannelCaptureRequest);
|
|
|
+ Assert.assertNotNull(liveChannelCaptureResponse);
|
|
|
+ return JSON.toJSONString(liveChannelCaptureResponse);
|
|
|
+ } catch (PloyvSdkException | IOException | NoSuchAlgorithmException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ return "";
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 创建直播分类
|
|
|
+ *
|
|
|
+ * @throws Exception
|
|
|
+ */
|
|
|
+ public LiveCreateCategoryResponse createCategory(String categoryname) {
|
|
|
+ LiveCreateCategoryRequest liveCreateCategoryRequest = new LiveCreateCategoryRequest();
|
|
|
+ LiveCreateCategoryResponse liveCreateCategoryResponse;
|
|
|
+ try {
|
|
|
+ liveCreateCategoryRequest.setCategoryName(categoryname);
|
|
|
+ liveCreateCategoryResponse = new LiveAccountServiceImpl().createCategory(liveCreateCategoryRequest);
|
|
|
+ Assert.assertNotNull(liveCreateCategoryResponse);
|
|
|
+ return liveCreateCategoryResponse;
|
|
|
+ } catch (PloyvSdkException | IOException | NoSuchAlgorithmException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取直播分类列表
|
|
|
+ *
|
|
|
+ * @throws Exception
|
|
|
+ */
|
|
|
+ public List<LiveListCategoryResponse.LiveCategory> listCategory() {
|
|
|
+ LiveListCategoryRequest liveListCategoryRequest = new LiveListCategoryRequest();
|
|
|
+ LiveListCategoryResponse liveListCategoryResponse;
|
|
|
+ try {
|
|
|
+ liveListCategoryResponse = new LiveAccountServiceImpl().listCategory(liveListCategoryRequest);
|
|
|
+ Assert.assertNotNull(liveListCategoryResponse);
|
|
|
+ return liveListCategoryResponse.getLiveCategories();
|
|
|
+ } catch (PloyvSdkException | IOException | NoSuchAlgorithmException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ return new ArrayList<>();
|
|
|
+ }
|
|
|
+}
|