Explorar el Código

Add abstact wrapper and create data structures

JoostSijm hace 4 años
padre
commit
8fec38a74f

+ 11 - 0
src/rival_regions_wrapper/api_wrapper/abstract_wrapper.py

@@ -0,0 +1,11 @@
+"""
+Abstract wrapper module
+"""
+
+from abc import ABC
+
+
+class AbstractWrapper(ABC):
+    """abstract base class for wrappers"""
+    def __init__(self, api_wrapper):
+        self.api_wrapper = api_wrapper

+ 3 - 4
src/rival_regions_wrapper/api_wrapper/article.py

@@ -7,12 +7,11 @@ from bs4 import BeautifulSoup
 
 from rival_regions_wrapper import functions
 
+from .abstract_wrapper import AbstractWrapper
 
-class Article():
-    """Wrapper class for article"""
-    def __init__(self, api_wrapper):
-        self.api_wrapper = api_wrapper
 
+class Article(AbstractWrapper):
+    """Wrapper class for article"""
     def info(self, article_id):
         """Get artcile"""
         path = 'news/show/{}'.format(article_id)

+ 3 - 4
src/rival_regions_wrapper/api_wrapper/conference.py

@@ -1,11 +1,10 @@
 """Conference class"""
 
+from .abstract_wrapper import AbstractWrapper
 
-class Conference():
-    """Wrapper class for confernce"""
-    def __init__(self, api_wrapper):
-        self.api_wrapper = api_wrapper
 
+class Conference(AbstractWrapper):
+    """Wrapper class for confernce"""
     def send_message(self, conference_id, message):
         """send conference message"""
         self.api_wrapper.send_conference_message(conference_id, message)

+ 7 - 29
src/rival_regions_wrapper/api_wrapper/craft.py

@@ -4,39 +4,17 @@ import re
 
 from bs4 import BeautifulSoup
 
+from rival_regions_wrapper import data_structures
 
-KEYS = {
-    'oil': 3,
-    'ore': 4,
-    'uranium': 11,
-    'diamonds': 15,
-    'liquid_oxygen': 21,
-    'helium-3': 24,
-    'rivalium': 26,
-    'antirad': 13,
-    'energy_drink': 17,
-    'spacerockets': 20,
-    'lss': 25,
-    'tanks': 2,
-    'aircrafts': 1,
-    'missiles': 14,
-    'bombers': 16,
-    'battleships': 18,
-    'laser_drones': 27,
-    'moon_tanks': 22,
-    'space_stations': 23
-}
+from .abstract_wrapper import AbstractWrapper
 
 
-class Craft():
+class Craft(AbstractWrapper):
     """Wrapper class for crafting"""
-    def __init__(self, api_wrapper):
-        self.api_wrapper = api_wrapper
-
     def info(self, item):
         """Get craft"""
-        if isinstance(item, str) and item in KEYS:
-            item = KEYS[item]
+        if isinstance(item, str) and item in data_structures.ITEM_KEYS:
+            item = data_structures.ITEM_KEYS[item]
         path = 'storage/produce/{}'.format(item)
         response = self.api_wrapper.get(path)
         soup = BeautifulSoup(response, 'html.parser')
@@ -68,7 +46,7 @@ class Craft():
 
     def produce(self, item, amount):
         """Craft item"""
-        if isinstance(item, str) and item in KEYS:
-            item = KEYS[item]
+        if isinstance(item, str) and item in data_structures.ITEM_KEYS:
+            item = data_structures.ITEM_KEYS[item]
         self.api_wrapper.post('storage/newproduce/{}/{}'.format(item, amount))
         return True

+ 7 - 27
src/rival_regions_wrapper/api_wrapper/market.py

@@ -4,37 +4,17 @@ import re
 
 from bs4 import BeautifulSoup
 
+from rival_regions_wrapper import data_structures
+
+from .abstract_wrapper import AbstractWrapper
 
-class Market():
-    """Wrapper class for profile"""
-    def __init__(self, api_wrapper):
-        self.api_wrapper = api_wrapper
 
+class Market(AbstractWrapper):
+    """Wrapper class for profile"""
     def info(self, resource):
         """Get profile"""
