Skip to content

Commit

Permalink
Scrape new /share/ remotely
Browse files Browse the repository at this point in the history
  • Loading branch information
Wikidepia committed Jan 29, 2025
1 parent cc8e736 commit a832837
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 15 deletions.
16 changes: 8 additions & 8 deletions handlers/embed.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func mediaidToCode(mediaID int) string {
}

func getSharePostID(postID string) (string, error) {
req, err := http.NewRequest("HEAD", "https://www.instagram.com/share/reel/"+postID+"/", nil)
req, err := http.NewRequest("HEAD", "https://www.instagram.com/share/p/"+postID+"/", nil)
if err != nil {
return postID, err
}
Expand All @@ -43,11 +43,11 @@ func getSharePostID(postID string) (string, error) {
if err != nil {
return postID, err
}
postID = path.Base(redirURL.Path)
if postID == "login" {
postIDTemp := path.Base(redirURL.Path)
if postIDTemp == "login" {
return postID, errors.New("not logged in")
}
return postID, nil
return postIDTemp, nil
}

func Embed(w http.ResponseWriter, r *http.Request) {
Expand Down Expand Up @@ -98,7 +98,7 @@ func Embed(w http.ResponseWriter, r *http.Request) {
postID = mediaidToCode(mediaID)
} else if strings.Contains(r.URL.Path, "/share/") {
postID, err = getSharePostID(postID)
if err != nil {
if err != nil && scraper.GetRemoteSessCount() == 0 {
slog.Error("Failed to get new postID from share URL", "postID", postID, "err", err)
viewsData.Description = "Failed to get new postID from share URL"
views.Embed(viewsData, w)
Expand Down Expand Up @@ -148,19 +148,19 @@ func Embed(w http.ResponseWriter, r *http.Request) {
case mediaNum == 0 && isImage && len(item.Medias) > 1:
viewsData.Card = "summary_large_image"
sb.WriteString("/grid/")
sb.WriteString(postID)
sb.WriteString(item.PostID)
viewsData.ImageURL = sb.String()
case isImage:
viewsData.Card = "summary_large_image"
sb.WriteString("/images/")
sb.WriteString(postID)
sb.WriteString(item.PostID)
sb.WriteString("/")
sb.WriteString(strconv.Itoa(max(1, mediaNum)))
viewsData.ImageURL = sb.String()
default:
viewsData.Card = "player"
sb.WriteString("/videos/")
sb.WriteString(postID)
sb.WriteString(item.PostID)
sb.WriteString("/")
sb.WriteString(strconv.Itoa(max(1, mediaNum)))
viewsData.VideoURL = sb.String()
Expand Down
11 changes: 4 additions & 7 deletions handlers/scraper/data.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,10 @@ import (
)

var (
ErrNotFound = errors.New("post not found")
timeout = 5 * time.Second
transport http.RoundTripper
transportNoProxy *http.Transport
sflightScraper singleflight.Group
ErrNotFound = errors.New("post not found")
timeout = 5 * time.Second
transport http.RoundTripper
sflightScraper singleflight.Group
)

//go:embed dictionary.bin
Expand All @@ -49,8 +48,6 @@ type InstaData struct {

func init() {
transport = gzhttp.Transport(http.DefaultTransport, gzhttp.TransportAlwaysDecompress(true))
transportNoProxy = http.DefaultTransport.(*http.Transport).Clone()
transportNoProxy.Proxy = nil // Skip any proxy
}

func GetData(postID string) (*InstaData, error) {
Expand Down
4 changes: 4 additions & 0 deletions handlers/scraper/remote.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,3 +148,7 @@ func ScrapeRemote(i *InstaData) error {
return errors.New("failed to get data from remote scraper")
}
}

func GetRemoteSessCount() int {
return int(sessCount.Load())
}

0 comments on commit a832837

Please sign in to comment.