tumblrbot 1.4.6__tar.gz → 1.4.7__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,5 +1,4 @@
1
1
  # Custom
2
- .vscode
3
2
  data
4
3
  *.toml
5
4
  *.jsonl
@@ -192,7 +191,7 @@ cython_debug/
192
191
  # that can be found at https://github.com/github/gitignore/blob/main/Global/VisualStudioCode.gitignore
193
192
  # and can be added to the global gitignore or merged into this file. However, if you prefer,
194
193
  # you could uncomment the following to ignore the entire vscode folder
195
- # .vscode/
194
+ .vscode/
196
195
 
197
196
  # Ruff stuff:
198
197
  .ruff_cache/
@@ -200,13 +199,6 @@ cython_debug/
200
199
  # PyPI configuration file
201
200
  .pypirc
202
201
 
203
- # Cursor
204
- # Cursor is an AI-powered code editor. `.cursorignore` specifies files/directories to
205
- # exclude from AI features like autocomplete and code analysis. Recommended for sensitive data
206
- # refer to https://docs.cursor.com/context/ignore-files
207
- .cursorignore
208
- .cursorindexingignore
209
-
210
202
  # Marimo
211
203
  marimo/_static/
212
204
  marimo/_lsp/
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: tumblrbot
3
- Version: 1.4.6
3
+ Version: 1.4.7
4
4
  Summary: An updated bot that posts to Tumblr, based on your very own blog!
5
5
  Requires-Python: >= 3.13
6
6
  Description-Content-Type: text/markdown
@@ -8,13 +8,12 @@ Requires-Dist: keyring
8
8
  Requires-Dist: openai
9
9
  Requires-Dist: pwinput
10
10
  Requires-Dist: pydantic
11
- Requires-Dist: pydantic-settings
12
11
  Requires-Dist: requests
13
12
  Requires-Dist: requests-oauthlib
14
13
  Requires-Dist: rich
15
14
  Requires-Dist: tiktoken
16
15
  Requires-Dist: tomlkit
17
- Project-URL: Source, https://github.com/MaidThatPrograms/tumblrbot
16
+ Project-URL: Source, https://github.com/MaidScientistIzutsumiMarin/tumblrbot
18
17
 
19
18
  # tumblrbot
20
19
 
@@ -57,7 +56,6 @@ Features:
57
56
  - An [interactive console][Main] for all steps of generating posts for the blog:
58
57
  1. Asks for [OpenAI] and [Tumblr] tokens.
59
58
  - Stores API tokens using [keyring].
60
- - Prevents API tokens from printing to the console.
61
59
  1. Retrieves [Tumblr] [OAuth] tokens.
62
60
  1. [Downloads posts][Download] from the [configured][config] [Tumblr] blogs.
63
61
  - Skips redownloading already downloaded posts.
@@ -67,8 +65,8 @@ Features:
67
65
  - Filters out any posts flagged by the [OpenAI] [Moderation API] (optional).
68
66
  - Shows progress and previews the current post.
69
67
  - Adds custom user messages and assistant responses to the dataset from the [configured][config] file.
70
- 1. Provides cost estimates if the currently saved examples are used to fine-tune the [configured][config] model.
71
68
  1. [Uploads examples][Fine-Tune] to [OpenAI] and begins the fine-tuning process.
69
+ - Provides cost estimates if the currently saved examples are used to fine-tune the [configured][config] model.
72
70
  - Resumes monitoring the same fine-tuning process when restarted.
73
71
  - Deletes the uploaded examples file if fine-tuning does not succeed (optional).
74
72
  - Stores the output model automatically when fine-tuning is completed.
@@ -39,7 +39,6 @@ Features:
39
39
  - An [interactive console][Main] for all steps of generating posts for the blog:
40
40
  1. Asks for [OpenAI] and [Tumblr] tokens.
41
41
  - Stores API tokens using [keyring].
42
- - Prevents API tokens from printing to the console.
43
42
  1. Retrieves [Tumblr] [OAuth] tokens.
