Browse Source

Add check for state for refill

JoostSijm 5 years ago
parent
commit
e68a4df8e1
2 changed files with 25 additions and 7 deletions
  1. 8 6
      hvs/__main__.py
  2. 17 1
      hvs/app.py

+ 8 - 6
hvs/__main__.py

@@ -8,7 +8,7 @@ from hvs import scheduler
 from hvs.app import download_resources, need_refill, refill, save_resources, print_resources
 
 
-def job_check_resources(state_id, resource_id, capital_id):
+def job_check_resources(state_id, capital_id, resource_id):
     """Check resources and refill if necessary"""
     regions = download_resources(state_id, resource_id)
     save_resources(state_id, regions)
@@ -27,23 +27,25 @@ def job_check_resources(state_id, resource_id, capital_id):
             scheduler.add_job(
                 job_refill_resource,
                 'date',
-                args=[4002, resource_id],
+                args=[state_id, capital_id, resource_id],
                 id=job_id,
                 run_date=scheduled_date
             )
 
-def job_refill_resource(capital_id, resource_id):
+def job_refill_resource(state_id, capital_id, resource_id):
     """Execute refill job"""
-    refill(capital_id, resource_id)
+    refill(state_id, capital_id, resource_id)
 
 if __name__ == '__main__':
     # jobs
+    # job_refill_resource(2788, 4002, 0)
+    # job_check_resources(2788, 4002, 0)
     VN_CHECK_GOLD_JOB = scheduler.get_job('vn_check_gold')
     if not VN_CHECK_GOLD_JOB:
         scheduler.add_job(
             job_check_resources,
             'cron',
-            args=[2788, 0, 4002],
+            args=[2788, 4002, 0],
             id='vn_check_gold',
             minute='0,15,30,45'
         )
@@ -52,7 +54,7 @@ if __name__ == '__main__':
         scheduler.add_job(
             job_check_resources,
             'cron',
-            args=[2788, 11, 4002],
+            args=[2788, 4002, 11],
             id='vn_check_uranium',
             minute='0'
         )

+ 17 - 1
hvs/app.py

@@ -79,14 +79,30 @@ def need_refill(regions):
     return False
 
 
-def refill(capital_id, resource_id):
+def refill(state_id, capital_id, resource_id):
     """Main function"""
+    # Check location
+    response = requests.get(
+        '{}main/content'.format(BASE_URL),
+        headers=HEADERS
+    )
+    soup = BeautifulSoup(response.text, 'html.parser')
+    state_div = soup.find_all('div', {'class': 'index_case_50'})[1]
+    action = state_div.findChild()['action']
+    current_state_id = int(re.sub('.*/', '', action))
+    print('Current state: {}'.format(current_state_id))
+
     data = {
         'tmp_gov': resource_id
     }
+    params = {}
+    if current_state_id != state_id:
+        params['alt'] = True
+
     requests.post(
         '{}parliament/donew/42/{}/0'.format(BASE_URL, resource_id),
         headers=HEADERS,
+        params=params,
         data=data
     )