aboutsummaryrefslogtreecommitdiff
path: root/cmd
diff options
context:
space:
mode:
authorXe Iaso <me@xeiaso.net>2024-06-16 16:54:41 -0400
committerXe Iaso <me@xeiaso.net>2024-06-16 16:54:41 -0400
commit9dd33711159888efee90ec90f22ef8a8b92c310a (patch)
treefb93a5e190ade80a55e5bc5570abfa436562775a /cmd
parent03bccb563082ae2168960d3c7a9c81126e98c440 (diff)
downloadx-9dd33711159888efee90ec90f22ef8a8b92c310a.tar.xz
x-9dd33711159888efee90ec90f22ef8a8b92c310a.zip
get paths to the root
Signed-off-by: Xe Iaso <me@xeiaso.net>
Diffstat (limited to 'cmd')
-rw-r--r--cmd/hnscrape/hn.go18
-rw-r--r--cmd/hnscrape/main.go10
2 files changed, 27 insertions, 1 deletions
diff --git a/cmd/hnscrape/hn.go b/cmd/hnscrape/hn.go
index c580988..f05f989 100644
--- a/cmd/hnscrape/hn.go
+++ b/cmd/hnscrape/hn.go
@@ -219,6 +219,24 @@ func (h *HNClient) getItem(ctx context.Context, id int) (*HNItem, error) {
return &item, nil
}
+func (h *HNClient) PathToRoot(ctx context.Context, id int) ([]int, error) {
+ item, err := h.GetItem(ctx, id)
+ if err != nil {
+ return nil, err
+ }
+
+ if item.Parent == nil {
+ return []int{id}, nil
+ }
+
+ parents, err := h.PathToRoot(ctx, *item.Parent)
+ if err != nil {
+ return nil, err
+ }
+
+ return append(parents, id), nil
+}
+
func (h *HNClient) GetUltimateParent(ctx context.Context, id int) (*HNItem, error) {
item, err := h.GetItem(ctx, id)
if err != nil {
diff --git a/cmd/hnscrape/main.go b/cmd/hnscrape/main.go
index 5e3440a..78dc4f4 100644
--- a/cmd/hnscrape/main.go
+++ b/cmd/hnscrape/main.go
@@ -42,7 +42,7 @@ func main() {
os.Exit(1)
}
- slog.Info("got user", "user", u)
+ slog.Info("got user", "user", u.Created.String(), "karma", u.Karma, "submitted", len(u.Submitted))
// itemsCommentedIn := make([]int, 0)
//
@@ -142,6 +142,14 @@ func main() {
os.Exit(1)
}
*/
+
+ pathToRoot, err := hn.PathToRoot(ctx, 40699123)
+ if err != nil {
+ slog.Error("failed to get path to root", "err", err)
+ os.Exit(1)
+ }
+
+ slog.Info("got path to root", "pathToRoot", pathToRoot)
}
func ControlCContext() (context.Context, context.CancelFunc) {