44
43
  1. [Downloads posts][Download] from the [configured][config] [Tumblr] blogs.
45
44
  - Skips redownloading already downloaded posts.
@@ -49,8 +48,8 @@ Features:
49
48
  - Filters out any posts flagged by the [OpenAI] [Moderation API] (optional).
50
49
  - Shows progress and previews the current post.
51
50
  - Adds custom user messages and assistant responses to the dataset from the [configured][config] file.
52
- 1. Provides cost estimates if the currently saved examples are used to fine-tune the [configured][config] model.
53
51
  1. [Uploads examples][Fine-Tune] to [OpenAI] and begins the fine-tuning process.
52
+ - Provides cost estimates if the currently saved examples are used to fine-tune the [configured][config] model.
54
53
  - Resumes monitoring the same fine-tuning process when restarted.
55
54
  - Deletes the uploaded examples file if fine-tuning does not succeed (optional).
56
55
  - Stores the output model automatically when fine-tuning is completed.
@@ -1,6 +1,6 @@
1
1
  [project]
2
2
  name = "tumblrbot"
3
- version = "1.4.6"
3
+ version = "1.4.7"
4
4
  description = "An updated bot that posts to Tumblr, based on your very own blog!"
5
5
  readme = "README.md"
6
6
  requires-python = ">= 3.13"
