Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,4 @@ Pip*
.env
venv
.idea
*.ignore.py
4 changes: 2 additions & 2 deletions diffengine/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import unicodedata

from diffengine.sendgrid import SendgridHandler
from diffengine.text import to_utf8
from diffengine.twitter import TwitterHandler

from exceptions.webdriver import UnknownWebdriverError
Expand Down Expand Up @@ -173,8 +174,7 @@ def get_latest(self):
if resp.status_code != 200:
logging.warn("Got %s when fetching %s", resp.status_code, self.url)
return None

doc = readability.Document(resp.text)
doc = readability.Document(to_utf8(resp.text))
title = doc.title()
summary = doc.summary(html_partial=True)
summary = bleach.clean(summary, tags=["p"], strip=True)
Expand Down
14 changes: 14 additions & 0 deletions diffengine/text_builder.py → diffengine/text.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,17 @@ def build_with_default_content(diff):
text = text[0:225] + "…"
text += " " + diff.url
return text


def to_utf8(text):
for encoding in ["latin1", "ascii"]:
try:
result = text.encode(encoding).decode("utf8", "strict")
break
except (UnicodeEncodeError, UnicodeDecodeError):
result = None

if result is None:
return text

return result
2 changes: 1 addition & 1 deletion diffengine/twitter.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

from datetime import datetime

from diffengine.text_builder import build_text
from diffengine.text import build_text
from exceptions.twitter import (
AlreadyTweetedError,
TwitterConfigNotFoundError,
Expand Down
29 changes: 21 additions & 8 deletions test_diffengine.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
SendgridHandler,
_fingerprint,
)
from diffengine.text_builder import build_text
from diffengine.text import build_text, to_utf8
from diffengine.utils import generate_config
from exceptions.sendgrid import (
SendgridConfigNotFoundError,
Expand Down Expand Up @@ -663,8 +663,8 @@ def get_mocked_diff(with_archive_urls=True):

class TextBuilderTest(TestCase):
@patch("logging.warning")
@patch("diffengine.text_builder.build_with_lang")
@patch("diffengine.text_builder.build_with_default_content")
@patch("diffengine.text.build_with_lang")
@patch("diffengine.text.build_with_default_content")
def test_build_with_default_content_when_no_lang_given(
self, mocked_build_with_default_content, mocked_build_from_lang, mocked_warning
):
Expand All @@ -679,8 +679,8 @@ def test_build_with_default_content_when_no_lang_given(
mocked_build_from_lang.assert_not_called()

@patch("logging.warning")
@patch("diffengine.text_builder.build_with_lang")
@patch("diffengine.text_builder.build_with_default_content")
@patch("diffengine.text.build_with_lang")
@patch("diffengine.text.build_with_default_content")
def test_build_with_default_content_when_lang_is_incomplete(
self, mocked_build_with_default_content, mocked_build_from_lang, mocked_warning
):
Expand All @@ -700,8 +700,8 @@ def test_build_with_default_content_when_lang_is_incomplete(
mocked_build_from_lang.assert_not_called()

@patch("logging.warning")
@patch("diffengine.text_builder.build_with_lang")
@patch("diffengine.text_builder.build_with_default_content")
@patch("diffengine.text.build_with_lang")
@patch("diffengine.text.build_with_default_content")
def test_build_with_lang_when_lang_given(
self, mocked_build_with_default_content, mocked_build_from_lang, mocked_warning
):
Expand All @@ -722,7 +722,7 @@ def test_build_with_lang_when_lang_given(
mocked_build_with_default_content.assert_not_called()
mocked_build_from_lang.assert_called_once()

@patch("diffengine.text_builder.build_with_lang")
@patch("diffengine.text.build_with_lang")
def test_default_content_text(self, mocked_build_from_lang):
diff = get_mocked_diff()
type(diff.new).title = "Test"
Expand Down Expand Up @@ -804,3 +804,16 @@ def test_lang_content_text(self):
self.assertEqual(
text, "change in the URL, the title and the summary\n%s" % diff.url
)


class EncodingTest(TestCase):
def test_utf8_do_nothingg(self):
text_utf8 = "Me preocupa más la parte futbolística"
result = to_utf8(text_utf8)
self.assertEquals(result, text_utf8)

def test_latin1_to_utf8(self):
text_latin = "Me preocupa más la parte futbolística"
text_utf8 = "Me preocupa más la parte futbolística"
result = to_utf8(text_latin)
self.assertEquals(result, text_utf8)