package com.izouma.walkchina; import com.github.kevinsawicki.http.HttpRequest; import com.google.gson.Gson; import com.izouma.walkchina.domain.City; import com.izouma.walkchina.domain.UserInfo; import com.izouma.walkchina.dto.Location; import com.izouma.walkchina.dto.LogisticsInfo; import com.izouma.walkchina.utils.ImageUtils; import net.sourceforge.pinyin4j.PinyinHelper; import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType; import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat; import net.sourceforge.pinyin4j.format.HanyuPinyinToneType; import net.sourceforge.pinyin4j.format.HanyuPinyinVCharType; import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination; import org.apache.commons.beanutils.BeanUtils; import org.apache.commons.text.StringEscapeUtils; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; import org.junit.Test; import javax.imageio.ImageIO; import java.io.File; import java.io.FileWriter; import java.io.IOException; import java.lang.reflect.InvocationTargetException; import java.math.BigDecimal; import java.math.RoundingMode; import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.nio.file.Paths; import java.text.MessageFormat; import java.time.Instant; import java.time.ZoneId; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.stream.Stream; public class CommonTest { @Test public void testImg() throws IOException { ImageIO.write(ImageUtils.makeMarker("user", "https://ws1.sinaimg.cn/large/0065oQSqgy1fze94uew3jj30qo10cdka.jpg"), "png", new File("image1.png")); ImageIO.write(ImageUtils.makeMarker("location", "https://ws1.sinaimg.cn/large/0065oQSqgy1fze94uew3jj30qo10cdka.jpg"), "png", new File("image2.png")); } @Test public void testStringFmt() { String string = String.format("name=%s, age=%d %s", "huhx", 25, 1.1); System.out.println(string); String message = MessageFormat.format("name={1}, age={0}, {1} {2,number,#.#}", 25, "huhx", 1.1111); System.out.println(message); System.out.println(String.format("%.2f", BigDecimal.valueOf(1.11111))); } @Test public void testCopyProperties() throws InvocationTargetException, IllegalAccessException { UserInfo u1 = UserInfo.builder() .nickname("aaa") .phone("111") .build(); UserInfo u2 = UserInfo.builder() .nickname("bbb") .sex(1) .build(); BeanUtils.copyProperties(u1, u2); System.out.println(); } @Test public void readArea() throws IOException, JSONException, InterruptedException, BadHanyuPinyinOutputFormatCombination { File file = new File("/Users/drew/Projects/china_area/area_code_2019.json"); StringBuilder contentBuilder = new StringBuilder(); try (Stream stream = Files.lines(Paths.get("/Users/drew/Projects/china_area/area_code_2019.json"), StandardCharsets.UTF_8)) { stream.forEach(s -> contentBuilder.append(s).append("\n")); } catch (IOException e) { e.printStackTrace(); } JSONArray jsonArray = new JSONArray(contentBuilder.toString()); List list = new ArrayList<>(); getCity(list, jsonArray); List cityList = new ArrayList<>(); for (JSONObject jsonObject : list) { String fullname = jsonObject.getString("name"); String name = fullname.replace("市", ""); String code = jsonObject.getString("code"); Long provinceId = Long.parseLong(code.substring(0, 2) + "0000"); Location location = search(fullname); if (location != null) { City city = City.builder() .id(Long.parseLong(code.substring(0, 6))) .fullname(fullname) .name(name) .pinyin(getpinyin(name)) .latitude(location.getLatitude()) .longitude(location.getLongitude()) .provinceId(provinceId) .build(); cityList.add(city); } } Gson gson = new Gson(); File file1 = new File("city1.json"); FileWriter fileWriter = new FileWriter(file1); fileWriter.write(gson.toJson(cityList)); fileWriter.flush(); fileWriter.close(); File file2 = new File("city2.json"); FileWriter fileWriter1 = new FileWriter(file2); fileWriter1.write(gson.toJson(list)); fileWriter1.flush(); fileWriter1.close(); } public Location search(String name) throws InterruptedException { Thread.sleep(210); try { Map params = new HashMap<>(); params.put("key", "YO4BZ-G75L5-CWJIV-QDPOY-77OIH-LGFMT"); params.put("keyword", name); params.put("boundary", "region(北京,1)"); String body = HttpRequest.get("https://apis.map.qq.com/ws/place/v1/search", params, false).body(); System.out.println(body); JSONObject jsonObject = new JSONObject(body); if (jsonObject.optInt("status") == 0) { JSONObject data = jsonObject.getJSONArray("data").getJSONObject(0); if (data.getInt("type") == 4) { return new Location(data.getJSONObject("location").getDouble("lat"), data.getJSONObject("location").getDouble("lng")); } } } catch (Exception e) { e.printStackTrace(); } return null; } public String getpinyin(String name) throws BadHanyuPinyinOutputFormatCombination { HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat(); defaultFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE); // 输出拼音全部小写 defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE); // 不带声调 defaultFormat.setVCharType(HanyuPinyinVCharType.WITH_V); StringBuilder pinyin = new StringBuilder(); for (int i = 0; i < name.length(); i++) { if (i != 0) { pinyin.append(" "); } pinyin.append(PinyinHelper.toHanyuPinyinStringArray(name.charAt(i), defaultFormat)[0]); } return pinyin.toString(); } private void getCity(List list, JSONArray jsonArray) throws JSONException { if (jsonArray == null) return; for (int i = 0; i < jsonArray.length(); i++) { JSONObject jsonObject = jsonArray.getJSONObject(i); if (jsonObject.getInt("level") < 3) { getCity(list, jsonObject.optJSONArray("children")); } else if (jsonObject.getInt("level") == 3) { String name = jsonObject.getString("name"); if (name.endsWith("市") || name.endsWith("县")) { list.add(jsonObject); } } } } @Test public void testLogistic() throws JSONException { String url = "https://sp0.baidu.com/9_Q4sjW91Qh3otqbppnN2DJv/pae/channel/data/asyncqury?appid=4001&com=&nu=1121271618192"; String body = HttpRequest.get(url, true) .header("Cookie", "BAIDUID=0B261C5E2F8B53A17D49E0CE5033E19B:FG=1; BIDUPSID=0B261C5E2F8B53A17D49E0CE5033E19B; PSTM=1551535964; BDUSS=ElnRXAwdkcyMmNEdUVuQjIwcFl5ejl5YzB5elNSOFdEbi02UVBiZXdNZmo3S1ZjQVFBQUFBJCQAAAAAAAAAAAEAAACQUvsNzvu5~rTz0NwAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAONfflzjX35cM; MCITY=-315%3A; BDORZ=B490B5EBF6F3CD402E515D22BCDA1598; H_PS_PSSID=26524_1450_21125_29522_29520_29098_29567_28838_29221_26350_29589; delPer=0; BDRCVFR[feWj1Vr5u3D]=mk3SLVN4HKm; PSINO=7") .body(); JSONObject jsonObject = new JSONObject(StringEscapeUtils.unescapeJava(body)); try { JSONArray jsonArray = jsonObject.getJSONObject("data").getJSONObject("info").getJSONArray("context"); LogisticsInfo logisticsInfo = new LogisticsInfo(); List trackingList = new ArrayList<>(); for (int i = 0; i < jsonArray.length(); i++) { JSONObject jsonInfo = jsonArray.getJSONObject(i); LogisticsInfo.TrackingInfo info = LogisticsInfo.TrackingInfo.builder() .time(Instant.ofEpochSecond(jsonInfo.getLong("time")).atZone(ZoneId.systemDefault()).toLocalDateTime()) .desc(jsonInfo.getString("desc")) .build(); trackingList.add(info); if (i == 0) { logisticsInfo.setLatest(info); } } logisticsInfo.setTrackingList(trackingList); System.out.println(logisticsInfo); } catch (Exception e) { e.printStackTrace(); } } @Test public void aaa() { BigDecimal amount; do { amount = BigDecimal.valueOf(Math.random() * 1.00).setScale(2, RoundingMode.HALF_UP); } while (amount.compareTo(BigDecimal.valueOf(0.01)) <= 0 || amount.compareTo(BigDecimal.valueOf(1)) >= 0); System.out.println(amount); } @Test public void bbb() { Boolean b = null; System.out.println(b ? 1 : 2); } }