tidy-up refactor, add readme
This commit is contained in:
parent
6bde84b7e3
commit
cd48757272
6 changed files with 104 additions and 77 deletions
60
funcs.go
60
funcs.go
|
@ -8,25 +8,15 @@ import (
|
|||
"net/http"
|
||||
"net/url"
|
||||
"os"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
func ImportProfile(session *AtprotoSession, pdsUrl string, fromAccount string, dryrun bool, verbose bool) error {
|
||||
fmt.Printf("fetching profile record from @%s...\n", fromAccount)
|
||||
func ImportProfile(fromSession *AtprotoSession, toSession *AtprotoSession, dryrun bool, verbose bool) error {
|
||||
fmt.Printf("fetching profile record from @%s...\n", fromSession.Handle)
|
||||
|
||||
var err error
|
||||
fromDid := fromAccount
|
||||
if !strings.HasPrefix(fromAccount, "did:") {
|
||||
fromDid, err = ResolveAtprotoHandle(session, pdsUrl, fromAccount)
|
||||
if err != nil {
|
||||
return errors.New(fmt.Sprintf("failed to resolve identity: %v\n", err))
|
||||
}
|
||||
}
|
||||
|
||||
reqUrl, _ := url.Parse(pdsUrl + "/xrpc/com.atproto.repo.getRecord")
|
||||
reqUrl, _ := url.Parse(fromSession.PdsUrl + "/xrpc/com.atproto.repo.getRecord")
|
||||
reqUrl.RawQuery = url.Values{
|
||||
"repo": { fromAccount },
|
||||
"repo": { fromSession.Did },
|
||||
"collection": { "app.bsky.actor.profile" },
|
||||
"rkey": { "self" },
|
||||
}.Encode()
|
||||
|
@ -64,47 +54,47 @@ func ImportProfile(session *AtprotoSession, pdsUrl string, fromAccount string, d
|
|||
|
||||
if !dryrun {
|
||||
// import avatar
|
||||
avatarBytes, err := GetAtprotoBlob(pdsUrl, fromDid, profile.Avatar.Ref.Link)
|
||||
avatarBytes, err := GetAtprotoBlob(fromSession.PdsUrl, fromSession.Did, profile.Avatar.Ref.Link)
|
||||
if err != nil { return errors.New(fmt.Sprintf("failed to download avatar: %v\n", err)) }
|
||||
|
||||
avatarBlob, err := UploadAtprotoBlob(session, pdsUrl, avatarBytes, profile.Avatar.MimeType)
|
||||
avatarBlob, err := UploadAtprotoBlob(toSession, avatarBytes, profile.Avatar.MimeType)
|
||||
if err != nil { return errors.New(fmt.Sprintf("failed to upload avatar: %v\n", err)) }
|
||||
newProfile.Avatar = avatarBlob
|
||||
|
||||
// import banner
|
||||
bannerBytes, err := GetAtprotoBlob(pdsUrl, fromDid, profile.Banner.Ref.Link)
|
||||
bannerBytes, err := GetAtprotoBlob(fromSession.PdsUrl, fromSession.Did, profile.Banner.Ref.Link)
|
||||
if err != nil { return errors.New(fmt.Sprintf("failed to download banner: %v\n", err)) }
|
||||
|
||||
bannerBlob, err := UploadAtprotoBlob(session, pdsUrl, bannerBytes, profile.Banner.MimeType)
|
||||
bannerBlob, err := UploadAtprotoBlob(toSession, bannerBytes, profile.Banner.MimeType)
|
||||
if err != nil { return errors.New(fmt.Sprintf("failed to upload banner: %v\n", err)) }
|
||||
newProfile.Banner = bannerBlob
|
||||
|
||||
// import all details
|
||||
_, err = PutAtprotoRecord(session, pdsUrl, "app.bsky.actor.profile", newProfile)
|
||||
_, err = PutAtprotoRecord(toSession, "app.bsky.actor.profile", newProfile)
|
||||
if err != nil {
|
||||
return errors.New(fmt.Sprintf("failed to update profile record: %v\n", err))
|
||||
}
|
||||
}
|
||||
fmt.Printf("profile imported from @%s successfully.\n", fromAccount)
|
||||
fmt.Printf("profile imported from @%s to @%s successfully.\n", fromSession.Handle, toSession.Handle)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func ImportFollows(session *AtprotoSession, pdsUrl string, fromAccount string, dryrun bool, verbose bool) error {
|
||||
func ImportFollows(fromSession *AtprotoSession, toSession *AtprotoSession, dryrun bool, verbose bool) error {
|
||||
followDids := []string{}
|
||||
cursor := ""
|
||||
|
||||
fmt.Printf("fetching follow records from @%s...\n", fromAccount)
|
||||
fmt.Printf("fetching follow records from @%s...\n", fromSession.Handle)
|
||||
for {
|
||||
reqUrl, _ := url.Parse(pdsUrl + "/xrpc/app.bsky.graph.getFollows")
|
||||
reqUrl, _ := url.Parse(fromSession.PdsUrl + "/xrpc/app.bsky.graph.getFollows")
|
||||
reqUrlValues := url.Values{
|
||||
"actor": { fromAccount },
|
||||
"actor": { fromSession.Did },
|
||||
"limit": { "100" },
|
||||
}
|
||||
if cursor != "" { reqUrlValues.Set("cursor", cursor) }
|
||||
reqUrl.RawQuery = reqUrlValues.Encode()
|
||||
req, _ := http.NewRequest(http.MethodGet, reqUrl.String(), nil)
|
||||
req.Header.Set("Authorization", "Bearer " + session.AccessJwt)
|
||||
req.Header.Set("Authorization", "Bearer " + fromSession.AccessJwt)
|
||||
|
||||
res, err := http.DefaultClient.Do(req)
|
||||
if err != nil {
|
||||
|
@ -130,9 +120,9 @@ func ImportFollows(session *AtprotoSession, pdsUrl string, fromAccount string, d
|
|||
}
|
||||
fmt.Printf("fetched %d follow records.\n", len(followDids))
|
||||
|
||||
fmt.Printf("importing follow records to @%s...\n", session.Handle)
|
||||
fmt.Printf("importing follow records to @%s...\n", toSession.Handle)
|
||||
successful := 0
|
||||
for i, did := range followDids[len(followDids)-3:] {
|
||||
for i, did := range followDids {
|
||||
if dryrun {
|
||||
if verbose {
|
||||
fmt.Printf("(%d/%d) followed %s (dry run)\n", i, len(followDids), did)
|
||||
|
@ -145,7 +135,7 @@ func ImportFollows(session *AtprotoSession, pdsUrl string, fromAccount string, d
|
|||
CreatedAt: time.Now().Format(time.RFC3339),
|
||||
}
|
||||
recordJson, _ := json.Marshal(record)
|
||||
newRecord, err := CreateAtprotoRecord(session, pdsUrl, "app.bsky.graph.follow", recordJson)
|
||||
newRecord, err := CreateAtprotoRecord(toSession, "app.bsky.graph.follow", recordJson)
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "warn: failed to create follow record for %s: %v\n", did, err)
|
||||
continue
|
||||
|
@ -156,15 +146,21 @@ func ImportFollows(session *AtprotoSession, pdsUrl string, fromAccount string, d
|
|||
successful += 1
|
||||
}
|
||||
|
||||
fmt.Printf("%d/%d follow records imported successfully!\n", successful, len(followDids))
|
||||
fmt.Printf(
|
||||
"%d/%d follow records imported from @%s to @%s successfully!\n",
|
||||
successful,
|
||||
len(followDids),
|
||||
fromSession.Handle,
|
||||
toSession.Handle,
|
||||
)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func ImportPreferences(fromSession *AtprotoSession, toSession *AtprotoSession, pdsUrl string, dryrun bool, verbose bool) error {
|
||||
func ImportPreferences(fromSession *AtprotoSession, toSession *AtprotoSession, dryrun bool, verbose bool) error {
|
||||
fmt.Printf("fetching preferences from @%s...\n", fromSession.Handle)
|
||||
|
||||
req, _ := http.NewRequest(http.MethodGet, pdsUrl + "/xrpc/app.bsky.actor.getPreferences", nil)
|
||||
req, _ := http.NewRequest(http.MethodGet, fromSession.PdsUrl + "/xrpc/app.bsky.actor.getPreferences", nil)
|
||||
req.Header.Set("Authorization", "Bearer " + fromSession.AccessJwt)
|
||||
res, err := http.DefaultClient.Do(req)
|
||||
if err != nil {
|
||||
|
@ -182,7 +178,7 @@ func ImportPreferences(fromSession *AtprotoSession, toSession *AtprotoSession, p
|
|||
fmt.Printf("importing preferences to @%s...\n", toSession.Handle)
|
||||
if !dryrun {
|
||||
reqBodyBytes, _ := json.Marshal(data)
|
||||
req, _ := http.NewRequest(http.MethodPost, pdsUrl + "/xrpc/app.bsky.actor.putPreferences", bytes.NewReader(reqBodyBytes))
|
||||
req, _ := http.NewRequest(http.MethodPost, toSession.PdsUrl + "/xrpc/app.bsky.actor.putPreferences", bytes.NewReader(reqBodyBytes))
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
req.Header.Set("Authorization", "Bearer " + toSession.AccessJwt)
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue