start working on an export command

This commit is contained in:
Ted Unangst 2023-08-18 11:51:28 -04:00
parent 0e4a87c523
commit 1910279c6b
2 changed files with 66 additions and 0 deletions

View File

@ -16,6 +16,7 @@
package main package main
import ( import (
"archive/zip"
"encoding/csv" "encoding/csv"
"encoding/json" "encoding/json"
"fmt" "fmt"
@ -27,6 +28,8 @@ import (
"sort" "sort"
"strings" "strings"
"time" "time"
"humungus.tedunangst.com/r/webs/junk"
) )
func importMain(username, flavor, source string) { func importMain(username, flavor, source string) {
@ -513,3 +516,61 @@ func importInstagram(username, source string) {
log.Printf("honk saved %v -> %v", xid, err) log.Printf("honk saved %v -> %v", xid, err)
} }
} }
func export(username, file string) {
user, err := butwhatabout(username)
if err != nil {
elog.Fatal(err)
}
fd, err := os.OpenFile(file, os.O_RDWR|os.O_CREATE|os.O_EXCL, 0666)
if err != nil {
elog.Fatal(err)
}
zd := zip.NewWriter(fd)
donks := make(map[string]bool)
{
w, err := zd.Create("outbox.json")
if err != nil {
elog.Fatal(err)
}
var jonks []junk.Junk
rows, err := stmtUserHonks.Query(0, 3, user.Name, "0", 1234567)
honks := getsomehonks(rows, err)
for _, honk := range honks {
noise := honk.Noise
j, jo := jonkjonk(user, honk)
if honk.Format == "markdown" {
jo["source"] = noise
}
for _, donk := range honk.Donks {
donks[donk.XID] = true
}
jonks = append(jonks, j)
}
j := junk.New()
j["@context"] = itiswhatitis
j["id"] = user.URL + "/outbox"
j["attributedTo"] = user.URL
j["type"] = "OrderedCollection"
j["totalItems"] = len(jonks)
j["orderedItems"] = jonks
j.Write(w)
}
zd.Create("media/")
for donk := range donks {
var media string
var data []byte
w, err := zd.Create("media/" + donk)
if err != nil {
elog.Fatal(err)
}
row := stmtGetFileData.QueryRow(donk)
err = row.Scan(&media, &data)
if err != nil {
elog.Fatal(err)
}
w.Write(data)
}
zd.Close()
fd.Close()
}

View File

@ -122,6 +122,11 @@ func main() {
elog.Fatal("import username mastodon|twitter srcdir") elog.Fatal("import username mastodon|twitter srcdir")
} }
importMain(args[1], args[2], args[3]) importMain(args[1], args[2], args[3])
case "export":
if len(args) != 3 {
elog.Fatal("export username destdir")
}
export(args[1], args[2])
case "devel": case "devel":
if len(args) != 2 { if len(args) != 2 {
elog.Fatal("need an argument: devel (on|off)") elog.Fatal("need an argument: devel (on|off)")