正则匹配酷6视频url后使用接口引用案例

前端之家收集整理的这篇文章主要介绍了正则匹配酷6视频url后使用接口引用案例前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

通过酷6的一个转发接口,使用java正则来获取匹配酷6网大部分视频网址链接,抓取获得ip、flash、title等属性用于内嵌视频展示;关键代码是抓取数据,展示代码很简单。大致效果图如下:

引用视频:


展示视频:


1.抓取数据代码

<span style="font-family:KaiTi_GB2312;font-size:18px;"> /**
     * 酷6新接口方法
     * add by yangkai
     * @param videoUrl
     * @return
     */
    protected VideoPropertyUnitForKu6fetchVideoForKu6New(String videoUrl) {
        String videoId = null;
        String videoTitle = null;
        String flashUrl = null;
        String imgUrl = null;
        String result = null;
        // 1.从接口获取xml数据
        try {
            // 先判断用户填写的几种url格式(目前发现的有10种格式可以兼容)
            if (StringUtils.isNotBlank(videoUrl)) {
                String ku6Url ="http://v.ku6.com/show/";
                //http://player.ku6.com/refer/flKjsasaK0W9fdCsFYO99A../v.swf
                // <embedsrc="http://player.ku6.com/refer/flKjsasaK0W9fdCsFYO99A../v.swf"width="480" height="400"allowscriptaccess="always" allowfullscreen="true"type="application/x-shockwave-flash"flashvars="from=ku6"></embed>
                if(videoUrl.contains("/refer/")) {
                    ku6Url += videoUrl.replace("http://","").split("/")[2] + ".html";
                } else if(videoUrl.contains("</script>")) { // <scriptdata-vid="flKjsasaK0W9fdCsFYO99A.."src="//player.ku6.com/out/v.js" data-width="480" data-height="400"></script>
                    ku6Url = patternUrl("<script\\s*data-vid=\"([^<>\"]*)\"\\s*[^<>]*>\\s*</script>",videoUrl.replace("http://",""),ku6Url) +".html";
                } else if(videoUrl.contains("/film/")) { //http://v.ku6.com/film/show_136598/Jtj7tCVdd81bGIUyf39Few...html?hpsrc=1_35_1_1_0
                    videoUrl =videoUrl.replace("http://","").split("\\?")[0];
                    ku6Url += videoUrl.split("/")[3];
                } else if(videoUrl.contains("topic.ku6.com")) { //http://topic.ku6.com/show-21451-xHZVk57-sZkcy5Mrf5MtSQ...html?st=5_1_0_1
                    ku6Url =patternUrl(".*http://topic\\.ku6\\.com/show-[^-]+-(.+\\.html).*",videoUrl,ku6Url);
                } else if (videoUrl.contains("my.ku6.com")) {// http://my.ku6.com/watch?v=HMvaiRJpdzhXBqqF9oMamQ..
                    ku6Url =patternUrl(".*http://my\\.ku6\\.com/watch\\?v=(.+).*",ku6Url) + ".html";
                } else if (videoUrl.contains("/special/")) {// http://my.ku6.com/watch?v=HMvaiRJpdzhXBqqF9oMamQ..
                    ku6Url =patternUrl(".*http://v\\.ku6\\.com/special/.+/(.+).*",ku6Url);
                } else if (videoUrl.contains("/mt/")) { //http://v.ku6.com/u/291222/mt/_SqfhQTRM1yRhmOZ.html
                    ku6Url =patternUrl(".*http://v\\.ku6\\.com/.+/.+/mt/(.+).*",ku6Url);
                } else {
                    ku6Url =patternUrl(".*http://v\\.ku6\\.com/show/(.+\\.html).*",ku6Url);
                }
                if (!StringUtils.equals(ku6Url,"http://v.ku6.com/show/")) {
                    StringBuffer sbUrl = newStringBuffer("http://v.ku6.com/repaste.htm?url=");
                    sbUrl.append(URLEncoder.encode(ku6Url,"utf-8"));
                    result = sendRequest(sbUrl.toString());
                }
            }
        } catch (UnsupportedEncodingExceptione) {
           logger.error(HuDongErrorCodeEnum.RemoteRequestError,"从接口获取数据失败",e);
        }
        // 2.解析xml数据,取得所需参数
        VideoPropertyUnitForKu6videoPropertyUnitForKu6 = null;
        try {
            if (StringUtils.isNotBlank(result)) {
                Document document =DocumentHelper.parseText(result);
                List<Element> root =document.getRootElement().elements();
                if (root != null &&root.size() > 0) {
                    for (Element i : root) {
                        videoId =i.element("vid").getTextTrim(); // id
                        imgUrl =i.element("coverurl").getTextTrim(); // img
                        flashUrl =i.element("flash").getTextTrim(); // flash
                        videoTitle =i.element("title").getTextTrim(); // title
                    }
                }
                // 拼装酷6视频对象
                videoPropertyUnitForKu6 = newVideoPropertyUnitForKu6();
                videoPropertyUnitForKu6.setSourceCode(flashUrl);
               videoPropertyUnitForKu6.setTitle(videoTitle);
               videoPropertyUnitForKu6.setVideoId(videoId);
               videoPropertyUnitForKu6.setImgUrl(imgUrl);
            }
        } catch (Exception e) {
            logger.error(HuDongErrorCodeEnum.RemoteRequestError,"解析xml失败",e);
        }
        return videoPropertyUnitForKu6;
    }
 
    /**
     * 通用正则匹配工具
     * add by yangkai
     *
     * @param patternStr
     * @param videoUrl
     * @param ku6Url
     *@return
     */
    public static String patternUrl(StringpatternStr,String videoUrl,String ku6Url) {
        Matcher m =Pattern.compile(patternStr).matcher(videoUrl);
        while (m.find())
            ku6Url += m.group(1);
        return ku6Url;
    }
 
    /**
     * 访问酷6接口工具
     * add by yangkai
     * @param url
     * @return
     */
    private static String sendRequest(Stringurl) {
        InputStream inputStream = null;
        GetMethod getMethod = null;
        try {
            getMethod = new GetMethod(url);
            HttpClient httpClient = newHttpClient();
            int statusCode = 0;
            statusCode =httpClient.executeMethod(getMethod);
            if (statusCode != HttpStatus.SC_OK){
                logger.debug("访问目标url:{0} 失败,http状态码:{1}",new Object[] { url,statusCode });
                return StringUtils.EMPTY;
            }
            inputStream =getMethod.getResponseBodyAsStream();
            if (inputStream == null) {
                logger.debug("访问目标url:{0} 失败,返回io流为空",new Object[] { url });
                return StringUtils.EMPTY;
            }
            returnIoUtils.toString(inputStream,"UTF-8");
        } catch (HttpException e) {
           logger.error(HuDongErrorCodeEnum.RemoteRequestError,"网络原因,访问目标url:" + url + "失败",e);
            return StringUtils.EMPTY;
        } catch (IOException e) {
           logger.error(HuDongErrorCodeEnum.RemoteRequestError,"io原因,e);
            return StringUtils.EMPTY;
        } catch (Exception e) {
           logger.error(HuDongErrorCodeEnum.RemoteRequestError,"访问目标url:" + url + "失败",e);
            return StringUtils.EMPTY;
        } finally {
            if (getMethod != null) {
                getMethod.releaseConnection();
            }
            if (inputStream != null) {
                try {
                    inputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
}
 </span>

2.展示代码

展示其实很简单,动态的拼装一个embed标签放到html或jsp页面就行;格式如下:

<embedsrc="http://player.ku6.com/refer/flKjsasaK0W9fdCsFYO99A../v.swf"width="480" height="400"allowscriptaccess="always" allowfullscreen="true"type="application/x-shockwave-flash" ></embed>

在action中动态拼装的参数为src,src就是上面抓取程序抓到的参数了,还有一些抓到的title和description用于内嵌页面的其他样式展示用。


关于embed标签的使用方式可参考:http://www.jb51.cc/article/p-gakwyywu-da.html


转载请指明:杨凯专属频道

转载地址:http://blog.csdn.net/yangkai_hudong

原文链接:https://www.f2er.com/regex/361631.html

猜你在找的正则表达式相关文章