Skip to content

Commit 889f393

Browse files
committed
do not download wallpapers
1 parent 2a2acc0 commit 889f393

3 files changed

Lines changed: 8 additions & 80 deletions

File tree

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ go 1.25
55
require (
66
cloud.google.com/go/firestore v1.15.0
77
cloud.google.com/go/pubsub v1.38.0
8-
cloud.google.com/go/storage v1.40.0
98
cloud.google.com/go/translate v1.10.3
109
cloud.google.com/go/vision/v2 v2.8.2
1110
firebase.google.com/go v3.13.0+incompatible
@@ -30,6 +29,7 @@ require (
3029
cloud.google.com/go/compute/metadata v0.3.0 // indirect
3130
cloud.google.com/go/iam v1.1.8 // indirect
3231
cloud.google.com/go/longrunning v0.5.7 // indirect
32+
cloud.google.com/go/storage v1.40.0 // indirect
3333
github.com/cespare/xxhash/v2 v2.3.0 // indirect
3434
github.com/davecgh/go-spew v1.1.1 // indirect
3535
github.com/dlclark/regexp2 v1.11.5 // indirect

internal/updater/image/image.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ type Image struct {
3232
}
3333

3434
func (i Image) URL(bingURL string) string {
35-
return bingURL + i.URLBase + "_1920x1200.jpg"
35+
return bingURL + i.URLBase + "_1920x1080.jpg"
3636
}
3737

3838
func From(bw bing.Image, market string) (Image, error) {

internal/updater/updater.go

Lines changed: 6 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package updater
33
import (
44
"context"
55
"fmt"
6-
"io"
76
"net/http"
87
"os"
98
"slices"
@@ -14,7 +13,6 @@ import (
1413
"api/internal/updater/bing"
1514
"api/internal/updater/firestore"
1615
imgpkg "api/internal/updater/image"
17-
"cloud.google.com/go/storage"
1816
"cloud.google.com/go/translate"
1917
"cloud.google.com/go/vision/v2/apiv1"
2018
"cloud.google.com/go/vision/v2/apiv1/visionpb"
@@ -27,7 +25,6 @@ import (
2725
const (
2826
TopicID = "update-wallpapers-v2"
2927
SubID = "update-wallpapers-v2-sub"
30-
bucketName = "images.sonurai.com"
3128
firestoreCollection = "BingWallpapers"
3229
bingURL = "https://www.bing.com"
3330
)
@@ -68,19 +65,6 @@ func New() (*Updater, error) {
6865
return nil, err
6966
}
7067

71-
storageClient, err := firebaseClient.Storage(ctx)
72-
if err != nil {
73-
return nil, err
74-
}
75-
76-
bucket, err := storageClient.Bucket(bucketName)
77-
if err != nil {
78-
return nil, err
79-
}
80-
if _, err := bucket.Attrs(ctx); err != nil {
81-
return nil, err
82-
}
83-
8468
annoClient, err := vision.NewImageAnnotatorClient(ctx)
8569
if err != nil {
8670
return nil, err
@@ -93,19 +77,15 @@ func New() (*Updater, error) {
9377

9478
return &Updater{
9579
annoClient: annoClient,
96-
bucket: bucket,
9780
firestoreClient: firestoreClient,
98-
httpClient: httpClient,
9981
imageClient: imageClient,
10082
translateClient: translateClient,
10183
}, nil
10284
}
10385

10486
type Updater struct {
10587
annoClient *vision.ImageAnnotatorClient
106-
bucket *storage.BucketHandle
10788
firestoreClient *firestore.Client
108-
httpClient *http.Client
10989
imageClient *bing.Client
11090
translateClient *translate.Client
11191
}
@@ -146,18 +126,8 @@ func (u *Updater) Update(ctx context.Context) error {
146126
continue
147127
}
148128

149-
imageURL := image.URL(bingURL)
150-
if !u.fileExists(imageURL) {
151-
continue
152-
}
153-
154129
fmt.Printf("%s new wallpaper found\n", image.ID)
155130

156-
// todo: add retry if error
157-
if err := u.downloadFile(ctx, imageURL, image.Filename+".jpg"); err != nil {
158-
return err
159-
}
160-
161131
// translate title if not english
162132
if slices.Contains(nonENMarkets, image.Market) {
163133
translatedTitle, err := u.translateText(ctx, image.Title)
@@ -168,7 +138,7 @@ func (u *Updater) Update(ctx context.Context) error {
168138
}
169139
}
170140

171-
anno, err := u.annotateImage(ctx, image.Filename+".jpg")
141+
anno, err := u.annotateImage(ctx, image.URL(bingURL))
172142
if err != nil {
173143
return err
174144
}
@@ -219,13 +189,12 @@ func (u *Updater) Update(ctx context.Context) error {
219189
}
220190

221191
func (u *Updater) annotateImage(ctx context.Context, url string) (*visionpb.AnnotateImageResponse, error) {
222-
url = fmt.Sprintf("gs://%s/%s", bucketName, url)
223192
req := &visionpb.BatchAnnotateImagesRequest{
224193
Requests: []*visionpb.AnnotateImageRequest{
225194
{
226195
Image: &visionpb.Image{
227196
Source: &visionpb.ImageSource{
228-
GcsImageUri: url,
197+
ImageUri: url,
229198
},
230199
},
231200
Features: []*visionpb.Feature{
@@ -259,36 +228,6 @@ func (u *Updater) annotateImage(ctx context.Context, url string) (*visionpb.Anno
259228
return resp, nil
260229
}
261230

262-
func (u *Updater) downloadFile(ctx context.Context, url string, name string) error {
263-
req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil)
264-
if err != nil {
265-
return fmt.Errorf("failed to create request: %w", err)
266-
}
267-
268-
resp, err := u.httpClient.Do(req)
269-
if err != nil {
270-
return fmt.Errorf("failed to download file: %w", err)
271-
}
272-
defer resp.Body.Close()
273-
274-
if resp.StatusCode != http.StatusOK {
275-
return fmt.Errorf("failed to download file: unexpected status %d", resp.StatusCode)
276-
}
277-
278-
objWriter := u.bucket.Object(name).NewWriter(ctx)
279-
280-
_, err = io.Copy(objWriter, resp.Body)
281-
if err != nil {
282-
return fmt.Errorf("failed to write file: %w", err)
283-
}
284-
285-
if err = objWriter.Close(); err != nil {
286-
return fmt.Errorf("failed to close object writer: %w", err)
287-
}
288-
289-
return nil
290-
}
291-
292231
func (u *Updater) fetchAndDedupeImages(ctx context.Context, markets []string, out map[string]imgpkg.Image) error {
293232
for _, market := range markets {
294233
bi, err := u.imageClient.List(ctx, market)
@@ -297,6 +236,10 @@ func (u *Updater) fetchAndDedupeImages(ctx context.Context, markets []string, ou
297236
}
298237

299238
for _, v := range bi {
239+
if !v.WP {
240+
continue
241+
}
242+
300243
image, err := imgpkg.From(v, market)
301244
if err != nil {
302245
return err
@@ -310,21 +253,6 @@ func (u *Updater) fetchAndDedupeImages(ctx context.Context, markets []string, ou
310253
return nil
311254
}
312255

313-
func (u *Updater) fileExists(url string) bool {
314-
req, err := http.NewRequest(http.MethodHead, url, nil)
315-
if err != nil {
316-
return false
317-
}
318-
319-
resp, err := u.httpClient.Do(req)
320-
if err != nil {
321-
return false
322-
}
323-
defer resp.Body.Close()
324-
325-
return resp.StatusCode == http.StatusOK
326-
}
327-
328256
func (u *Updater) translateText(ctx context.Context, text string) (string, error) {
329257
lang, _ := language.Parse("en")
330258
opts := &translate.Options{Format: "text"}

0 commit comments

Comments
 (0)