增加新闻头条和万年历接口调用

master
WeiRan 2024-08-29 21:31:53 +08:00
parent 15c19e7ad7
commit 41002f971f
5 changed files with 207 additions and 4 deletions

View File

@ -0,0 +1,54 @@
package com.muyu.cloud.background.domin.api;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
/**
* @Authorweiran
* @Packagecom.muyu.cloud.background.domin.api
* @Projectcloud-background
* @nameNewsdomain
* @Date2024/8/29 20:58
*/
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Newsdomain {
private String type;
private String page;
private String pageSize;
private String isFilter;
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getPage() {
return page;
}
public void setPage(String page) {
this.page = page;
}
public String getPageSize() {
return pageSize;
}
public void setPageSize(String pageSize) {
this.pageSize = pageSize;
}
public String getIsFilter() {
return isFilter;
}
public void setIsFilter(String isFilter) {
this.isFilter = isFilter;
}
}

View File

@ -0,0 +1,59 @@
package com.muyu.cloud.background.api;
import com.muyu.common.core.domain.Result;
import org.springframework.context.annotation.Configuration;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;
import java.util.stream.Collectors;
/**
* @Authorweiran
* @Packagecom.muyu.cloud.background.api
* @Projectcloud-background
* @nameJavaGet
* @Date2024/8/29 20:24
*/
/**
*
*/
@Configuration
public class DateCalendar {
public static Result fetchCalendarData(String date) throws Exception {
String apiKey = "31300263d8b712fa888b492dc818b1ba";
String apiUrl = "http://v.juhe.cn/calendar/day";
HashMap<String, String> map = new HashMap<>();
map.put("key", apiKey);
map.put("date", date);
URL url = new URL(String.format(apiUrl + "?" + params(map)));
BufferedReader in = new BufferedReader(new InputStreamReader((url.openConnection()).getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
System.out.println(response);
return Result.success(response);
}
public static String params(Map<String, String> map) {
return map.entrySet().stream()
.map(entry -> {
try {
return entry.getKey() + "=" + URLEncoder.encode(entry.getValue(), StandardCharsets.UTF_8.toString());
} catch (Exception e) {
e.printStackTrace();
return entry.getKey() + "=" + entry.getValue();
}
})
.collect(Collectors.joining("&"));
}
}

View File

@ -20,6 +20,10 @@ import java.util.Map;
* @nameMobileLocation
* @Date2024/8/29 14:58
*/
/**
*
*/
@Configuration
public class MobileLocation {
// 手机归属地查询接口地址

View File

@ -0,0 +1,55 @@
package com.muyu.cloud.background.api;
import org.springframework.context.annotation.Configuration;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;
import java.util.stream.Collectors;
/**
* @Authorweiran
* @Packagecom.muyu.cloud.background.api
* @Projectcloud-background
* @nameNews
* @Date2024/8/29 20:49
*/
@Configuration
public class News {
public static String fetchNews(String type, String page, String pageSize, String isFilter) throws Exception {
String apiKey = "c8217e24acd5fc85578d30800749f3a6";
String apiUrl = "http://v.juhe.cn/toutiao/index";
HashMap<String, String> map = new HashMap<>();
map.put("key", apiKey);
map.put("type", type);
map.put("page", page);
map.put("page_size",pageSize);
map.put("is_filter",isFilter);
URL url = new URL(String.format(apiUrl + "?" + params(map)));
BufferedReader in = new BufferedReader(new InputStreamReader((url.openConnection()).getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
return response.toString();
}
public static String params(Map<String, String> map) {
return map.entrySet().stream()
.map(entry -> {
try {
return entry.getKey() + "=" + URLEncoder.encode(entry.getValue(), StandardCharsets.UTF_8.toString());
} catch (Exception e) {
e.printStackTrace();
return entry.getKey() + "=" + entry.getValue();
}
})
.collect(Collectors.joining("&"));
}
}

View File

@ -1,16 +1,16 @@
package com.muyu.cloud.background.controller;
import com.muyu.cloud.background.api.DateCalendar;
import com.muyu.cloud.background.api.MobileLocation;
import com.muyu.cloud.background.api.News;
import com.muyu.cloud.background.domin.api.MobileLocationdomain;
import com.muyu.cloud.background.domin.api.Newsdomain;
import com.muyu.common.core.domain.Result;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.log4j.Log4j2;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;
/**
* @Authorweiran
@ -36,4 +36,35 @@ public class ApiController {
MobileLocationdomain mobileLocationdomain = MobileLocation.queryMobileLocation(phone);
return Result.success(mobileLocationdomain);
}
/**
*
* @param date
* @return
* @throws Exception
*/
@GetMapping("/bydate/{date}")
@Operation(summary = "万年历",description = "查询万年历")
public Result JavaGet(@Validated @PathVariable String date) throws Exception {
Result result = DateCalendar.fetchCalendarData(date);
return result;
}
@GetMapping("/news")
@Operation(summary = "新闻头条",description = "查询新闻头条")
public Result JavaGet(@Validated @RequestBody Newsdomain newsdomain) throws Exception {
String type = newsdomain.getType();
String page = newsdomain.getPage();
String pageSize = newsdomain.getPageSize();
String isFilter = newsdomain.getIsFilter();
String response = News.fetchNews(type, page, pageSize, isFilter);
return Result.success(response);
}
}