Browse Source

Adding support for conferences again

JoostSijm 4 years ago
parent
commit
e8f3fb093b

+ 1 - 4
README.md

@@ -76,14 +76,11 @@ region = authentication.get('listed/upgrades/{}'.format(region_id))
 
 Example of API wrapper to get oil current available resources from a state
 ```python
-from rival_regions_wrapper import apiWrapper
 from rival_regions_wrapper.api_wrapper import ResourceState
 
-api_wrapper = ApiWrapper(authentication)
-
 state = 3382
 resource = 'oil'
-response = ResourceState(api_wrapper, state).info(resource)
+response = ResourceState(authentication, state).info(resource)
 ```
 
 For more examples look at the unit tests.

+ 1 - 1
setup.py

@@ -7,7 +7,7 @@ with open("README.md", "r") as fh:
 
 setuptools.setup(
     name="rival_regions_wrapper",
-    version="1.2.1",
+    version="1.2.3",
     author="Joost Sijm",
     author_email="joostsijm@gmail.com",
     description="Rival Regions API wrapper",

+ 0 - 1
src/rival_regions_wrapper/__init__.py

@@ -8,4 +8,3 @@ of some Rival Regions functionalities.
 
 from .authentication_handler import AuthenticationHandler
 from .middleware import LocalAuthentication, RemoteAuthentication
-from .api_wrapper import ApiWrapper

+ 2 - 17
src/rival_regions_wrapper/api_wrapper/__init__.py

@@ -1,5 +1,6 @@
 """API wrapper for Rival Regions"""
 
+from rival_regions_wrapper.middleware import LocalAuthentication
 
 from .profile import Profile
 from .storage import Storage
@@ -11,20 +12,4 @@ from .overview import Overview
 from .war import War
 from .work import Work
 from .article import Article
-
-
-class ApiWrapper:
-    """API wrapper"""
-    authentication = None
-
-    def __init__(self, authentication):
-        """Initialize API wrapper with authentication"""
-        self.authentication = authentication
-
-    def get(self, path):
-        """Send get requests"""
-        return self.authentication.get(path)
-
-    def post(self, path, data=None):
-        """Send post request"""
-        return self.authentication.post(path, data=data)
+from .conference import Conference 

+ 13 - 0
src/rival_regions_wrapper/api_wrapper/conference.py

@@ -0,0 +1,13 @@
+"""Conference class"""
+
+from rival_regions_wrapper import functions
+
+
+class Conference():
+    """Wrapper class for confernce"""
+    def __init__(self, api_wrapper):
+        self.api_wrapper = api_wrapper
+
+    def send_message(self, conference_id, message):
+        """send conference message"""
+        self.api_wrapper.send_conference_message(conference_id, message)

+ 15 - 14
src/rival_regions_wrapper/authentication_handler.py

@@ -116,7 +116,6 @@ class AuthenticationHandler:
         self.login()
 
     def login(self):
-        self.remove_cookie(self.username)
         """Login user if needed"""
         LOGGER.info('"%s": start login, method: "%s"',
                     self.username, self.login_method)
@@ -195,7 +194,7 @@ class AuthenticationHandler:
             LOGGER.debug('"%s": closing login tab', self.username)
             browser.close_current_tab()
         else:
-            LOGGER.info('Cookies found')
+            LOGGER.info('"%s": Cookies found', self.username)
 
         self.session = cfscrape.CloudflareScraper()
         for cookie in cookies:
@@ -298,7 +297,7 @@ class AuthenticationHandler:
 
         with open('{}/cookies.json'.format(DATA_DIR), 'w+') as cookies_file:
             json.dump(cookies, cookies_file)
-        LOGGER.info('"%s": Saved cookie for', username)
+        LOGGER.info('"%s": Saved cookie', username)
 
     @classmethod
     def get_cookies(cls, username):
@@ -331,7 +330,7 @@ class AuthenticationHandler:
     @classmethod
     def remove_cookie(cls, username):
         """Remove cookie from storage"""
-        LOGGER.info('"%s": Removing cookie for', username)
+        LOGGER.info('"%s": Removing cookie', username)
         cookies = None
         try:
             with open('{}/cookies.json'.format(DATA_DIR), 'r') as cookies_file:
@@ -464,17 +463,19 @@ class AuthenticationHandler:
     def send_conference_message(self, conference_id, message):
         """send conference message"""
         LOGGER.info(
-                '"%s" CONF: id %s',
+                '"%s": CONF "%s": send message',
                 self.username, conference_id
             )
         if self.session:
             response = self.session.get("https://rivalregions.com/#overview")
             if "Session expired, please, reload the page" in response.text:
                 raise SessionExpireException()
-            browser = Browser(showWindow=self.show_window)
+            browser = Browser(showWindow=True)
             browser.go_to('https://rivalregions.com/')
-            for cookie in self.get_cookies(self.username):
-                browser.add_cookie(cookie)
+            for cookie_name, value in self.session.cookies.get_dict().items():
+                browser.add_cookie(
+                    self.create_cookie(cookie_name, None, value)
+                )
             browser.go_to(
                     'https://rivalregions.com/#slide/conference/{}'
                     .format(conference_id)
@@ -495,7 +496,7 @@ class AuthenticationHandler:
                                 ' '.join(tmp_sentence)
                             )
                         LOGGER.info(
-                                '"%s" CONF: id %s, next message length: %s',
+                                '"%s": CONF "%s": next message length: %s',
                                 self.username, conference_id, len(message)
                             )
                         browser.type(message, id='message')
@@ -523,15 +524,15 @@ class AuthenticationHandler:
             if tmp_messages:
                 message = '\n'.join(tmp_messages)
                 LOGGER.info(
-                        'conference %s: next message length: %s',
-                        conference_id, len(message)
+                        '"%s": CONF "%s": next message length: %s',
+                        self.username, conference_id, len(message)
                     )
                 browser.type(message, id='message')
                 browser.click(id='chat_send')
 
             LOGGER.info(
-                    'conference %s: finished sending message',
-                    conference_id
+                    '"%s": CONF "%s": finished sending message',
+                    self.username, conference_id
                 )
             browser.close_current_tab()
         else:
@@ -541,7 +542,7 @@ class AuthenticationHandler:
     def send_conference_notification(self, conference_id, message, sound):
         """send conference notification"""
         LOGGER.info(
-                '"%s" CONF: id %s notification ',
+                '"%s": CONF: %s notification',
                 self.username, conference_id
             )
         data = {

+ 1 - 1
src/rival_regions_wrapper/middleware.py

@@ -52,7 +52,7 @@ class LocalAuthentication(MiddlewareBase):
 
     def send_conference_notification(self, conference_id, message, sound):
         """Send conference notification"""
-        return self.client.send_conference_notification(conference_id, message, sound):
+        return self.client.send_conference_notification(conference_id, message, sound)
 
 class RemoteAuthentication(MiddlewareBase):
     """Remote authentication"""

+ 2 - 3
tests/conftest.py

@@ -5,7 +5,7 @@ import os
 import pytest
 from dotenv import load_dotenv
 
-from rival_regions_wrapper import LocalAuthentication, ApiWrapper
+from rival_regions_wrapper import LocalAuthentication
 
 
 load_dotenv()
@@ -33,5 +33,4 @@ def api_wrapper():
             'Load the following variables in your user environment: '
             'username, password, login_method'
         )
-    authentication = LocalAuthentication(username, password, login_method)
-    return ApiWrapper(authentication)
+    return LocalAuthentication(username, password, login_method)

+ 8 - 1
tests/test_rival_regions_wrapper.py

@@ -5,7 +5,7 @@ from datetime import datetime, timedelta
 import pytest
 
 from rival_regions_wrapper.api_wrapper import Profile, Storage, Market, ResourceState, Perks, \
-    Craft, Overview, War, Work, Article
+    Craft, Overview, War, Work, Article, Conference
 
 
 @pytest.fixture
@@ -323,3 +323,10 @@ def test_article_info_two(api_wrapper, article_keys):
     assert isinstance(response['rating'], int), "Rating should be an integer"
     assert isinstance(response['comments'], int), "Comments should be an integer"
     assert isinstance(response['post_date'], datetime), "Post date should be a datetime"
+
+# @pytest.mark.skip(reason="message request")
+def test_conference_message(api_wrapper):
+    """Test conference message"""
+    conference_id = 439289
+    response = Conference(api_wrapper).send_message(conference_id, 'hi')
+