@@ -41,28 +41,18 @@ func md5hash(s string) string {
4141 return hex .EncodeToString (sum )
4242}
4343
44- // false -> MISS
45- // true -> HIT
46- func openOrCreateFile (filename string ) (* os.File , bool , error ) {
47- f , err := os .Open (filename )
44+ // false -> new file
45+ // true -> already exists
46+ func openFileIfNotExists (filename string ) (* os.File , bool , error ) {
47+ _ , err := os .Stat (filename )
4848 if os .IsNotExist (err ) {
4949 f , err := os .Create (filename )
5050 return f , false , err
5151 }
52- return f , true , err
53- }
54-
55- func placeBrokenImage () string {
56- filename := "/tmp/net.nkcmr.alfred-apple-app-search-broken-image.png"
57- f , ok , err := openOrCreateFile (filename )
5852 if err != nil {
59- return ""
60- }
61- defer f .Close ()
62- if ! ok {
63- f .Write (brokenImageImage )
53+ return nil , false , err
6454 }
65- return filename
55+ return nil , true , err
6656}
6757
6858func downloadAllImages (ctx context.Context , concurrency int , urls []string ) []* aw.Icon {
@@ -75,12 +65,8 @@ func downloadAllImages(ctx context.Context, concurrency int, urls []string) []*a
7565 output := make ([]* aw.Icon , len (urls ))
7666 var wg sync.WaitGroup
7767 sem := make (chan bool , concurrency )
78- brokenimage := placeBrokenImage ()
7968 dl := func (i int , url string ) {
80- output [i ] = & aw.Icon {
81- Type : aw .IconTypeImage ,
82- Value : brokenimage ,
83- }
69+ output [i ] = aw .IconError
8470 filename := fmt .Sprintf (
8571 "%s/net.nkcmr.alfred-apple-app-search/%s.png" ,
8672 strings .TrimRight (os .TempDir (), "/" ),
@@ -90,16 +76,20 @@ func downloadAllImages(ctx context.Context, concurrency int, urls []string) []*a
9076 die (err .Error ())
9177 return
9278 }
93- f , hit , err := openOrCreateFile (filename )
79+ f , x , err := openFileIfNotExists (filename )
9480 if err != nil {
9581 die (
9682 "failed to create or open file for downloaded artwork: %s" ,
9783 err .Error (),
9884 )
9985 return
10086 }
101- defer f .Close ()
102- if ! hit {
87+ defer func () {
88+ if f != nil {
89+ defer f .Close ()
90+ }
91+ }()
92+ if ! x {
10393 debug ("downloading: %s to %s" , url , filename )
10494 resp , err := client .Get (url )
10595 if err != nil {
@@ -114,7 +104,10 @@ func downloadAllImages(ctx context.Context, concurrency int, urls []string) []*a
114104 } else {
115105 debug ("file is cached (%s)" , filename )
116106 }
117- output [i ].Value = filename
107+ output [i ] = & aw.Icon {
108+ Type : aw .IconTypeImage ,
109+ Value : filename ,
110+ }
118111 }
119112 for i , u := range urls {
120113 wg .Add (1 )
@@ -151,14 +144,13 @@ func main() {
151144 }()
152145 ctx := sigContext ()
153146 url , err := url .ParseRequestURI (
154- "https://itunes.apple.com/search?media=software&entity=macSoftware" ,
147+ "https://itunes.apple.com/search?media=software&entity=macSoftware&limit=20 " ,
155148 )
156149 if err != nil {
157150 panic (err )
158151 }
159152 q := url .Query ()
160153 q .Set ("term" , os .Args [1 ])
161- q .Set ("limit" , "20" )
162154 url .RawQuery = q .Encode ()
163155 req , err := http .NewRequest ("GET" , url .String (), http .NoBody )
164156 if err != nil {
@@ -195,9 +187,14 @@ func main() {
195187 Title (res .Name ).
196188 Subtitle (
197189 fmt .Sprintf (
198- "%s | %s (%d ratings)" ,
190+ "%s | %s(%d ratings)" ,
199191 res .PriceFmt ,
200- strings .Repeat (string (star ), int (res .Rating )),
192+ func () string {
193+ if res .Rating == float64 (0 ) {
194+ return ""
195+ }
196+ return strings .Repeat (string (star ), int (res .Rating )) + " "
197+ }(),
201198 res .NumRatings ,
202199 ),
203200 ).
@@ -214,108 +211,3 @@ func main() {
214211 }
215212 json .NewEncoder (os .Stdout ).Encode (fb )
216213}
217-
218- // raw data from https://github.com/chromium/chromium/blob/952de60c9b492e04e7bb4ded108bff186010be0a/third_party/blink/public/default_200_percent/blink/broken_image.png
219- var brokenImageImage = []byte {
220- 0x89 , 0x50 , 0x4e , 0x47 , 0x0d , 0x0a , 0x1a , 0x0a ,
221- 0x00 , 0x00 , 0x00 , 0x0d , 0x49 , 0x48 , 0x44 , 0x52 ,
222- 0x00 , 0x00 , 0x00 , 0x1c , 0x00 , 0x00 , 0x00 , 0x20 ,
223- 0x08 , 0x06 , 0x00 , 0x00 , 0x00 , 0x01 , 0xb5 , 0x18 ,
224- 0x42 , 0x00 , 0x00 , 0x02 , 0xf1 , 0x49 , 0x44 , 0x41 ,
225- 0x54 , 0x78 , 0x5e , 0xbd , 0x96 , 0xcb , 0x6b , 0x13 ,
226- 0x51 , 0x14 , 0x87 , 0x7f , 0x77 , 0x32 , 0x49 , 0xda ,
227- 0x5a , 0x6b , 0x2d , 0xe2 , 0x0b , 0xc1 , 0xae , 0xc4 ,
228- 0xee , 0x14 , 0xbb , 0x10 , 0x71 , 0x27 , 0xa2 , 0x2b ,
229- 0x17 , 0x05 , 0x05 , 0x75 , 0xd5 , 0xd4 , 0x8a , 0x7f ,
230- 0x45 , 0xbb , 0x71 , 0xe1 , 0xc2 , 0x85 , 0xa8 , 0x20 ,
231- 0xb6 , 0x4d , 0x8b , 0x4a , 0x95 , 0xaa , 0x18 , 0x70 ,
232- 0x61 , 0x29 , 0x8a , 0xa5 , 0x2a , 0x16 , 0x69 , 0xa5 ,
233- 0x0f , 0x51 , 0x14 , 0x7c , 0x94 , 0x16 , 0x1f , 0x6d ,
234- 0x4d , 0xd2 , 0xa6 , 0xc9 , 0x34 , 0x99 , 0x4c , 0xe6 ,
235- 0x98 , 0x1c , 0x99 , 0x0e , 0xc3 , 0x9d , 0xa1 , 0xb6 ,
236- 0x49 , 0xfc , 0xe0 , 0x70 , 0xb9 , 0x93 , 0x21 , 0xdf ,
237- 0xfc , 0xce , 0x9c , 0x4b , 0x22 , 0xba , 0xba , 0xc2 ,
238- 0x84 , 0x12 , 0x92 , 0xcd , 0x66 , 0x1b , 0x9b , 0xce ,
239- 0x9e , 0x7f , 0x0b , 0x0f , 0x58 , 0x78 , 0xe2 , 0x64 ,
240- 0x33 , 0x4c , 0x2a , 0xce , 0xab , 0x08 , 0x81 , 0x81 ,
241- 0xc7 , 0xbd , 0x48 , 0x26 , 0x93 , 0x00 , 0x84 , 0xa7 ,
242- 0x54 , 0x25 , 0x22 , 0xa4 , 0x32 , 0x84 , 0x4c , 0xb6 ,
243- 0x38 , 0x61 , 0xd0 , 0x0f , 0xa6 , 0xa5 , 0xe5 , 0x1c ,
244- 0xc2 , 0xe1 , 0xae , 0xd1 , 0x47 , 0xbd , 0x1d , 0x8d ,
245- 0x4d , 0x67 , 0x5a , 0x25 , 0xa9 , 0xca , 0x6d , 0xc8 ,
246- 0x11 , 0x96 , 0x75 , 0xb3 , 0xb8 , 0x84 , 0x8a , 0xc2 ,
247- 0xab , 0x43 , 0x7a , 0xb7 , 0x53 , 0x92 , 0x2a , 0x44 ,
248- 0x04 , 0xe2 , 0x0d , 0x15 , 0x55 , 0x04 , 0xc6 , 0xda ,
249- 0xe7 , 0xa5 , 0x2d , 0x00 , 0xa8 , 0x20 , 0x3d , 0x20 ,
250- 0x25 , 0xb4 , 0x74 , 0x25 , 0x86 , 0xa5 , 0xe1 , 0x70 ,
251- 0xd8 , 0x91 , 0x94 , 0x13 , 0xb2 , 0x8d , 0x48 , 0xaa ,
252- 0xcd , 0x55 , 0x02 , 0x0d , 0x3b , 0xfd , 0xd8 , 0xb7 ,
253- 0x3b , 0x50 , 0x58 , 0x79 , 0x0f , 0x22 , 0x8f , 0x02 ,
254- 0x43 , 0x44 , 0x8e , 0x0a , 0x85 , 0x42 , 0x2b , 0x49 ,
255- 0x89 , 0xc0 , 0x43 , 0x63 , 0xf9 , 0x1c , 0xd4 , 0x6e ,
256- 0x50 , 0x50 , 0xbf , 0xd5 , 0x0f , 0x8b , 0x8a , 0x80 ,
257- 0xe0 , 0x3d , 0xcd , 0x66 , 0x11 , 0x4f , 0xc9 , 0xef ,
258- 0x9b , 0xac , 0x95 , 0x20 , 0xd1 , 0xdc , 0x1c , 0x42 ,
259- 0x4f , 0x4f , 0xf7 , 0x68 , 0xe4 , 0x5e , 0x67 , 0xa3 ,
260- 0x0a , 0x8f , 0x1b , 0x77 , 0x6c , 0x56 , 0xe1 , 0xc6 ,
261- 0xf6 , 0xfc , 0xf5 , 0x58 , 0x52 , 0x87 , 0x0b , 0x7c ,
262- 0x24 , 0xf2 , 0x5f , 0x0c , 0x2f , 0x84 , 0x10 , 0xa3 ,
263- 0x9c , 0xb0 , 0x80 , 0xb5 , 0x6e , 0xaa , 0xe2 , 0x64 ,
264- 0xf0 , 0xab , 0x02 , 0x6e , 0x54 , 0x06 , 0x04 , 0x6a ,
265- 0x2a , 0x05 , 0x16 , 0x35 , 0x67 , 0xca , 0x4c , 0xc6 ,
266- 0xc0 , 0xe1 , 0x63 , 0xa7 , 0xf3 , 0x9f , 0x2b , 0x70 ,
267- 0xc3 , 0xaf , 0xfa , 0xf0 , 0x24 , 0x72 , 0x07 , 0xb6 ,
268- 0x10 , 0xbc , 0xca , 0x32 , 0x19 , 0xbe , 0x67 , 0x7c ,
269- 0x2a , 0x0d , 0x0b , 0xeb , 0x68 , 0xa9 , 0xbe , 0x20 ,
270- 0x96 , 0x73 , 0x1e , 0xe9 , 0xfc , 0x01 , 0x0e , 0xc5 ,
271- 0x42 , 0x10 , 0x58 , 0xd7 , 0xb0 , 0x2b , 0xc8 , 0xb2 ,
272- 0x55 , 0x11 , 0xf2 , 0x54 , 0xe7 , 0x28 , 0x5f , 0x06 ,
273- 0xc1 , 0x0b , 0xbf , 0x4a , 0xb6 , 0x90 , 0x31 , 0xd9 ,
274- 0xba , 0x2a , 0xba , 0x41 , 0x98 , 0x9a , 0xd5 , 0xf9 ,
275- 0xfe , 0xb5 , 0xc2 , 0x42 , 0x80 , 0x3d , 0xec , 0x7b ,
276- 0x3f , 0x9d , 0x46 , 0xb9 , 0x20 , 0x30 , 0xd2 , 0x3b ,
277- 0x2c , 0x27 , 0x76 , 0x4b , 0xd9 , 0x49 , 0xe5 , 0x96 ,
278- 0xd9 , 0x42 , 0xcb , 0xfe , 0x3f , 0x12 , 0x8a , 0x15 ,
279- 0xa1 , 0x59 , 0x7e , 0x9f , 0xf8 , 0x9f , 0x2d , 0x15 ,
280- 0x52 , 0x4b , 0xcd , 0x7f , 0x32 , 0x12 , 0xe2 , 0xfa ,
281- 0x0c , 0x34 , 0x23 , 0x86 , 0xa0 , 0xaf , 0x1a , 0x75 ,
282- 0x81 , 0x7a , 0x28 , 0x42 , 0x5d , 0x55 , 0xe6 , 0x7a ,
283- 0x2c , 0x40 , 0x26 , 0xbc , 0xc8 , 0x98 , 0x29 , 0x4c ,
284- 0xc6 , 0x23 , 0xf8 , 0x90 , 0xe8 , 0x67 , 0x99 , 0x45 ,
285- 0x50 , 0xa9 , 0xc6 , 0xde , 0x9a , 0xa3 , 0xd8 , 0x5f ,
286- 0x77 , 0x0a , 0x95 , 0xbe , 0x5a , 0x57 , 0x99 , 0xe7 ,
287- 0xb1 , 0x30 , 0x3d , 0x7c , 0xd3 , 0xda , 0x08 , 0x86 ,
288- 0xe6 , 0xae , 0x40 , 0xcb , 0xc5 , 0x5c , 0x1e , 0x24 ,
289- 0x89 , 0x89 , 0x85 , 0x08 , 0x3e , 0x26 , 0x9e , 0xe2 ,
290- 0xd0 , 0x96 , 0x0b , 0xd8 , 0xb3 , 0xf1 , 0x88 , 0x53 ,
291- 0x26 , 0x0f , 0x8d , 0xfd , 0x7b , 0xe8 , 0xc6 , 0x58 ,
292- 0xbc , 0x0f , 0xfd , 0x3f , 0xdb , 0x25 , 0x99 , 0x9b ,
293- 0x78 , 0x70 , 0xee , 0x32 , 0x86 , 0xa3 , 0x1d , 0x00 ,
294- 0xc8 , 0x55 , 0x06 , 0x62 , 0x21 , 0x71 , 0x42 , 0xe2 ,
295- 0x84 , 0x4e , 0xeb , 0x9b , 0x58 , 0x37 , 0x26 , 0x17 ,
296- 0x1f , 0x62 , 0x2d , 0x4c , 0xe6 , 0xd3 , 0x12 , 0x41 ,
297- 0x1c , 0xac , 0x6b , 0x75 , 0xf5 , 0xd9 , 0x09 , 0x19 ,
298- 0x5a , 0xa9 , 0xf1 , 0x85 , 0xfb , 0x2c , 0x5b , 0x07 ,
299- 0xe2 , 0xdd , 0x62 , 0x04 , 0x9f , 0x96 , 0x06 , 0x58 ,
300- 0x61 , 0x17 , 0x4b , 0xc0 , 0x42 , 0x38 , 0xc1 , 0xb7 ,
301- 0xd4 , 0x2b , 0x8c , 0xc4 , 0x6f , 0xad , 0x4b , 0x66 ,
302- 0x6d , 0x5e , 0x47 , 0x6f , 0x62 , 0xc9 , 0xf8 , 0xe5 ,
303- 0x9c , 0x17 , 0xb7 , 0xa1 , 0x49 , 0x18 , 0x3f , 0xf0 ,
304- 0xe2 , 0xf7 , 0x55 , 0x00 , 0xb4 , 0x5e , 0x19 , 0x63 ,
305- 0x50 , 0x1a , 0x2f , 0xe7 , 0xaf , 0xe3 , 0xf8 , 0xb6 ,
306- 0x8b , 0x0e , 0x99 , 0xa3 , 0xa5 , 0x39 , 0x33 , 0x8b ,
307- 0xe7 , 0xf3 , 0x97 , 0xa0 , 0x9b , 0xa9 , 0x22 , 0x64 ,
308- 0x36 , 0xdf , 0xd3 , 0x63 , 0xf4 , 0x39 , 0x39 , 0x58 ,
309- 0x90 , 0x70 , 0x49 , 0x2d , 0x9d , 0x48 , 0x3c , 0x40 ,
310- 0x54 , 0xff , 0x52 , 0xac , 0xcc , 0xd1 , 0xc6 , 0x91 ,
311- 0x85 , 0x1e , 0x9e , 0x60 , 0x02 , 0x39 , 0xff , 0x08 ,
312- 0x47 , 0xd3 , 0x33 , 0x79 , 0x61 , 0x5f , 0x29 , 0x65 ,
313- 0x8c , 0x96 , 0x8b , 0x62 , 0x38 , 0x7e , 0xc3 , 0xba ,
314- 0x68 , 0x27 , 0x7c , 0x36 , 0x73 , 0x0d , 0x26 , 0x19 ,
315- 0x25 , 0x94 , 0xd9 , 0x7c , 0x4d , 0x0d , 0x61 , 0x5c ,
316- 0xbb , 0x0d , 0x3d , 0xa7 , 0xf1 , 0x5e , 0xb4 , 0xb5 ,
317- 0xb5 , 0x93 , 0xa6 , 0x69 , 0x25 , 0x96 , 0xc9 , 0x90 ,
318- 0x20 , 0x54 , 0x54 , 0x05 , 0xf0 , 0x07 , 0x9f , 0xba ,
319- 0x0c , 0x56 , 0x38 , 0x85 , 0x33 , 0xe1 , 0x00 , 0x00 ,
320- 0x00 , 0x00 , 0x49 , 0x45 , 0x4e , 0x44 , 0xae , 0x42 ,
321- }
0 commit comments