From ed692199be381350cd9709afc2b79d4ccf239fc4 Mon Sep 17 00:00:00 2001 From: Yutousama <583819556@qq.com> Date: Sun, 11 Apr 2021 23:17:30 +0800 Subject: [PATCH] update --- .../com/yutou/nas/utils/ApplicationInit.java | 38 +++++++++- .../yutou/nas/utils/BTDownloadManager.java | 37 ++++------ .../java/com/yutou/nas/utils/HttpTools.java | 69 ++++++++++++++++--- 3 files changed, 110 insertions(+), 34 deletions(-) diff --git a/src/main/java/com/yutou/nas/utils/ApplicationInit.java b/src/main/java/com/yutou/nas/utils/ApplicationInit.java index 61a9df2..b1c0b11 100644 --- a/src/main/java/com/yutou/nas/utils/ApplicationInit.java +++ b/src/main/java/com/yutou/nas/utils/ApplicationInit.java @@ -18,6 +18,9 @@ import java.util.TimerTask; public class ApplicationInit implements ApplicationRunner { @Resource MusicToolsServiceImpl musicTools; + + private static String oldTime = ""; + @Override public void run(ApplicationArguments args) throws Exception { AudioTools.init(); @@ -25,7 +28,11 @@ public class ApplicationInit implements ApplicationRunner { @Override public void run() { String time = new SimpleDateFormat("HH:mm").format(new Date()); - switch (time){ + if (time.equals(oldTime)) { + return; + } + oldTime = time; + switch (time) { case "00:00": musicTools.scanMusic(); break; @@ -38,7 +45,34 @@ public class ApplicationInit implements ApplicationRunner { break; } + switch (time) { + case "00:00": + case "01:00": + case "02:00": + case "03:00": + case "04:00": + case "05:00": + case "06:00": + case "07:00": + case "08:00": + case "09:00": + case "10:00": + case "11:00": + case "12:00": + case "13:00": + case "14:00": + case "15:00": + case "16:00": + case "17:00": + case "18:00": + case "19:00": + case "20:00": + case "21:00": + case "22:00": + case "23:00": + BTDownloadManager.getInstance().start(); + } } - },0,35 * 1000); + }, 0, 35 * 1000); } } diff --git a/src/main/java/com/yutou/nas/utils/BTDownloadManager.java b/src/main/java/com/yutou/nas/utils/BTDownloadManager.java index ea27bd9..98b893f 100644 --- a/src/main/java/com/yutou/nas/utils/BTDownloadManager.java +++ b/src/main/java/com/yutou/nas/utils/BTDownloadManager.java @@ -41,16 +41,16 @@ public class BTDownloadManager implements ApplicationContextAware { List list = bangumiService.getAllBangumi(); AnimationData data = new AnimationData(); for (BangumiItem item : list) { + item.setAuthor(data.nameToValue(item.getAuthor(), true) + ""); + item.setCategories(data.nameToValue(item.getCategories(), false) + ""); String url = getRSSUrl(item); String _json = HttpTools.get(url); if (!StringUtils.isEmpty(_json)) { JSONObject json = JSONObject.parseObject(_json); - item.setAuthor(data.nameToValue(item.getAuthor(),true)+""); - item.setCategories(data.nameToValue(item.getCategories(),false)+""); download(item, json); - }else{ - QQBotManager.getInstance().sendMessage(item.getTitle()+"\n下载失败"); - Log.i(item.getTitle()+"\n下载失败"); + } else { + QQBotManager.getInstance().sendMessage(item.getTitle() + "\n下载失败"); + Log.i(item.getTitle() + "\n下载失败"); } } } @@ -84,21 +84,8 @@ public class BTDownloadManager implements ApplicationContextAware { array.add(item.getString("title")); RedisTools.set(bangumiItem.getTitle(), array.toJSONString()); if (item.containsKey("thumbnail")) { - HttpTools.download(item.getString("thumbnail"), bangumiItem.getTitle() + ".jpg", new DownloadInterface() { - @Override - public void onDownload(File file) { - super.onDownload(file); - onSend(file, item); - } - - @Override - public void onError(Exception e) { - super.onError(e); - onSend(null, item); - } - - - }); + File file = HttpTools.syncDownload(item.getString("thumbnail"), bangumiItem.getTitle() + ".jpg"); + onSend(file, item); } else { onSend(null, item); } @@ -120,12 +107,14 @@ public class BTDownloadManager implements ApplicationContextAware { public boolean download(String title, String url) { try { - Runtime.getRuntime().exec(new String[]{"sh" - , "-c" - , String.format("transmission-remote -n yutou:34864394 -w /media/yutou/4t/public/download/anim/%s -a \"%s\" " + String exec = String.format("transmission-remote -n yutou:34864394 -w /media/yutou/4t/public/download/anim/%s -a \"%s\" " , title , url - )}); + ); + Log.i(exec); + Runtime.getRuntime().exec(new String[]{"sh" + , "-c" + , exec}); return true; } catch (IOException e) { e.printStackTrace(); diff --git a/src/main/java/com/yutou/nas/utils/HttpTools.java b/src/main/java/com/yutou/nas/utils/HttpTools.java index eed8ad0..7ec475f 100644 --- a/src/main/java/com/yutou/nas/utils/HttpTools.java +++ b/src/main/java/com/yutou/nas/utils/HttpTools.java @@ -3,6 +3,7 @@ package com.yutou.nas.utils; import com.alibaba.fastjson.JSONObject; import com.yutou.nas.interfaces.DownloadInterface; import com.yutou.nas.utils.Interfaces.NetworkInterface; +import org.springframework.util.StringUtils; import java.io.*; import java.net.HttpURLConnection; @@ -14,7 +15,7 @@ public class HttpTools { public static String get(String url) { try { HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection(); - connection.setRequestProperty("User-Agent", getKuKuUA()); + connection.setRequestProperty("User-Agent", getExtUa()); BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream())); StringBuilder str = new StringBuilder(); String tmp; @@ -43,6 +44,7 @@ public class HttpTools { connection.setRequestMethod("POST"); connection.setDoOutput(true); connection.setDoInput(true); + connection.addRequestProperty("User-Agent", getExtUa()); connection.setConnectTimeout(5 * 1000); connection.setReadTimeout(10 * 1000); //connection.addRequestProperty("Connection", "keep-alive"); @@ -127,7 +129,9 @@ public class HttpTools { com.yutou.nas.utils.Log.i(url); //String str=get(url); } - private static String donwloadPath="tmp"+File.separator; + + private static String donwloadPath = "tmp" + File.separator; + public synchronized static void download(final String url, final String saveName, final DownloadInterface downloadInterface) { new Thread(new Runnable() { @Override @@ -140,7 +144,7 @@ public class HttpTools { } Log.i("DOWNLOAD", "下载文件:" + url + " 保存文件:" + saveName); HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection(); - + connection.addRequestProperty("User-Agent", getExtUa()); // Log.i(TAG,"获取到网络请求:"+connection.getResponseCode()); @@ -150,14 +154,14 @@ public class HttpTools { Log.i("DOWNLOAD", "临时保存文件:" + jar.getAbsolutePath()); OutputStream outputStream = new FileOutputStream(jar); byte[] bytes = new byte[1024]; - double size=connection.getContentLength(); - double downSize=0; + double size = connection.getContentLength(); + double downSize = 0; int len; while ((len = inputStream.read(bytes)) > 0) { outputStream.write(bytes, 0, len); - downSize+=len; - if(downloadInterface!=null){ - downloadInterface.onDownloading(downSize,size); + downSize += len; + if (downloadInterface != null) { + downloadInterface.onDownloading(downSize, size); } } outputStream.close(); @@ -183,4 +187,53 @@ public class HttpTools { } }).start(); } + + public synchronized static File syncDownload(final String url, final String saveName) { + if(StringUtils.isEmpty(url)){ + return null; + } + File jar = null; + try { + File savePath = new File(donwloadPath); + if (!savePath.exists()) { + savePath.mkdirs(); + } + Log.i("DOWNLOAD", "下载文件:" + url + " 保存文件:" + saveName); + HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection(); + connection.addRequestProperty("User-Agent", getExtUa()); + // Log.i(TAG,"获取到网络请求:"+connection.getResponseCode()); + + + InputStream inputStream = connection.getInputStream(); + jar = new File(donwloadPath + saveName + "_tmp.tmp"); + jar.createNewFile(); + Log.i("DOWNLOAD", "临时保存文件:" + jar.getAbsolutePath()); + OutputStream outputStream = new FileOutputStream(jar); + byte[] bytes = new byte[1024]; + double size = connection.getContentLength(); + double downSize = 0; + int len; + while ((len = inputStream.read(bytes)) > 0) { + outputStream.write(bytes, 0, len); + downSize += len; + } + outputStream.close(); + inputStream.close(); + File oldJar = new File(donwloadPath + saveName); + if (oldJar.exists()) { + oldJar.delete(); + } + connection.disconnect(); + jar.renameTo(oldJar); + Log.i("DOWNLOAD", "实际保存:" + oldJar.getAbsolutePath() + " " + oldJar.getName()); + return oldJar; + + } catch (Exception e) { + e.printStackTrace(); + if (jar != null) { + jar.delete(); + } + return null; + } + } }