From 6ec18f206a152639e4a48cb314b78a750194ea63 Mon Sep 17 00:00:00 2001 From: Marc Falzon Date: Tue, 15 Oct 2019 21:05:30 +0200 Subject: [PATCH 1/2] Add support for location-type users This change adds support for location-type usernames, i.e. specifying the IG username `loc:6811413` will retrieve latest posts corresponding to this location. Fixes #3. --- instafeed.go | 40 +++++++++++++++++++++++++++++++++++++--- 1 file changed, 37 insertions(+), 3 deletions(-) diff --git a/instafeed.go b/instafeed.go index 6891bff..f0aa729 100644 --- a/instafeed.go +++ b/instafeed.go @@ -6,6 +6,7 @@ import ( "io/ioutil" "os" "path" + "strconv" "strings" "time" @@ -48,6 +49,7 @@ func init() { func main() { var ( igUsers []string + items []*feeds.Item err error ) @@ -103,9 +105,21 @@ func main() { } for _, u := range igUsers { - items, err := fetchUserFeedItems(u) - if err != nil { - dieOnError(fmt.Sprintf("unable to retrieve user %q feed: %s", u, err)) + switch { + case strings.HasPrefix(u, "loc:"): + id, err := strconv.Atoi(strings.TrimPrefix(u, "loc:")) + if err != nil { + dieOnError("invalid location-type value %q", u) + } + + if items, err = fetchLocationFeedItems(int64(id)); err != nil { + dieOnError(fmt.Sprintf("unable to retrieve user %q feed: %s", u, err)) + } + + default: + if items, err = fetchUserFeedItems(u); err != nil { + dieOnError(fmt.Sprintf("unable to retrieve user %q feed: %s", u, err)) + } } for _, item := range items { @@ -157,6 +171,26 @@ func fetchUserFeedItems(name string) ([]*feeds.Item, error) { return items, nil } +func fetchLocationFeedItems(id int64) ([]*feeds.Item, error) { + section, err := insta.Locations.Feeds(6811413) + if err != nil { + return nil, errors.Wrap(err, "unable to retrieve location feeds") + } + + items := make([]*feeds.Item, 0) + + for _, l := range section.Sections { + for _, m := range l.LayoutContent.Medias { + items = append(items, formatFeedItem(&m.Media)) + if len(items) >= feedMaxItems { + return items, nil + } + } + } + + return items, nil +} + func formatFeedItem(item *goinsta.Item) *feeds.Item { shortDesc, _ := goutils.Abbreviate(item.Caption.Text, 50) From 59b46959460a46b7fe78dba2aef54a87a432b63c Mon Sep 17 00:00:00 2001 From: Marc Falzon Date: Wed, 16 Oct 2019 17:27:34 +0200 Subject: [PATCH 2/2] fixup! Add support for location-type users --- instafeed.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/instafeed.go b/instafeed.go index f0aa729..4b0b251 100644 --- a/instafeed.go +++ b/instafeed.go @@ -172,7 +172,7 @@ func fetchUserFeedItems(name string) ([]*feeds.Item, error) { } func fetchLocationFeedItems(id int64) ([]*feeds.Item, error) { - section, err := insta.Locations.Feeds(6811413) + section, err := insta.Locations.Feeds(id) if err != nil { return nil, errors.Wrap(err, "unable to retrieve location feeds") }