refactored to be simpler

This commit is contained in:
Hellow
2024-02-13 21:25:47 +01:00
parent 75d3d5f1aa
commit 61536240f3
10 changed files with 150 additions and 122 deletions

View File

@@ -1,23 +1,11 @@
import logging
from .interface import Feed
from .mastodon_feed import MastodonFeed
from .twitter_feed import TwitterFeed
class Feed:
@classmethod
def prompt_auth(cls, existing_config: dict) -> dict:
return existing_config
def __init__(self, config: dict = None, **kwargs):
self.logger = logging.getLogger(self.__class__.__name__)
self.config = config or {}
def __enter__(self):
return self
def post(self, message: str, **kwargs):
print(f"Posting {message}")
def run(self):
self.post("Hello, World!")
def __exit__(self, exc_type, exc_value, traceback):
pass
__all__ = ["Feed", "MastodonFeed", "TwitterFeed"]

View File

@@ -0,0 +1,17 @@
import logging
class Feed:
__config_name__ = "feed"
@classmethod
def prompt_auth(cls, existing_config: dict) -> dict:
return existing_config
def __init__(self):
self.logger = logging.getLogger(self.__class__.__name__)
def post(self, message: str):
self.logger.info(f"Posting message to {self.__class__.__name__}: {message}")
pass

View File

@@ -3,14 +3,15 @@ from pathlib import Path
from mastodon import Mastodon
from . import Feed
from ..utils import Paths, PROGRAM_NAME, prompt
from ..utils import PROGRAM_NAME, prompt, config
class MastodonFeed(Feed):
CLIENTCRED_PATH: Path = Paths.CONFIG_PATH.joinpath("mastodon_clientcred.secret")
__config_name__ = "mastodon"
CLIENTCRED_PATH: Path = config.CONFIG_PATH.joinpath("mastodon_clientcred.secret")
@classmethod
def prompt_auth(cls, existing_config: dict) -> dict:
def prompt_auth(cls) -> dict:
"""
mastodon needs:
- the instance used
@@ -18,7 +19,6 @@ class MastodonFeed(Feed):
"""
return {
**existing_config,
"api_base_url": prompt.for_string("The instance you use", "https://mastodon.social"),
"access_token": prompt.for_password("Access token"),
}
@@ -30,11 +30,9 @@ class MastodonFeed(Feed):
access_token=access_token,
)
super().__init__(**kwargs)
super().__init__()
def post(self, message: str):
kwargs = locals().copy()
self.mastodon.toot(message)
Feed.post(**kwargs)
super().post(message)

View File

@@ -4,14 +4,14 @@ from twikit import Client
from twikit.errors import Forbidden, Unauthorized
from . import Feed
from ..utils import Paths, PROGRAM_NAME, prompt
from ..utils import config, PROGRAM_NAME, prompt
class TwitterFeed(Feed):
CLIENTCRED_PATH: Path = Paths.CONFIG_PATH.joinpath("mastodon_clientcred.secret")
__config_name__ = "twitter"
@classmethod
def prompt_auth(cls, existing_config: dict) -> dict:
def prompt_auth(cls) -> dict:
"""
client.login(
auth_info_1=USERNAME ,
@@ -28,13 +28,12 @@ class TwitterFeed(Feed):
# https://github.com/d60/twikit
def __init__(self, auth_info_1: str, auth_info_2: str, password: str, cookies: dict = None, **kwargs):
super().__init__(**kwargs)
super().__init__()
self.client = Client('en-US')
logged_in = False
cookies = cookies or {}
if cookies is not None:
self.client.http.client.cookies = cookies
try:
@@ -53,16 +52,14 @@ class TwitterFeed(Feed):
)
logged_in = True
self.config['cookies'] = dict(self.client.http.client.cookies)
print(self.client.user_id())
print(self.client._base_headers)
config.set_field(self.__config_name__, {
'cookies': dict(self.client.http.client.cookies)
}, update_dict=True)
def post(self, message: str):
kwargs = locals().copy()
self.client.create_tweet(
text=message,
)
Feed.post(**kwargs)
super().post(message)