Skip to content

Commit

Permalink
Merge pull request #930 from marwoodandrew/apple-format
Browse files Browse the repository at this point in the history
Format changes for Apple News
  • Loading branch information
marwoodandrew authored May 28, 2024
2 parents a62beaf + cc843b9 commit fa9c1df
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 71 deletions.
95 changes: 35 additions & 60 deletions server/aap/publish/formatters/aap_apple_news_formatter.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
from textwrap import dedent
from urllib.parse import urlparse, unquote
from superdesk.publish.formatters import Formatter
from superdesk.metadata.item import FORMAT, FORMATS
from superdesk.metadata.item import FORMAT, FORMATS, CONTENT_TYPE, ITEM_TYPE
from superdesk import get_resource_service
from superdesk.utils import json_serialize_datetime_objectId
from superdesk.utc import utc_to_local
Expand Down Expand Up @@ -112,37 +112,19 @@ def _remove_unwanted_embeds(self, article):
self._remove_embeds(article, remove_keys)

def format_dateline(self, located, current_timestamp):
"""
Formats dateline to "Location, Month Date Source -"
:return: formatted dateline string
"""

dateline_location = "{city_code}"
dateline_location_format_fields = located.get("dateline", "city")
dateline_location_format_fields = dateline_location_format_fields.split(",")
if "country" in dateline_location_format_fields and "state" in dateline_location_format_fields:
dateline_location = "{city_code}, {state_code}, {country_code}"
elif "state" in dateline_location_format_fields:
dateline_location = "{city_code}, {state_code}"
elif "country" in dateline_location_format_fields:
dateline_location = "{city_code}, {country_code}"
dateline_location = dateline_location.format(**located)

if located.get("tz") and located["tz"] != "UTC":
current_timestamp = datetime.fromtimestamp(current_timestamp.timestamp(), tz=timezone(located["tz"]))
else:
current_timestamp = utc_to_local(config.DEFAULT_TIMEZONE, current_timestamp)
if current_timestamp.month == 9:
formatted_date = "Sept {}".format(current_timestamp.strftime("%-d"))
formatted_date = "Sept {}".format(current_timestamp.strftime("%-d, %Y"))
elif 3 <= current_timestamp.month <= 7:
formatted_date = current_timestamp.strftime("%B %-d")
formatted_date = current_timestamp.strftime("%B %-d, %Y")
else:
formatted_date = current_timestamp.strftime("%b %-d")
formatted_date = current_timestamp.strftime("%b %-d, %Y")

return "{location}, {mmmdd} at {hhmmpa}".format(
location=dateline_location.upper(), mmmdd=formatted_date, hhmmpa=current_timestamp.strftime('%I:%M%p')
)
return "{mmmdd}".format(mmmdd=formatted_date)

def _format(self, article):
# Remove any video or audio embeds since for apple news they must be externally hosted
Expand All @@ -157,7 +139,8 @@ def _format(self, article):

def can_format(self, format_type, article):
"""Can format text article that are not preformatted"""
return format_type == self.format_type and article.get(FORMAT) == FORMATS.HTML
return format_type == self.format_type and article.get(FORMAT) == FORMATS.HTML and article[ITEM_TYPE] in [
CONTENT_TYPE.TEXT]

