CommonTest.java 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. package com.izouma.walkchina;
  2. import com.github.kevinsawicki.http.HttpRequest;
  3. import com.google.gson.Gson;
  4. import com.izouma.walkchina.domain.City;
  5. import com.izouma.walkchina.domain.UserInfo;
  6. import com.izouma.walkchina.dto.Location;
  7. import com.izouma.walkchina.dto.LogisticsInfo;
  8. import com.izouma.walkchina.utils.ImageUtils;
  9. import net.sourceforge.pinyin4j.PinyinHelper;
  10. import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType;
  11. import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
  12. import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;
  13. import net.sourceforge.pinyin4j.format.HanyuPinyinVCharType;
  14. import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;
  15. import org.apache.commons.beanutils.BeanUtils;
  16. import org.apache.commons.text.StringEscapeUtils;
  17. import org.json.JSONArray;
  18. import org.json.JSONException;
  19. import org.json.JSONObject;
  20. import org.junit.Test;
  21. import javax.imageio.ImageIO;
  22. import java.io.File;
  23. import java.io.FileWriter;
  24. import java.io.IOException;
  25. import java.lang.reflect.InvocationTargetException;
  26. import java.math.BigDecimal;
  27. import java.math.RoundingMode;
  28. import java.nio.charset.StandardCharsets;
  29. import java.nio.file.Files;
  30. import java.nio.file.Paths;
  31. import java.text.MessageFormat;
  32. import java.time.Instant;
  33. import java.time.ZoneId;
  34. import java.util.ArrayList;
  35. import java.util.HashMap;
  36. import java.util.List;
  37. import java.util.Map;
  38. import java.util.stream.Stream;
  39. public class CommonTest {
  40. @Test
  41. public void testImg() throws IOException {
  42. ImageIO.write(ImageUtils.makeMarker("user", "https://ws1.sinaimg.cn/large/0065oQSqgy1fze94uew3jj30qo10cdka.jpg"), "png", new File("image1.png"));
  43. ImageIO.write(ImageUtils.makeMarker("location", "https://ws1.sinaimg.cn/large/0065oQSqgy1fze94uew3jj30qo10cdka.jpg"), "png", new File("image2.png"));
  44. }
  45. @Test
  46. public void testStringFmt() {
  47. String string = String.format("name=%s, age=%d %s", "huhx", 25, 1.1);
  48. System.out.println(string);
  49. String message = MessageFormat.format("name={1}, age={0}, {1} {2,number,#.#}", 25, "huhx", 1.1111);
  50. System.out.println(message);
  51. System.out.println(String.format("%.2f", BigDecimal.valueOf(1.11111)));
  52. }
  53. @Test
  54. public void testCopyProperties() throws InvocationTargetException, IllegalAccessException {
  55. UserInfo u1 = UserInfo.builder()
  56. .nickname("aaa")
  57. .phone("111")
  58. .build();
  59. UserInfo u2 = UserInfo.builder()
  60. .nickname("bbb")
  61. .sex(1)
  62. .build();
  63. BeanUtils.copyProperties(u1, u2);
  64. System.out.println();
  65. }
  66. @Test
  67. public void readArea() throws IOException, JSONException, InterruptedException, BadHanyuPinyinOutputFormatCombination {
  68. File file = new File("/Users/drew/Projects/china_area/area_code_2019.json");
  69. StringBuilder contentBuilder = new StringBuilder();
  70. try (Stream<String> stream = Files.lines(Paths.get("/Users/drew/Projects/china_area/area_code_2019.json"), StandardCharsets.UTF_8)) {
  71. stream.forEach(s -> contentBuilder.append(s).append("\n"));
  72. } catch (IOException e) {
  73. e.printStackTrace();
  74. }
  75. JSONArray jsonArray = new JSONArray(contentBuilder.toString());
  76. List<JSONObject> list = new ArrayList<>();
  77. getCity(list, jsonArray);
  78. List<City> cityList = new ArrayList<>();
  79. for (JSONObject jsonObject : list) {
  80. String fullname = jsonObject.getString("name");
  81. String name = fullname.replace("市", "");
  82. String code = jsonObject.getString("code");
  83. Long provinceId = Long.parseLong(code.substring(0, 2) + "0000");
  84. Location location = search(fullname);
  85. if (location != null) {
  86. City city = City.builder()
  87. .id(Long.parseLong(code.substring(0, 6)))
  88. .fullname(fullname)
  89. .name(name)
  90. .pinyin(getpinyin(name))
  91. .latitude(location.getLatitude())
  92. .longitude(location.getLongitude())
  93. .provinceId(provinceId)
  94. .build();
  95. cityList.add(city);
  96. }
  97. }
  98. Gson gson = new Gson();
  99. File file1 = new File("city1.json");
  100. FileWriter fileWriter = new FileWriter(file1);
  101. fileWriter.write(gson.toJson(cityList));
  102. fileWriter.flush();
  103. fileWriter.close();
  104. File file2 = new File("city2.json");
  105. FileWriter fileWriter1 = new FileWriter(file2);
  106. fileWriter1.write(gson.toJson(list));
  107. fileWriter1.flush();
  108. fileWriter1.close();
  109. }
  110. public Location search(String name) throws InterruptedException {
  111. Thread.sleep(210);
  112. try {
  113. Map<String, String> params = new HashMap<>();
  114. params.put("key", "YO4BZ-G75L5-CWJIV-QDPOY-77OIH-LGFMT");
  115. params.put("keyword", name);
  116. params.put("boundary", "region(北京,1)");
  117. String body = HttpRequest.get("https://apis.map.qq.com/ws/place/v1/search", params, false).body();
  118. System.out.println(body);
  119. JSONObject jsonObject = new JSONObject(body);
  120. if (jsonObject.optInt("status") == 0) {
  121. JSONObject data = jsonObject.getJSONArray("data").getJSONObject(0);
  122. if (data.getInt("type") == 4) {
  123. return new Location(data.getJSONObject("location").getDouble("lat"), data.getJSONObject("location").getDouble("lng"));
  124. }
  125. }
  126. } catch (Exception e) {
  127. e.printStackTrace();
  128. }
  129. return null;
  130. }
  131. public String getpinyin(String name) throws BadHanyuPinyinOutputFormatCombination {
  132. HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat();
  133. defaultFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE); // 输出拼音全部小写
  134. defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE); // 不带声调
  135. defaultFormat.setVCharType(HanyuPinyinVCharType.WITH_V);
  136. StringBuilder pinyin = new StringBuilder();
  137. for (int i = 0; i < name.length(); i++) {
  138. if (i != 0) {
  139. pinyin.append(" ");
  140. }
  141. pinyin.append(PinyinHelper.toHanyuPinyinStringArray(name.charAt(i), defaultFormat)[0]);
  142. }
  143. return pinyin.toString();
  144. }
  145. private void getCity(List<JSONObject> list, JSONArray jsonArray) throws JSONException {
  146. if (jsonArray == null) return;
  147. for (int i = 0; i < jsonArray.length(); i++) {
  148. JSONObject jsonObject = jsonArray.getJSONObject(i);
  149. if (jsonObject.getInt("level") < 3) {
  150. getCity(list, jsonObject.optJSONArray("children"));
  151. } else if (jsonObject.getInt("level") == 3) {
  152. String name = jsonObject.getString("name");
  153. if (name.endsWith("市") || name.endsWith("县")) {
  154. list.add(jsonObject);
  155. }
  156. }
  157. }
  158. }
  159. @Test
  160. public void testLogistic() throws JSONException {
  161. String url = "https://sp0.baidu.com/9_Q4sjW91Qh3otqbppnN2DJv/pae/channel/data/asyncqury?appid=4001&com=&nu=1121271618192";
  162. String body = HttpRequest.get(url, true)
  163. .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")
  164. .body();
  165. JSONObject jsonObject = new JSONObject(StringEscapeUtils.unescapeJava(body));
  166. try {
  167. JSONArray jsonArray = jsonObject.getJSONObject("data").getJSONObject("info").getJSONArray("context");
  168. LogisticsInfo logisticsInfo = new LogisticsInfo();
  169. List<LogisticsInfo.TrackingInfo> trackingList = new ArrayList<>();
  170. for (int i = 0; i < jsonArray.length(); i++) {
  171. JSONObject jsonInfo = jsonArray.getJSONObject(i);
  172. LogisticsInfo.TrackingInfo info = LogisticsInfo.TrackingInfo.builder()
  173. .time(Instant.ofEpochSecond(jsonInfo.getLong("time")).atZone(ZoneId.systemDefault()).toLocalDateTime())
  174. .desc(jsonInfo.getString("desc"))
  175. .build();
  176. trackingList.add(info);
  177. if (i == 0) {
  178. logisticsInfo.setLatest(info);
  179. }
  180. }
  181. logisticsInfo.setTrackingList(trackingList);
  182. System.out.println(logisticsInfo);
  183. } catch (Exception e) {
  184. e.printStackTrace();
  185. }
  186. }
  187. @Test
  188. public void aaa() {
  189. BigDecimal amount;
  190. do {
  191. amount = BigDecimal.valueOf(Math.random() * 1.00).setScale(2, RoundingMode.HALF_UP);
  192. } while (amount.compareTo(BigDecimal.valueOf(0.01)) <= 0 || amount.compareTo(BigDecimal.valueOf(1)) >= 0);
  193. System.out.println(amount);
  194. }
  195. @Test
  196. public void bbb() {
  197. Boolean b = null;
  198. System.out.println(b ? 1 : 2);
  199. }
  200. }