-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
107 lines (86 loc) · 2.57 KB
/
main.go
File metadata and controls
107 lines (86 loc) · 2.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package main
import (
"flag"
"fmt"
"os"
"github.com/rursache/reddit-cli/client"
"github.com/rursache/reddit-cli/parser"
)
var version = "dev"
func main() {
jsonOutput := flag.Bool("json", false, "Output as JSON instead of plain text")
showVersion := flag.Bool("version", false, "Show version and exit")
flag.Bool("v", false, "Show version and exit (shorthand)")
flag.Usage = func() {
fmt.Fprintf(os.Stderr, `reddit-cli - Extract content from Reddit posts
A CLI tool that parses Reddit post URLs and extracts text, links, and images.
Designed for AI agents to read Reddit posts without a browser.
USAGE:
reddit-cli [flags] <reddit-post-url>
ARGUMENTS:
<reddit-post-url> Full URL to a Reddit post
Supports: reddit.com, old.reddit.com, new.reddit.com
FLAGS:
--json Output as JSON instead of plain text
--version, -v Show version and exit
-h, --help Show this help message
EXAMPLES:
reddit-cli https://www.reddit.com/r/golang/comments/abc123/some_post/
reddit-cli --json https://old.reddit.com/r/linux/comments/xyz789/some_post/
reddit-cli https://reddit.com/r/pics/comments/def456/cool_photo/ | head -5
SUPPORTED POST TYPES:
- Text posts (self posts with body text)
- Link posts (external URLs)
- Image posts (single images, i.redd.it)
- Gallery posts (multiple images)
- Video posts (Reddit-hosted videos)
- Embedded content (YouTube, etc.)
- Crossposts (extracts original content)
OUTPUT:
By default, outputs clean plain text to stdout.
Use --json for structured JSON output.
Exit code 0 on success, 1 on error.
NOTE:
No API key or authentication required.
Uses Reddit's public JSON API (appending .json to post URLs).
`)
}
flag.Parse()
if *showVersion || isFlagSet("v") {
fmt.Printf("reddit-cli %s\n", version)
os.Exit(0)
}
args := flag.Args()
if len(args) == 0 {
fmt.Fprintln(os.Stderr, "error: no URL provided")
fmt.Fprintln(os.Stderr, "usage: reddit-cli [flags] <reddit-post-url>")
fmt.Fprintln(os.Stderr, "run 'reddit-cli -h' for help")
os.Exit(1)
}
rawURL := args[0]
post, err := client.FetchPost(rawURL)
if err != nil {
fmt.Fprintf(os.Stderr, "error: %v\n", err)
os.Exit(1)
}
parsed := parser.Parse(post)
if *jsonOutput {
output, err := parser.FormatJSON(parsed)
if err != nil {
fmt.Fprintf(os.Stderr, "error: %v\n", err)
os.Exit(1)
}
fmt.Println(output)
} else {
fmt.Print(parser.FormatText(parsed))
}
}
func isFlagSet(name string) bool {
found := false
flag.Visit(func(f *flag.Flag) {
if f.Name == name {
found = true
}
})
return found
}