From 739d15788a7ef2a6defdb2042c23d44176595169 Mon Sep 17 00:00:00 2001 From: Simon Tatham Date: Sat, 2 Dec 2023 17:21:50 +0000 Subject: [PATCH] Now actually use the login details. The public timeline is replaced with my home timeline, which I can now access because my client is logged in. --- client.py | 11 +++++++++-- login.py | 10 ++++++++++ mastodonochrome | 19 ++++++++++--------- 3 files changed, 29 insertions(+), 11 deletions(-) diff --git a/client.py b/client.py index c3ed0f6..5cec035 100644 --- a/client.py +++ b/client.py @@ -24,11 +24,18 @@ class Client: self.log_response = lambda *args, **kws: None def set_instance(self, instance): + self.instance = instance self.urls = { - 'auth': instance + "/oauth/", - 'api': instance + "/api/v1/", + 'auth': "https://" + instance + "/oauth/", + 'api': "https://" + instance + "/api/v1/", } + def set_username(self, username, account_id): + self.username = username + self.fq_username = f"{self.username}@{self.instance}" + self.at_fq_username = f"@{self.username}@{self.instance}" + self.account_id = account_id + def enable_debug(self, logfile): self.logfh = open(logfile, "w") pr = lambda *args, **kws: print(*args, file=self.logfh, **kws) diff --git a/login.py b/login.py index 231d91b..aab5afc 100644 --- a/login.py +++ b/login.py @@ -89,3 +89,13 @@ class LoginUI(client.Client): json.dump(data, fh, indent=4) finally: os.umask(old_umask) + +def setup_client(cl): + if isinstance(cl, LoginUI): + return # that would be silly + + with open(config_file()) as fh: + data = json.load(fh) + cl.set_instance(data['instance']) + cl.set_username(data['username'], data['account_id']) + cl.bearer_token = data['user_token'] diff --git a/mastodonochrome b/mastodonochrome index ebf056a..fdfd3ee 100755 --- a/mastodonochrome +++ b/mastodonochrome @@ -12,10 +12,10 @@ import client import cursesclient import login -class PublicTimelineUI(client.Client): +class TimelineUI(client.Client): def run(self): - self.set_instance("https://hachyderm.io") # FIXME - for item in self.get("timelines/public", limit=10): + login.setup_client(self) + for item in reversed(self.get("timelines/home", limit=20)): p = client.Status(item) for thing in p.text(): for line in thing.render(80): @@ -35,9 +35,9 @@ def main(): parser.add_argument("--log", help="File to log debug information to.") parser.add_argument("--test", nargs=argparse.REMAINDER, help="Run unit tests.") - parser.add_argument("--public", action="store_const", dest="action", - const=PublicTimelineUI, help="Temporary mode to fetch " - "a public timeline and print it on the terminal.") + parser.add_argument("--timeline", action="store_const", dest="action", + const=TimelineUI, help="Temporary mode to fetch " + "the user's timeline and print it on the terminal.") parser.add_argument("--login", action="store_const", dest="action", const=login.LoginUI, help="Log in to a user account.") parser.set_defaults(action=cursesclient.CursesUI) @@ -47,10 +47,11 @@ def main(): return unittest.main(argv=[sys.argv[0]] + args.test, testLoader=MyTestLoader()) - thing = args.action() + client = args.action() + login.setup_client(client) if args.log is not None: - thing.enable_debug(args.log) - thing.run() + client.enable_debug(args.log) + client.run() if __name__ == '__main__': main() -- 2.30.2