chiark / gitweb /
Now actually use the login details.
authorSimon Tatham <anakin@pobox.com>
Sat, 2 Dec 2023 17:21:50 +0000 (17:21 +0000)
committerSimon Tatham <anakin@pobox.com>
Sat, 2 Dec 2023 17:21:50 +0000 (17:21 +0000)
The public timeline is replaced with my home timeline, which I can now
access because my client is logged in.

client.py
login.py
mastodonochrome

index c3ed0f692b14b627a2016db03413a69144c716c5..5cec035eda671239d71a0b2cfcde728c363ff080 100644 (file)
--- 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)
index 231d91b6cf9684fe33968d3da10ca381af261a08..aab5afccfdbabdba7af8d3e6f5dc7cea56275af6 100644 (file)
--- 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']
index ebf056a28d5e36d72ee429cbb74d5de1181e05d2..fdfd3eedcf2f19a16c68e98388069ccf3d967bca 100755 (executable)
@@ -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()