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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
package com.yanzuoguang.util.helper;
import com.yanzuoguang.util.YzgError;
import com.yanzuoguang.util.exception.CodeException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* 百度工具类
*
* @author 颜佐光
*/
public class BaiduHelper {
public static final Pattern REGEX = Pattern.compile(".*\\((.*)\\).*");
public static final class BaiduDate {
public static final int version = 143;
private String status;
private List<BaiduData> data;
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
public List<BaiduData> getData() {
return data;
}
public void setData(List<BaiduData> data) {
this.data = data;
}
}
public static final class BaiduData {
private int _version;
private List<BaiduUlmanac> almanac;
public int get_version() {
return _version;
}
public void set_version(int _version) {
this._version = _version;
}
public List<BaiduUlmanac> getAlmanac() {
return almanac;
}
public void setAlmanac(List<BaiduUlmanac> almanac) {
this.almanac = almanac;
}
}
public static final class BaiduUlmanac {
// 牛
public String animal;
// 装修.动土.安床.出行.安葬.上梁.旅游.破土.修造.求医.竖柱.词讼.出师.打官司
public String avoid;
// 二
public String cnDay;
// 14
public String day;
// 说明
public String desc;
// 乙丑
public String gzDate;
// 丁酉
public String gzMonth;
// 辛丑
public String gzYear;
// 1 or ""
public String isBigMonth;
// 初八
public String lDate;
// 八
public String lMonth;
// 8
public String lunarDate;
// 8
public String lunarMonth;
// 2021
public String lunarYear;
// 9
public String month;
// 2021
public String oDate;
// 搬家.开业.结婚.入宅.领证.开工.订婚.开张.作灶.入学.求嗣.赴任.祈福.祭祀.开市.纳财.纳畜.裁衣.嫁娶.纳采.移徙.盖屋.冠笄.栽种.斋醮.求财.招赘.纳婿
public String suit;
// 24节气
public String term;
// t,i,a
public String type;
// 世界节日
public String value;
// 2021
public String year;
// status: 1-工作日,2-上班,3-默认
public String status;
}
/**
* 获取百度万年历
*
* @param dt
* @return
*/
public static BaiduDate getBaiduData(Date dt) {
String query = DateHelper.getDateTimeString("yyyy年M月", dt);
long time = System.currentTimeMillis();
Map<String, Object> map = new HashMap<>();
// map.put("query", "2021年10月");
map.put("query", query);
map.put("co", "");
map.put("resource_id", "39043");
map.put("t", time);
map.put("ie", "utf8");
map.put("oe", "gbk");
map.put("cb", "op_aladdin_callback");
map.put("format", "json");
map.put("tn", "wisetpl");
map.put("cb", "jQuery1102007795278844876075_" + (time - 30));
map.put("_", time - 50);
String para = HttpHelper.getUrlParameter(map);
String url = "https://sp0.baidu.com/8aQDcjqpAAV3otqbppnN2DJv/api.php?";
String fullUrl = url + para;
String post = HttpHelper.get(fullUrl, "gbk");
if (StringHelper.isEmpty(post)) {
throw YzgError.getRuntimeException("052");
}
Matcher matcher = REGEX.matcher(post);
if (!matcher.find()) {
throw YzgError.getRuntimeException("053");
}
String group = matcher.group(1);
BaiduDate deserialize = JsonHelper.deserialize(group, BaiduDate.class);
if (!StringHelper.compare(deserialize.status, "0")) {
throw YzgError.getRuntimeException("054", deserialize.status, deserialize);
}
if (ArrayHelper.isEmpty(deserialize.getData())) {
throw YzgError.getRuntimeException("055", deserialize);
}
if (ArrayHelper.isEmpty(deserialize.getData().get(0).almanac)) {
throw YzgError.getRuntimeException("055", deserialize);
}
return deserialize;
}
}