diff --git a/shell/lemmyseeds.sh b/shell/lemmyseeds.sh new file mode 100755 index 0000000..56563cd --- /dev/null +++ b/shell/lemmyseeds.sh @@ -0,0 +1,90 @@ +#!/usr/bin/env bash +# +# Create a Lemmy-specific JSON import file snippet with the top 50 communties +# from the top 25 instances as reported by https://lemmyverse.net data. Can be +# used to populate a new Lemmy instance with popular lemmyverse communities. +# +# Requires: curl, jq, sed, head, networking/internet +# +# Use of the resulting file: +# 1. create a new user as desired +# 2. Export user settings to lemmy_export.json +# 3. Run this script to create the output file +# 4. Use 'jq' to merge the files, e.g.: +# +# jq '.followed_communities = $inputs' lemmy_export.json \ +# --slurpfile inputs lemmyseeds_20250703_0734.json > lemmy_import.json +# +# NOTE: user imports adhere to Instance Blocks in place; if setting up a new +# instance, configure instance blocks first before importing this data +# +# SPDX-License-Identifier: MIT + +_VERSION=0.1 + +## settings + +# number of top scoring instances to query +_INUM=25 +# number of top communities on each instance +_CNUM=50 +# which view of communitiy data to use +_CTYPE="TopAll" + +# output +_DTS=$(date +%Y%m%d_%H%M) +_OUT="./lemmyseeds_${_DTS}.json" + +# current Lemmy API version +_API="v3" + +# raw JSON of instances with popularity scores +_LEMMS="https://data.lemmyverse.net/data/instance.min.json" + +## code + +# shellcheck disable=SC2026 +_INSTS=$(curl -sf "${_LEMMS}" | jq 'sort_by(.score) | reverse | .[] | .base' | head -"${_INUM}" | sed 's/\"//'g) + +# if curl fails, -f emits empty string / errno 22 +if [[ ! -v _INSTS ]]; then + echo "Failed to download from data.lemmyverse.net" + exit 22 +else + echo "Lemmyverse data downloaded, working..." +fi + +# build the API calls, top 50 +_URLS=() +for _inst in ${_INSTS}; do + _link="https://${_inst}/api/${_API}/community/list?type_=Local&sort=${_CTYPE}&limit=${_CNUM}" + _URLS+=("${_link}") +done + +# get absolute URLs to communities, used in Profile Import +_COMMS=() +for _url in "${_URLS[@]}"; do + echo "Querying: ${_url}" + _comm=$(curl -sf "${_url}" | jq '.communities[].community.actor_id') + if [[ -n "${_comm}" ]]; then + _COMMS+=("${_comm}") + fi +done + +# build the JSON jq-friendly merge object +_PRE="{\n \"followed_communities\": [\n" +_MID="" +_POST="\n ]\n}" +_SEEN=0 +# shellcheck disable=SC2068 +for _xxx in ${_COMMS[@]}; do + if [[ ${_SEEN} -eq 1 ]]; then + _MID+=",\n" + fi + _SEEN=1 + _MID+=" ${_xxx//$'\n'/}" +done +echo -e "${_PRE}${_MID}${_POST}" > "${_OUT}" +echo "Created: ${_OUT}" + +exit 0