Jelajahi Sumber

Breaking change:
- environment variables names changed to be more specific
- login method should now be alright if in capitals instead
- removed some unneccesary checks in login
- confirmed VK method does work - PyTests currently failing

Alexander 4 tahun lalu
induk
melakukan
2a46826cba

+ 36 - 0
src/rival_regions_wrapper/api_wrapper/work.py

@@ -3,6 +3,8 @@
 import re
 
 from bs4 import BeautifulSoup
+import logging
+LOGGER = logging.getLogger('rival_regions_wrapper.authentication_handler')
 
 
 RESOURCE_DICT = {
@@ -19,6 +21,40 @@ class Work():
     def __init__(self, api_wrapper):
         self.api_wrapper = api_wrapper
 
+    # TODO ENERGY
+    # def energy(self):
+    #    """Get current amount of energy"""
+    #    response = self.api_wrapper.get(path)
+
+    def work(self, amount=1, mentor=0):
+        """Work at given factory ID. Amount needs to be energy / 10."""
+        response = self.api_wrapper.post(f'factory/go/{amount}/{mentor}/')
+        soup = BeautifulSoup(response, 'html.parser')
+        """if str(soup.select_one('h1').text.replace('\xa0▶','')) == 'You need residency to work in this region ':
+            LOGGER.info(str(soup))
+            return False
+        else:"""
+        #factory = str(soup.select_one('h1').text.split('\xa0▶')[0])
+        #factory_type = str(soup.select_one('h1').text.split('\xa0▶')[1])
+        #income = str(soup.select_one('div.minwidth.imp').select('.work_results2')[-1].
+        #             select('span')[-1].text.replace('.', ''))
+        #LOGGER.info(str(soup.select_one('div.minwidth.imp').select('.work_results2')))
+        LOGGER.info(str([i for i in soup.stripped_strings]))
+        #  ['IndX GOLD', '▶', 'Gold mine', '—10 E\t\t\t\t\t\t\t\t\t\t\t (+9)', 'Working experience: +1 Pt.',
+        #  '0 $', 'Exp: +20', 'Total: 6900420', 'Taxes: +0 $ (10%)', 'Total: 5.657.542.880.418 $',
+        #  'Work again', 'Auto']
+        #income = [i.strip("+") for i in income.split(" ")]  # Split units from value and remove sign
+        #LOGGER.info(str(income))
+        #income[0] = int(income[0])  # Convert first part to an integer, second will be the units
+
+        #worked_info = {
+        #    'factory': factory,
+        #    'factory_type': factory_type,
+        #    'income': income
+        #}
+#
+        return ''# worked_info
+
     def page(self):
         """Get work page"""
         path = 'work'

+ 3 - 2
src/rival_regions_wrapper/authentication_handler.py

@@ -101,8 +101,9 @@ class AuthenticationHandler:
     password = None
     session = None
 
-    def __init__(self, show_window=False):
+    def __init__(self, show_window=False, new_cookie=False):
         self.show_window = show_window
+        self.new_cookie = new_cookie
         LOGGER.info('Initialize authentication handler, show window: "%s"',
                     self.show_window)
 
@@ -119,7 +120,7 @@ class AuthenticationHandler:
         LOGGER.info('"%s": start login, method: "%s"',
                     self.username, self.login_method)
         cookie = self.get_cookie(self.username)
-        if cookie is None:
+        if cookie is None or self.new_cookie:
             LOGGER.info('"%s": no cookie, new login, method "%s"',
                         self.username, self.login_method)
 

+ 2 - 2
src/rival_regions_wrapper/middleware.py

@@ -22,8 +22,8 @@ class MiddlewareBase(ABC):
 class LocalAuthentication(MiddlewareBase):
     """Local authentication"""
 
-    def __init__(self, username, password, login_method, show_window=False):
-        self.client = AuthenticationHandler(show_window)
+    def __init__(self, username, password, login_method, show_window=False, new_cookie=False):
+        self.client = AuthenticationHandler(show_window, new_cookie)
         self.client.set_credentials({
             'username': username,
             'password': password,

+ 11 - 0
tests/test_rival_regions_wrapper.py

@@ -265,6 +265,17 @@ def test_work_info(api_wrapper):
     assert isinstance(response['resources_left'], dict), "Resources left should be a dict"
     assert isinstance(response['work_exp'], dict), "Work exp should be a dict"
 
+@pytest.mark.vcr()
+def test_work_do_work(api_wrapper):
+    response = Work(api_wrapper).work()
+    if not response:
+        assert isinstance(response ,bool), "Should be False if can't work"
+    else:
+        assert isinstance(response['factory'], str), "The factory should be a string with name"
+        assert isinstance(response['factory_type'], str), "The factory type should be a string of factory type"
+        assert isinstance(response['income'][0], int), "The income first index should be a integer"
+        assert isinstance(response['income'][1], str), "The income second index should be a string of units"
+
 @pytest.fixture
 def article_keys():
     """Standard key fro article"""