Skip to content

Commit

Permalink
Fix video content processing and shortcode extraction
Browse files Browse the repository at this point in the history
  • Loading branch information
aarxns committed Feb 27, 2025
1 parent 91772ca commit dec0b13
Showing 1 changed file with 79 additions and 12 deletions.
91 changes: 79 additions & 12 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/json"
"flag"
"fmt"
"io"
"net/http"
"os"
"path/filepath"
Expand All @@ -17,6 +18,7 @@ func main() {
token := flag.String("token", "", "Token")
user := flag.String("user", "", "User ID")
out := flag.String("output", "content/instagram", "Output directory")
debug := flag.Bool("debug", false, "Enable detailed debugging")
flag.Parse()

if *token == "" || *user == "" {
Expand All @@ -26,58 +28,123 @@ func main() {
}

os.MkdirAll(*out, 0755)
fmt.Printf("%d posts processed to %s\n", getAndProcessPosts(*user, *token, *out), *out)
fmt.Printf("%d posts processed to %s\n", getAndProcessPosts(*user, *token, *out, *debug), *out)
}

func getAndProcessPosts(user, token, outDir string) int {
count, url := 0, fmt.Sprintf("https://graph.instagram.com/v22.0/%s/media?fields=id,permalink,timestamp&access_token=%s", user, token)
func getAndProcessPosts(user, token, outDir string, debug bool) int {
count, url := 0, fmt.Sprintf("https://graph.instagram.com/v22.0/%s/media?fields=id,permalink,timestamp,media_type,media_url&access_token=%s", user, token)
re := regexp.MustCompile(`instagram\.com/p/([^/]+)`)
reelRe := regexp.MustCompile(`instagram\.com/reel/([^/]+)`)

for url != "" {
// API request and parsing
if debug {
fmt.Printf("Fetching URL: %s\n", url)
}

resp, err := http.Get(url)
if err != nil || resp.StatusCode != 200 {
fmt.Printf("Error fetching from API: %v, status: %d\n", err, resp.StatusCode)
if resp != nil && resp.Body != nil {
body, _ := io.ReadAll(resp.Body)
fmt.Printf("Response body: %s\n", string(body))
resp.Body.Close()
}
break
}

var result struct {
Data []struct {
ID, Permalink, Timestamp string
MediaType string `json:"media_type,omitempty"`
MediaURL string `json:"media_url,omitempty"`
} `json:"data"`
Paging struct {
Next string `json:"next,omitempty"`
} `json:"paging"`
}

if err := json.NewDecoder(resp.Body).Decode(&result); err != nil {
resp.Body.Close()
body, _ := io.ReadAll(resp.Body)
resp.Body.Close()

if debug {
fmt.Printf("Raw API response: %s\n", string(body))
}

if err := json.Unmarshal(body, &result); err != nil {
fmt.Printf("Error decoding response: %v\n", err)
break
}
resp.Body.Close()

// Process posts
for _, p := range result.Data {
id := strings.Split(p.ID, "_")[0] // First ID component
sc := id // Shortcode (Fallback = ID)
if m := re.FindStringSubmatch(p.Permalink); len(m) > 1 {
sc = m[1]

if debug {
fmt.Printf("\nProcessing post:\n")
fmt.Printf(" ID: %s\n", p.ID)
fmt.Printf(" Permalink: %s\n", p.Permalink)
fmt.Printf(" Timestamp: %s\n", p.Timestamp)
fmt.Printf(" Media Type: %s\n", p.MediaType)
fmt.Printf(" Media URL: %s\n", p.MediaURL)
}

if p.Permalink == "" {
fmt.Printf("Warning: Post %s has no permalink, using ID as shortcode\n", id)
} else {
// Try to extract shortcode from permalink
if m := re.FindStringSubmatch(p.Permalink); len(m) > 1 {
sc = m[1]
if debug {
fmt.Printf(" Extracted shortcode from /p/ URL: %s\n", sc)
}
} else if m := reelRe.FindStringSubmatch(p.Permalink); len(m) > 1 {
sc = m[1]
if debug {
fmt.Printf(" Extracted shortcode from /reel/ URL: %s\n", sc)
}
} else {
fmt.Printf("Warning: Could not extract shortcode from permalink '%s' for post %s (type: %s), using ID\n",
p.Permalink, id, p.MediaType)

// Try to extract shortcode from the last part of the URL
parts := strings.Split(strings.TrimRight(p.Permalink, "/"), "/")
if len(parts) > 0 {
possibleSc := parts[len(parts)-1]
if possibleSc != "" && possibleSc != id {
sc = possibleSc
fmt.Printf(" Extracted possible shortcode from URL path: %s\n", sc)
}
}
}
}

t, _ := time.Parse("2006-01-02T15:04:05-0700", p.Timestamp)
if t.IsZero() {
t, err := time.Parse("2006-01-02T15:04:05-0700", p.Timestamp)
if err != nil {
fmt.Printf("Error parsing timestamp '%s': %v, using current time\n", p.Timestamp, err)
t = time.Now()
}

fn := filepath.Join(outDir, fmt.Sprintf("%s-%s.md", t.Format("2006-01-02"), id))
if err := os.WriteFile(fn, []byte(fmt.Sprintf(`---
content := fmt.Sprintf(`---
date: %s
draft: false
tags:
- instagram
instagram_id: %s
instagram_permalink: %s
media_type: %s
shortcode: %s
---
{{< instagram %s >}}`, t.Format("2006-01-02T15:04:05-07:00"), sc)), 0644); err == nil {
{{< instagram %s >}}`, t.Format("2006-01-02T15:04:05-07:00"), id, p.Permalink, p.MediaType, sc, sc)

if err := os.WriteFile(fn, []byte(content), 0644); err == nil {
count++
fmt.Printf("Created post %s with shortcode %s\n", fn, sc)
} else {
fmt.Printf("Error writing file %s: %v\n", fn, err)
}
}

Expand Down

0 comments on commit dec0b13

Please sign in to comment.