package com.ms.biz.test;

import com.ms.biz.tool.CommonTool;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class GetUrlFromHtmlTest {

    public List<String> extractImgUrls(String input) {
        List<String> matchUrls = new ArrayList<>();
        String pattern1 = "background\\s*=\\s*(?:'|\\\")\\s*([^<>]*)(?:'|\\\")\\s*";
        Pattern p1 = Pattern.compile(pattern1, Pattern.CASE_INSENSITIVE | Pattern.DOTALL);
        Matcher m1 = p1.matcher(input);
        while (m1.find()) {
            matchUrls.add(m1.group(1));
        }

        String pattern2 = "background\\s*:\\s*url\\((?:'|\\\"|)([^<>]*)(?:'|\\\"|)\\)";
        Pattern p2 = Pattern.compile(pattern2, Pattern.CASE_INSENSITIVE | Pattern.DOTALL);
        Matcher m2 = p2.matcher(input);
        while (m2.find()) {
            matchUrls.add(m2.group(1));
        }

        String pattern3 = "src\\s*=\\s*[\"|'](.*)[\"|']";
        Pattern p3 = Pattern.compile(pattern3, Pattern.CASE_INSENSITIVE | Pattern.DOTALL);
        Matcher m3 = p3.matcher(input);
        while (m3.find()) {
            if (m3.group(1).matches(".*(?<!/)$")) {
                matchUrls.add(m3.group(1));
            }
        }

        List<String> newImgUrls = new ArrayList<>();
        for (String originUrl : matchUrls) {
            if (originUrl.endsWith(".mp4")) {
                continue;
            }
            if (originUrl.startsWith("http")) {
                newImgUrls.add(originUrl);
            } else {
                newImgUrls.add("http:" + originUrl);
            }
        }

        return new ArrayList<>(new HashSet<>(newImgUrls));
    }

    public static void main(String[] args) {
        String input = "<div id=\"offer-template-0\"></div><p style=\"text-align: center;\"><span style=\"color: #ff0000;font-size: 26.0pt;\"><img alt=\"im2Ti/iky9QSPhtB9FO7xmy5X0TG5V9u3RvN\" src=\"https://cbu01.alicdn.com/img/ibank/O1CN01Jo7k0u29J7QfgSnqO_!!995828046-0-cib.jpg\" alt=\"IMG_1266(20210611-082535)_副本\"/><br/>乌龙铁观音茶叶（<span style=\"color: #000000;\">实图拍摄</span>）还有更多中低档茶叶量大可直接联系掌柜</span></p>\n" +
                "<p style=\"text-align: center;\"><span style=\"color: #ff0000;font-size: 12.0pt;\"> <strong>厂家直销乌龙茶中低档安溪铁观音1725铁观音青茶花香味</strong></span></p>\n" +
                "<p style=\"text-align: left;\"><span style=\"color: #ff0000;font-size: 12.0pt;\"><strong> 茶农师傅经典制作的新茶上市，减少中间环节，</strong><strong>其每道制作工艺由经验丰富老茶师亲手筛选、炒制、层层把关、严格控制. 观其形-外观紧结、沉重、呈沙绿色察其色-汤色清澈、淡黄微绿闻其香-浓韵、观音韵、带有兰花香品其味-鲜纯、浓爽、音韵明显、回甘较久！价格实惠产地货源！不可多得哦！</strong></span></p>\n" +
                "<p style=\"text-align: left;\"><span style=\"color: #ff0000;font-size: 12.0pt;\">主要特点：</span></p>\n" +
                "<p style=\"text-align: left;\"><span style=\"color: #ff0000;font-size: 12.0pt;\">【茶树新】铁观音茶树更新，滋味就更鲜、更甘醇。</span></p>\n" +
                "<div style=\"text-align: center;\">\n" +
                "<p style=\"text-align: left;\"><span style=\"color: #ff0000;font-size: 12.0pt;\">【手工】严格按照传统手工制作，干茶砂绿色明显，因走水充分汤色黄绿、明亮；汤味滑、活、厚，香气浓郁、观音韵明显，回味甘甜，口齿留香；叶底色黄绿，有光泽，底片柔软。</span></p>\n" +
                "<p style=\"text-align: center;\"><span style=\"color: #000000;font-size: 22.0pt;font-family: simhei;\">实图拍摄，盗图必究</span></p>\n" +
                "</div>\n" +
                "<p style=\"text-align: center;\"><span><img src=\"https://cbu01.alicdn.com/img/ibank/2014/672/647/1390746276_1412110255.jpg\" alt=\"12\" width=\"751\" height=\"697\"/></span></p>\n" +
                "<p> </p>\n" +
                "<p><img style=\"display: block;margin-left: auto;margin-right: auto;\" src=\"https://cbu01.alicdn.com/img/ibank/2014/201/792/1390297102_1412110255.jpg\" alt=\"undefined\" width=\"749\" height=\"696\"/></p>\n" +
                "<p><br/><img style=\"display: block;margin-left: auto;margin-right: auto;\" src=\"https://cbu01.alicdn.com/img/ibank/2016/710/774/2794477017_1412110255.jpg\" alt=\"124\" width=\"752\" height=\"752\"/></p>\n" +
                "<p style=\"text-align: center;\"> <img src=\"https://cbu01.alicdn.com/img/ibank/2017/694/637/3871736496_1412110255.jpg\" alt=\"5715fc48N4af9cb31\" width=\"751\" height=\"751\"/> <img src=\"https://cbu01.alicdn.com/img/ibank/2018/977/697/8940796779_1412110255.jpg\" alt=\"IMG_0598\" width=\"751\" height=\"755\"/></p>\n" +
                "<p style=\"text-align: center;\"><img src=\"https://cbu01.alicdn.com/img/ibank/2017/022/100/3970001220_1983945663.jpg\" alt=\"undefined\"/></p>\n" +
                "<p style=\"text-align: center;\"><img src=\"https://cbu01.alicdn.com/img/ibank/2017/881/400/3970004188_1983945663.jpg\" alt=\"undefined\"/></p>\n" +
                "<p style=\"text-align: center;\"><img style=\"line-height: 1.5;\" src=\"https://cbu01.alicdn.com/img/ibank/2017/682/100/3970001286_1983945663.jpg\" alt=\"undefined\"/></p>\n" +
                "<p style=\"text-align: center;\"><img src=\"https://cbu01.alicdn.com/img/ibank/2017/844/798/3972897448_1983945663.jpg\" alt=\"undefined\"/><span style=\"line-height: 1.5;\">   </span> <img src=\"https://cbu01.alicdn.com/img/ibank/2018/766/300/8901003667_1412110255.jpg\" alt=\"IMG_3730\" width=\"790\" height=\"790\"/></p>\n" +
                "<p style=\"text-align: center;\"><span style=\"color: #ff0000;font-size: 24.0pt;\">小泡真空包装袋随市场更新而更换，介意的亲们请备注</span></p>\n" +
                "<p style=\"text-align: center;\"><span style=\"color: #ff0000;font-size: 24.0pt;\"><img src=\"https://cbu01.alicdn.com/img/ibank/2018/125/635/10218536521_1412110255.jpg\" alt=\"undefined\" width=\"790\" height=\"526.6666666666666\"/><br/><img src=\"https://cbu01.alicdn.com/img/ibank/2018/240/875/10218578042_1412110255.jpg\" alt=\"undefined\" width=\"790\" height=\"526.6666666666666\"/><br/><img src=\"https://cbu01.alicdn.com/img/ibank/2018/727/466/10194664727_1412110255.jpg\" alt=\"undefined\" width=\"790\" height=\"526.6666666666666\"/><br/><img src=\"https://cbu01.alicdn.com/img/ibank/2018/346/125/10218521643_1412110255.jpg\" alt=\"undefined\" width=\"790\" height=\"526.6666666666666\"/><br/><img src=\"https://cbu01.alicdn.com/img/ibank/2018/734/155/10218551437_1412110255.jpg\" alt=\"undefined\" width=\"790\" height=\"526.6666666666666\"/><br/><img src=\"https://cbu01.alicdn.com/img/ibank/2018/547/780/10167087745_1412110255.jpg\" alt=\"undefined\" width=\"790\" height=\"526.6666666666666\"/><br/>礼盒规格：38*29*10（厘米）</span></p>\n" +
                "<p style=\"text-align: center;\"><span style=\"font-family: simhei;font-size: 15.0pt;\">挑完梗后真空包装，使用全自动真空包装，茶叶不落地，干净卫生</span></p>\n" +
                "<p style=\"text-align: center;\"><br/><img src=\"https://cbu01.alicdn.com/img/ibank/2014/300/296/1390692003_1412110255.jpg\" alt=\"机器\" width=\"750\" height=\"562\"/><br/><br/></p>\n" +
                "<p style=\"text-align: center;\"><img src=\"https://cbu01.alicdn.com/img/ibank/2013/649/894/816498946_1412110255.jpg\" alt=\"20130412_120307\" width=\"750\" height=\"562\"/><br/><br/><img src=\"https://cbu01.alicdn.com/img/ibank/2013/058/994/816499850_1412110255.jpg\" alt=\"20130415_113407\" width=\"750\" height=\"1000\"/></p>\n" +
                "<p style=\"text-align: center;\"> </p>\n" +
                "<p style=\"text-align: center;\"> <img src=\"https://cbu01.alicdn.com/img/ibank/2018/316/448/8940844613_1412110255.jpg\" alt=\"IMG_1127_副本\" width=\"752\" height=\"790\"/><br/><br/><img src=\"https://cbu01.alicdn.com/img/ibank/2018/820/400/8921004028_1412110255.jpg\" alt=\"IMG_1128_副本\" width=\"753\" height=\"790\"/><br/><br/></p>\n" +
                "<p style=\"text-align: center;\"><span><img src=\"https://cbu01.alicdn.com/img/ibank/2018/337/630/8901036733_1412110255.jpg\" alt=\"IMG_1012\" width=\"752\" height=\"1412\"/><br/><br/><img src=\"https://cbu01.alicdn.com/img/ibank/O1CN01qeSC8m29J7Qhhp5mE_!!995828046-0-cib.jpg\" alt=\"undefined\" width=\"790\" height=\"1223.2482394366198\"/><br/><br/></span></p>\n" +
                "<p style=\"text-align: center;\"><img style=\"display: block;margin-left: auto;margin-right: auto;\" src=\"https://cbu01.alicdn.com/img/ibank/2012/554/070/546070455_1412110255.jpg\" width=\"752\" height=\"590\"/></p>\n" +
                "<p style=\"text-align: center;\"> </p>\n" +
                "<p style=\"text-align: center;\"><img style=\"display: block;margin-left: auto;margin-right: auto;\" src=\"https://cbu01.alicdn.com/img/ibank/2012/274/070/546070472_1412110255.jpg\" width=\"742\" height=\"553\"/></p>\n" +
                "<p style=\"text-align: center;\"> </p>\n" +
                "<p style=\"text-align: center;\"><img style=\"display: block;margin-left: auto;margin-right: auto;\" src=\"https://cbu01.alicdn.com/img/ibank/2013/120/951/761159021_1412110255.jpg\" alt=\"茶季忙时\" width=\"750\" height=\"499\"/></p>\n" +
                "<p style=\"text-align: center;\"><br/><img style=\"display: block;margin-left: auto;margin-right: auto;\" src=\"https://cbu01.alicdn.com/img/ibank/2013/620/951/761159026_1412110255.jpg\" alt=\"茶季忙时\" width=\"750\" height=\"499\"/><br/><img style=\"display: block;margin-left: auto;margin-right: auto;\" src=\"https://cbu01.alicdn.com/img/ibank/2013/230/951/761159032_1412110255.jpg\" alt=\"茶季忙时\" width=\"750\" height=\"499\"/><br/><img src=\"https://cbu01.alicdn.com/img/ibank/2013/450/951/761159054_1412110255.jpg\" alt=\"茶季忙时\" width=\"748\" height=\"498\"/><br/><br/><img src=\"https://cbu01.alicdn.com/img/ibank/2013/340/951/761159043_1412110255.jpg\" alt=\"茶季忙时\" width=\"750\" height=\"499\"/><br/><br/><img src=\"https://cbu01.alicdn.com/img/ibank/2016/687/693/3038396786_1412110255.jpg\" alt=\"dbb44aed2e738bd4615cb861a18b87\" width=\"753\" height=\"503\"/><br/><br/><img src=\"https://cbu01.alicdn.com/img/ibank/2016/631/816/3037618136_1412110255.jpg\" alt=\"mmexport1462243402203\" width=\"758\" height=\"748\"/><br/><br/><img src=\"https://cbu01.alicdn.com/img/ibank/2016/611/819/3039918116_1412110255.jpg\" alt=\"mmexport1462243413897\" width=\"758\" height=\"567\"/><br/><br/><img src=\"https://cbu01.alicdn.com/img/ibank/2013/370/951/761159073_1412110255.jpg\" alt=\"茶季忙时\" width=\"751\" height=\"555\"/><br/><br/><img src=\"https://cbu01.alicdn.com/img/ibank/2016/181/039/3039930181_1412110255.jpg\" alt=\"mmexport1461107560050\" width=\"747\" height=\"622\"/><br/><br/></p>\n" +
                "<p style=\"text-align: left;\" align=\"left\"><span style=\"font-size: 12.0pt;\"><strong>邮资说明：</strong></span></p>\n" +
                "<p style=\"text-align: left;\"><span style=\"font-size: 12.0pt;\"> </span></p>\n" +
                "<p style=\"text-align: left;\" align=\"left\"><span style=\"font-size: 12.0pt;\">为了保证运输安全，包装都外加一个PVC硬盒/纸箱，为了增加效率，统一采用快递，一般3天内到达，优先使用快递，快递不能到达的地方使用EMS，运费算法详细见报价单：<br/>☆ 可随时查询定单运送情况：订单有快递跟踪记录也可直接联系掌柜查询。<br/>☆也可以备注需要发什么快递</span></p>";
        GetUrlFromHtmlTest test = new GetUrlFromHtmlTest();
        List<String> urlsA = test.extractImgUrls(input);
        List<String> urlsB = CommonTool.getUrlsFromHtml(input);
        for (String url : urlsA) {
            System.out.println(url);
        }
        System.out.println("============================");
        for (String url : urlsB) {
            System.out.println(url);
        }
    }

}


