From dace014779f8154c30c97a9ba57d2d8caae0e7d5 Mon Sep 17 00:00:00 2001 From: sneakers-the-rat Date: Mon, 16 Oct 2023 16:24:30 -0700 Subject: [PATCH] v0.1.4 - overwrite correctly --- docs/changelog.md | 6 +++- pyproject.toml | 2 +- wiki_postbot/interfaces/mediawiki.py | 41 +++++++++++++++++----------- 3 files changed, 31 insertions(+), 18 deletions(-) diff --git a/docs/changelog.md b/docs/changelog.md index f118eb2..c7eebc6 100644 --- a/docs/changelog.md +++ b/docs/changelog.md @@ -1,3 +1,7 @@ # 0.1.3 -- Add overwrite option to append_text in wiki interface \ No newline at end of file +- Add overwrite option to append_text in wiki interface + +# 0.1.4 + +- Like 0.1.3 but actually doing it right \ No newline at end of file diff --git a/pyproject.toml b/pyproject.toml index a40e8ef..df643d6 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "wiki-postbot" -version = "0.1.3" +version = "0.1.4" description = "Add posts to the wiki!" authors = ["sneakers-the-rat "] license = "GPL-3.0" diff --git a/wiki_postbot/interfaces/mediawiki.py b/wiki_postbot/interfaces/mediawiki.py index 751acbf..b276eba 100644 --- a/wiki_postbot/interfaces/mediawiki.py +++ b/wiki_postbot/interfaces/mediawiki.py @@ -87,10 +87,6 @@ class Wiki: return WikiPage.from_source(title=content['parse']['title'], source=content['parse']['wikitext']) def insert_text(self, page, section, text, overwrite:bool=False): - if overwrite: - operation_key = 'text' - else: - operation_key = 'appendtext' # TODO: Move finding section IDs into the page class! page_text = self.get_page(page) @@ -117,17 +113,30 @@ class Wiki: if matching_section >= 0: print(f'found matching section {matching_section}') - result = self.sess.post( - self.api_url, - data={ - "action":"edit", - "title":page, - "section":str(matching_section), - operation_key:text, - "format":"json", - "token":token - } - ) + if overwrite: + page_text.content.sections[matching_section].contents = text + result = self.sess.post( + self.api_url, + data={ + "action":"edit", + "title":page, + "text": page_text.content.string, + "format":"json", + "token":token + } + ) + else: + result = self.sess.post( + self.api_url, + data={ + "action":"edit", + "title":page, + "section":str(matching_section), + "appendtext":text, + "format":"json", + "token":token + } + ) else: self.logger.debug('making new section') result = self.sess.post( @@ -137,7 +146,7 @@ class Wiki: "title":page, "section":"new", "sectiontitle":section, - operation_key:text, + "appendtext":text, "format":"json", "token":token }