Last active
February 22, 2023 04:10
-
-
Save ilovecomputers/3297f9882cecb91849a452e3d110b81a to your computer and use it in GitHub Desktop.
Sort your mastodon posts by most favs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class FetchUtil { | |
/** | |
* @param {string|URL} url | |
* @param {Object} body | |
* @returns {Promise<Response>} | |
*/ | |
static async post(url, body) { | |
return FetchUtil.#fetchResponse(url, { | |
method: "POST", | |
headers: { | |
"Content-Type": "application/json" | |
}, | |
body: JSON.stringify(body) | |
}); | |
} | |
/** | |
* @param {string|URL} url | |
* @returns {Promise<Response>} | |
*/ | |
static async get(url) { | |
return FetchUtil.#fetchResponse(url); | |
} | |
/** | |
* @param {string|URL} url | |
* @param {RequestInit} [init] | |
* @returns {Promise<Response>} | |
*/ | |
static async #fetchResponse(url, init) { | |
let response; | |
try { | |
response = await fetch(url, { | |
headers: { | |
'Access-Control-Allow-Origin': "*" | |
} | |
}); | |
} catch (error) { | |
error.message = "There was a connection error of some sort" + error.message; | |
throw error; | |
} | |
if (!response.ok) { | |
throw new Error(`Error with ${init ? init.method : "GET"} request: ${url}`); | |
} | |
return response; | |
} | |
} | |
function parseLinkHeader(header) { | |
if (!header) return ""; | |
const linkexp = /<[^>]*>\s*(\s*;\s*[^\(\)<>@,;:"\/\[\]\?={} \t]+=(([^\(\)<>@,;:"\/\[\]\?={} \t]+)|("[^"]*")))*(,|$)/g; | |
const paramexp = /[^\(\)<>@,;:"\/\[\]\?={} \t]+=(([^\(\)<>@,;:"\/\[\]\?={} \t]+)|("[^"]*"))/g; | |
const matches = header.match(linkexp); | |
const rels = {}; | |
for (let i = 0; i < matches.length; i++) { | |
const split = matches[i].split('>'); | |
const href = split[0].substring(1); | |
const ps = split[1]; | |
const s = ps.match(paramexp); | |
for (let j = 0; j < s.length; j++) { | |
const p = s[j]; | |
const paramsplit = p.split('='); | |
const rel = paramsplit[1].replace(/["']/g, ''); | |
rels[rel] = href; | |
} | |
} | |
return rels; | |
} | |
async function fetchAll(url) { | |
if (!url.searchParams.has('limit')) { | |
url.searchParams.set('limit', '40'); | |
url.searchParams.set('exclude_replies', 'true'); | |
url.searchParams.set('exclude_reblogs', 'true'); | |
} | |
try { | |
const response = await FetchUtil.get(url); | |
let statuses = await response.json(); | |
let next = (parseLinkHeader(response.headers.get('link'))['next']); | |
if (next) { | |
const moreStatuses = await fetchAll(new URL(next)); | |
if (Array.isArray(moreStatuses)) { | |
return statuses.concat(moreStatuses); | |
} | |
} | |
return statuses; | |
} catch (e) { | |
return []; | |
} | |
} | |
// Just copy this entire script, paste into your browser's console, and edit the URL below. | |
// This won't work in Safari since its console doesn't support top-level await calls. | |
// | |
// You account ID is a number. You can get it by viewing your profile and checking | |
// your browser's network panel | |
var statuses = await fetchAll(new URL("https://{{YOUR INSTANCE DOMAIN}}/api/v1/accounts/{{YOUR ACCOUNT ID}}/statuses")); | |
console.log(statuses.sort((a, b) => b.favourites_count - a.favourites_count)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment