Browse Source

Add article module

JoostSijm 4 năm trước cách đây
mục cha
commit
f39e9f155e

+ 2 - 1
rival_regions_wrapper/__init__.py

@@ -8,4 +8,5 @@ of some Rival Regions functionalities.
 
 from .authentication_handler import AuthenticationHandler
 from .middleware import LocalAuthentication, RemoteAuthentication
-from .api_wrapper import Profile, Storage, Market, ResourceState, Perks, Craft, Overview, War, Work
+from .api_wrapper import Profile, Storage, Market, ResourceState, Perks, Craft, Overview, War, \
+    Work, Article

+ 3 - 2
rival_regions_wrapper/api_wrapper/__init__.py

@@ -26,8 +26,8 @@ if None in (USERNAME, PASSWORD, LOGIN_METHOD):
     )
 
 # api
-MIDDLEWARE = RemoteAuthentication(API_URL, AUTHORIZATION)
-# MIDDLEWARE = LocalAuthentication(USERNAME,PASSWORD,LOGIN_METHOD)
+# MIDDLEWARE = RemoteAuthentication(API_URL, AUTHORIZATION)
+MIDDLEWARE = LocalAuthentication(USERNAME, PASSWORD, LOGIN_METHOD)
 
 from .profile import Profile
 from .storage import Storage
@@ -38,3 +38,4 @@ from .craft import Craft
 from .overview import Overview
 from .war import War
 from .work import Work
+from .article import Article

+ 46 - 0
rival_regions_wrapper/api_wrapper/article.py

@@ -0,0 +1,46 @@
+"""Articl class"""
+
+import unicodedata
+import re
+
+from bs4 import BeautifulSoup
+
+from . import MIDDLEWARE
+
+
+class Article(object):
+    """Wrapper class for profile"""
+
+    @staticmethod
+    def info(article_id):
+        """Get artcile"""
+        path = 'news/show/{}'.format(article_id)
+        response = MIDDLEWARE.get(path)
+        soup = BeautifulSoup(response, 'html.parser')
+
+        links = soup.select('.newspaper_links')
+        newspaper = links[0]
+        author = links[1]
+        region = links[2]
+
+        news_content = soup.select_one('.news_content')
+
+        article_info = {
+            'article_id': article_id,
+            'article_title': unicodedata.normalize("NFKD", soup.select_one('.title_totr').text),
+            'newspaper_id': int(newspaper['action'].replace('newspaper/show/', '')),
+            'newspaper_name': newspaper.text,
+            'author_name': re.sub(r',\s\skarma.*$', '', author.text),
+            'author_id': int(author['action'].replace('slide/profile/', '')),
+            'region_name': region.text,
+            'region_id': int(region['action'].replace('map/details/', '')),
+            'content_text': news_content.text,
+            'content_html': news_content.prettify(),
+        }
+
+        result = re.search(r'.+(\s.+,)', soup.select_one('.tc.small').text)
+        try:
+            article_info['language'] = re.sub(r'\s|,', '', result[1].strip())
+        except IndexError:
+            pass
+        return article_info

+ 20 - 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
+    Craft, Overview, War, Work, Article
 
 
 @pytest.fixture
@@ -197,3 +197,22 @@ def test_work_info():
     assert isinstance(response['factory'], dict), "Factory should be a dict"
     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_article_info():
+    """Test article info"""
+    article_id = 2708696
+    response = Article.info(article_id)
+
+    assert isinstance(response, dict), "The resonse should be a dict"
+    assert isinstance(response['article_id'], int), "Article id should be an integer"
+    assert isinstance(response['article_title'], str), "Article title should be a str"
+    assert isinstance(response['newspaper_id'], int), "Newspaper id should be an integer"
+    assert isinstance(response['newspaper_name'], str), "Newspaper name should be a string"
+    assert isinstance(response['author_name'], str), "Author name should be a string"
+    assert isinstance(response['author_id'], int), "Author id should be an integer"
+    assert isinstance(response['region_name'], str), "Region name should be a string"
+    assert isinstance(response['region_id'], int), "Region id should be an integer"
+    assert isinstance(response['content_text'], str), "Content text should be a string"
+    assert isinstance(response['content_html'], str), "Content html should be a string"
+    assert isinstance(response['language'], str), "Language should be a string"