api / main.go
cuorz's picture
Update main.go
8444fd4 verified
Raw
History Blame Contribute Delete
3.39 kB
package main
import (
"encoding/json"
"fmt"
"log"
"net/http"
"strings"
"github.com/gin-gonic/gin"
)
func main() {
gin.SetMode(gin.ReleaseMode)
r := GinRouter()
fmt.Println("Starting server on :2304")
r.Run("0.0.0.0:7860")
}
func GinRouter() *gin.Engine {
r := gin.New()
r.Use(gin.Recovery(), gin.Logger())
r.GET("/", func(c *gin.Context) {
c.String(http.StatusOK, "Hello, World!")
})
r.GET("/instagram/post", func(c *gin.Context) {
shortcode := c.Query("shortcode") // Instagram dùng shortcode (ví dụ: C1abcde) thay vì UID cho post
if shortcode == "" {
c.JSON(http.StatusBadRequest, gin.H{"error": "Thiếu tham số shortcode"})
return
}
result, err := instagramPost(shortcode)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
c.JSON(http.StatusOK, result)
})
return r
}
func instagramPost(uid string) (map[string]interface{}, error) {
client := &http.Client{}
var data = strings.NewReader(`lsd=AdTG3HXj-AuAqL1v6Ppe6xBXk0s&jazoest=2957&fb_api_caller_class=RelayModern&fb_api_req_friendly_name=PolarisPostActionLoadPostQueryQuery&variables={"shortcode":"` + uid + `","fetch_comment_count":0,"fetch_related_profile_media_count":0,"parent_comment_count":0,"child_comment_count":0,"fetch_like_count":0,"fetch_tagged_user_count":null,"fetch_preview_comment_count":0,"has_threaded_comments":true,"hoisted_comment_id":null,"hoisted_reply_id":null}&server_timestamps=true&doc_id=10015901848480474`)
req, err := http.NewRequest("POST", "https://www.instagram.com/api/graphql", data)
if err != nil {
log.Fatal(err)
}
req.Header.Set("authority", "www.instagram.com")
req.Header.Set("accept", "*/*")
req.Header.Set("accept-language", "vi-VN,vi;q=0.9,fr-FR;q=0.8,fr;q=0.7,en-US;q=0.6,en;q=0.5")
req.Header.Set("content-type", "application/x-www-form-urlencoded")
req.Header.Set("dpr", "2.625")
req.Header.Set("origin", "https://www.instagram.com")
req.Header.Set("referer", "https://www.instagram.com/")
req.Header.Set("sec-ch-prefers-color-scheme", "dark")
req.Header.Set("sec-ch-ua", `"Chromium";v="107", "Not=A?Brand";v="24"`)
req.Header.Set("sec-ch-ua-full-version-list", `"Chromium";v="107.0.5304.74", "Not=A?Brand";v="24.0.0.0"`)
req.Header.Set("sec-ch-ua-mobile", "?1")
req.Header.Set("sec-ch-ua-model", `"SM-A336E"`)
req.Header.Set("sec-ch-ua-platform", `"Android"`)
req.Header.Set("sec-ch-ua-platform-version", `"13.0.0"`)
req.Header.Set("sec-fetch-dest", "empty")
req.Header.Set("sec-fetch-mode", "cors")
req.Header.Set("sec-fetch-site", "same-origin")
req.Header.Set("user-agent", "Mozilla/5.0 (Linux; Android 13; SM-A336E) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0 Mobile Safari/537.36")
req.Header.Set("x-asbd-id", "129477")
req.Header.Set("x-csrftoken", "qPAu6oXeZT5gjItJk4yFAB")
req.Header.Set("x-fb-friendly-name", "PolarisPostActionLoadPostQueryQuery")
req.Header.Set("x-fb-lsd", "AdTG3HXj-AuAqL1v6Ppe6xBXk0s")
req.Header.Set("x-ig-app-id", "936619743392459")
resp, err := client.Do(req)
if err != nil {
log.Fatal(err)
}
defer resp.Body.Close()
var result map[string]interface{}
if err := json.NewDecoder(resp.Body).Decode(&result); err != nil {
return nil, fmt.Errorf("lỗi giải mã JSON:")
}
return result, nil
}