finished the config
This commit is contained in:
parent
22e49aa83c
commit
b4926729d6
@ -1,21 +1,33 @@
|
||||
from pathlib import Path
|
||||
import json
|
||||
|
||||
from config_path import ConfigPath
|
||||
import configparser
|
||||
import toml
|
||||
|
||||
from .utils import Paths, PROGRAM_NAME
|
||||
|
||||
|
||||
class PublishMeetups:
|
||||
def __init__(self):
|
||||
self.config_file: Path = Path(ConfigPath("publish-meetups", "hzl", ".ini").saveFilePath(mkdir=True))
|
||||
self.config = configparser.ConfigParser()
|
||||
self.config_file: Path = Path(Paths.CONFIG_PATH, "publish-meetups.toml")
|
||||
print(self.config_file)
|
||||
self.config = {}
|
||||
|
||||
self.config["DEFAULT"] = {
|
||||
self.config = {
|
||||
"active_feeds": [
|
||||
"mastodon",
|
||||
],
|
||||
"mastodon": {},
|
||||
}
|
||||
|
||||
print(self.config_file)
|
||||
|
||||
def __enter__(self):
|
||||
if self.config_file.exists():
|
||||
with self.config_file.open("r") as f:
|
||||
self.config.update(toml.load(f))
|
||||
|
||||
print(self.config['active_feeds'][0])
|
||||
|
||||
return self
|
||||
|
||||
def __exit__(self, exc_type, exc_val, exc_tb):
|
||||
pass
|
||||
with self.config_file.open("w") as f:
|
||||
toml.dump(self.config, f)
|
||||
|
9
publish_meetups/feeds/__init__.py
Normal file
9
publish_meetups/feeds/__init__.py
Normal file
@ -0,0 +1,9 @@
|
||||
class Feed:
|
||||
def __init__(self):
|
||||
pass
|
||||
|
||||
def prompt_auth(self):
|
||||
return {}
|
||||
|
||||
def create_post(self):
|
||||
pass
|
50
publish_meetups/feeds/mastodon_feed.py
Normal file
50
publish_meetups/feeds/mastodon_feed.py
Normal file
@ -0,0 +1,50 @@
|
||||
from pathlib import Path
|
||||
|
||||
from mastodon import Mastodon
|
||||
|
||||
from . import Feed
|
||||
from ..utils import CONFIG_PATH, PROGRAM_NAME, prompt
|
||||
|
||||
|
||||
class MastodonFeed(Feed):
|
||||
CLIENTCRED_PATH: Path = CONFIG_PATH.joinpath("mastodon_clientcred.secret")
|
||||
USERCRED_PATH: Path = CONFIG_PATH.joinpath("mastodon_usercred.secret")
|
||||
|
||||
# https://github.com/halcy/Mastodon.py
|
||||
def __init__(self, username: str, password: str, api_base_url: str):
|
||||
if not self.CLIENTCRED_PATH.exists():
|
||||
self._create_app(api_base_url)
|
||||
self.mastodon = Mastodon(client_id=self.CLIENTCRED_PATH)
|
||||
|
||||
self.mastodon.log_in(
|
||||
username=username,
|
||||
password=password,
|
||||
)
|
||||
|
||||
super().__init__()
|
||||
|
||||
|
||||
def _create_app(self, api_base_url: str):
|
||||
client_id, client_secret = Mastodon.create_app(
|
||||
PROGRAM_NAME,
|
||||
api_base_url=api_base_url,
|
||||
to_file=self.CLIENTCRED_PATH
|
||||
)
|
||||
|
||||
self._save_clientcred(client_id, client_secret)
|
||||
|
||||
|
||||
def prompt_auth(self) -> dict:
|
||||
"""
|
||||
mastodon needs:
|
||||
- the instance used
|
||||
- the email of the user
|
||||
- the password of the user
|
||||
"""
|
||||
|
||||
return {
|
||||
"api_base_url": prompt.for_string("The instance you use", "https://mastodon.social"),
|
||||
"username": prompt.for_string("E-Mail"),
|
||||
"password": prompt.for_password("Password"),
|
||||
}
|
||||
|
19
publish_meetups/utils/__init__.py
Normal file
19
publish_meetups/utils/__init__.py
Normal file
@ -0,0 +1,19 @@
|
||||
from pathlib import Path
|
||||
|
||||
import platformdirs
|
||||
|
||||
|
||||
PROGRAM_NAME: str = "publish-meetups"
|
||||
|
||||
|
||||
class Paths:
|
||||
CONFIG_PATH: Path = Path(platformdirs.user_config_path(appname=PROGRAM_NAME))
|
||||
|
||||
|
||||
Paths.CONFIG_PATH.mkdir(parents=True, exist_ok=True)
|
||||
|
||||
|
||||
|
||||
__all__ = ["prompt", "CONFIG_PATH", "PROGRAM_NAME", "errors"]
|
||||
|
||||
|
4
publish_meetups/utils/error.py
Normal file
4
publish_meetups/utils/error.py
Normal file
@ -0,0 +1,4 @@
|
||||
class InvalidCredential(Exception):
|
||||
def __init__(self, message):
|
||||
super().__init__(message)
|
||||
|
21
publish_meetups/utils/prompt.py
Normal file
21
publish_meetups/utils/prompt.py
Normal file
@ -0,0 +1,21 @@
|
||||
from typing import Optional
|
||||
|
||||
from getpass import getpass
|
||||
|
||||
|
||||
def for_string(msg: str, default: Optional[str] = None) -> str:
|
||||
def _real_prompt() -> str:
|
||||
nonlocal msg, default
|
||||
|
||||
if default is not None:
|
||||
return input(f"{msg} [{default}]: ").strip() or default
|
||||
|
||||
return input(msg + ': ').strip()
|
||||
|
||||
result = _real_prompt
|
||||
print("> " + result)
|
||||
return result
|
||||
|
||||
|
||||
def for_password(msg: str) -> str:
|
||||
return getpass.getpass(msg + ': ').strip()
|
@ -35,8 +35,8 @@ classifiers = [
|
||||
dependencies = [
|
||||
"Mastodon.py~=1.8.1",
|
||||
"twikit~=1.1.12",
|
||||
"configparser~=6.0.0",
|
||||
"config-path~=1.0.5",
|
||||
"toml~=0.10.2",
|
||||
"platformdirs~=3.2.0",
|
||||
]
|
||||
|
||||
[project.urls]
|
||||
|
Loading…
Reference in New Issue
Block a user