Ver código fonte

Add resource state

JoostSijm 4 anos atrás
pai
commit
64accafb12

+ 1 - 1
rival_regions_wrapper/__init__.py

@@ -8,4 +8,4 @@ of some Rival Regions functionalities.
 
 
 from .authentication_handler import AuthenticationHandler
 from .authentication_handler import AuthenticationHandler
 from .middleware import LocalAuthentication, RemoteAuthentication
 from .middleware import LocalAuthentication, RemoteAuthentication
-from .api_wrapper import Profile, Storage, Market
+from .api_wrapper import Profile, Storage, Market, ResourceState

+ 1 - 0
rival_regions_wrapper/api_wrapper/__init__.py

@@ -32,3 +32,4 @@ MIDDLEWARE = RemoteAuthentication(API_URL, AUTHORIZATION)
 from .profile import Profile
 from .profile import Profile
 from .storage import Storage
 from .storage import Storage
 from .market import Market
 from .market import Market
+from .resource_state import ResourceState

+ 39 - 0
rival_regions_wrapper/api_wrapper/resource_state.py

@@ -0,0 +1,39 @@
+"""Profile class"""
+
+import re
+
+from bs4 import BeautifulSoup
+
+from . import MIDDLEWARE
+
+
+class ResourceState(object):
+    """Wrapper class for profile"""
+
+    @staticmethod
+    def info(state_id, resource):
+        """Get profile"""
+        keys = {
+            3: 'oil',
+            4: 'ore',
+            11: 'uranium',
+            15: 'diamonds'
+        }
+        if isinstance(resource, int) and resource in keys:
+            resource = keys[resource]
+        path = 'listed/stateresources/{}/{}'.format(state_id, resource)
+        response = MIDDLEWARE.get(path)
+        soup = BeautifulSoup(response, 'html.parser')
+        regions_tree = soup.find_all(class_='list_link')
+        regions = []
+        for region_tree in regions_tree:
+            columns = region_tree.find_all('td')
+            regions.append({
+                'region_id': int(region_tree['user']),
+                'region_name': re.sub('Factories: .*$', '', columns[1].text),
+                'explored': float(columns[2].string),
+                'maximum': int(float(columns[3].string)),
+                'deep_exploration': int(columns[4].string),
+                'limit_left': int(columns[5].string),
+            })
+        return regions

+ 24 - 1
tests/test_rival_regions_wrapper.py

@@ -2,7 +2,7 @@
 
 
 import pytest
 import pytest
 
 
-from rival_regions_wrapper.api_wrapper import Profile, Storage, Market
+from rival_regions_wrapper.api_wrapper import Profile, Storage, Market, ResourceState
 
 
 
 
 @pytest.fixture
 @pytest.fixture
@@ -67,3 +67,26 @@ def test_market_info(market_keys):
         assert isinstance(response[0]['player_name'], str), "The player_name should be a int"
         assert isinstance(response[0]['player_name'], str), "The player_name should be a int"
         assert isinstance(response[0]['price'], int), "The price should be a int"
         assert isinstance(response[0]['price'], int), "The price should be a int"
         assert isinstance(response[0]['amount'], int), "The price should be a int"
         assert isinstance(response[0]['amount'], int), "The price should be a int"
+
+@pytest.fixture
+def resource_keys():
+    """Standard keys for resource"""
+    return ['region_id', 'region_name', 'explored', 'maximum', 'deep_exploration', 'limit_left']
+
+@pytest.mark.vcr()
+def test_resource_state_info(resource_keys):
+    """Test an API call to get market info"""
+    state = 3382
+    resource = 'oil'
+    response = ResourceState.info(state, resource)
+
+    assert isinstance(response, list), "The response should be a list"
+    if response:
+        assert isinstance(response[0], dict), "The first element should be a dict"
+        assert set(resource_keys).issubset(response[0].keys()), "All keys should be in the response"
+        assert isinstance(response[0]['region_id'], int), "The region_id should be a int"
+        assert isinstance(response[0]['region_name'], str), "The region_name should be a str"
+        assert isinstance(response[0]['explored'], float), "The explored should be a float"
+        assert isinstance(response[0]['maximum'], int), "The maximum should be a int"
+        assert isinstance(response[0]['deep_exploration'], int), "deep_exploration should be int"
+        assert isinstance(response[0]['limit_left'], int), "The limit_left should be a int"