def test_nothing_after_progress_section_heading(self): day_heading = self.get_day_heading_with_added_html(html_insert=""" <h3>Today's Progress</h3>""", ) from logtweet._content.exceptions import NoProgressPargraphsError from logtweet._content.extract import get_progress_paragraphs with pytest.raises(NoProgressPargraphsError): get_progress_paragraphs(day_heading)
def test_no_content_in_paragraphs(self): day_heading = self.get_day_heading_with_added_html(html_insert=""" <h3>Today's Progress</h3> <p></p>""", ) from logtweet._content.exceptions import EmptyProgressParagraphsError from logtweet._content.extract import get_progress_paragraphs with pytest.raises(EmptyProgressParagraphsError): get_progress_paragraphs(day_heading)
def test_returns_content_from_one_paragraph(self): day_heading = self.get_day_heading_with_added_html(html_insert=""" <h3>Today's Progress</h3> <p>Finally a paragraph with content.</p>""", ) expected = ("Finally a paragraph with content.", ) from logtweet._content.extract import get_progress_paragraphs actual = get_progress_paragraphs(day_heading) assert actual == expected
def test_returns_content_second_if_first_paragraph_empty(self): """Only first paragraph empty.""" day_heading = self.get_day_heading_with_added_html(html_insert=""" <h3>Today's Progress</h3> <p></p> <p>A second paragraph with content.</p>""", ) expected = ("A second paragraph with content.", ) from logtweet._content.extract import get_progress_paragraphs actual = get_progress_paragraphs(day_heading) assert actual == expected
def test_dont_return_paragraphs_after_next_section_heading(self): """Does not return paragraphs after next day section heading.""" day_heading = self.get_day_heading_with_added_html(html_insert=""" <h3>Today's Progress</h3> <p>Progress paragraph with content.</p> <h3>Thoughts</h3> <p>Not a progress paragraph.</p>""", ) expected = ("Progress paragraph with content.", ) from logtweet._content.extract import get_progress_paragraphs actual = get_progress_paragraphs(day_heading) assert actual == expected
def test_dont_return_paragraphs_after_next_day_heading(self): """Does not return progress content after next day heading.""" day_heading = self.get_day_heading_with_added_html(html_insert=""" <h3>Today's Progress</h3> <p>Progress paragraph with content.</p> <h2>Day 2: October 17, 2019, Thursday</h2> <h3>Today's Progress</h3> <p>Progress paragraph of a different day.</p> """, ) expected = ("Progress paragraph with content.", ) from logtweet._content.extract import get_progress_paragraphs actual = get_progress_paragraphs(day_heading) assert actual == expected
def test_filters_empty_paragraph_between_two_filled_paragraphs(self): """Returns content for two paragraphs with empty in between.""" day_heading = self.get_day_heading_with_added_html(html_insert=""" <h3>Today's Progress</h3> <p>First paragraph with content.</p> <p></p> <p>A second paragraph with content.</p>""", ) expected = ( "First paragraph with content.", "A second paragraph with content.", ) from logtweet._content.extract import get_progress_paragraphs actual = get_progress_paragraphs(day_heading) assert actual == expected
def get_tweet_content( log_string: str, day_date: datetime.date, bitly_api_key: Optional[str] = None, ) -> str: """ Get tweet content from a log string for a given date. Parameters ---------- log_string : str String representation of the log. The log string is expected to contain an HTML log. The HMTL log is expected to contain contain ``<h2>`` elements for the day headings and ``<h3>`` elements for the day's subsections. # TODO: Force these expectation on the content through custom # types. The generation of the custom type validates the # structure day_date : datetime.date Day for which the tweet is to be generated. bitly_api_key : Optional[str] While generating the tweet content, the first link from the "Link(s)" section is extracted and shortened. The user if an API key for the Bit.ly service is provided, that service is used. This argument defaults to ``None``, in which case `Shorten That URL_ is used. Returns ------- str Tweet content for the given `day_date` extracted from the `log_string`. .. _`Shorten That URL: https://s.lpld.io """ soup = bs4.BeautifulSoup(log_string, "html.parser") day_heading = extract.get_day_heading(soup, heading_date=day_date) day_number = extract.get_day_number_from_heading_string(day_heading.text) try: link = extract.get_first_link(day_heading) except LookupError: link = "" else: link = shortlink.get_short_link(link, bitly_api_key) # Generate tweet preamble (E.g. 77/#100DaysOfCode) preamble = build.make_preamble(day_number) # Calculate max message length. This needs to be the maximum tweet # length, reduced by the preamble and the link. max_tweet_msg_len = calc_max_tweet_msg_len(preamble, link) # Get content progress_paragraphs = extract.get_progress_paragraphs(day_heading) tweet_message = build.join_strings_to_max_len( strings=progress_paragraphs, max_len=max_tweet_msg_len, sep="\n\n", ) # Build content from preamble, message and link return build.make_tweet_content( preamble=preamble, message=tweet_message, link=link, )
def test_no_progress_section(self): day_heading = self.get_day_heading_with_added_html() from logtweet._content.extract import get_progress_paragraphs with pytest.raises(LookupError): get_progress_paragraphs(day_heading)