ckolderup / postmarks

a single-user bookmarking website designed to live on the Fediverse

Home Page:https://postmarks.glitch.me

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Archive bookmarks

TomCasavant opened this issue · comments

It might be a little outside of the scope for now or it might make more sense to handle this outside of the server using a future but just throwing this out there as a potential feature, I setup this function on my instance that I tied into the /bookmark/new endpoint so when a new bookmark is created it should archive the url on the internet archive wayback machine.

export async function archiveUrlOnInternetArchive(url) {
  const access = process.env.ARCHIVE_ACCESS
  const secret = process.env.ARCHIVE_SECRET
  if (access && secret) {
      try {
        const apiUrl = 'https://web.archive.org/save/';
        const data = `url=${url}&capture_all=1&delay_wb_availability=1&skip_first_archive=1`;

        const response = await axios.post(apiUrl, data, {
          headers: {
            'Content-Type': 'application/x-www-form-urlencoded',
            'Authorization': `LOW ${access}:${secret}`,
            'Accept': 'application/json',
          },
        });

        if (response.status === 200) {
          // Handle success
          console.log(response)
          console.log('URL archived successfully!');
        } else {
          console.error('Failed to archive URL. HTTP Status:', response.status);
        }
      } catch (error) {
        console.error('Error archiving URL:', error.message);
      }
  } else {
    console.error("No Archive access")
  }

I haven't figured out how to test if it works yet because I'm not entirely certain how often the archive's webcrawlers run after making a request It works now, this new function requires an API key from the internet archive though. But it might be useful to store the archived url alongside the actual url in case your bookmark gets removed years from now. Obviously if this does get implemented later on I would think we'd need to make it an optional thing in the settings, but I have ran into issues where a blog post I've saved disappears or moves after awhile and then I've lost it so hopefully this resolves that problem.

Edit: updated the function to one that works more effectively (explained here https://github.com/TomCasavant/tom-postmarks#features if someone needs it)

That's neat! I have some vague desire to support some kind of plugin system for things like this, though I haven't really thought about how we'd support it architecturally. I'd like to avoid having a preferences screen a mile long, and it seems like everyone having their own install of Postmarks lends nicely to supporting a way to bring in external packages on a per-install basis. This would be a great candidate for that kind of thing, imo.

(The other thought I had at an earlier point was to support webhooks as a kind of complement to the simple API (#61), so the API would be responsible for handling data coming into Postmarks, and webhooks would be responsible for handling data going out. But being a good webhooks provider would involve a significant increase in architectural needs, since we'd need to queue the calls asynchronously and manage retries, etc. So I think that's probably out of scope.)

Just dropped by to say that I ❤️ this idea - will definitely be looking at adding the same hook into my fork at some stage. Thanks for sharing!