@@ -9,7 +9,6 @@ dependencies = [
9
9
  "openai",
10
10
  "pwinput",
11
11
  "pydantic",
12
- "pydantic-settings",
13
12
  "requests",
14
13
  "requests-oauthlib",
15
14
  "rich",
@@ -18,7 +17,7 @@ dependencies = [
18
17
  ]
19
18
 
20
19
  [project.urls]
21
- Source = "https://github.com/MaidThatPrograms/tumblrbot"
20
+ Source = "https://github.com/MaidScientistIzutsumiMarin/tumblrbot"
22
21
 
23
22
  [project.scripts]
24
23
  tumblrbot = "tumblrbot.__main__:main"
@@ -14,7 +14,7 @@ from tumblrbot.utils.tumblr import TumblrSession
14
14
  def main() -> None:
15
15
  install()
16
16
 
17
- tokens = Tokens.read_from_keyring()
17
+ tokens = Tokens.load()
18
18
  with OpenAI(api_key=tokens.openai_api_key) as openai, TumblrSession(tokens) as tumblr:
19
19
  if Confirm.ask("Download latest posts?", default=False):
20
20
  PostDownloader(openai=openai, tumblr=tumblr).main()
@@ -18,7 +18,7 @@ class PostDownloader(FlowClass):
18
18
  completed = 0
19
19
  after = 0
20
20
  if data_path.exists():
21
- lines = data_path.read_text("utf_8").splitlines() if data_path.exists() else []
21
+ lines = data_path.read_bytes().splitlines() if data_path.exists() else []
22
22
  completed = len(lines)
23
23
  if lines:
24
24
  after = Post.model_validate_json(lines[-1]).timestamp
@@ -49,7 +49,7 @@ class ExamplesWriter(FlowClass):
49
49
  self.config.custom_prompts_file.parent.mkdir(parents=True, exist_ok=True)
50
50
  self.config.custom_prompts_file.touch(exist_ok=True)
51
51
 
52
- with self.config.custom_prompts_file.open("r", encoding="utf_8") as fp:
52
+ with self.config.custom_prompts_file.open("rb") as fp:
53
53
  for line in fp:
54
54
  data: dict[str, str] = loads(line)
55
55
  yield from data.items()
@@ -81,7 +81,7 @@ class ExamplesWriter(FlowClass):
81
81
 
82
82
  def get_valid_posts(self) -> Generator[Post]:
83
83
  for data_path in self.get_data_paths():
84
- with data_path.open(encoding="utf_8") as fp:
84
+ with data_path.open("rb") as fp:
85
85
  for line in fp:
86
86
  post = Post.model_validate_json(line)
87
87
  if post.valid_text_post():
@@ -98,8 +98,8 @@ class FineTuner(FlowClass):
98
98
  if job.status == "failed" and job.error is not None:
99
99
  raise RuntimeError(job.error.message)
100
100
 
101
- if job.fine_tuned_model:
102
- self.config.fine_tuned_model = job.fine_tuned_model or ""
101
+ if job.fine_tuned_model is not None:
102
+ self.config.fine_tuned_model = job.fine_tuned_model
103
103
 
104
104
  def print_estimates(self) -> None:
105
105
  estimated_tokens = sum(self.count_tokens())
@@ -1,4 +1,5 @@
1
1
  from abc import abstractmethod
2
+ from pathlib import Path
2
3
  from random import choice
3
4
  from typing import ClassVar, Self, override
4
5
 
@@ -10,15 +11,14 @@ from rich.live import Live
10
11
  from rich.progress import MofNCompleteColumn, Progress, SpinnerColumn, TimeElapsedColumn
11
12
  from rich.table import Table
12
13
 
13
- from tumblrbot.utils.config import Config, Path
14
- from tumblrbot.utils.models import FullyValidatedModel
14
+ from tumblrbot.utils.models import Config, FullyValidatedModel
15
15
  from tumblrbot.utils.tumblr import TumblrSession
16
16
 
17
17
 
18
18
  class FlowClass(FullyValidatedModel):
19
19
  model_config = ConfigDict(arbitrary_types_allowed=True)
20
20
 
21
- config: ClassVar = Config() # pyright: ignore[reportCallIssue]
21
+ config: ClassVar = Config.load()
22
22
 
23
23
  openai: OpenAI
24
24
  tumblr: TumblrSession
@@ -0,0 +1,209 @@
1
+ import tomllib
2
+ from abc import abstractmethod
3
+ from collections.abc import Generator
4
+ from pathlib import Path
5
+ from typing import Annotated, Any, ClassVar, Literal, Self, override
6
+
7
+ import rich
8
+ import tomlkit
9
+ from keyring import get_password, set_password
10
+ from openai.types import ChatModel
11
+ from pwinput import pwinput
12
+ from pydantic import BaseModel, ConfigDict, Field, NonNegativeFloat, PlainSerializer, PositiveFloat, PositiveInt, model_validator
13
+ from pydantic.json_schema import SkipJsonSchema
14
+ from requests_oauthlib import OAuth1Session
15
+ from rich.panel import Panel
16
+ from rich.prompt import Confirm, Prompt
17
+ from tomlkit import comment, document
18
+
19
+
20
+ class FullyValidatedModel(BaseModel):
21
+ model_config = ConfigDict(
22
+ extra="ignore",
23
+ validate_assignment=True,
24
+ validate_default=True,
25
+ validate_return=True,
26
+ validate_by_name=True,
27
+ )
28
+
29
+
30
+ class FileSyncSettings(FullyValidatedModel):
31
+ @classmethod
32
+ @abstractmethod
33
+ def read(cls) -> Self | dict[str, object] | str | None: ...
34
+
35
+ @classmethod
36
+ def load(cls) -> Self:
37
+ data = cls.read() or {}
38
+ return cls.model_validate_json(data) if isinstance(data, str) else cls.model_validate(data)
39
+
40
+ @model_validator(mode="after")
41
+ @abstractmethod
42
+ def write(self) -> Self: ...
43
+
44
+
45
+ class Config(FileSyncSettings):
46
+ toml_file: ClassVar = Path("config.toml")
47
+
48
+ # Downloading Posts & Writing Examples
49
+ download_blog_identifiers: list[str] = Field([], description="The identifiers of the blogs which post data will be downloaded from. These must be blogs associated with the same account as the configured Tumblr secret tokens.")
50
+ data_directory: Path = Field(Path("data"), description="Where to store downloaded post data.")
51
+
52
+ # Writing Examples
53
+ max_moderation_batch_size: PositiveInt = Field(100, description="How many posts, at most, to submit to the OpenAI moderation API. This is also capped by the API.")
54
+ custom_prompts_file: Path = Field(Path("custom_prompts.jsonl"), description="Where to read in custom prompts from.")
55
+
56
+ # Writing Examples & Fine-Tuning
57
+ examples_file: Path = Field(Path("examples.jsonl"), description="Where to output the examples that will be used to fine-tune the model.")
58
+
59
+ # Writing Examples & Generating
60
+ developer_message: str = Field("You are a Tumblr post bot. Please generate a Tumblr post in accordance with the user's request.", description="The developer message used by the OpenAI API to generate drafts.")
61
+ user_message: str = Field("Please write a comical Tumblr post.", description="The user input used by the OpenAI API to generate drafts.")
62
+
63
+ # Fine-Tuning
64
+ expected_epochs: PositiveInt = Field(3, description="The expected number of epochs fine-tuning will be run for. This will be updated during fine-tuning.")
65
+ token_price: PositiveFloat = Field(3, description="The expected price in USD per million tokens during fine-tuning for the current model.")
66
+ job_id: str = Field("", description="The fine-tuning job ID that will be polled on next run.")
67
+
68
+ # Fine-Tuning & Generating
69
+ base_model: ChatModel = Field("gpt-4o-mini-2024-07-18", description="The name of the model that will be fine-tuned by the generated training data.")
70
+ fine_tuned_model: str = Field("", description="The name of the OpenAI model that was fine-tuned with your posts.")
71
+
72
+ # Generating
73
+ upload_blog_identifier: str = Field("", description="The identifier of the blog which generated drafts will be uploaded to. This must be a blog associated with the same account as the configured Tumblr secret tokens.")
74
+ draft_count: PositiveInt = Field(150, description="The number of drafts to process. This will affect the number of tokens used with OpenAI")
75
+ tags_chance: NonNegativeFloat = Field(0.1, description="The chance to generate tags for any given post. This will incur extra calls to OpenAI.")
76
+ tags_developer_message: str = Field("You will be provided with a block of text, and your task is to extract a very short list of the most important subjects from it.", description="The developer message used to generate tags.")
77
+
78
+ @classmethod
79
+ @override
80
+ def read(cls) -> dict[str, object] | None:
81
+ return tomllib.loads(cls.toml_file.read_text("utf_8")) if cls.toml_file.exists() else None
82
+
83
+ @model_validator(mode="after")
84
+ @override
85
+ def write(self) -> Self:
86
+ if not self.download_blog_identifiers:
87
+ rich.print("Enter the [cyan]identifiers of your blogs[/] that data should be [bold purple]downloaded[/] from, separated by commas.")
88
+ self.download_blog_identifiers = list(map(str.strip, Prompt.ask("[bold][Example] [dim]staff.tumblr.com,changes").split(",")))
89
+
90
+ if not self.upload_blog_identifier:
91
+ rich.print("Enter the [cyan]identifier of your blog[/] that drafts should be [bold purple]uploaded[/] to.")
92
+ self.upload_blog_identifier = Prompt.ask("[bold][Example] [dim]staff.tumblr.com or changes").strip()
93
+
94
+ toml_table = document()
95
+
96
+ for (name, field), value in zip(self.__class__.model_fields.items(), self.model_dump(mode="json").values(), strict=True):
97
+ if field.description is not None:
98
+ for line in field.description.split(". "):
99
+ toml_table.add(comment(f"{line.removesuffix('.')}."))
100
+
101
+ toml_table[name] = value
102
+
103
+ Path(self.toml_file).write_text(tomlkit.dumps(toml_table), encoding="utf_8")
104
+
105
+ return self
106
+
107
+
108
+ class Tokens(FileSyncSettings):
109
+ class Tumblr(FullyValidatedModel):
110
+ client_key: str = ""
111
+ client_secret: str = ""
112
+ resource_owner_key: str = ""
113
+ resource_owner_secret: str = ""
114
+
115
+ service_name: ClassVar = "tumblrbot"
116
+ username: ClassVar = "tokens"
117
+
118
+ openai_api_key: str = ""
119
+ tumblr: Tumblr = Tumblr()
120
+
121
+ @staticmethod
122
+ def get_oauth_tokens(token: dict[str, str]) -> tuple[str, str]:
123
+ return token["oauth_token"], token["oauth_token_secret"]
124
+
125
+ @staticmethod
126
+ def online_token_prompt(url: str, *tokens: str) -> Generator[str]:
127
+ formatted_token_string = " and ".join(f"[cyan]{token}[/]" for token in tokens)
128
+
129
+ rich.print(f"Retrieve your {formatted_token_string} from: {url}")
130
+ for token in tokens:
131
+ yield pwinput(f"Enter your {token} (masked): ").strip()
132
+
133
+ rich.print()
134
+
135
+ @classmethod
136
+ @override
137
+ def read(cls) -> str | None:
138
+ return get_password(cls.service_name, cls.username)
139
+
140
+ @model_validator(mode="after")
141
+ @override
142
+ def write(self) -> Self:
143
+ if not self.openai_api_key or Confirm.ask("Reset OpenAI API key?", default=False):
144
+ (self.openai_api_key,) = self.online_token_prompt("https://platform.openai.com/api-keys", "API key")
145
+
146
+ if not all(self.tumblr.model_dump().values()) or Confirm.ask("Reset Tumblr API tokens?", default=False):
147
+ self.tumblr.client_key, self.tumblr.client_secret = self.online_token_prompt("https://tumblr.com/oauth/apps", "consumer key", "consumer secret")
148
+
149
+ with OAuth1Session(
150
+ self.tumblr.client_key,
151
+ self.tumblr.client_secret,
152
+ ) as oauth_session:
153
+ fetch_response = oauth_session.fetch_request_token("http://tumblr.com/oauth/request_token")
154
+ full_authorize_url = oauth_session.authorization_url("http://tumblr.com/oauth/authorize")
155
+ (redirect_response,) = self.online_token_prompt(full_authorize_url, "full redirect URL")
156
+ oauth_response = oauth_session.parse_authorization_response(redirect_response)
157
+
158
+ with OAuth1Session(
159
+ self.tumblr.client_key,
160
+ self.tumblr.client_secret,
161
+ *self.get_oauth_tokens(fetch_response),
162
+ verifier=oauth_response["oauth_verifier"],
163
+ ) as oauth_session:
164
+ oauth_tokens = oauth_session.fetch_access_token("http://tumblr.com/oauth/access_token")
165
+
166
+ self.tumblr.resource_owner_key, self.tumblr.resource_owner_secret = self.get_oauth_tokens(oauth_tokens)
167
+
168
+ set_password(self.service_name, self.username, self.model_dump_json())
169
+
170
+ return self
171
+
172
+
173
+ class Post(FullyValidatedModel):
174
+ class Block(FullyValidatedModel):
175
+ type: str = "text"
176
+ text: str = ""
177
+ blocks: list[int] = []
178
+
179
+ timestamp: SkipJsonSchema[int] = 0
180
+ tags: Annotated[list[str], PlainSerializer(",".join)] = []
181
+ state: SkipJsonSchema[Literal["published", "queued", "draft", "private", "unapproved"]] = "draft"
182
+
183
+ content: SkipJsonSchema[list[Block]] = []
184
+ layout: SkipJsonSchema[list[Block]] = []
185
+ trail: SkipJsonSchema[list[Any]] = []
186
+
187
+ is_submission: SkipJsonSchema[bool] = False
188
+
189
+ def __rich__(self) -> Panel:
190
+ return Panel(
191
+ self.get_content_text(),
192
+ title="Preview",
193
+ subtitle=" ".join(f"#{tag}" for tag in self.tags),
194
+ subtitle_align="left",
195
+ )
196
+
197
+ def valid_text_post(self) -> bool:
198
+ return bool(self.content) and all(block.type == "text" for block in self.content) and not (self.is_submission or self.trail or any(block.type == "ask" for block in self.layout))
199
+
200
+ def get_content_text(self) -> str:
201
+ return "\n\n".join(block.text for block in self.content)
202
+
203
+
204
+ class Example(FullyValidatedModel):
205
+ class Message(FullyValidatedModel):
206
+ role: Literal["developer", "user", "assistant"]
207
+ content: str
208
+
209
+ messages: list[Message]
@@ -19,8 +19,7 @@ class TumblrSession(OAuth1Session):
19
19
  try:
20
20
  response.raise_for_status()
21
21
  except HTTPError as error:
22
- if response.text:
23
- error.add_note(response.text)
22
+ error.add_note(response.text)
24
23
  raise
25
24
 
26
25
  def retrieve_published_posts(self, blog_identifier: str, after: int) -> Response:
@@ -1,97 +0,0 @@
1
- from collections.abc import Sequence
2
- from pathlib import Path
3
- from typing import TYPE_CHECKING, Self, override
4
-
5
- import rich
6
- import tomlkit
7
- from openai.types import ChatModel
8
- from pydantic import Field, NonNegativeFloat, PositiveFloat, PositiveInt, model_validator
9
- from pydantic_settings import BaseSettings, PydanticBaseSettingsSource, SettingsConfigDict, TomlConfigSettingsSource
10
- from rich.prompt import Prompt
11
- from tomlkit import comment, document
12
-
13
- if TYPE_CHECKING:
14
- from _typeshed import StrPath
15
-
16
-
17
- class Config(BaseSettings):
18
- model_config = SettingsConfigDict(
19
- extra="ignore",
20
- validate_assignment=True,
21
- validate_return=True,
22
- validate_by_name=True,
23
- cli_parse_args=True,
24
- cli_avoid_json=True,
25
- cli_kebab_case=True,
26
- toml_file="config.toml",
27
- )
28
-
29
- # Downloading Posts & Writing Examples
30
- download_blog_identifiers: list[str] = Field([], description="The identifiers of the blogs which post data will be downloaded from. These must be blogs associated with the same account as the configured Tumblr secret tokens.")
31
- data_directory: Path = Field(Path("data"), description="Where to store downloaded post data.")
32
-
33
- # Writing Examples
34
- max_moderation_batch_size: PositiveInt = Field(100, description="How many posts, at most, to submit to the OpenAI moderation API. This is also capped by the API.")
35
- custom_prompts_file: Path = Field(Path("custom_prompts.jsonl"), description="Where to read in custom prompts from.")
36
-
37
- # Writing Examples & Fine-Tuning
38
- examples_file: Path = Field(Path("examples.jsonl"), description="Where to output the examples that will be used to fine-tune the model.")
39
-
40
- # Writing Examples & Generating
41
- developer_message: str = Field("You are a Tumblr post bot. Please generate a Tumblr post in accordance with the user's request.", description="The developer message used by the OpenAI API to generate drafts.")
42
- user_message: str = Field("Please write a comical Tumblr post.", description="The user input used by the OpenAI API to generate drafts.")
43
-
44
- # Fine-Tuning
45
- expected_epochs: PositiveInt = Field(3, description="The expected number of epochs fine-tuning will be run for. This will be updated during fine-tuning.")
46
- token_price: PositiveFloat = Field(3, description="The expected price in USD per million tokens during fine-tuning for the current model.")
47
- job_id: str = Field("", description="The fine-tuning job ID that will be polled on next run.")
48
-
49
- # Fine-Tuning & Generating
50
- base_model: ChatModel = Field("gpt-4o-mini-2024-07-18", description="The name of the model that will be fine-tuned by the generated training data.")
51
- fine_tuned_model: str = Field("", description="The name of the OpenAI model that was fine-tuned with your posts.")
52
-
53
- # Generating
54
- upload_blog_identifier: str = Field("", description="The identifier of the blog which generated drafts will be uploaded to. This must be a blog associated with the same account as the configured Tumblr secret tokens.")
55
- draft_count: PositiveInt = Field(150, description="The number of drafts to process. This will affect the number of tokens used with OpenAI")
56
- tags_chance: NonNegativeFloat = Field(0.1, description="The chance to generate tags for any given post. This will incur extra calls to OpenAI.")
57
- tags_developer_message: str = Field("You will be provided with a block of text, and your task is to extract a very short list of the most important subjects from it.", description="The developer message used to generate tags.")
58
-
59
- @override
60
- @classmethod
61
- def settings_customise_sources(cls, settings_cls: type[BaseSettings], *args: PydanticBaseSettingsSource, **kwargs: PydanticBaseSettingsSource) -> tuple[PydanticBaseSettingsSource, ...]:
62
- return (TomlConfigSettingsSource(settings_cls),)
63
-
64
- @model_validator(mode="after")
65
- def write_to_file(self) -> Self:
66
- if not self.download_blog_identifiers:
67
- rich.print("Enter the [cyan]identifiers of your blogs[/] that data should be [bold purple]downloaded[/] from, separated by commas.")
68
- self.download_blog_identifiers = list(map(str.strip, Prompt.ask("[bold][Example] [dim]staff.tumblr.com,changes").split(",")))
69
-
70
- if not self.upload_blog_identifier:
71
- rich.print("Enter the [cyan]identifier of your blog[/] that drafts should be [bold purple]uploaded[/] to.")
72
- self.upload_blog_identifier = Prompt.ask("[bold][Example] [dim]staff.tumblr.com or changes").strip()
73
-
74
- toml_files = self.model_config.get("toml_file")
75
- if isinstance(toml_files, (Path, str)):
76
- self.dump_toml(toml_files)
77
- elif isinstance(toml_files, Sequence):
78
- for toml_file in toml_files:
79
- self.dump_toml(toml_file)
80
-
81
- return self
82
-
83
- def dump_toml(self, toml_file: "StrPath") -> None:
84
- toml_table = document()
85
-
86
- dumped_model = self.model_dump(mode="json")
87
- for name, field in self.__class__.model_fields.items():
88
- if field.description:
89
- for line in field.description.split(". "):
90
- toml_table.add(comment(f"{line.removesuffix('.')}."))
91
-
92
- toml_table[name] = dumped_model[name]
93
-
94
- Path(toml_file).write_text(
95
- tomlkit.dumps(toml_table),
96
- encoding="utf_8",
97
- )
@@ -1,126 +0,0 @@
1
- from collections.abc import Generator
2
- from typing import Annotated, Any, ClassVar, Literal, Self, override
3
-
4
- import rich
5
- from keyring import get_password, set_password
6
- from openai import BaseModel
7
- from pwinput import pwinput
8
- from pydantic import ConfigDict, PlainSerializer
9
- from pydantic.json_schema import SkipJsonSchema
10
- from requests_oauthlib import OAuth1Session
11
- from rich.panel import Panel
12
- from rich.prompt import Confirm
13
-
14
-
15
- class FullyValidatedModel(BaseModel):
16
- model_config = ConfigDict(
17
- extra="ignore",
18
- validate_assignment=True,
19
- validate_default=True,
20
- validate_return=True,
21
- validate_by_name=True,
22
- )
23
-
24
-
25
- class Tokens(FullyValidatedModel):
26
- class Tumblr(FullyValidatedModel):
27
- client_key: str = ""
28
- client_secret: str = ""
29
- resource_owner_key: str = ""
30
- resource_owner_secret: str = ""
31
-
32
- service_name: ClassVar = "tumblrbot"
33
- username: ClassVar = "tokens"
34
-
35
- openai_api_key: str = ""
36
- tumblr: Tumblr = Tumblr()
37
-
38
- @staticmethod
39
- def get_oauth_tokens(token: dict[str, str]) -> tuple[str, str]:
40
- return token["oauth_token"], token["oauth_token_secret"]
41
-
42
- @staticmethod
43
- def online_token_prompt(url: str, *tokens: str) -> Generator[str]:
44
- formatted_token_string = " and ".join(f"[cyan]{token}[/]" for token in tokens)
45
-
46
- rich.print(f"Retrieve your {formatted_token_string} from: {url}")
47
- for token in tokens:
48
- yield pwinput(f"Enter your {token} (masked): ").strip()
49
-
50
- rich.print()
51
-
52
- @classmethod
53
- def read_from_keyring(cls) -> Self:
54
- if json_data := get_password(cls.service_name, cls.username):
55
- return cls.model_validate_json(json_data)
56
- return cls()
57
-
58
- @override
59
- def model_post_init(self, context: object) -> None:
60
- super().model_post_init(context)
61
-
62
- if not self.openai_api_key or Confirm.ask("Reset OpenAI API key?", default=False):
63
- (self.openai_api_key,) = self.online_token_prompt("https://platform.openai.com/api-keys", "API key")
64
-
65
- if not all(self.tumblr.model_dump().values()) or Confirm.ask("Reset Tumblr API tokens?", default=False):
66
- self.tumblr.client_key, self.tumblr.client_secret = self.online_token_prompt("https://tumblr.com/oauth/apps", "consumer key", "consumer secret")
67
-
68
- with OAuth1Session(
69
- self.tumblr.client_key,
70
- self.tumblr.client_secret,
71
- ) as oauth_session:
72
- fetch_response = oauth_session.fetch_request_token("http://tumblr.com/oauth/request_token")
73
- full_authorize_url = oauth_session.authorization_url("http://tumblr.com/oauth/authorize")
74
- (redirect_response,) = self.online_token_prompt(full_authorize_url, "full redirect URL")
75
- oauth_response = oauth_session.parse_authorization_response(redirect_response)
76
-
77
- with OAuth1Session(
78
- self.tumblr.client_key,
79
- self.tumblr.client_secret,
80
- *self.get_oauth_tokens(fetch_response),
81
- verifier=oauth_response["oauth_verifier"],
82
- ) as oauth_session:
83
- oauth_tokens = oauth_session.fetch_access_token("http://tumblr.com/oauth/access_token")
84
-
85
- self.tumblr.resource_owner_key, self.tumblr.resource_owner_secret = self.get_oauth_tokens(oauth_tokens)
86
-
87
- set_password(self.service_name, self.username, self.model_dump_json())
88
-
89
-
90
- class Post(FullyValidatedModel):
91
- class Block(FullyValidatedModel):
92
- type: str = "text"
93
- text: str = ""
94
- blocks: list[int] = [] # noqa: RUF012
95
-
96
- timestamp: SkipJsonSchema[int] = 0
97
- tags: Annotated[list[str], PlainSerializer(",".join)] = [] # noqa: RUF012
98
- state: SkipJsonSchema[Literal["published", "queued", "draft", "private", "unapproved"]] = "draft"
99
-
100
- content: SkipJsonSchema[list[Block]] = [] # noqa: RUF012
101
- layout: SkipJsonSchema[list[Block]] = [] # noqa: RUF012
102
- trail: SkipJsonSchema[list[Any]] = [] # noqa: RUF012
103
-
104
- is_submission: SkipJsonSchema[bool] = False
105
-
106
- def __rich__(self) -> Panel:
107
- return Panel(
108
- self.get_content_text(),
109
- title="Preview",
110
- subtitle=" ".join(f"#{tag}" for tag in self.tags),
111
- subtitle_align="left",
112
- )
113
-
114
- def valid_text_post(self) -> bool:
115
- return bool(self.content) and all(block.type == "text" for block in self.content) and not (self.is_submission or self.trail or any(block.type == "ask" for block in self.layout))
116
-
117
- def get_content_text(self) -> str:
118
- return "\n\n".join(block.text for block in self.content)
119
-
120
-
121
- class Example(FullyValidatedModel):
122
- class Message(FullyValidatedModel):
123
- role: Literal["developer", "user", "assistant"]
124
- content: str
125
-
126
- messages: list[Message]
File without changes