Last active
April 5, 2023 23:03
-
-
Save judell/c7ffe7a7253ed68527459388f792264d to your computer and use it in GitHub Desktop.
transfer mastodon lists
This file contains 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
package main | |
import ( | |
"context" | |
"database/sql" | |
"fmt" | |
"strings" | |
"time" | |
_ "github.com/lib/pq" | |
"github.com/mattn/go-mastodon" | |
) | |
var originServer = "mastodon.social" // steampipe connection name: mastodon_social | |
var targetServer = "social.coop" // steampipe connection name: social_coop | |
var targetToken = "***" | |
func main() { | |
// connect to originServer's database, here running locally but could be in cloud.steampipe.io | |
psqlconnA := "host=localhost port=9193 user=steampipe password=*** dbname=steampipe" | |
dbA, err := sql.Open("postgres", psqlconnA) | |
CheckError(err) | |
defer dbA.Close() | |
// Connect to targetServer's Mastodon instance | |
ctx := context.Background() | |
client := mastodon.NewClient(&mastodon.Config{ | |
Server: "https://" + targetServer, | |
AccessToken: targetToken, | |
}) | |
// save title/acct/list_id from originServer in a table | |
rows1, err := dbA.Query(` | |
create table if not exists public.mastodon_accounts_by_list as | |
select | |
l.title, | |
l.acct, | |
a.list_id | |
from | |
mastodon_social.mastodon_my_list l | |
join | |
mastodon_social.mastodon_list_account a | |
on | |
l.id = a.list_id | |
`) | |
CheckError(err) | |
defer rows1.Close() | |
// join the table with targetServer's lists to obtain a mapping of title/acct from originServer to list on targetServer | |
rows2, err := dbA.Query(` | |
select | |
m.title, | |
m.acct, | |
s.id | |
from | |
public.mastodon_accounts_by_list m | |
join | |
social_coop.mastodon_my_list s | |
on | |
m.title = s.title | |
`) | |
CheckError(err) | |
defer rows2.Close() | |
for rows2.Next() { | |
var title string | |
var acct string | |
var id string | |
err = rows2.Scan(&title, &acct, &id) | |
CheckError(err) | |
// Add the account to the list on targetServer | |
adjustedAccount := adjustAcct(acct, originServer, targetServer) | |
fmt.Println(acct, adjustedAccount) | |
err = client.AddToList(ctx, mastodon.ID(id), mastodon.ID(adjustedAccount)) | |
fmt.Println(err, id, acct) | |
CheckError(err) | |
time.Sleep(1 * time.Second) | |
} | |
} | |
func adjustAcct(acct, originServer, targetServer string) string { | |
atIndex := strings.Index(acct, "@") | |
// If the account name doesn't have a domain, append the origin server domain. | |
if atIndex == -1 { | |
return acct + "@" + originServer | |
} | |
acctDomain := acct[atIndex+1:] | |
// If the account domain is the same as the target server domain, return the account name without the domain. | |
if acctDomain == targetServer { | |
return acct[:atIndex] | |
} | |
// In all other cases, return the account name unchanged. | |
return acct | |
} | |
func CheckError(err error) { | |
if err != nil { | |
fmt.Println("Error:", err.Error()) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment