Export blogs in micron format

main
bashrc 2026-03-02 13:39:48 +00:00
parent ed527e29dd
commit 2235ab7a78
4 changed files with 155 additions and 0 deletions

View File

@ -15,6 +15,7 @@ from utils import get_base_content_from_post
from utils import get_post_attachments
from utils import get_url_from_post
from utils import get_markdown_blog_filename
from utils import get_micron_blog_filename
from utils import get_gemini_blog_published
@ -581,3 +582,81 @@ def blog_to_markdown(base_dir: str, nickname: str, domain: str,
return False
return True
def blog_to_micron(base_dir: str, nickname: str, domain: str,
message_json: dict, system_language: str,
debug: bool, testing: bool) -> bool:
"""
Converts a blog post to micron format
Returns True on success
"""
if not testing:
account_dir = acct_dir(base_dir, nickname, domain)
else:
account_dir = base_dir
if os.path.isdir(account_dir + '/microntest'):
shutil.rmtree(account_dir + '/microntest', ignore_errors=True)
if not os.path.isdir(account_dir):
if debug:
print('WARN: blog_to_micron account directory not found ' +
account_dir)
return False
published = get_gemini_blog_published(message_json, debug)
if not published:
return False
# get the blog content
content_str = get_base_content_from_post(message_json, system_language)
if not content_str:
if debug:
print('WARN: blog_to_micron no content ' +
str(message_json))
return False
content_text = remove_html(content_str)
# create micron blog directory
if not testing:
micron_blog_dir = account_dir + '/micron'
else:
micron_blog_dir = account_dir + '/microntest'
if not os.path.isdir(micron_blog_dir):
os.mkdir(micron_blog_dir)
micron_blog_filename = \
get_micron_blog_filename(base_dir, nickname, domain,
message_json, system_language,
debug, testing)
# get attachments
links: list[str] = []
post_attachments = get_post_attachments(message_json)
if post_attachments:
descriptions = ''
for attach in post_attachments:
if not isinstance(attach, dict):
continue
if not attach.get('name'):
continue
descriptions += attach['name'] + ' '
if attach.get('url'):
links.append('`[' + attach['name'] + '`' +
get_url_from_post(attach['url']) + ']')
# add links to the end of the content
if links:
content_text += '\n\n-\n'
for link_str in links:
content_text += link_str + '\n'
try:
with open(micron_blog_filename, 'w+',
encoding='utf-8') as fp_micron:
fp_micron.write(published + '\n\n' + content_text)
except OSError:
print('EX: blog_to_micron unable to write ' + micron_blog_filename)
return False
return True

View File

@ -72,6 +72,7 @@ from reading import has_edition_tag
from inbox_receive import inbox_update_index
from gemini import blog_to_gemini
from markdown import blog_to_markdown
from markdown import blog_to_micron
def _localonly_not_local(message_json: {}, domain_full: str) -> bool:
@ -572,6 +573,9 @@ def post_message_to_outbox(session, translate: {},
# export blog post in markdown format
blog_to_markdown(base_dir, post_to_nickname, domain,
message_json, system_language, debug, False)
# export blog post in micron format
blog_to_micron(base_dir, post_to_nickname, domain,
message_json, system_language, debug, False)
# update the speaker endpoint for speech synthesis
actor_url = get_actor_from_post(message_json)

View File

@ -205,6 +205,7 @@ from webapp_post import prepare_html_post_nickname
from speaker import speaker_replace_links
from markdown import markdown_to_html
from markdown import blog_to_markdown
from markdown import blog_to_micron
from languages import get_reply_language
from languages import set_actor_languages
from languages import get_actor_languages
@ -9614,6 +9615,36 @@ def _test_markdown_blog(base_dir: str) -> None:
shutil.rmtree(markdown_blog_dir, ignore_errors=True)
def _test_micron_blog(base_dir: str) -> None:
print('micron_blog')
micron_blog_dir = base_dir + '/microntest'
published = '2022-02-25T20:15:00Z'
title = 'Micron test title'
content = 'This is a micron test'
link = 'https://some.link'
micron_blog_filename = \
micron_blog_dir + '/2022-02-25_' + \
title.replace(' ', '_').lower() + '.mu'
system_language = 'en'
debug = True
message_json = {
'object': {
'published': published,
'summary': title,
'content': content + ' ' + link
}
}
result = blog_to_micron(base_dir, 'someuser', 'somedomain',
message_json, system_language,
debug, True)
assert result
assert os.path.isdir(micron_blog_dir)
assert os.path.isfile(micron_blog_filename)
assert text_in_file(content, micron_blog_filename)
assert text_in_file('2022-02-25', micron_blog_filename)
shutil.rmtree(micron_blog_dir, ignore_errors=True)
def _test_replace_gemini_links() -> None:
print('replace_gemini_links')
content = 'Some content'
@ -9685,6 +9716,7 @@ def run_all_tests():
_test_checkbox_names()
_test_thread_functions()
_test_functions()
_test_micron_blog(base_dir)
_test_yggdrasil_addresses()
_test_replace_gemini_links()
_test_markdown_blog(base_dir)

View File

@ -2325,6 +2325,31 @@ def get_markdown_blog_filename(base_dir: str, nickname: str, domain: str,
return markdown_blog_filename
def get_micron_blog_filename(base_dir: str, nickname: str, domain: str,
message_json: dict, system_language: str,
debug: bool, testing: bool) -> str:
"""Returns the filename for a micron blog post
"""
title_text = get_gemini_blog_title(message_json, system_language)
published = get_gemini_blog_published(message_json, debug)
if not published:
return ''
title_text2 = title_text.replace('.', ' ')
title_text2 = title_text2.replace(' ', '_')
title_text2 = title_text2.replace('"', '')
if not testing:
account_dir = acct_dir(base_dir, nickname, domain)
micron_blog_dir = account_dir + '/micron'
else:
account_dir = base_dir
micron_blog_dir = account_dir + '/microntest'
micron_blog_filename = \
micron_blog_dir + '/' + published + '_' + title_text2.lower() + '.mu'
return micron_blog_filename
def delete_post(base_dir: str, http_prefix: str,
nickname: str, domain: str, post_filename: str,
debug: bool, recent_posts_cache: {},
@ -2400,6 +2425,21 @@ def delete_post(base_dir: str, http_prefix: str,
print('EX: delete_post unable to delete markdown post ' +
str(markdown_blog_filename))
# delete micron blog post
micron_blog_filename = \
get_micron_blog_filename(base_dir, nickname, domain,
post_json_object, '',
debug, False)
if micron_blog_filename:
if os.path.isfile(micron_blog_filename):
try:
os.remove(micron_blog_filename)
return True
except OSError:
if debug:
print('EX: delete_post unable to delete micron post ' +
str(micron_blog_filename))
# remove from recent posts cache in memory
remove_post_from_cache(post_json_object, recent_posts_cache)