def _set_advertising_settings(self, apple_news):
"""Function to set the advertising settings"""
Expand Down Expand Up @@ -237,16 +220,24 @@ def _set_component_layouts(self, apple_news):
"top": 15
}
},
"bodyImageLayout": {
"columnSpan": 7,
"columnStart": 0,
"margin": {
"bottom": 5,
"top": 2
}
},
"fixed_image_header_container": {
"columnSpan": 7,
"columnStart": 0,
"ignoreDocumentMargin": True,
"minimumHeight": "45vh"
},
"titleLayout": {
"horizontalContentAlignment": "center",
"columnSpan": 5,
"columnStart": 1,
"horizontalContentAlignment": "left",
"columnSpan": 6,
"columnStart": 0,
"margin": {
"bottom": 5,
"top": 5
Expand All @@ -266,21 +257,21 @@ def _set_component_layouts(self, apple_news):
"columnSpan": 5,
"columnStart": 1,
"margin": {
"bottom": 5,
"top": 5
"bottom": 2,
"top": 2
}
},
"bylineLayout": {
"columnSpan": 5,
"columnStart": 1,
"columnSpan": 6,
"columnStart": 0,
"margin": {
"bottom": 2,
"top": 5
}
},
"dateLineLayout": {
"columnSpan": 5,
"columnStart": 1,
"columnSpan": 6,
"columnStart": 0,
"margin": {
"bottom": 5,
"top": 2
Expand All @@ -298,9 +289,9 @@ def _set_component_styles(self, apple_news):
"bodyStyle": {
"fontName": "HelveticaNeue",
"fontSize": 16,
"lineHeight": 26,
"lineHeight": 24,
"linkStyle": {
"textColor": "#000",
"textColor": "#CCCCCC",
"underline": {
"color": "#000"
}
Expand All @@ -312,28 +303,28 @@ def _set_component_styles(self, apple_news):
"fontName": "HelveticaNeue-Bold",
"fontSize": 18,
"lineHeight": 18,
"textAlignment": "center",
"textAlignment": "left",
"textColor": "#000"
},
"dateLineStyle": {
"fontName": "HelveticaNeue-Bold",
"fontSize": 18,
"lineHeight": 18,
"textAlignment": "center",
"fontSize": 12,
"lineHeight": 16,
"textAlignment": "left",
"textColor": "#000"
},
"titleStyle": {
"fontName": "HelveticaNeue-CondensedBlack",
"fontSize": 40,
"lineHeight": 50,
"textAlignment": "center",
"lineHeight": 44,
"textAlignment": "left",
"textColor": "#000"
},
"captionStyle": {
"fontName": "HelveticaNeue-Italic",
"fontSize": 12,
"hyphenation": False,
"lineHeight": 15,
"lineHeight": 16,
"textAlignment": "left",
"textColor": "#000"
}
Expand Down Expand Up @@ -418,7 +409,7 @@ def generate_article_content(self, article):
'identifier': key,
'accessibilityCaption': elem.find('./img').attrib['alt'],
'caption': elem.find('./figcaption').text,
'layout': 'bodyLayout'
'layout': 'bodyImageLayout'
},
{
"layout": "BodyCaptionLayout",
Expand Down Expand Up @@ -487,25 +478,9 @@ def _set_story_component(self, article):
"textStyle": "titleStyle",
"format": "html"
},
{
'role': 'divider',
'layout': {
'columnStart': 2,
'columnSpan': 3,
'margin': {
'top': 5,
'bottom': 5
}
},
'stroke': {
'color': '#063c7f',
'style': 'solid',
'width': 1
}
},
{
'role': 'byline',
'text': 'By {}'.format(article.get('byline')),
'text': 'By {}'.format(article.get('byline')) if article.get('byline') else '',
'layout': 'bylineLayout',
'textStyle': 'bylineStyle'
},
Expand Down
16 changes: 6 additions & 10 deletions server/aap/publish/formatters/aap_apple_news_formatter_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,14 +85,10 @@ def test_format_title(self):
"role": "title", "text": "Headline of the story",
"textStyle": "titleStyle",
"format": "html"},
{"role": "divider",
"layout": {"columnStart": 2, "columnSpan": 3,
"margin": {"top": 5, "bottom": 5}},
"stroke": {"color": "#063c7f", "style": "solid", "width": 1}},
{"role": "byline", "text": "By John Doe",
"layout": "bylineLayout",
"textStyle": "bylineStyle"},
{"role": "byline", "text": "SYDNEY, Feb 16 at 12:45AM",
{"role": "byline", "text": "Feb 16, 2018",
"layout": "dateLineLayout", "textStyle": "dateLineStyle"},
{"format": "html", "layout": "bodyLayout", "role": "body",
"text": "<p>The Statement</p>"
Expand Down Expand Up @@ -275,9 +271,9 @@ def test_format_article_with_embeds(self):
]
}}
apple_news = self.formatter._format(article)
self.assertEqual(apple_news['components'][7]['URL'], 'bundle://editor_1')
self.assertEqual(apple_news['components'][6]['URL'], 'bundle://editor_1')
self.assertEqual(apple_news['components'][0]['style']['fill']['URL'], 'bundle://featuremedia')
self.assertEqual(apple_news['components'][10]['URL'], 'https://twitter.com/AAPNewswire/status/1')
self.assertEqual(apple_news['components'][9]['URL'], 'https://twitter.com/AAPNewswire/status/1')

def test_format_article_with_instagram(self):
article = self._get_article()
Expand Down Expand Up @@ -345,7 +341,7 @@ def test_format_article_with_instagram(self):
}
}
apple_news = self.formatter._format(article)
self.assertEqual(apple_news['components'][5]['URL'], "https://www.instagram.com/reel/C")
self.assertEqual(apple_news['components'][4]['URL'], "https://www.instagram.com/reel/C")

def test_format_article_with_facebook(self):
article = self._get_article()
Expand Down Expand Up @@ -412,7 +408,7 @@ def test_format_article_with_facebook(self):
}
}
apple_news = self.formatter._format(article)
self.assertEqual(apple_news['components'][5]['URL'], 'https://www.facebook.com/aapnewswire/posts/pfbid')
self.assertEqual(apple_news['components'][4]['URL'], 'https://www.facebook.com/aapnewswire/posts/pfbid')

def test_format_article_with_tik_tok(self):
article = self._get_article()
Expand Down Expand Up @@ -485,4 +481,4 @@ def test_format_article_with_tik_tok(self):
}
}
apple_news = self.formatter._format(article)
self.assertEqual(apple_news['components'][5]['URL'], 'https://www.tiktok.com/@dic/video/7')
self.assertEqual(apple_news['components'][4]['URL'], 'https://www.tiktok.com/@dic/video/7')
2 changes: 1 addition & 1 deletion server/features/aap_mm.feature
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ Feature: AAP Multimedia Feature
"operation": "fetch",
"sign_off": "abc",
"byline": "Julian Smith/AAP PHOTOS",
"firstcreated": "2015-03-29T11:42:29+0000"
"firstcreated": "2015-03-28T21:49:44+0000"
}
]}
"""
Expand Down

0 comments on commit fa9c1df

Please sign in to comment.