157 lines
4.7 KiB
Python
Executable File
157 lines
4.7 KiB
Python
Executable File
#! /usr/bin/env python3
|
|
|
|
# Yandere Lewd Bot, an image posting bot for Pleroma
|
|
# Copyright (C) 2022 Anon
|
|
#
|
|
# This program is free software: you can redistribute it and/or modify
|
|
# it under the terms of the GNU General Public License as published by
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
# (at your option) any later version.
|
|
#
|
|
# This program is distributed in the hope that it will be useful,
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
# GNU General Public License for more details.
|
|
#
|
|
# You should have received a copy of the GNU General Public License
|
|
# along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
import getpass
|
|
import re
|
|
import sys
|
|
import datetime
|
|
import importlib
|
|
from mastodon import Mastodon, MastodonIllegalArgumentError, MastodonAPIError, MastodonIOError
|
|
from collections import OrderedDict
|
|
import yanlib
|
|
|
|
|
|
class CreateAppError(Exception):
|
|
pass
|
|
|
|
|
|
def get_client_id_and_secret(app, permissions, domain):
|
|
try:
|
|
return Mastodon.create_app(app, scopes=permissions, api_base_url=domain)
|
|
except MastodonIOError:
|
|
raise CreateAppError("An error occurred. Make sure the domain name is correct.")
|
|
|
|
|
|
def get_api(client_id, client_secret, domain):
|
|
api = Mastodon(client_id, client_secret, api_base_url=domain)
|
|
return api
|
|
|
|
|
|
def get_token(api, email, password, permissions):
|
|
try:
|
|
token = api.log_in(email, password, scopes=permissions)
|
|
return token
|
|
except MastodonIllegalArgumentError:
|
|
raise CreateAppError("Username or Password is incorrect.")
|
|
except MastodonAPIError:
|
|
raise CreateAppError("Could not grant scopes:", ", ".join(permissions))
|
|
|
|
|
|
def get_cfg(cfg_name):
|
|
try:
|
|
cfg = importlib.import_module(cfg_name)
|
|
return cfg
|
|
except ImportError:
|
|
raise CreateAppError("Cannot import module:", cfg_name, "Make sure you omitted the .py extension and try again")
|
|
|
|
|
|
def package_settings(app, domain, client_id, client_secret, token):
|
|
settings_server = OrderedDict([
|
|
("app_name", app),
|
|
("api_base_url", domain),
|
|
("client_id", client_id),
|
|
("client_secret", client_secret),
|
|
("access_token", token)
|
|
])
|
|
return settings_server
|
|
|
|
|
|
def get_setting_reminder(fmt):
|
|
dt_now = datetime.datetime.now()
|
|
dt_now = dt_now.replace(year=dt_now.year + 1)
|
|
settings_reminder = dt_now.strftime(fmt)
|
|
return settings_reminder
|
|
|
|
|
|
def main():
|
|
# Create App
|
|
print("Generate and register Mastodon App")
|
|
print("You can just hit enter to accept the default for prompts that have them")
|
|
print("Ctrl+C to Quit\n")
|
|
|
|
try:
|
|
# Get instance information
|
|
app = input("Enter your app name (Default: app): ") or "app"
|
|
domain = input("URL of Instance (Default: https://yandere.cc): ") or "https://yandere.cc"
|
|
email = input("Enter your email: ")
|
|
password = getpass.getpass("Enter password: ")
|
|
print("Scopes: read, write, follow, push")
|
|
print("Separate each scope with a comma (example above).")
|
|
print("!!! Accept the default unless you intend to modify Yandere Lewd Bot !!!")
|
|
ans = input("Scopes (Default: write): ") or "write"
|
|
ans = re.sub(r"\s+", "", ans, flags=re.UNICODE)
|
|
permissions = ans.split(",")
|
|
print("Granting:", permissions)
|
|
|
|
# Begin logging in
|
|
client_id, client_secret = get_client_id_and_secret(app, permissions, domain)
|
|
api = get_api(client_id, client_secret, domain)
|
|
token = get_token(api, email, password, permissions)
|
|
|
|
# Get user setting for settings_time["long_date_format"]
|
|
# Needed for setting_reminder
|
|
print("What is the name of your main configuration file? Exclude the '.py' extension.")
|
|
ans = input("(Default: cfg): ") or "cfg"
|
|
cfg = get_cfg(ans)
|
|
|
|
# Credentials (unencrypted)
|
|
encrypt, salt = False, ""
|
|
settings_server = package_settings(app, domain, client_id, client_secret, token)
|
|
reminder = get_setting_reminder(cfg.settings_time["long_date_format"])
|
|
|
|
# Encrypt
|
|
ans = input("Do you want to encrypt your credentials (y/N)? ")
|
|
if ans.upper() in ("Y", "YES"):
|
|
import encryption
|
|
encrypt = True
|
|
salt, settings_server = encryption.settings_server_encrypt_cfg(settings_server)
|
|
|
|
settings_encrypt = OrderedDict([
|
|
("encrypt", encrypt),
|
|
("salt", salt),
|
|
])
|
|
|
|
# Output the user's new credentials
|
|
print("Copy to your config file!!!")
|
|
yanlib.pp_ordered_dict("settings_server", settings_server)
|
|
print('\nsettings_reminder = "{}"\n'.format(reminder))
|
|
yanlib.pp_dict("settings_encrypt", settings_encrypt)
|
|
|
|
return 0
|
|
except (KeyboardInterrupt, EOFError):
|
|
print("Quitting...")
|
|
return 1
|
|
except CreateAppError as e:
|
|
print(e)
|
|
return 2
|
|
except Exception as e:
|
|
print("Unhandled Exception!!", e)
|
|
return 3
|
|
|
|
|
|
if __name__ == "__main__":
|
|
status_code = main()
|
|
status_msg = {
|
|
0: "Success :)",
|
|
1: "User Quit :|",
|
|
2: "Error :(",
|
|
3: "Unhandled Exception ¯\\_(ツ)_/¯"
|
|
}
|
|
print(status_msg[status_code])
|
|
sys.exit(status_code)
|