Explorar o código

force timeouts

Jason hai 1 ano
pai
achega
67478bc316
Modificáronse 3 ficheiros con 81 adicións e 43 borrados
  1. 33 14
      website_clickjobs/gen_seo2a.py
  2. 24 15
      website_clickjobs/type-V/c2.py
  3. 24 14
      website_clickjobs/type-V/c2S.py

+ 33 - 14
website_clickjobs/gen_seo2a.py

@@ -24,6 +24,7 @@ import time
 import requests
 import pymysql
 import urllib.parse
+import concurrent.futures
 pymysql.install_as_MySQLdb()
 from userAgentRandomizer import userAgents
 driver=None
@@ -77,7 +78,7 @@ def getDriver():
     return driver
 
 
-def run_once(jsobj):
+def run_once(jsobj,db):
 
     table=db['nda_log']
     print(jsobj)
@@ -214,7 +215,7 @@ def run_once(jsobj):
     driver.quit()
     # sys.exit()
 
-while True:
+def exe():
     try:
         db = dataset.connect('mysql://choozmo:pAssw0rd@db.ptt.cx:3306/seo?charset=utf8mb4')
         cursor=db.query('select json from seo.seo_jobs where cust="啟翔" and plan="形象SEO" and json like "%陳百欽%" and (json like "%chinatimes.com%") order by rand() limit 1')
@@ -234,12 +235,15 @@ while True:
             kw=prefix+" "+kw2+" "+kw1
             code='03'
 
-        run_once({'domain':domain,'kw':kw, 'cust':'啟翔'})
-        if driver is not None:
-            try:
-                driver.quit()
-            except:
-                pass
+        run_once({'domain':domain,'kw':kw, 'cust':'啟翔'},db)
+        try:
+            if driver is not None:
+                try:
+                    driver.quit()
+                except:
+                    pass
+        except:
+            pass
         cursor=None
         driver=None
         db.close()
@@ -248,12 +252,27 @@ while True:
     except:
         traceback.print_exc()
         print("Execution Error")
-        if driver is not None:
-            try:
-                driver.quit()
-            except:
-                pass
+        try:
+            if driver is not None:
+                try:
+                    driver.quit()
+                except:
+                    pass
+        except:
+            pass
         cursor=None
         driver=None
         db.close()
-        time.sleep(20)
+        time.sleep(20)
+
+runcount=1
+while True:
+    print("Run "+ str(runcount))
+    with concurrent.futures.ThreadPoolExecutor(max_workers=1) as executor:
+        future = executor.submit(exe)
+        try:
+            result = future.result(timeout=120)
+            print("Function completed")
+            runcount+=1
+        except concurrent.futures.TimeoutError:
+            print("Function execution timed out")

+ 24 - 15
website_clickjobs/type-V/c2.py

@@ -1,22 +1,31 @@
 #coding=utf-8
 from _execute import *
+import concurrent.futures
+
+clickvideo = 2 # set to 1 to alert if video is found (DOES NOT CLICK!), requires whitelist
+# ^ set to 2 to click the video when found, requires whitelist
+# ^ any other value will only search for videos based on termlist
+savefile = 0 # set to 1 to save screenshot and ranking to file
+sec = 1
+client = "信義房屋"
 
 def addvids(target):
     pwhitelist = ['https://csr.sinyi.com.tw/governance/index.php','https://www.facebook.com/SinyiRealtyInc.HR/','https://csr.sinyi.com.tw/interactive/video.php','https://www.youtube.com/playlist?list=PLftfxwJcQDUCL8v2oB_jHWHue4I6vHyBz','https://hr.sinyi.com.tw/events/tvc/','https://hr.sinyi.com.tw/video','https://www.youtube.com/watch?v=rGQqegE8rbc','https://www.facebook.com/sinyifans/','https://www.youtube.com/channel/UCU76s7FGtDvDta2mf2Zdn4Q','https://csr.sinyi.com.tw/','https://events.sinyi.com.tw/20190828_webintro/','https://www.youtube.com/user/sinyicity','https://events.sinyi.com.tw/tvc2018-forhome/','https://hr.sinyi.com.tw/events/1/','https://hr.sinyi.com.tw/events/recommend/','https://events.sinyi.com.tw/sinyi40/','https://csr.sinyi.com.tw/interactive/video-employee.php','https://csr.sinyi.com.tw/interactive/video-environment.php','https://csr.sinyi.com.tw/interactive/video-society.php','https://csr.sinyi.com.tw/society/culture.php','https://csr.sinyi.com.tw/employee/index.php','https://www.youtube.com/watch?v=FqiKpqgB-bU','https://www.youtube.com/watch?v=UkxtlJAaqP8','https://www.youtube.com/watch?v=A5GuB3Enw9U','https://www.youtube.com/watch?v=fx4fTBh9PFo','https://events.sinyi.com.tw/tvc/''https://csr.sinyi.com.tw/interactive/video-enterprise.php','https://www.youtube.com/watch?v=GDYKd3cvEXk','https://www.youtube.com/watch?v=tEY5HCqs9oY','https://www.youtube.com/watch?v=7UGiKEc2JEU','https://www.youtube.com/watch?v=7Ay7ZQdV1Gw','https://www.youtube.com/watch?v=QzyTD949cVk','https://www.youtube.com/watch?v=4-sJLbMLP-0']
-
     for v in pwhitelist:
         if v not in target:
             target.append(v)
     
     return target
 
