diff options
Diffstat (limited to 'hello-fusepy.py')
-rw-r--r-- | hello-fusepy.py | 77 |
1 files changed, 64 insertions, 13 deletions
diff --git a/hello-fusepy.py b/hello-fusepy.py index 010ef9d..eed2371 100644 --- a/hello-fusepy.py +++ b/hello-fusepy.py @@ -1,8 +1,11 @@ -import sys +import argparse +import enum import errno -import stat import logging import requests +import sys +import stat + from datetime import datetime as dtp from fuse import ( FUSE, @@ -13,7 +16,13 @@ from fuse import ( fuse_get_context, ) -api_url = "https://furry.engineer/users/uvok/outbox?min_id=0&page=true" + +class APIChoice(enum.Enum): + ACTIVITYPUB = "ActivityPub" + MASTODON = "Mastodon" + + +api_url_template_ap = "https://{server}/users/{user}/outbox?min_id=0&page=true" class Status(object): @@ -24,9 +33,12 @@ class Status(object): class HelloWorld(Operations, LoggingMixIn): - def __init__(self): + def __init__(self, api: APIChoice, server: str, user: str): self.statuses: list[Status] = [] self.fd = 0 + self.api = api + self.server = server + self.user = user def getattr(self, path, fh=None): (uid, gid, _) = fuse_get_context() @@ -44,6 +56,7 @@ class HelloWorld(Operations, LoggingMixIn): return { "st_mode": (stat.S_IFREG | 0o400), "st_size": len(found.content.encode("utf8")), + "st_nlink": 1, "st_uid": uid, "st_gid": gid, "st_ctime": pubunix, @@ -52,12 +65,17 @@ class HelloWorld(Operations, LoggingMixIn): raise FuseOSError(errno.ENOENT) def load_statuses(self): - res = requests.get(api_url) + url = api_url_template_ap.format(server=self.server, user=self.user) + res = requests.get(url) res.raise_for_status() stats = res.json() logging.debug(f"Status: ${stats['id']}") self.statuses = [ - Status(s["object"]["id"].split("/")[-1], s["object"]["content"], s["object"]["published"]) + Status( + s["object"]["id"].split("/")[-1], + s["object"]["content"], + s["object"]["published"], + ) for s in stats["orderedItems"] ] pass @@ -86,9 +104,45 @@ class HelloWorld(Operations, LoggingMixIn): raise FuseOSError(errno.ENOENT) -def main(mountpoint): +def parse_arguments(): + parser = argparse.ArgumentParser( + description="Mount a read-only FUSE filesystem for ActivityPub or Mastodon" + ) + parser.add_argument( + "mountpoint", help="The directory where the filesystem will be mounted" + ) + group = parser.add_mutually_exclusive_group(required=True) + group.add_argument( + "-a", "--activitypub", action="store_true", help="Use ActivityPub API" + ) + group.add_argument("-m", "--mastodon", action="store_true", help="Use Mastodon API") + parser.add_argument("-s", "--server", required=True, help="The server/host URL") + parser.add_argument( + "-u", "--username", required=True, help="The username to fetch statuses for" + ) + + args = parser.parse_args() + + if args.activitypub: + args.api_choice = APIChoice.ACTIVITYPUB + elif args.mastodon: + args.api_choice = APIChoice.MASTODON + else: + parser.error("Must choose either ActivityPub or Mastodon API") + + if args.api_choice != APIChoice.ACTIVITYPUB: + raise NotImplementedError("Not supported yet") + + return args + +def main(args): try: - myfuse = FUSE(HelloWorld(), mountpoint, nothreads=True, foreground=True) + FUSE( + HelloWorld(args.api_choice, args.server, args.username), + args.mountpoint, + nothreads=True, + foreground=True, + ) except: fuse_exit() raise @@ -96,8 +150,5 @@ def main(mountpoint): if __name__ == "__main__": logging.basicConfig(level=logging.DEBUG) - if len(sys.argv) != 2: - print(f"Usage: {sys.argv[0]} <mountpoint>") - sys.exit(1) - - main(sys.argv[1]) + args = parse_arguments() + main(args) |