|
| 1 | +package main |
| 2 | +import ( |
| 3 | + "fmt" |
| 4 | + "net/http" |
| 5 | + "io/ioutil" |
| 6 | + "time" |
| 7 | + "encoding/json" |
| 8 | +) |
| 9 | + |
| 10 | +// This struct formats the answers provided by the Bing Web Search API. |
| 11 | +type BingAnswer struct { |
| 12 | + Type string `json:"_type"` |
| 13 | + QueryContext struct { |
| 14 | + OriginalQuery string `json:"originalQuery"` |
| 15 | + } `json:"queryContext"` |
| 16 | + WebPages struct { |
| 17 | + WebSearchURL string `json:"webSearchUrl"` |
| 18 | + TotalEstimatedMatches int `json:"totalEstimatedMatches"` |
| 19 | + Value []struct { |
| 20 | + ID string `json:"id"` |
| 21 | + Name string `json:"name"` |
| 22 | + URL string `json:"url"` |
| 23 | + IsFamilyFriendly bool `json:"isFamilyFriendly"` |
| 24 | + DisplayURL string `json:"displayUrl"` |
| 25 | + Snippet string `json:"snippet"` |
| 26 | + DateLastCrawled time.Time `json:"dateLastCrawled"` |
| 27 | + SearchTags []struct { |
| 28 | + Name string `json:"name"` |
| 29 | + Content string `json:"content"` |
| 30 | + } `json:"searchTags,omitempty"` |
| 31 | + About []struct { |
| 32 | + Name string `json:"name"` |
| 33 | + } `json:"about,omitempty"` |
| 34 | + } `json:"value"` |
| 35 | + } `json:"webPages"` |
| 36 | + RelatedSearches struct { |
| 37 | + ID string `json:"id"` |
| 38 | + Value []struct { |
| 39 | + Text string `json:"text"` |
| 40 | + DisplayText string `json:"displayText"` |
| 41 | + WebSearchURL string `json:"webSearchUrl"` |
| 42 | + } `json:"value"` |
| 43 | + } `json:"relatedSearches"` |
| 44 | + RankingResponse struct { |
| 45 | + Mainline struct { |
| 46 | + Items []struct { |
| 47 | + AnswerType string `json:"answerType"` |
| 48 | + ResultIndex int `json:"resultIndex"` |
| 49 | + Value struct { |
| 50 | + ID string `json:"id"` |
| 51 | + } `json:"value"` |
| 52 | + } `json:"items"` |
| 53 | + } `json:"mainline"` |
| 54 | + Sidebar struct { |
| 55 | + Items []struct { |
| 56 | + AnswerType string `json:"answerType"` |
| 57 | + Value struct { |
| 58 | + ID string `json:"id"` |
| 59 | + } `json:"value"` |
| 60 | + } `json:"items"` |
| 61 | + } `json:"sidebar"` |
| 62 | + } `json:"rankingResponse"` |
| 63 | +} |
| 64 | + |
| 65 | +// Declare the main function. This is required for all Go programs. |
| 66 | +func main() { |
| 67 | + const endpoint = "https://api.cognitive.microsoft.com/bing/v7.0/search" |
| 68 | + token := "YOUR-ACCESS-KEY" |
| 69 | + searchTerm := "Microsoft Cognitive Services" |
| 70 | + |
| 71 | + // Declare a new GET request. |
| 72 | + req, err := http.NewRequest("GET", endpoint, nil) |
| 73 | + if err != nil { |
| 74 | + panic(err) |
| 75 | + } |
| 76 | + |
| 77 | + // Add the payload to the request. |
| 78 | + param := req.URL.Query() |
| 79 | + param.Add("q", searchTerm) |
| 80 | + req.URL.RawQuery = param.Encode() |
| 81 | + |
| 82 | + // Insert the request header. |
| 83 | + req.Header.Add("Ocp-Apim-Subscription-Key", token) |
| 84 | + |
| 85 | + // Instantiate a client. |
| 86 | + client := new(http.Client) |
| 87 | + // Send the request to Bing. |
| 88 | + resp, err := client.Do(req) |
| 89 | + if err != nil { |
| 90 | + panic(err) |
| 91 | + } |
| 92 | + // Close the connection. |
| 93 | + defer resp.Body.Close() |
| 94 | + body, err := ioutil.ReadAll(resp.Body) |
| 95 | + if err != nil { |
| 96 | + panic(err) |
| 97 | + } |
| 98 | + //Create a new answer struct. |
| 99 | + ans := new(BingAnswer) |
| 100 | + err = json.Unmarshal(body, &ans) |
| 101 | + if err != nil { |
| 102 | + fmt.Println(err) |
| 103 | + } |
| 104 | + // Iterate over search results and print the result name and URL. |
| 105 | + for _, result := range ans.WebPages.Value { |
| 106 | + fmt.Println(result.Name, "||", result.URL) |
| 107 | + } |
| 108 | + |
| 109 | +} |
0 commit comments