Skip to content

Commit

Permalink
Add support for new URL path in reel shares. Fixes #51
Browse files Browse the repository at this point in the history
  • Loading branch information
tulir committed Apr 16, 2024
1 parent 8b6d1e3 commit 0db89e8
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 7 deletions.
7 changes: 3 additions & 4 deletions messagix/instagram.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"fmt"
"net/url"
"strconv"
"strings"

"github.com/google/go-querystring/query"

Expand Down Expand Up @@ -113,12 +112,12 @@ func (ig *InstagramMethods) FetchProfile(username string) (*responses.ProfileInf
return profileInfo, nil
}

func (ig *InstagramMethods) FetchMedia(mediaID, nativeURL string) (*responses.FetchMediaResponse, error) {
func (ig *InstagramMethods) FetchMedia(mediaID, mediaShortcode string) (*responses.FetchMediaResponse, error) {
h := ig.client.buildHeaders(true)
h.Set("x-requested-with", "XMLHttpRequest")
referer := ig.client.getEndpoint("base_url")
if strings.HasPrefix(nativeURL, "instagram://media/?shortcode=") {
referer = fmt.Sprintf("%s/p/%s/", referer, strings.TrimPrefix(nativeURL, "instagram://media/?shortcode="))
if mediaShortcode != "" {
referer = fmt.Sprintf("%s/p/%s/", referer, mediaShortcode)
}
h.Set("referer", referer)
h.Set("Accept", "*/*")
Expand Down
8 changes: 5 additions & 3 deletions msgconv/from-meta.go
Original file line number Diff line number Diff line change
Expand Up @@ -394,14 +394,16 @@ func (mc *MessageConverter) fetchFullXMA(ctx context.Context, att *table.Wrapped
}
log := zerolog.Ctx(ctx)
switch {
case strings.HasPrefix(att.CTA.NativeUrl, "instagram://media/?shortcode="):
case strings.HasPrefix(att.CTA.NativeUrl, "instagram://media/?shortcode="), strings.HasPrefix(att.CTA.NativeUrl, "instagram://reels_share/?shortcode="):
actionURL, _ := url.Parse(removeLPHP(att.CTA.ActionUrl))
var carouselChildMediaID string
if actionURL != nil {
carouselChildMediaID = actionURL.Query().Get("carousel_share_child_media_id")
}

externalURL := fmt.Sprintf("https://www.instagram.com/p/%s/", strings.TrimPrefix(att.CTA.NativeUrl, "instagram://media/?shortcode="))
mediaShortcode := strings.TrimPrefix(att.CTA.NativeUrl, "instagram://media/?shortcode=")
mediaShortcode = strings.TrimPrefix(mediaShortcode, "instagram://reels_share/?shortcode=")
externalURL := fmt.Sprintf("https://www.instagram.com/p/%s/", mediaShortcode)
minimalConverted.Extra["external_url"] = externalURL
addExternalURLCaption(minimalConverted.Content, externalURL)
if !mc.ShouldFetchXMA(ctx) {
Expand All @@ -411,7 +413,7 @@ func (mc *MessageConverter) fetchFullXMA(ctx context.Context, att *table.Wrapped
}

log.Trace().Any("cta_data", att.CTA).Msg("Fetching XMA media from CTA data")
resp, err := ig.FetchMedia(strconv.FormatInt(att.CTA.TargetId, 10), att.CTA.NativeUrl)
resp, err := ig.FetchMedia(strconv.FormatInt(att.CTA.TargetId, 10), mediaShortcode)
if err != nil {
log.Err(err).Int64("target_id", att.CTA.TargetId).Msg("Failed to fetch XMA media")
minimalConverted.Extra["fi.mau.meta.xma_fetch_status"] = "fetch fail"
Expand Down

0 comments on commit 0db89e8

Please sign in to comment.