前言:自己开发的小程序想加个去水印的功能 网上借鉴了一番,自己做了个整理,效果不错
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
| import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; import org.slf4j.Logger; import org.slf4j.LoggerFactory;
import java.io.*; import java.net.HttpURLConnection; import java.net.URL; import java.util.ArrayList; import java.util.List; import java.util.regex.Matcher; import java.util.regex.Pattern;
public class DouyinUtils {
private static final Logger logger = LoggerFactory.getLogger(DouyinUtils.class);
private static final String API_URL = "https://api3-normal-c-hl.amemv.com/aweme/v1/multi/aweme/detail/"; private static final String UUID = "262745062603948";
public static void main(String[] args) { String url = "6.15 YZM:/ 收到蜂花的小玩偶了\uD83E\uDDF8 已经不想和没有蜂花玩偶的人玩儿了~# 九樽洗发水# 蜂花 https://v.douyin.com/ie6Gu4gu/ 复制此链接,打开Dou音搜索,直接观看视频!"; JSONObject dewatermarking = dewatermarking(url); }
public static JSONObject dewatermarking(String url) { String regexPattern = "https://[a-zA-Z0-9./]+"; Pattern pattern = Pattern.compile(regexPattern); Matcher matcher = pattern.matcher(url); List<String> urls = new ArrayList<>(); while (matcher.find()) { String shareUrl = matcher.group(); urls.add(shareUrl); } String ret; String vid = null; ret = GetUrl(urls.get(0)); String regEx = "7\\d{18}"; Pattern p = Pattern.compile(regEx); Matcher m = p.matcher(ret); if (m.find()) { vid = m.group(0); } return getVideo(vid); }
public static JSONObject getVideo(String videoId) { String t = String.valueOf(System.currentTimeMillis()); String url = String.format(API_URL, UUID, t.substring(0, 10), t, "3479331501001230", "10:2a:b3:7e:e5:2e", "a8f935d1-6b88-458a-8d15-1561ed4256b6", "af455a5a9cd3e9b4", "242369266188781");
String params = "aweme_ids=[%s]&origin_type=goods_rank_list_0&push_params=&request_source=0"; params = String.format(params, videoId); String ret = PostUrl(url, params);
JSONObject jsonObject = JSONObject.parseObject(ret); JSONArray awemeDetails = jsonObject.getJSONArray("aweme_details"); JSONObject awemeDetail = awemeDetails.getJSONObject(0);
String previewTitle = awemeDetail.getString("preview_title"); logger.info("视频标题: " + previewTitle);
JSONObject video = awemeDetail.getJSONObject("video"); JSONObject playAddr = video.getJSONObject("play_addr"); JSONArray urlList = playAddr.getJSONArray("url_list"); String videoUrl = urlList.getString(0); logger.info("视频链接: " + videoUrl);
JSONObject author = awemeDetail.getJSONObject("author"); String nickName = author.getString("nickname"); logger.info("用户名称: " + nickName);
JSONObject avatar300 = author.getJSONObject("avatar_300x300"); JSONArray authorUrlList = avatar300.getJSONArray("url_list"); String avatarUrl = authorUrlList.getString(0); logger.info("用户头像: " + avatarUrl);
JSONObject music = awemeDetail.getJSONObject("music"); JSONObject musicPlayUrl = music.getJSONObject("play_url"); JSONArray musicUrlList = musicPlayUrl.getJSONArray("url_list"); String musicUrl = musicUrlList.getString(0); logger.info("视频音乐链接: " + musicUrl);
JSONObject result = new JSONObject(); result.put("title", previewTitle); result.put("videoUrl", videoUrl); result.put("musicUrl", musicUrl); result.put("avatarUrl", avatarUrl); result.put("author", nickName); return result; }
public static String PostUrl(String httpUrl, String param) { try { URL url = new URL(httpUrl); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setRequestMethod("POST"); connection.setDoOutput(true); connection.setDoInput(true); connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); try (OutputStream os = connection.getOutputStream()) { os.write(param.getBytes()); } if (connection.getResponseCode() == 200) { try (InputStream is = connection.getInputStream(); BufferedReader br = new BufferedReader(new InputStreamReader(is, "UTF-8"))) { StringBuilder sbf = new StringBuilder(); String temp; while ((temp = br.readLine()) != null) { sbf.append(temp); sbf.append("\r\n"); } return sbf.toString(); } } } catch (IOException e) { logger.error("Error in POST request: " + e.getMessage()); } return null; }
public static String GetUrl(String url) { BufferedReader reader = null; String result = ""; try { URL realloc = new URL(url); HttpURLConnection connection = (HttpURLConnection) realloc.openConnection(); connection.setRequestMethod("GET"); connection.setRequestProperty("user-agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:101.0) Gecko/20100101 Firefox/101.0"); connection.connect(); connection.setInstanceFollowRedirects(false); String location = connection.getHeaderField("location"); result = location; } catch (Exception e) { logger.error("Error in GET request: " + e.getMessage()); } finally { if (reader != null) { try { reader.close(); } catch (Exception e) { logger.error("Error closing BufferedReader: " + e.getMessage()); } } } return result; } }
|