sync
This commit is contained in:
parent
ff3e0de135
commit
798e01b43f
|
@ -1,37 +1,45 @@
|
|||
package com.dengqn.igps2xingzhe.controller;
|
||||
|
||||
import cn.hutool.core.collection.CollUtil;
|
||||
import cn.hutool.core.io.FileUtil;
|
||||
import cn.hutool.core.io.IoUtil;
|
||||
import cn.hutool.core.io.file.PathUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import cn.hutool.crypto.KeyUtil;
|
||||
import cn.hutool.crypto.SecureUtil;
|
||||
import cn.hutool.crypto.asymmetric.AsymmetricAlgorithm;
|
||||
import cn.hutool.crypto.asymmetric.KeyType;
|
||||
import cn.hutool.crypto.asymmetric.RSA;
|
||||
import cn.hutool.json.JSONConfig;
|
||||
import cn.hutool.json.JSONUtil;
|
||||
import com.dengqn.igps2xingzhe.config.IGPSport;
|
||||
import com.dengqn.igps2xingzhe.config.XingZhe;
|
||||
import com.dengqn.igps2xingzhe.vo.IGPSActivity;
|
||||
import com.dengqn.igps2xingzhe.vo.IGPSActivityResp;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.catalina.security.SecurityUtil;
|
||||
import org.apache.hc.client5.http.classic.HttpClient;
|
||||
import org.apache.hc.client5.http.cookie.Cookie;
|
||||
import org.apache.hc.client5.http.cookie.CookieStore;
|
||||
import org.apache.hc.client5.http.entity.UrlEncodedFormEntity;
|
||||
import org.apache.hc.client5.http.entity.mime.FileBody;
|
||||
import org.apache.hc.client5.http.entity.mime.MultipartEntityBuilder;
|
||||
import org.apache.hc.client5.http.entity.mime.StringBody;
|
||||
import org.apache.hc.client5.http.impl.classic.CloseableHttpResponse;
|
||||
import org.apache.hc.core5.http.ContentType;
|
||||
import org.apache.hc.core5.http.HttpEntity;
|
||||
import org.apache.hc.core5.http.HttpResponse;
|
||||
import org.apache.hc.core5.http.*;
|
||||
import org.apache.hc.core5.http.io.entity.BasicHttpEntity;
|
||||
import org.apache.hc.core5.http.io.support.ClassicRequestBuilder;
|
||||
import org.apache.hc.core5.http.message.BasicHttpResponse;
|
||||
import org.apache.hc.core5.http.message.BasicNameValuePair;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.*;
|
||||
import java.nio.charset.Charset;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.security.KeyFactory;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
|
@ -57,6 +65,8 @@ public class Trigger {
|
|||
private HttpClient httpClient;
|
||||
@Autowired
|
||||
private CookieStore cookieStore;
|
||||
@Value("${dataDir:/data}")
|
||||
private String fileDir;
|
||||
|
||||
|
||||
@Autowired
|
||||
|
@ -67,13 +77,53 @@ public class Trigger {
|
|||
@GetMapping("/sync/igps2xingzhe")
|
||||
public ResponseEntity<String> onSyncIGPS2XingZhe() throws IOException, NoSuchAlgorithmException, InvalidKeySpecException {
|
||||
// 1. 登录行者
|
||||
// xingzheLogin();
|
||||
xingzheLogin();
|
||||
// 2. 登录igps
|
||||
igpsLogin();
|
||||
// 3. 获取igps活动
|
||||
syncIgpsActicities();
|
||||
|
||||
return ResponseEntity.ok("ok");
|
||||
}
|
||||
|
||||
private void syncIgpsActicities() throws IOException {
|
||||
CloseableHttpResponse activityResp = (CloseableHttpResponse) httpClient.execute(ClassicRequestBuilder.get("https://my.igpsport.com/Activity/MyActivityList").build());
|
||||
String activityRespStr = new String(IoUtil.readBytes(activityResp.getEntity().getContent()));
|
||||
IGPSActivityResp activityRespData = JSONUtil.toBean(activityRespStr, IGPSActivityResp.class, JSONConfig.create()
|
||||
.setIgnoreCase(true)
|
||||
.isIgnoreError());
|
||||
for (IGPSActivity activity : activityRespData.getItem()) {
|
||||
// 1. 判断是否本地有文件
|
||||
String filePath = fileDir + File.separator + activity.getFileName();
|
||||
|
||||
if (FileUtil.exist(filePath)) {
|
||||
log.info("文件已存在" + filePath);
|
||||
continue;
|
||||
}
|
||||
|
||||
// 下载文件
|
||||
String downloadUrl = "https://my.igpsport.com/fit/activity?type=0&rideid=" + activity.getRideId();
|
||||
log.info("尝试下载" + downloadUrl);
|
||||
|
||||
CloseableHttpResponse downloadResp = (CloseableHttpResponse) httpClient.execute(ClassicRequestBuilder.get(downloadUrl).build());
|
||||
InputStream downloadInputStream = downloadResp.getEntity().getContent();
|
||||
long copied = IoUtil.copy(downloadInputStream, new FileOutputStream(filePath));
|
||||
log.info("{} bytes copied", copied);
|
||||
|
||||
// 上传到xingzhe
|
||||
String uploadUrl = "https://www.imxingzhe.com/api/v4/upload_fits";
|
||||
HttpEntity uploadForm = MultipartEntityBuilder.create()
|
||||
.addPart("upload_file_name", new FileBody(new File(filePath)))
|
||||
.addPart("title", new StringBody(activity.getFileName(), ContentType.create("text/plain", Charset.defaultCharset())))
|
||||
.addPart("device", new StringBody("3", ContentType.create("text/plain", Charset.defaultCharset())))
|
||||
.addPart("sport", new StringBody("3", ContentType.create("text/plain", Charset.defaultCharset())))
|
||||
.build();
|
||||
|
||||
CloseableHttpResponse uploadResponse = (CloseableHttpResponse) httpClient.execute(ClassicRequestBuilder.post(uploadUrl).setEntity(uploadForm).build());
|
||||
log.info("upload result: {}", new String(IoUtil.readBytes(uploadResponse.getEntity().getContent())));
|
||||
}
|
||||
}
|
||||
|
||||
private void igpsLogin() throws IOException {
|
||||
// 3. igps 登录 formData
|
||||
UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(CollUtil.toList(
|
||||
|
|
|
@ -0,0 +1,35 @@
|
|||
package com.dengqn.igps2xingzhe.vo;
|
||||
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
import java.io.Serial;
|
||||
import java.io.Serializable;
|
||||
|
||||
@Data
|
||||
@Builder
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
public class IGPSActivity implements Serializable {
|
||||
@Serial
|
||||
private static final long serialVersionUID = 2974104022106192054L;
|
||||
|
||||
private String RideId;
|
||||
private String MemberId;
|
||||
private String Title;
|
||||
private String StartTime;
|
||||
private String StartTimeString;
|
||||
private String RideDistance;
|
||||
private String TotalAscent;
|
||||
private String MovingTime;
|
||||
private String OpenStatus;
|
||||
private String Status;
|
||||
private String SportType;
|
||||
|
||||
public String getFileName() {
|
||||
return StrUtil.format("igpsport-{}-{}-{}.fit", RideId, StartTimeString, RideDistance);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,25 @@
|
|||
package com.dengqn.igps2xingzhe.vo;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
import java.io.Serial;
|
||||
import java.io.Serializable;
|
||||
import java.util.List;
|
||||
|
||||
@Data
|
||||
@Builder
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
public class IGPSActivityResp implements Serializable {
|
||||
@Serial
|
||||
private static final long serialVersionUID = -2545669204414828896L;
|
||||
|
||||
private Integer total;
|
||||
|
||||
private List<IGPSActivity> item;
|
||||
|
||||
private Integer unit;
|
||||
}
|
Loading…
Reference in New Issue