kensanata / mastodon-archive

Archive your statuses, favorites and media using the Mastodon API (i.e. login required)
https://alexschroeder.ch/software/Mastodon_Archive
GNU General Public License v3.0
358 stars 33 forks source link

Feature: Backup / Export Filters #89

Open sammcj opened 1 year ago

sammcj commented 1 year ago

It would be awesome if this tool could backup / export your Filters, currently I don't think there is a way to do this.

kensanata commented 1 year ago

Yeah, a bit like archiving accounts? I'm not quite convinced that I'd ever want to look at them… but who knows.

sammcj commented 1 year ago

Sort of - but the use case I was thinking of was if you wanted to manage filters across multiple accounts.

Personally I have a huge list of filters and if for whatever reason the instance I am currently on went away I'd have to craft them all again.

Last night I had a crack at exporting them using a JS/TS library called Megalodon which made it surprisingly easy to export filters to JSON

async function filters() {
  const filters = await client.getFilters();
  return filters.data;
}

function backupFilters() {
  filters().then((filterObj) => {
    const json = JSON.stringify(filterObj, null, 2);
    fs.writeFileSync(`${outdir}/filters.json`, json);
  });
}
kensanata commented 1 year ago

It sounds like this wouldn't be all that useful unless there was also a way to upload those filters to a different account…

sammcj commented 1 year ago

You can!

Here's an example I whipped up using that same JS/TS library:

async function uploadFilters(json: string) {
  const filterObj = JSON.parse(json);

  filterObj.forEach((filter: Entity.Filter) => {
    client.createFilter(filter.phrase, filter.context);
  });
}

Src: https://github.com/sammcj/mastapi/blob/main/src/upload.ts

kensanata commented 1 year ago

Interesting. If somebody adds it to mastodon-archive, I guess that would make sense. It'll need more command line arguments and all that, for sure.

roycewilliams commented 1 year ago

Strong second for exporting filters. I also have a huge number of filters. I've invested many hours in creating and improving them. Not only do I want them to be portable to other instances, I also want to back the filters up for BC/DR purposes (in case that instance is somehow lost, the instance accidentally deletes my filters, etc.).

kensanata commented 1 year ago

If somebody writes it, I'll add it.

lapineige commented 1 year ago

For reference to anyone willing to try to implement it, here is the related API : https://docs.joinmastodon.org/methods/filters/