-        keys = {
-            'oil': 3,
-            'ore': 4,
-            'uranium': 11,
-            'diamonds': 15,
-            'liquid_oxygen': 21,
-            'helium-3': 24,
-            'rivalium': 26,
-            'antirad': 13,
-            'energy_drink': 17,
-            'spacerockets': 20,
-            'lss': 25,
-            'tanks': 2,
-            'aircrafts': 1,
-            'missiles': 14,
-            'bombers': 16,
-            'battleships': 18,
-            'laser_drones': 27,
-            'moon_tanks': 22,
-            'space_stations': 23
-        }
-        if isinstance(resource, str) and resource in keys:
-            resource = keys[resource]
+        if isinstance(resource, str) and resource in data_structures.ITEM_KEYS:
+            resource = data_structures.ITEM_KEYS[resource]
         path = 'storage/listed/{}'.format(resource)
         response = self.api_wrapper.get(path)
         soup = BeautifulSoup(response, 'html.parser')

+ 6 - 34
src/rival_regions_wrapper/api_wrapper/overview.py

@@ -1,54 +1,26 @@
 """Profile class"""
 
-import re
-
 from bs4 import BeautifulSoup
 
-from rival_regions_wrapper import functions
+from .abstract_wrapper import AbstractWrapper
+from .perks import Perks
 
 
-class Overview():
+class Overview(AbstractWrapper):
     """Wrapper class for perks"""
-    def __init__(self, api_wrapper):
-        self.api_wrapper = api_wrapper
-
     def info(self):
-        """Get perks"""
+        """Get overview """
         path = 'main/content'
         response = self.api_wrapper.get(path)
         soup = BeautifulSoup(response, 'html.parser')
