Skip to content
This repository was archived by the owner on Jun 24, 2025. It is now read-only.

Commit d4f090a

Browse files
author
Bash06
committed
fix: scraping not working
1 parent b270fe6 commit d4f090a

File tree

4 files changed

+13
-71
lines changed

4 files changed

+13
-71
lines changed

api/share.go

Lines changed: 1 addition & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -18,21 +18,14 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.
1818
package api
1919

2020
import (
21-
"bash06/vxinstagram/flags"
22-
"bash06/vxinstagram/utils"
23-
"log/slog"
2421
"bash06/vxinstagram/flags"
2522
"bash06/vxinstagram/utils"
2623
"log/slog"
2724
"net/http"
2825
"net/http/httputil"
2926
"net/url"
3027
"strings"
31-
"net/http/httputil"
32-
"net/url"
33-
"strings"
3428

35-
"github.com/getsentry/sentry-go"
3629
"github.com/getsentry/sentry-go"
3730
"github.com/gin-gonic/gin"
3831
)
@@ -64,17 +57,6 @@ func FollowShare(c *gin.Context) {
6457
return
6558
}
6659

67-
res, err := client.Do(req)
68-
if err != nil {
69-
slog.Error("Failed to follow redirects", slog.Any("err", err))
70-
sentry.CaptureException(err)
71-
c.HTML(http.StatusOK, "embed.html", &HtmlOpenGraphData{
72-
Title: "VxInstagram - Server Error",
73-
Description: "VxInstagram encountered a server side error while processing your request. Request ID:`" + span.SpanID.String() + "`",
74-
})
75-
return
76-
}
77-
res.Body.Close()
7860
res, err := client.Do(req)
7961
if err != nil {
8062
slog.Error("Failed to follow redirects", slog.Any("err", err))
@@ -87,8 +69,6 @@ func FollowShare(c *gin.Context) {
8769
}
8870
res.Body.Close()
8971

90-
urlSplit := strings.Split(res.Request.URL.String(), "/")
91-
postId := urlSplit[len(urlSplit)-2]
9272
urlSplit := strings.Split(res.Request.URL.String(), "/")
9373
postId := urlSplit[len(urlSplit)-2]
9474

@@ -130,16 +110,6 @@ func FollowShare(c *gin.Context) {
130110
})
131111
}
132112
}
133-
videoUrl = data.Items[0].VideoVersions[0].URL
134-
135-
if videoUrl == "" {
136-
slog.Debug("No video URL found! :(")
137-
c.HTML(http.StatusOK, "embed.html", &HtmlOpenGraphData{
138-
Title: "VxInstagram - Empty Response",
139-
Description: "Instagram returned an empty response meaning we can't embed the video. You'll need to watch it in your browser. Sorry!",
140-
})
141-
}
142-
}
143113

144114
remote, err := url.Parse(videoUrl)
145115
if err != nil {
@@ -150,24 +120,9 @@ func FollowShare(c *gin.Context) {
150120
Description: "VxInstagram encountered a server side error while processing your request. Request ID:`" + span.SpanID.String() + "`",
151121
})
152122
return
153-
}
154-
remote, err := url.Parse(videoUrl)
155-
if err != nil {
156-
slog.Error("Failed to parse CDN video URL", slog.Any("err", err))
157-
sentry.CaptureException(err)
158-
c.HTML(http.StatusOK, "embed.html", &HtmlOpenGraphData{
159-
Title: "VxInstagram - Server Error",
160-
Description: "VxInstagram encountered a server side error while processing your request. Request ID:`" + span.SpanID.String() + "`",
161-
})
162-
return
123+
163124
}
164125

165-
proxy := httputil.NewSingleHostReverseProxy(remote)
166-
proxy.Director = func(r *http.Request) {
167-
r.Header = c.Request.Header
168-
r.Host = remote.Host
169-
r.URL = remote
170-
r.Header = c.Request.Header.Clone()
171126
proxy := httputil.NewSingleHostReverseProxy(remote)
172127
proxy.Director = func(r *http.Request) {
173128
r.Header = c.Request.Header
@@ -178,14 +133,6 @@ func FollowShare(c *gin.Context) {
178133
hopHeaders := []string{
179134
"Connection", "Keep-Alive", "Proxy-Authenticate", "Proxy-Authorization", "Te", "Trailer", "Transfer-Encoding",
180135
}
181-
hopHeaders := []string{
182-
"Connection", "Keep-Alive", "Proxy-Authenticate", "Proxy-Authorization", "Te", "Trailer", "Transfer-Encoding",
183-
}
184-
185-
for _, h := range hopHeaders {
186-
r.Header.Del(h)
187-
}
188-
}
189136
for _, h := range hopHeaders {
190137
r.Header.Del(h)
191138
}
@@ -194,7 +141,4 @@ func FollowShare(c *gin.Context) {
194141
slog.Debug("Success!")
195142
c.Header("Cache-Control", "max-age=43200")
196143
proxy.ServeHTTP(c.Writer, c.Request)
197-
slog.Debug("Success!")
198-
c.Header("Cache-Control", "max-age=43200")
199-
proxy.ServeHTTP(c.Writer, c.Request)
200144
}

utils/extractUrl.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,10 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.
1717
*/
1818
package utils
1919

20-
import "strings"
20+
import (
21+
"fmt"
22+
"strings"
23+
)
2124

2225
const (
2326
prefix = `\"video_url\":`
@@ -50,5 +53,7 @@ func ExtractUrl(s string) (string, bool) {
5053
result = UnescapeJSONString(result)
5154
result = strings.ReplaceAll(result, `\/`, `/`)
5255

56+
fmt.Println(result)
57+
5358
return result[1:], true
5459
}

utils/fetch.go

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ import (
2121
"bash06/vxinstagram/flags"
2222
"fmt"
2323
"net/http"
24-
"net/url"
2524

2625
jsoniter "github.com/json-iterator/go"
2726
)
@@ -74,25 +73,19 @@ func FetchPost(postId string) (*IgResponse, error) {
7473

7574
baseURL := "https://www.instagram.com/p/" + postId + "?__a=1&__d=dis"
7675

77-
url, err := url.Parse(baseURL)
78-
if err != nil {
79-
return nil, err
80-
}
81-
82-
q := url.Query()
83-
q.Set("__a", "1")
84-
q.Set("__d", "dis")
85-
url.RawQuery = q.Encode()
86-
87-
req, err := http.NewRequest("GET", url.String(), nil)
76+
req, err := http.NewRequest("GET", baseURL, nil)
8877
if err != nil {
8978
return nil, err
9079
}
9180

9281
req.Header.Set("User-Agent", *flags.InstagramBrowserAgent)
9382
req.Header.Set("Cookie", *flags.InstagramCookie)
9483
req.Header.Set("X-IG-App-ID", *flags.InstagramXIGAppID)
84+
85+
// Set headers so we look more like a real browser
9586
req.Header.Set("Sec-Fetch-Site", "same-origin")
87+
req.Header.Set("Origin", "https://www.instagram.com")
88+
req.Header.Set("Referer", "https://www.instagram.com")
9689

9790
resp, err := GetIpRotationClient(5).Do(req)
9891
if err != nil {

utils/scrape.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ func ScrapeFromHTML(postId string) (string, error) {
3535
return "", fmt.Errorf("failed to prepare HTTP request: %v", err)
3636
}
3737

38-
req.Header.Set("User-Agent", *flags.InstagramBrowserAgent)
38+
req.Header.Set("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36")
3939

4040
var client *http.Client
4141

0 commit comments

Comments
 (0)