-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
211 lines (177 loc) · 4.89 KB
/
main.go
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
package main
import (
"encoding/json"
"flag"
"fmt"
"image"
"image/gif"
"image/jpeg"
"image/png"
"io"
"log"
"net/http"
"os"
"path/filepath"
"regexp"
"strings"
"golang.org/x/image/draw"
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/app"
"fyne.io/fyne/v2/canvas"
"fyne.io/fyne/v2/container"
"fyne.io/fyne/v2/theme"
)
type Post struct {
Title string `json:"title"`
URL string `json:"url"`
}
type RedditResponse struct {
Data struct {
After string `json:"after"`
Children []struct {
Data Post `json:"data"`
} `json:"children"`
} `json:"data"`
}
func fetchRedditData(subreddit string, limit int) ([]Post, error) {
var allPosts []Post
after := ""
for {
url := fmt.Sprintf("https://www.reddit.com/r/%s/.json?limit=%d&after=%s", subreddit, limit, after)
log.Println("Fetching URL:", url)
req, _ := http.NewRequest("GET", url, nil)
req.Header.Set("User-Agent", "Go-Reddit-Client")
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
log.Fatalf("Error making HTTP request: %v", err)
return nil, err
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
log.Fatalf("Error reading response body: %v", err)
return nil, err
}
var redditResponse RedditResponse
err = json.Unmarshal(body, &redditResponse)
if err != nil {
log.Fatalf("Error unmarshalling JSON: %v", err)
return nil, err
}
for _, child := range redditResponse.Data.Children {
allPosts = append(allPosts, child.Data)
}
if len(allPosts) >= limit || redditResponse.Data.After == "" {
break
}
after = redditResponse.Data.After
}
log.Printf("Fetched %d posts", len(allPosts))
return allPosts[:limit], nil
}
func isValidImageURL(url string) bool {
re := regexp.MustCompile(`\.(gif|jpeg|jpg|png)$`)
return re.MatchString(strings.ToLower(url))
}
func downloadImage(url string) (image.Image, error) {
resp, err := http.Get(url)
if err != nil {
return nil, fmt.Errorf("failed to download image: %w", err)
}
defer resp.Body.Close()
img, format, err := image.Decode(resp.Body)
if err != nil {
return nil, fmt.Errorf("failed to decode image: %w", err)
}
log.Printf("Image format: %s", format)
return img, nil
}
func saveImageToFile(img image.Image, fileName string) error {
dir := "imgDls"
if err := os.MkdirAll(dir, 0755); err != nil {
return fmt.Errorf("failed to create directory: %w", err)
}
path := filepath.Join(dir, fileName)
file, err := os.Create(path)
if err != nil {
return fmt.Errorf("failed to create file: %w", err)
}
defer file.Close()
switch filepath.Ext(path) {
case ".jpg", ".jpeg":
err = jpeg.Encode(file, img, nil)
case ".png":
err = png.Encode(file, img)
case ".gif":
err = gif.Encode(file, img, nil)
default:
return fmt.Errorf("unsupported file extension: %s", filepath.Ext(path))
}
if err != nil {
return fmt.Errorf("failed to encode image: %w", err)
}
return nil
}
func resizeImage(img image.Image, maxWidth int) image.Image {
bounds := img.Bounds()
width := bounds.Dx()
height := bounds.Dy()
if width > maxWidth {
ratio := float64(maxWidth) / float64(width)
newWidth := int(float64(width) * ratio)
newHeight := int(float64(height) * ratio)
newImage := image.NewRGBA(image.Rect(0, 0, newWidth, newHeight))
draw.CatmullRom.Scale(newImage, newImage.Bounds(), img, bounds, draw.Over, nil)
return newImage
}
return img
}
func main() {
subreddit := flag.String("subreddit", "archlinux", "Name of the subreddit to fetch images from")
download := flag.Bool("download", false, "Download images to the current directory when true")
limit := flag.Int("limit", 25, "Number of posts to fetch")
flag.Parse()
a := app.New()
w := a.NewWindow("Reddit Image Feed")
log.Println("Fetching data from subreddit:", *subreddit)
posts, err := fetchRedditData(*subreddit, *limit)
if err != nil {
log.Fatalf("Error fetching data: %v", err)
return
}
content := container.NewVBox()
for _, post := range posts {
if isValidImageURL(post.URL) {
img, err := downloadImage(post.URL)
if err != nil {
log.Printf("Skipping post: %s - %s. Error: %v", post.Title, post.URL, err)
continue
}
img = resizeImage(img, 400)
image := canvas.NewImageFromImage(img)
image.FillMode = canvas.ImageFillOriginal
title := canvas.NewText(post.Title, theme.ForegroundColor())
title.TextStyle = fyne.TextStyle{Bold: true}
title.TextSize = 16
content.Add(title)
content.Add(image)
if *download {
fileName := fmt.Sprintf("%s%s", strings.ReplaceAll(post.Title, " ", "_"), filepath.Ext(post.URL))
filePath := filepath.Join(".", fileName)
err := saveImageToFile(img, filePath)
if err != nil {
log.Printf("Failed to save image: %v", err)
} else {
log.Printf("Saved image: %s", filePath)
}
}
} else {
log.Printf("Skipping non-image URL: %s", post.URL)
}
}
scroll := container.NewScroll(content)
w.SetContent(scroll)
w.Resize(fyne.NewSize(800, 600))
w.ShowAndRun()
}