Kaynağa Gözat

Working on war info

JoostSijm 4 yıl önce
ebeveyn
işleme
1a314cb198

+ 26 - 0
rival_regions_wrapper/api_wrapper/war.py

@@ -1,6 +1,7 @@
 """Profile class"""
 
 import re
+from datetime import timedelta
 
 from bs4 import BeautifulSoup
 
@@ -27,3 +28,28 @@ class War(object):
             'training_war': training_war
         }
         return page
+
+    @staticmethod
+    def info(war_id):
+        """Get war info"""
+        path = 'war/details/{}'.format(war_id)
+        response = MIDDLEWARE.get(path)
+        soup = BeautifulSoup(response, 'html.parser')
+        # heading = soup.find('h1')
+        pattern = re.compile('.war_det_cou')
+        pattern = re.compile('.war_det_cou')
+        script = soup.find('script', text=pattern)
+        search_result = re.search(r'\'\d+\'', str(script))
+        if search_result:
+            seconds = int(search_result.group(0).replace('\'', ''))
+            time_left = timedelta(seconds=seconds)
+            time_left = time_left - timedelta(time_left.days)
+        war_info = {
+            'attack': {
+                'damage': int(soup.select_one('.war_w_target_a').text.replace('.', ''))
+            },
+            'defence': {
+                'damage': int(soup.select_one('.war_w_target_d').text.replace('.', ''))
+            }
+        }
+        return war_info

+ 10 - 0
tests/test_rival_regions_wrapper.py

@@ -159,3 +159,13 @@ def test_war_page():
 
     assert isinstance(response, dict), "The response should be a dict"
     assert isinstance(response['training_war'], int), "The training_war should be an int"
+
+@pytest.mark.vcr()
+def test_war_info():
+    """Test war info"""
+    war_page = War.page()
+    print(war_page['training_war'])
+    war_id = war_page['training_war']
+    response = War.info(war_id)
+
+    print(response)