diff options
Diffstat (limited to 'Utilities/AccountManager.py')
-rw-r--r-- | Utilities/AccountManager.py | 107 |
1 files changed, 107 insertions, 0 deletions
diff --git a/Utilities/AccountManager.py b/Utilities/AccountManager.py new file mode 100644 index 0000000..daad76d --- /dev/null +++ b/Utilities/AccountManager.py @@ -0,0 +1,107 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- +#****************************************************************************** +# Copyright (C) 2017 Thomas "Cakeisalie5" Touhey <thomas@touhey.fr> +# +# 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 2 +# 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, write to the Free Software +# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, +# MA 02110-1301, USA. +#****************************************************************************** +""" The account manager for the Silicium Bot. + Manages Mastodon and Twitter accounts. +""" + +import os, pickle +from mastodon import Mastodon + +__all__ = ["AccountManager"] +_VER = 'ohmygoditsmickbumhole' + +class AccountManager: + def __init__(self, path, ids_folder): + self.__path = path + self.__fold = ids_folder + self.__mastodon_app = None + self.__mastodon_users = {} + try: + data = pickle.load(open(self.__path, 'rb')) + if type(data) == dict and 'ver' in data and data['ver'] == _VER: + self.__mastodon_app = data["mastodon-app"] + if self.__mastodon_app: + self.__mastodon_users = data["mastodon-users"] + except: + pass + + def save(self): + data = {'ver': _VER, 'mastodon-app': self.__mastodon_app, + 'mastodon-users': self.__mastodon_users} + pickle.dump(data, open(self.__path, 'wb')) + + def mastodon_delete_app(self): + if not self.__mastodon_app: + return + + for key in self.__mastodon_users: + try: os.remove(key) + except FileNotFoundError: pass + self.__mastodon_users = [] + + try: os.remove(self.__mastodon_app[0]) + except FileNotFoundError: pass + self.__mastodon_app = None + + self.save() + + def mastodon_create_app(self, base): + app_id = 'mastodon_app.secret' + + self.mastodon_delete_app() + Mastodon.create_app('Silicium.org Bot', + api_base_url = base, + to_file = os.path.join(self.__fold, app_id)) + self.__mastodon_app = (app_id, base) + self.save() + + def mastodon_connect(self, name, password): + if not self.__mastodon_app: + raise Exception("You need to create an application first!") + if name in self.__mastodon_users: + raise Exception("This user is already connected!") + + user_id = 'mastodon_user_' + user_id += ''.join(c for c in name.replace('@', 'AT') if c in \ + 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_') + user_id += '.secret' + + m = Mastodon( + client_id = os.path.join(self.__fold, self.__mastodon_app[0]), + api_base_url = self.__mastodon_app[1]) + m.log_in( + name, password, + to_file = os.path.join(self.__fold, user_id) + ) + + self.__mastodon_users[name] = user_id + self.save() + + def post(self, message): + if self.__mastodon_app: + mapp = os.path.join(self.__fold, self.__mastodon_app[0]) + mbase = self.__mastodon_app[1] + for user in self.__mastodon_users: + muser = os.path.join(self.__fold, self.__mastodon_users[user]) + Mastodon(client_id = mapp, api_base_url = mbase, + access_token = muser).toot(message) + +# End of file. |