Skip to content

Commit

Permalink
Fix time formatting and other small format changes
Browse files Browse the repository at this point in the history
  • Loading branch information
mheguy committed Jul 26, 2024
1 parent 5490448 commit 7e2b774
Show file tree
Hide file tree
Showing 15 changed files with 35 additions and 25 deletions.
1 change: 1 addition & 0 deletions sgu/data/templates/dumbest.j2x
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@
|article_title = ((( article_title )))
|publication = ((( publication )))
}}

((( transcript )))
1 change: 1 addition & 0 deletions sgu/data/templates/email.j2x
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@
text: ((( item )))
((* endfor *))
-->

((( transcript )))
2 changes: 1 addition & 1 deletion sgu/data/templates/interview.j2x
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{{anchor|interview}}
== Interview with ((( name ))) <small>( ((( start_time ))) )</small> ==
((( source )))

((( transcript )))
2 changes: 1 addition & 1 deletion sgu/data/templates/logical_fallacy.j2x
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{{anchor|ntlf}}
== Name That Logical Fallacy <small>( ((( start_time ))) )</small> ==
((( source )))

((( transcript )))
4 changes: 2 additions & 2 deletions sgu/data/templates/news.j2x
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
{{anchor|news((( item_number )))}}
=== News Item #((( item_number ))) - ((( topic ))) <small>( ((( start_time ))) )</small> ===
== News Item #((( item_number ))) - ((( topic ))) <small>( ((( start_time ))) )</small> ==
{{shownotes
|weblink = ((( url )))
|article_title = ((( topic ))) ((# TODO: Replace with title #))
|publication = ((( publication )))
}}
((( source )))

((( transcript )))
2 changes: 1 addition & 1 deletion sgu/data/templates/noisy.j2x
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{{anchor|wtn}}
{{anchor|futureWTN}}
== Who's That Noisy? + Announcements <small>( ((( start_time ))) )</small> ==
((( source )))

((( transcript )))
2 changes: 1 addition & 1 deletion sgu/data/templates/quickie.j2x
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{{anchor|quickie}}
== ((( title ))): ((( subject ))) <small>( ((( start_time ))) )</small> ==
((( source )))

((( transcript )))
2 changes: 1 addition & 1 deletion sgu/data/templates/quote.j2x
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{{anchor|qow}}
== Skeptical Quote of the Week <small>( ((( start_time ))) )</small> ==
((( source )))
{{qow
|text = ((( quote )))
|author = ((( attribution )))
|lived =
|desc =
}}

((( transcript )))
2 changes: 1 addition & 1 deletion sgu/data/templates/science_or_fiction.j2x
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
{{anchor|sof}}
{{anchor|theme}}
== Science or Fiction <small>( ((( start_time ))) )</small> ==
((( source )))
{{SOFinfo
|theme = ((( theme )))
|hiddentheme =
Expand Down Expand Up @@ -31,4 +30,5 @@
|win = <!-- at least one Rogue guessed wrong, but not them all -->
|swept = <!-- all the Rogues guessed right -->
}}

((( transcript )))
2 changes: 1 addition & 1 deletion sgu/data/templates/tiktok.j2x
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{{anchor|tiktok}}
== From TikTok: ((( title ))) <small>( ((( start_time ))) )</small> ==
((( source )))
((( url )))

((( transcript )))
2 changes: 1 addition & 1 deletion sgu/data/templates/unknown.j2x
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
== ((( title ))) <small>( ((( start_time ))) )</small> ==
((( source )))

((( transcript )))
2 changes: 1 addition & 1 deletion sgu/data/templates/whats_the_word.j2x
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{{anchor|wtw}}
== What's the Word? <small>( ((( start_time ))) )</small> ==
((( source )))
* ((( word )))

((( transcript )))
4 changes: 2 additions & 2 deletions sgu/episode_segments.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from sgu.exceptions import StartTimeNotFoundError
from sgu.helpers import are_strings_in_string, find_single_element, string_is_url
from sgu.template_environment import template_env
from sgu.transcript_formatting import format_transcript_for_wiki
from sgu.transcript_formatting import format_time, format_transcript_for_wiki

if TYPE_CHECKING:
from sgu.transcription import DiarizedTranscript
Expand Down Expand Up @@ -62,7 +62,7 @@ def to_wiki(self) -> str:
template = template_env.get_template(f"{self.template_name}.j2x")
template_values = self.get_template_values()
return template.render(
start_time=self.start_time,
start_time=format_time(self.start_time),
transcript=format_transcript_for_wiki(self.transcript),
source=f"<!-- data source: {self.source.value} -->",
**template_values,
Expand Down
27 changes: 18 additions & 9 deletions sgu/transcript_formatting.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,28 @@ def format_transcript_for_wiki(transcript: "DiarizedTranscript") -> str:
transcript = _join_speaker_segments(transcript)
_abbreviate_speakers(transcript)

text_segments: list[str] = []
for transcript_chunk in transcript:
start_time = _format_time(transcript_chunk["start"])
end_time = _format_time(transcript_chunk["end"])
text_segments = [
f"'''{transcript_chunk['speaker']}''':{transcript_chunk['text']}" for transcript_chunk in transcript
]

text_segments.append(f"<!-- {start_time} - {end_time} -->")
text_segments.append(f"'''{transcript_chunk['speaker']}''':{transcript_chunk['text']}<br />")
return "\n\n".join(text_segments)

return "\n".join(text_segments)

def format_time(time: float | None) -> str:
"""Format a float time to h:mm:ss or mm:ss if < 1 hour."""
if not time:
return "???"

def _format_time(time: float) -> str:
return f"{int(time) // 3600:02d}:{int(time) // 60 % 60:02d}:{int(time) % 60:02d}"
hour_count = int(time) // 3600

hour = ""
if hour_count:
hour = f"{hour_count}:"

minutes = f"{int(time) // 60 % 60:02d}:"
seconds = f"{int(time) % 60:02d}"

return f"{hour}{minutes}{seconds}"


def _join_speaker_segments(transcript: "DiarizedTranscript") -> "DiarizedTranscript":
Expand Down
5 changes: 2 additions & 3 deletions sgu/wiki.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ async def create_podcast_wiki_page(client: "Session", podcast: "PodcastEpisode")

# Above: Generic actions
# Below: Wiki-specific actions
# convert segments to wiki
# we must grab speaker data before we convert transcript to wiki
speakers = {s["speaker"].lower() for s in episode_data.transcript}
wiki_segments = "\n".join(s.to_wiki() for s in episode_segments)
qotw_segment = _extract_quote_of_the_week_for_wiki(episode_segments)

Expand All @@ -58,8 +59,6 @@ async def create_podcast_wiki_page(client: "Session", podcast: "PodcastEpisode")
episode_image_url = get_episode_image_url(episode_data.show_notes)
episode_icon_name = _upload_image_to_wiki(client, episode_image_url, episode_data.podcast.episode_number)

speakers = {s["speaker"].lower() for s in episode_data.transcript}

logger.debug("Creating wiki page...")
wiki_page = _construct_wiki_page(episode_data, episode_icon_name, wiki_segments, qotw_segment, speakers)
_edit_page(client, page_text=wiki_page) # TODO: Change for "Create page"
Expand Down

0 comments on commit 7e2b774

Please sign in to comment.