@@ -320,34 +320,27 @@ func fmtText(text string) string {
320320 return t
321321}
322322
323- func FetchGoogle (conf * Config ) ( * GoogleSearchResult , error ) {
323+ func FetchGoogle (conf * Config , results map [ int ] * Result ) error {
324324 url := fmt .Sprintf ("https://www.googleapis.com/customsearch/v1?key=%s&cx=%s&q=%s" ,
325325 conf .ApiKey , conf .SearchEngine , netUrl .QueryEscape (conf .Query ))
326326 req , err := http .NewRequest (http .MethodGet , url , nil )
327327 if err != nil {
328- return nil , err
328+ return err
329329 }
330330 res , err := conf .Client .Do (req )
331331 if err != nil {
332- return nil , fmt .Errorf ("failed connecting to Google API: check your internet connection" )
332+ return fmt .Errorf ("failed connecting to Google API: check your internet connection" )
333333 }
334334 defer res .Body .Close ()
335335 if res .StatusCode > 299 {
336- return nil , fmt .Errorf ("failed connecting to Google API: %s" , res .Status )
336+ return fmt .Errorf ("failed connecting to Google API: %s" , res .Status )
337337 }
338338 var gsResp GoogleSearchResult
339339 err = json .NewDecoder (res .Body ).Decode (& gsResp )
340340 if err != nil {
341- return nil , err
341+ return err
342342 }
343- return & gsResp , nil
344- }
345-
346- func FetchStackOverflow (conf * Config , gr * GoogleSearchResult ) (map [int ]* Result , error ) {
347-
348- results := make (map [int ]* Result )
349- questions := make ([]string , 0 , len (gr .Items ))
350- for _ , item := range gr .Items {
343+ for _ , item := range gsResp .Items {
351344 var upvoteCount int
352345 var dateCreated time.Time
353346 if len (item .Pagemap .Question ) > 0 {
@@ -360,9 +353,7 @@ func FetchStackOverflow(conf *Config, gr *GoogleSearchResult) (map[int]*Result,
360353 dateCreated , _ = time .Parse ("2006-01-02T15:04:05" , question .Datecreated )
361354 }
362355 u , _ := netUrl .Parse (item .Link )
363- questionStr := strings .Split (u .Path , "/" )[2 ]
364- questions = append (questions , questionStr )
365- questionId , _ := strconv .Atoi (questionStr )
356+ questionId , _ := strconv .Atoi (strings .Split (u .Path , "/" )[2 ])
366357 results [questionId ] = & Result {
367358 Title : item .Title ,
368359 Link : item .Link ,
@@ -371,24 +362,34 @@ func FetchStackOverflow(conf *Config, gr *GoogleSearchResult) (map[int]*Result,
371362 Date : dateCreated ,
372363 }
373364 }
365+ return nil
366+ }
367+
368+ func FetchStackOverflow (conf * Config , results map [int ]* Result ) error {
369+ questions := make ([]string , len (results ))
370+ var idx int
371+ for question := range maps .Keys (results ) {
372+ questions [idx ] = strconv .Itoa (question )
373+ idx ++
374+ }
374375 url := fmt .Sprintf ("https://api.stackexchange.com/2.3/questions/%s/answers?order=desc&sort=votes&site=stackoverflow&filter=withbody" ,
375376 netUrl .QueryEscape (strings .Join (questions , ";" )))
376377 req , err := http .NewRequest (http .MethodGet , url , nil )
377378 if err != nil {
378- return nil , err
379+ return err
379380 }
380381 res , err := conf .Client .Do (req )
381382 if err != nil {
382- return nil , err
383+ return err
383384 }
384385 defer res .Body .Close ()
385386 if res .StatusCode > 299 {
386- return nil , fmt .Errorf ("failed connecting to Stack Overflow API: %s" , res .Status )
387+ return fmt .Errorf ("failed connecting to Stack Overflow API: %s" , res .Status )
387388 }
388389 var soResp StackOverflowResult
389390 err = json .NewDecoder (res .Body ).Decode (& soResp )
390391 if err != nil {
391- return nil , err
392+ return err
392393 }
393394 for _ , item := range soResp .Items {
394395 result , ok := results [item .QuestionID ]
@@ -405,12 +406,12 @@ func FetchStackOverflow(conf *Config, gr *GoogleSearchResult) (map[int]*Result,
405406 Date : time .Unix (int64 (item .CreationDate ), 0 ).UTC (),
406407 })
407408 }
408- return results , nil
409+ return nil
409410}
410411
411412func GetAnswers (conf * Config ,
412- fetchResults func (* Config ) ( * GoogleSearchResult , error ) ,
413- fetchAnswers func (* Config , * GoogleSearchResult ) ( map [int ]* Result , error ) ,
413+ fetchResults func (* Config , map [ int ] * Result ) error ,
414+ fetchAnswers func (* Config , map [int ]* Result ) error ,
414415) (string , error ) {
415416 var err error
416417 if term .IsTerminal (0 ) {
@@ -435,11 +436,12 @@ func GetAnswers(conf *Config,
435436 if lexer == nil {
436437 lexer = lexers .Fallback
437438 }
438- gsResp , err := fetchResults (conf )
439+ results := make (map [int ]* Result )
440+ err = fetchResults (conf , results )
439441 if err != nil {
440442 return "" , err
441443 }
442- results , err : = fetchAnswers (conf , gsResp )
444+ err = fetchAnswers (conf , results )
443445 if err != nil {
444446 return "" , err
445447 }
0 commit comments