69 lines
1.9 KiB
Python
69 lines
1.9 KiB
Python
|
from pathlib import Path
|
||
|
|
||
|
from twikit import Client
|
||
|
from twikit.errors import Forbidden, Unauthorized
|
||
|
|
||
|
from . import Feed
|
||
|
from ..utils import Paths, PROGRAM_NAME, prompt
|
||
|
|
||
|
|
||
|
class TwitterFeed(Feed):
|
||
|
CLIENTCRED_PATH: Path = Paths.CONFIG_PATH.joinpath("mastodon_clientcred.secret")
|
||
|
|
||
|
@classmethod
|
||
|
def prompt_auth(cls, existing_config: dict) -> dict:
|
||
|
"""
|
||
|
client.login(
|
||
|
auth_info_1=USERNAME ,
|
||
|
auth_info_2=EMAIL,
|
||
|
password=PASSWORD
|
||
|
)
|
||
|
"""
|
||
|
|
||
|
return {
|
||
|
"auth_info_1": prompt.for_string("Username"),
|
||
|
"auth_info_2": prompt.for_string("Email"),
|
||
|
"password": prompt.for_password("Password"),
|
||
|
}
|
||
|
|
||
|
# 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)
|
||
|
|
||
|
self.client = Client('en-US')
|
||
|
|
||
|
logged_in = False
|
||
|
|
||
|
cookies = cookies or {}
|
||
|
if cookies is not None:
|
||
|
self.client.http.client.cookies = cookies
|
||
|
try:
|
||
|
self.client.user_id()
|
||
|
logged_in = True
|
||
|
except (Forbidden, Unauthorized):
|
||
|
self.logger.warning("Cookies are expired.")
|
||
|
self.client.http.client.cookies.clear()
|
||
|
|
||
|
if not logged_in:
|
||
|
self.logger.info("Logging in with username and email.")
|
||
|
self.client.login(
|
||
|
auth_info_1=auth_info_1,
|
||
|
auth_info_2=auth_info_2,
|
||
|
password=password,
|
||
|
)
|
||
|
logged_in = True
|
||
|
|
||
|
self.config['cookies'] = dict(self.client.http.client.cookies)
|
||
|
print(self.client.user_id())
|
||
|
print(self.client._base_headers)
|
||
|
|
||
|
|
||
|
def post(self, message: str):
|
||
|
kwargs = locals().copy()
|
||
|
|
||
|
self.client.create_tweet(
|
||
|
text=message,
|
||
|
)
|
||
|
|
||
|
Feed.post(**kwargs)
|