-        perks = soup.select('.perk_source_4')
-        upgrade_perk = None
-        upgrade_date = None
-        for perk in perks:
-            date_string = perk.select_one('.small')
-            if date_string:
-                upgrade_perk = int(perk['perk'])
-                date_string = re.sub(
-                        r'^.*:\s', '',
-                        soup.select_one('.perk_source_4 .small').text
-                    )
-                upgrade_date = functions.parse_date(date_string)
-                break
+        perks = Perks.info_parse(soup)
         auto_war = soup.select_one('.war_index_war span.pointer:nth-child(4)')
         if auto_war and auto_war.has_attr('action'):
             auto_war = auto_war['action'].replace('war/details/', '')
         else:
             auto_war = None
         overview = {
-            'perks': {
-                'strenght': int(soup.find(
-                    'div', {'perk': 1, 'class': 'perk_source_2'}).text
-                ),
-                'education': int(soup.find(
-                    'div', {'perk': 2, 'class': 'perk_source_2'}).text
-                ),
-                'endurance': int(soup.find(
-                    'div', {'perk': 3, 'class': 'perk_source_2'}).text
-                ),
-                'upgrade_date': upgrade_date,
-                'upgrade_perk': upgrade_perk
-            },
+            'perks': perks,
             'war': {
                 'auto_war': auto_war,
             }

+ 7 - 4
src/rival_regions_wrapper/api_wrapper/perks.py

@@ -5,18 +5,21 @@ import re
 from bs4 import BeautifulSoup
 
 from rival_regions_wrapper import functions
+from .abstract_wrapper import AbstractWrapper
 
 
-class Perks():
+class Perks(AbstractWrapper):
     """Wrapper class for perks"""
-    def __init__(self, api_wrapper):
-        self.api_wrapper = api_wrapper
-
     def info(self):
         """Get perks"""
         path = 'main/content'
         response = self.api_wrapper.get(path)
         soup = BeautifulSoup(response, 'html.parser')
+        return self.info_parse(soup)
+
+    @staticmethod
+    def info_parse(soup):
+        """Parse perk info"""
         perks = soup.select('.perk_source_4')
         upgrade_perk = None
         upgrade_date = None

+ 4 - 2
src/rival_regions_wrapper/api_wrapper/profile.py

@@ -4,11 +4,13 @@ import re
 
 from bs4 import BeautifulSoup
 
+from .abstract_wrapper import AbstractWrapper
 
-class Profile():
+
+class Profile(AbstractWrapper):
     """Wrapper class for profile"""
     def __init__(self, api_wrapper, profile_id):
-        self.api_wrapper = api_wrapper
+        AbstractWrapper.__init__(self, api_wrapper)
         self.profile_id = profile_id
 
     def info(self):

+ 4 - 2
src/rival_regions_wrapper/api_wrapper/resource_state.py

@@ -4,11 +4,13 @@ import re
 
 from bs4 import BeautifulSoup
 
+from .abstract_wrapper import AbstractWrapper
 
-class ResourceState():
+
+class ResourceState(AbstractWrapper):
     """Wrapper class for resource state"""
     def __init__(self, api_wrapper, state_id):
-        self.api_wrapper = api_wrapper
+        AbstractWrapper.__init__(self, api_wrapper)
         self.state_id = state_id
 
     def info(self, resource):

+ 6 - 26
src/rival_regions_wrapper/api_wrapper/storage.py

@@ -2,40 +2,20 @@
 
 from bs4 import BeautifulSoup
 
+from rival_regions_wrapper import data_structures
+
+from .abstract_wrapper import AbstractWrapper
 
-class Storage():
-    """Wrapper class for storage"""
-    def __init__(self, api_wrapper):
-        self.api_wrapper = api_wrapper
 
+class Storage(AbstractWrapper):
+    """Wrapper class for storage"""
     def info(self):
         """storage info"""
         path = 'storage'
         response = self.api_wrapper.get(path)
         soup = BeautifulSoup(response, 'html.parser')
-        keys = {
-            'oil': 3,
-            'ore': 4,
-            'uranium': 11,
-            'diamonds': 15,
-            'liquid_oxygen': 21,
-            'helium-3': 24,
-            'rivalium': 26,
-            'antirad': 13,
-            'energy_drink': 17,
-            'spacerockets': 20,
-            'lss': 25,
-            'tanks': 2,
-            'aircrafts': 1,
-            'missiles': 14,
-            'bombers': 16,
-            'battleships': 18,
-            'laser_drones': 27,
-            'moon_tanks': 22,
-            'space_stations': 23
-        }
         storage = {}
-        for key, item_id in keys.items():
+        for key, item_id in data_structures.ITEM_KEYS.items():
             storage[key] = int(
                 soup.find('span', {'urlbar': item_id}).text.replace('.', '')
             )

+ 2 - 4
src/rival_regions_wrapper/api_wrapper/war.py

@@ -7,13 +7,11 @@ import unicodedata
 from bs4 import BeautifulSoup
 
 from rival_regions_wrapper import functions
+from .abstract_wrapper import AbstractWrapper
 
 
-class War():
+class War(AbstractWrapper):
     """Wrapper class for war"""
-    def __init__(self, api_wrapper):
-        self.api_wrapper = api_wrapper
-
     def page(self):
         """Get training war"""
         path = 'war'

+ 2 - 4
src/rival_regions_wrapper/api_wrapper/work.py

@@ -4,6 +4,7 @@ import re
 
 from bs4 import BeautifulSoup
 
+from .abstract_wrapper import AbstractWrapper
 
 RESOURCE_DICT = {
     'oil': 'oil',
@@ -14,11 +15,8 @@ RESOURCE_DICT = {
 }
 
 
-class Work():
+class Work(AbstractWrapper):
     """Wrapper class for work"""
-    def __init__(self, api_wrapper):
-        self.api_wrapper = api_wrapper
-
     def page(self):
         """Get work page"""
         path = 'work'

+ 25 - 0
src/rival_regions_wrapper/data_structures.py

@@ -0,0 +1,25 @@
+"""
+Different datastructures that modules might need
+"""
+
+ITEM_KEYS = {
+    'oil': 3,
+    'ore': 4,
+    'uranium': 11,
+    'diamonds': 15,
+    'liquid_oxygen': 21,
+    'helium-3': 24,
+    'rivalium': 26,
+    'antirad': 13,
+    'energy_drink': 17,
+    'spacerockets': 20,
+    'lss': 25,
+    'tanks': 2,
+    'aircrafts': 1,
+    'missiles': 14,
+    'bombers': 16,
+    'battleships': 18,
+    'laser_drones': 27,
+    'moon_tanks': 22,
+    'space_stations': 23
+}