Kaynağa Gözat

Improve war module

JoostSijm 4 yıl önce
ebeveyn
işleme
669cad843e

+ 34 - 15
rival_regions_wrapper/api_wrapper/war.py

@@ -1,7 +1,7 @@
 """Profile class"""
 
 import re
-from datetime import timedelta
+from datetime import datetime, timedelta
 
 from bs4 import BeautifulSoup
 
@@ -35,21 +35,40 @@ class War(object):
         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)
+        war_info = {
+            'damage': int(soup.select_one('.war_w_target_o').text.replace('.', '')),
+            'attack_damage': int(soup.select_one('.war_w_target_a').text.replace('.', '')),
+            'defence_damage': int(soup.select_one('.war_w_target_d').text.replace('.', '')),
+            'attack_hourly_available': bool(soup.select_one('.hide_once_war')),
+        }
+        heading = soup.find('h1')
+        energ_drinks = re.search(r'\d+$', heading.select_one('.small').text)
+        if energ_drinks:
+            war_info['energ_drinks'] = int(energ_drinks.group(0))
+
+        header_texts = heading.select('.float_left')
+        try:
+            war_info['name'] = header_texts[1].text
+        except IndexError:
+            pass
+
+        max_hero = heading.select_one('.max_hero')
+        war_info['max_hero_name'] = max_hero.text
+        war_info['max_hero_id'] = max_hero['action'].replace('slide/profile/', '')
+
+        max_hero_damage_str = ''.join(heading.find_all(text=True, recursive=False)).strip()
+        max_hero_damage = re.search(r'(\d|\.)+', max_hero_damage_str)
+        if max_hero_damage:
+            war_info['max_hero_damage'] = int(max_hero_damage.group(0).replace('.', ''))
+
+        script = soup.find('script', text=re.compile('.war_det_cou'))
         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('.', ''))
-            }
-        }
+            war_info['time_left'] = timedelta(seconds=seconds)
+            war_info['finish_date'] = datetime.now() + war_info['time_left']
+
+        war_info['war_units'] = {}
+        for war_unit in soup.select('.war_w_unit_div'):
+            war_info['war_units'][war_unit['url']] = war_unit.text
         return war_info

+ 14 - 2
tests/test_rival_regions_wrapper.py

@@ -1,6 +1,6 @@
 """Wrapper test"""
 
-from datetime import datetime
+from datetime import datetime, timedelta
 
 import pytest
 
@@ -164,8 +164,20 @@ def test_war_page():
 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)
+    assert isinstance(response, dict), "The response should be a dict"
+    assert isinstance(response['damage'], int), "Damage should be an int"
+    assert isinstance(response['attack_damage'], int), "Attack damage should be an int"
+    assert isinstance(response['defence_damage'], int), "Defence damage should be an int"
+    assert isinstance(response['attack_hourly'], bool), "Attack hourly should be a bool"
+    assert isinstance(response['energy_drink'], int), "Energy drinks should be an int"
+    assert isinstance(response['name'], str), "Name should be a str"
+    assert isinstance(response['max_hero_name'], str), "max hero name should be a str"
+    assert isinstance(response['max_hero_damage'], int), "max hero damage should be an int"
+    if 'time_left' in response:
+        assert isinstance(response['time_left'], timedelta), "time left should be a time delta"
+    assert isinstance(response['finish_date'], datetime), "Finish date should be a date"
+    assert isinstance(response['war_units'], dict), "war units should be a dict"