-clickvideo = 2 # set to 1 to alert if video is found (DOES NOT CLICK!), requires whitelist
-# ^ set to 2 to click the video when found, requires whitelist
-# ^ any other value will only search for videos based on termlist
-savefile = 0 # set to 1 to save screenshot and ranking to file
-sec = 1
-client = "信義房屋"
-target=[]
+def exe():
+    start_time = time.time()
+    target=[]
+    kw = pickterm(client)
+    target = addvids(target)
+    print(len(target))
+    process_one(kw, target, clickvideo, savefile, sec)
+    duration = time.time()-start_time
+    print("Runs: " + str(runcount) + " | Duration: " + str(duration))
 
 if clickvideo == 1 or clickvideo == 2:
     runcount=0
@@ -24,13 +33,13 @@ if clickvideo == 1 or clickvideo == 2:
     while True:
         try:
             runcount+=1
-            start_time = time.time()
-            kw = pickterm(client)
-            target = addvids(target)
-            print(len(target))
-            process_one(kw, target, clickvideo, savefile, sec)
-            duration = time.time()-start_time
-            print("Runs: " + str(runcount) + " | Duration: " + str(duration))
+            with concurrent.futures.ThreadPoolExecutor(max_workers=1) as executor:
+                future = executor.submit(exe)
+                try:
+                    result = future.result(timeout=120)
+                    print("Function completed")
+                except concurrent.futures.TimeoutError:
+                    print("Function execution timed out")
         except:
             print('Runtime Error')
             traceback.print_exc()

+ 24 - 14
website_clickjobs/type-V/c2S.py

@@ -1,5 +1,13 @@
 #coding=utf-8
 from _execute import *
+import concurrent.futures
+
+clickvideo = 2 # set to 1 to alert if video is found (DOES NOT CLICK!), requires whitelist
+# ^ set to 2 to click the video when found, requires whitelist
+# ^ any other value will only search for videos based on termlist
+savefile = 0 # set to 1 to save screenshot and ranking to file
+sec = 1
+client = "信義房屋"
 
 def addvids(target):
     pwhitelist = ['https://csr.sinyi.com.tw/governance/index.php','https://www.facebook.com/SinyiRealtyInc.HR/','https://csr.sinyi.com.tw/interactive/video.php','https://www.youtube.com/playlist?list=PLftfxwJcQDUCL8v2oB_jHWHue4I6vHyBz','https://hr.sinyi.com.tw/events/tvc/','https://hr.sinyi.com.tw/video','https://www.youtube.com/watch?v=rGQqegE8rbc','https://www.facebook.com/sinyifans/','https://www.youtube.com/channel/UCU76s7FGtDvDta2mf2Zdn4Q','https://csr.sinyi.com.tw/','https://events.sinyi.com.tw/20190828_webintro/','https://www.youtube.com/user/sinyicity','https://events.sinyi.com.tw/tvc2018-forhome/','https://hr.sinyi.com.tw/events/1/','https://hr.sinyi.com.tw/events/recommend/','https://events.sinyi.com.tw/sinyi40/','https://csr.sinyi.com.tw/interactive/video-employee.php','https://csr.sinyi.com.tw/interactive/video-environment.php','https://csr.sinyi.com.tw/interactive/video-society.php','https://csr.sinyi.com.tw/society/culture.php','https://csr.sinyi.com.tw/employee/index.php','https://www.youtube.com/watch?v=FqiKpqgB-bU','https://www.youtube.com/watch?v=UkxtlJAaqP8','https://www.youtube.com/watch?v=A5GuB3Enw9U','https://www.youtube.com/watch?v=fx4fTBh9PFo','https://events.sinyi.com.tw/tvc/''https://csr.sinyi.com.tw/interactive/video-enterprise.php','https://www.youtube.com/watch?v=GDYKd3cvEXk','https://www.youtube.com/watch?v=tEY5HCqs9oY','https://www.youtube.com/watch?v=7UGiKEc2JEU','https://www.youtube.com/watch?v=7Ay7ZQdV1Gw','https://www.youtube.com/watch?v=QzyTD949cVk','https://www.youtube.com/watch?v=4-sJLbMLP-0']
@@ -9,26 +17,28 @@ def addvids(target):
     
     return target
 
-clickvideo = 2 # set to 1 to alert if video is found (DOES NOT CLICK!), requires whitelist
-# ^ set to 2 to click the video when found, requires whitelist
-# ^ any other value will only search for videos based on termlist
-savefile = 0 # set to 1 to save screenshot and ranking to file
-sec = 1
-client = "信義房屋"
-target=[]
+def exe():
+    start_time = time.time()
+    target=[]
+    kw = pickterm(client)
+    target = addvids(target)
+    print(len(target))
+    process_one(kw, target, clickvideo, savefile, sec)
+    duration = time.time()-start_time
+    print("Runs: " + str(runcount) + " | Duration: " + str(duration))
 
 if clickvideo == 1 or clickvideo == 2:
     runcount=0
     kws=None
     try:
         runcount+=1
-        start_time = time.time()
-        kw = pickterm(client)
-        target = addvids(target)
-        print(len(target))
-        process_one(kw, target, clickvideo, savefile, sec)
-        duration = time.time()-start_time
-        print("Runs: " + str(runcount) + " | Duration: " + str(duration))
+        with concurrent.futures.ThreadPoolExecutor(max_workers=1) as executor:
+            future = executor.submit(exe)
+            try:
+                result = future.result(timeout=120)
+                print("Function completed")
+            except concurrent.futures.TimeoutError:
+                print("Function execution timed out")
     except:
         print('Runtime Error')
         traceback.print_exc()