Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Sign in / Register
Toggle navigation
Y
yzg-util
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
YZG
yzg-util
Commits
a4dd8d3c
Commit
a4dd8d3c
authored
Apr 26, 2021
by
yanzg
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
不记录系统日志
parent
68bfd9d6
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
169 additions
and
1 deletion
+169
-1
BaiduHelper.java
...rc/main/java/com/yanzuoguang/util/helper/BaiduHelper.java
+161
-0
HttpHelperTest.java
yzg-util-base/src/test/java/helper/HttpHelperTest.java
+8
-1
No files found.
yzg-util-base/src/main/java/com/yanzuoguang/util/helper/BaiduHelper.java
0 → 100644
View file @
a4dd8d3c
package
com
.
yanzuoguang
.
util
.
helper
;
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
;
public
int
get_version
()
{
return
_version
;
}
public
void
set_version
(
int
_version
)
{
this
.
_version
=
_version
;
}
private
List
<
BaiduUlmanac
>
almanac
;
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
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
;
// 2021
public
String
year
;
}
/**
* 获取百度万年历
*
* @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
new
CodeException
(
"获取百度接口日期失败"
);
}
Matcher
matcher
=
REGEX
.
matcher
(
post
);
if
(!
matcher
.
find
())
{
throw
new
CodeException
(
"获取百度日期正则表达式失败"
);
}
String
group
=
matcher
.
group
(
1
);
BaiduDate
deserialize
=
JsonHelper
.
deserialize
(
group
,
BaiduDate
.
class
);
if
(!
StringHelper
.
compare
(
deserialize
.
status
,
"0"
))
{
throw
new
CodeException
(
"获取百度日期返回错误状态:"
+
deserialize
.
status
,
deserialize
);
}
if
(
ArrayHelper
.
isEmpty
(
deserialize
.
getData
()))
{
throw
new
CodeException
(
"获取百度日期数据格式已修改"
,
deserialize
);
}
if
(
ArrayHelper
.
isEmpty
(
deserialize
.
getData
().
get
(
0
).
almanac
))
{
throw
new
CodeException
(
"获取百度日期数据格式已修改"
,
deserialize
);
}
return
deserialize
;
}
}
yzg-util-base/src/test/java/helper/HttpHelperTest.java
View file @
a4dd8d3c
package
helper
;
import
com.yanzuoguang.util.helper.BaiduHelper
;
import
com.yanzuoguang.util.helper.HttpHelper
;
import
com.yanzuoguang.util.helper.JsonHelper
;
import
helper.vo.BaiduDate
;
import
org.junit.Test
;
import
java.util.Date
;
import
java.util.HashMap
;
import
java.util.Map
;
import
java.util.regex.Matcher
;
...
...
@@ -37,7 +39,6 @@ public class HttpHelperTest {
String
post
=
HttpHelper
.
get
(
fullUrl
,
"gbk"
);
System
.
out
.
println
(
post
);
Pattern
compile
=
Pattern
.
compile
(
".*\\((.*)\\).*"
);
Matcher
matcher
=
compile
.
matcher
(
post
);
if
(
matcher
.
find
())
{
...
...
@@ -47,4 +48,10 @@ public class HttpHelperTest {
System
.
out
.
println
(
result
.
getStatus
());
}
}
@Test
public
void
testBaidu
()
{
BaiduHelper
.
BaiduDate
result
=
BaiduHelper
.
getBaiduData
(
new
Date
());
System
.
out
.
println
(
result
.
getStatus
());
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment