about summary refs log tree commit diff
diff options
context:
space:
mode:
authorNguyễn Gia Phong <mcsinyx@disroot.org>2022-08-04 17:01:43 +0900
committerNguyễn Gia Phong <mcsinyx@disroot.org>2022-08-04 17:26:48 +0900
commit978611599094c21d4b2865f417479795608c4a83 (patch)
treed14a7cd55011e85fa944c9bc0ca92522ab194629
parent9162549a27b02f5f9ce9adea3b957618db460779 (diff)
downloadrsskey-main.tar.gz
Truncate long titles HEAD 0.2.0 main
Introduce option 'cw' for this and rename 'limit' to 'text' for clarity.
-rw-r--r--README.md6
-rw-r--r--pyproject.toml2
-rwxr-xr-xsrc/rsskey.py11
3 files changed, 14 insertions, 5 deletions
diff --git a/README.md b/README.md
index c463fcf..86b9a66 100644
--- a/README.md
+++ b/README.md
@@ -24,8 +24,10 @@ declare the mirroring jobs in `jobs.conf`, for example:
 source = https://stallman.org/rss/rss.xml
 ; URL to destination Misskey instance's API
 dest = https://birb.space/api
-; Character limit of the Misskey instance
-limit = 420
+; Note character limit of the Misskey instance
+text = 420
+; Content warning character limit of the Misskey instance
+cw = 69
 ; Misskey user ID for searching previous notes
 user = 8rt4sahf1j
 ; Access token with permission to compose notes
diff --git a/pyproject.toml b/pyproject.toml
index 963225a..ce6b419 100644
--- a/pyproject.toml
+++ b/pyproject.toml
@@ -4,7 +4,7 @@ build-backend = "flit_core.buildapi"
 
 [project]
 name = "rsskey"
-version = "0.1.0"
+version = "0.2.0"
 description = "RSS feed mirror on Misskey"
 readme = "README.md"
 requires-python = ">=3.7"
diff --git a/src/rsskey.py b/src/rsskey.py
index c8acf53..91bf789 100755
--- a/src/rsskey.py
+++ b/src/rsskey.py
@@ -27,6 +27,12 @@ from markdownify import markdownify as md
 from trio import open_nursery, run
 
 
+def truncate(string, length, end='…'):
+    """Return string truncated to length, ending with given character."""
+    if len(string) <= length: return string
+    return string[:length-len(end)] + end
+
+
 async def create(client, **note):
     """Create the given note and return its ID."""
     response = await client.post('notes/create', json=note)
@@ -42,12 +48,13 @@ async def post(job, client, link, title, summary):
                                                      'userId': job['user']})
     if search.json(): return
 
-    note = partial(create, client, i=job['token'], visibility='home', cw=title)
+    note = partial(create, client, i=job['token'], visibility='home',
+                   cw=truncate(title, job.getint('cw')))
     original = f'Original: {link}'
     rest = '\n\n'.join((*map(partial(sub, r'\s+', ' '),
                              split(r'\s*\n{2,}\s*', md(summary).strip())),
                         original))
-    limit = int(job['limit'])
+    limit = job.getint('text')
     parent = None
 
     while len(rest) > limit: