Selaa lähdekoodia

自动保存图片

詹子聪 5 vuotta sitten
vanhempi
commit
e883e8123c

+ 2 - 2
src/main/java/com/miekir/shibei/controller/task/OnStartServer.java

@@ -25,7 +25,7 @@ public class OnStartServer {
     public void init() {
         // 执行一次
         // 一启动完成就开始去爬虫
-        if (OnStartServer.mStarted) {
+        /*if (OnStartServer.mStarted) {
             return;
         }
         OnStartServer.mStarted = true;
@@ -38,6 +38,6 @@ public class OnStartServer {
                     shouldContinue = Sex8Tool.startGettingData();
                 }
             }
-        });
+        });*/
     }
 }

+ 42 - 0
src/main/java/com/miekir/shibei/tool/start/AllStrategiesExampleBean.java

@@ -0,0 +1,42 @@
+package com.miekir.shibei.tool.start;
+
+import com.miekir.shibei.tool.web.Sex8Tool;
+import org.springframework.beans.factory.InitializingBean;
+import org.springframework.context.annotation.Scope;
+import org.springframework.stereotype.Component;
+
+import javax.annotation.PostConstruct;
+import java.util.concurrent.Executors;
+
+@Component
+@Scope(value = "prototype")
+public class AllStrategiesExampleBean implements InitializingBean {
+
+    public AllStrategiesExampleBean() {
+        //System.out.println();
+    }
+ 
+    @Override
+    public void afterPropertiesSet() throws Exception {
+        // 在IDE debug是初始化两次,但真正打包发布之后,只会执行一次
+        Executors.newSingleThreadExecutor().submit(new Runnable() {
+            @Override
+            public void run() {
+                // 递归会导致StackOverflowError,所以用循环代替
+                boolean shouldContinue = true;
+                while (shouldContinue) {
+                    shouldContinue = Sex8Tool.startGettingData();
+                }
+            }
+        });
+    }
+ 
+    @PostConstruct
+    public void postConstruct() {
+        //System.out.println();
+    }
+ 
+    public void init() {
+        //System.out.println();
+    }
+}

+ 24 - 0
src/main/java/com/miekir/shibei/tool/start/ServletInitializer.java

@@ -0,0 +1,24 @@
+package com.miekir.shibei.tool.start;
+
+import com.miekir.shibei.tool.web.Sex8Tool;
+import org.springframework.boot.builder.SpringApplicationBuilder;
+import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
+
+import java.util.concurrent.Executors;
+
+public class ServletInitializer extends SpringBootServletInitializer {
+    @Override
+    protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
+        /*Executors.newSingleThreadExecutor().submit(new Runnable() {
+            @Override
+            public void run() {
+                // 递归会导致StackOverflowError,所以用循环代替
+                boolean shouldContinue = true;
+                while (shouldContinue) {
+                    shouldContinue = Sex8Tool.startGettingData();
+                }
+            }
+        });*/
+        return super.configure(builder);
+    }
+}

+ 5 - 5
src/main/java/com/miekir/shibei/tool/web/Sex8Tool.java

@@ -22,7 +22,7 @@ public class Sex8Tool {
 
     private static boolean mIsLoading;
     // 从第二页开始爬
-    private static int mCurrentPage = 3;
+    private static int mCurrentPage = 349;
 
     /**
      * 为了防止被百度拦截(人机识别),需要按F12查看浏览器的Header,设置相关属性
@@ -147,7 +147,7 @@ public class Sex8Tool {
             e.printStackTrace();
         }
 
-        // 爬完一页,页数要变化,歇一歇
+        // 爬完一页,页数要变化
         mCurrentPage--;
         return mCurrentPage != 0;
     }
@@ -181,10 +181,10 @@ public class Sex8Tool {
     }
 
     private static void justWait() {
-        int waitMinute = new Random().nextInt(4) + 1;
+        int waitMinute = new Random().nextInt(40) + 20;
         try {
-            //Thread.sleep(waitMinute * 60 * 1000);
-            Thread.sleep(1000);
+            Thread.sleep(waitMinute * 1000);
+            //Thread.sleep(1000);
         } catch (Exception e) {
             e.printStackTrace();
         }

+ 21 - 3
src/main/java/com/miekir/shibei/tool/web/WebImageTool.java

@@ -12,6 +12,11 @@ import java.net.URL;
 location /manual/ {
 alias /file/images/eden/manual/;
 }*/
+
+/*images目录下其他目录也公开读写权限
+sudo chmod 777 -R /file/images/
+恢复默认权限
+sudo chmod 755 /file/images/*/
 public class WebImageTool {
     private static final String PATH_AUTO_FOLDER = "/file/images/eden/auto/";
     private static final String CMD_GET_AUTO_FILE_COUNT = "ls -l " + PATH_AUTO_FOLDER + " | grep \"^-\"|wc -l";
@@ -45,7 +50,8 @@ public class WebImageTool {
         }
 
         // 获取下一文件名
-        int futureFileCount = getFilesCount(CMD_GET_AUTO_FILE_COUNT) + 1;
+        //int futureFileCount = getFilesCount(CMD_GET_AUTO_FILE_COUNT) + 1;
+        int futureFileCount = getFilesCount(folder) + 1;
         File file = new File(folder, String.valueOf(futureFileCount) + format);
         if (file.exists()) {
             file.delete();
@@ -102,6 +108,18 @@ public class WebImageTool {
         return null;
     }
 
+    public static int getFilesCount(File folderFile) {
+        if (folderFile == null) {
+            return 0;
+        }
+
+        String[] files = folderFile.list();
+        if (files == null) {
+            return 0;
+        }
+
+        return files.length;
+    }
 
     /**
      * 获取文件目录的文件数
@@ -112,11 +130,11 @@ public class WebImageTool {
         try {
             Process p = Runtime.getRuntime().exec(commandStr);
             br = new BufferedReader(new InputStreamReader(p.getInputStream()));
-            String line = null;
+            /*String line = null;
             StringBuilder sb = new StringBuilder();
             while ((line = br.readLine()) != null) {
                 sb.append(line + "\n");
-            }
+            }*/
             return Integer.parseInt(br.readLine());
         } catch (Exception e) {
             e.printStackTrace();

+ 4 - 0
src/main/webapp/WEB-INF/mvc-dispatcher-servlet.xml

@@ -98,6 +98,10 @@
         <property name="maxUploadSize" value="2097152"/>
     </bean>
 
+    <bean id="initMethodExampleBean"
+          class="com.miekir.shibei.tool.start.AllStrategiesExampleBean"
+          init-method="init">
+    </bean>
 
     <tx:annotation-driven transaction-manager="transactionManager"/>
 

+ 1 - 0
src/main/webapp/WEB-INF/web.xml

@@ -38,4 +38,5 @@
         <filter-name>encodingFilter</filter-name>
         <url-pattern>/*</url-pattern>
     </filter-mapping>
+
 </web-app>