Преглед на файлове

Improve article message format

JoostSijm преди 4 години
родител
ревизия
1defef44a1
променени са 4 файла, в които са добавени 50 реда и са изтрити 24 реда
  1. 1 1
      Pipfile.lock
  2. 16 21
      src/vboo_info_bot/functions.py
  3. 13 0
      tests/conftest.py
  4. 20 2
      tests/test_vboo_info_bot_functions.py

+ 1 - 1
Pipfile.lock

@@ -190,7 +190,7 @@
         "rival-regions-wrapper": {
             "editable": true,
             "git": "https://github.com/jjoo914/rival_regions_wrapper",
-            "ref": "91965797a657c1c45d739e6a0889ec75402bdccc"
+            "ref": "df3133455205cdf4e28edcd61c36399fe4f5e7ad"
         },
         "selenium": {
             "hashes": [

+ 16 - 21
src/vboo_info_bot/functions.py

@@ -5,30 +5,27 @@ from telegram.utils.helpers import escape_markdown
 
 def telegram_format_article(article):
     """Format article object for telegram"""
-    title = '*[{}](https://m.rivalregions.com/#news/show/{})*'.format(
-        escape_markdown(article['article_title'], 2),
-        article['article_id'],
-    )
-    author = '[{}](https://m.rivalregions.com/#slide/profile/{})'.format(
+    row_list = []
+    row_list.append('*{}*'.format(escape_markdown(article['article_title'], 2)))
+    row_list.append('by: [{}](https://m.rivalregions.com/#slide/profile/{})'.format(
         escape_markdown(article['author_name'], 2),
         article['author_id'],
-    )
-    underline = '{}, rating: {}, comments: {}'.format(
+    ))
+    row_list.append('{}, rating: {}, comments: {}'.format(
         escape_markdown(article['post_date'].strftime('%Y-%m-%d %H:%M'), 2),
         article['rating'],
         article['comments'],
-    )
-    if article['content_text'].strip():
-        article_content = '_{}_'.format(escape_markdown(article['content_text'][0:144].strip(), 2))
-    else:
-        article_content = ''
-    formatted_article = '{} by: {}\n{}\n{}'.format(
-        title,
-        author,
-        underline,
-        article_content,
-    )
-    return formatted_article
+    ))
+    if article['content_text']:
+        content = article['content_text'].replace('\n', ' ')[0:144]
+        content += '...' if len(content) >= 144 else ''
+        row_list.append('_{}_'.format(escape_markdown(content, 2)))
+
+    row_list.append('{} \\| {}'.format(
+        '[mobile](https://m.rivalregions.com/#news/show/{})'.format(article['article_id']),
+        '[desktop](http://rivalregions.com/#news/show/{})'.format(article['article_id']),
+    ))
+    return '\n'.join(row_list)
 
 def abbreviate(string, max_lenght):
     """Abriviate string to only first letters"""
@@ -63,8 +60,6 @@ def roundk(integer):
         return '{}{}'.format(integer, 'k' * thousand)
     return '{}.{}{}'.format(integer, decimal, 'k' * thousand)
 
-
-
 def telegram_format_war(war):
     """Format war object for article"""
     row_list = []

+ 13 - 0
tests/conftest.py

@@ -43,3 +43,16 @@ def api_wrapper():
         )
     authentication = LocalAuthentication(rr_username, rr_password, rr_login_method)
     return ApiWrapper(authentication)
+
+def pytest_addoption(parser):
+    """Pytest parser options"""
+    parser.addoption('--message', action='store_true', dest="message", \
+        default=False, help="enable messagedecorated tests")
+
+def pytest_configure(config):
+    """Pytest config"""
+    if not config.option.message:
+        setattr(config.option, 'markexpr', 'not message')
+    config.addinivalue_line(
+        "markers", "message: send telegram message"
+    )

+ 20 - 2
tests/test_vboo_info_bot_functions.py

@@ -60,8 +60,25 @@ def test_article_info_two(api_wrapper, article_keys):
     assert isinstance(response['language'], str), "Language should be a string"
     assert isinstance(response['rating'], int), "Rating should be an integer"
 
+@pytest.mark.message
 @pytest.mark.vcr()
-def test_formatting_war_ground(api_wrapper, telegram_bot, telegram_channel):
+def test_send_article_message(api_wrapper, telegram_bot, telegram_channel):
+    """Test format war"""
+    article_id = 2867567
+    response = Article(api_wrapper).info(article_id)
+
+    assert isinstance(response, dict), "The response should be a dict"
+    formatted_article = functions.telegram_format_article(response)
+    if telegram_channel:
+        telegram_bot.sendMessage(
+            telegram_channel,
+            formatted_article,
+            parse_mode=ParseMode.MARKDOWN_V2
+        )
+
+@pytest.mark.message
+@pytest.mark.vcr()
+def test_send_war_ground_message(api_wrapper, telegram_bot, telegram_channel):
     """Test format war"""
     war_id = 329541
     response = War(api_wrapper).info(war_id)
@@ -71,8 +88,9 @@ def test_formatting_war_ground(api_wrapper, telegram_bot, telegram_channel):
     if telegram_channel:
         telegram_bot.sendMessage(telegram_channel, formatted_war, parse_mode=ParseMode.MARKDOWN_V2)
 
+@pytest.mark.message
 @pytest.mark.vcr()
-def test_formatting_war_coup(api_wrapper, telegram_bot, telegram_channel):
+def test_send_war_coup_message(api_wrapper, telegram_bot, telegram_channel):
     """Test format war"""
     war_id = 329518
     response = War(api_wrapper).info(war_id)