bingapi: github.com/borglefink/bingapi Index | Files

package bingapi

import "github.com/borglefink/bingapi"

Package bingapi is an implementation of the Bing Search API v2.

Bing Search API v2 was closed down 31st of March 2017.

So, what is the Bing Search API? Microsoft explains:

The Bing Search API enables developers to embed search results in applications or
websites using XML or JSON. Add search functionality to a website, create unique
consumer or enterprise apps, or develop new mash-ups.

Registered users can (as of this writing) have 5000 queries/month for free, after that a fee is applied (until the next month). Read more about the Bing Search API at the Azure Data Marketplace, see https://datamarket.azure.com/dataset/bing/search.

To be able to use the Bing Search API an authorization key is needed. Get it from the Azure Data Marketplace Account Keys, see https://datamarket.azure.com/account/keys.

Example of usage:

package main

import (
	"fmt"
	"github.com/borglefink/bingapi"
)

func main() {
	var parameters = bingapi.GetParameters("[my query]")
	var client = bingapi.GetClient("[MyBingAuthKey]")

	var container, err = client.Web(parameters)

	if err != nil {
		panic(err)
	}

	for _, item := range container.Results {
		fmt.Printf("%s\n", item.Title)
	}
}

There are 7 different search formats; Web, News, Image, Video, Related, Spelling and Composite, the latter being a combination of the previous formats, and Spelling being returned with the Composite format. The parameters to the various searches differ, so be sure to use the correct parameters with each format, as described below.

Query                string  // All formats
Adult                string  // All formats
Latitude             float64 // All formats
Longitude            float64 // All formats
Market               string  // All formats
Options              string  // All formats

Sources              string  // Composite, web+image+video+news+spell

ImageFilters         string  // Image, Composite (when Sources=image)

NewsCategory         string  // News, Composite (when Sources=news)
NewsLocationOverride string  // News, Composite (when Sources=news)
NewsSortBy           string  // News, Composite (when Sources=news)

VideoFilters         string  // Video, Composite (when Sources=video)
VideoSortBy          string  // Video, Composite (when Sources=video)

WebFileType          string  // Web, Composite (when Sources=web)
WebSearchOptions     string  // Web, Composite (when Sources=web)

The data structures for returning the result are described separately, but the gist of it is that Bing returns a json structure similar to the json below, and this needs to be put into Go structs.

{                          // (wrapper)
  "d": {                   // e.g. WebResultContainer
    "results": [{          // e.g. []WebResult
      "__metadata": {
          "uri": "https://api.datamarket.azure.com/Data.ashx/Bing/Search/v1/Web?Query=\u0027xbox\u0027&$skip=0&$top=0",
          "type": "WebResult"
      },
      "ID": "03cfa295-fe87-4fd4-b919-0f85841a67d1",
      "Title": "Xbox | Official Site",
      "Description": "Experience the new generation of games and entertainment with Xbox.",
      "DisplayUrl": "www.xbox.com",
      "Url": "http://www.xbox.com/"
    }, {

      (plus 49 more results)

    }],
    "__next": "https://api.datamarket.azure.com/Data.ashx/Bing/Search/v1/Web?Query=\u0027xbox\u0027&$skip=50&$top=50"
  }
}

NB: Bing Search API can return results as json and as XML. Only json is implemented in this version of the package.

Index

Package Files

client.go common.go compositeresult.go doc.go imageresult.go newsresult.go parameters.go relatedresult.go spellingresult.go videoresult.go webresult.go

Constants

const (
    SearchTypeComposite = "Composite" // Composite type Bing Search API search
    SearchTypeWeb       = "Web"       // Web type Bing Search API search
    SearchTypeNews      = "News"      // News type Bing Search API search
    SearchTypeImage     = "Image"     // Image type Bing Search API search
    SearchTypeVideo     = "Video"     // Video type Bing Search API search
    SearchTypeRelated   = "Related"   // Related type Bing Search API search
)

Constants for searchtypes

type Client Uses

type Client struct {
    AccKey     string // Bing access key
    Method     string // Http method
    UserAgent  string // Bing call UserAgent
    ReqTimeout int32  // Request timeout
}

Client contains http and auth parameters for the API call.

func GetClient Uses

func GetClient(accessKey string) Client

GetClient creates a new set of API call parameters

func (Client) Composite Uses

func (client Client) Composite(parameters Parameters) (CompositeResultContainer, error)

Composite - executing Composite type Bing Search API search. Returns a CompositeResultContainer containing a slice of CompositeResult, where only the first element is used. See documentation for CompositeResultContainer and CompositeResult.

func (Client) Image Uses

func (client Client) Image(parameters Parameters) (ImageResultContainer, error)

Image - executing Image type Bing Image API search. Returns an ImageResultContainer containing a slice of ImageResult and an URI for next page. See documentation for ImageResultContainer and ImageResult.

func (Client) News Uses

func (client Client) News(parameters Parameters) (NewsResultContainer, error)

News - executing News type Bing Search API search. Returns a NewsResultContainer containing a slice of NewsResult and an URI for next page. See documentation for NewsResultContainer and NewsResult.

func (Client) Related Uses

func (client Client) Related(parameters Parameters) (RelatedResultContainer, error)

Related - executing Related type Bing Related API search. Returns a RelatedResultContainer containing a slice of RelatedResult and an URI for next page. See documentation for RelatedResultContainer and RelatedResult.

func (Client) Video Uses

func (client Client) Video(parameters Parameters) (VideoResultContainer, error)

Video - executing Video type Bing Search API search. Returns a VideoResultContainer containing a slice of VideoResult and an URI for next page. See documentation for VideoResultContainer and VideoResult.

func (Client) Web Uses

func (client Client) Web(parameters Parameters) (WebResultContainer, error)

Web - executing Web type Bing Search API search. Returns a WebResultContainer containing a slice of WebResult and an URI for next page. See documentation for WebResultContainer and WebResult.

type CompositeResult Uses

type CompositeResult struct {
    MetaData                 MetaData `json:"__metadata"`
    ID                       string
    WebTotal                 string // originally int64
    WebOffset                string // originally int64
    ImageTotal               string // originally int64
    ImageOffset              string // originally int64
    VideoTotal               string // originally int64
    VideoOffset              string // originally int64
    NewsTotal                string // originally int64
    NewsOffset               string // originally int64
    SpellingSuggestionsTotal string // originally int64
    AlteredQuery             string
    AlterationOverrideQuery  string
    Web                      []WebResult
    Image                    []ImageResult
    Video                    []VideoResult
    News                     []NewsResult
    RelatedSearch            []RelatedResult
    SpellingSuggestions      []SpellResult
}

CompositeResult receives all results from a composite Bing search. The totals and offsets are in the Bing documentation described as int64, but in the received json they were strings.

type CompositeResultContainer Uses

type CompositeResultContainer struct {
    Results []CompositeResult `json:"results"`
}

CompositeResultContainer receives a slice of CompositeResult. Only the first element in the slice is used.

type ImageResult Uses

type ImageResult struct {
    MetaData    MetaData `json:"__metadata"`
    ID          string
    Title       string
    MediaURL    string `json:"MediaUrl"`
    SourceURL   string `json:"SourceUrl"`
    DisplayURL  string `json:"DisplayUrl"`
    Width       int32
    Height      int32
    FileSize    int64
    ContentType string
    Thumbnail   Thumbnail
}

ImageResult receives a single result from the Bing search.

type ImageResultContainer Uses

type ImageResultContainer struct {
    Results     []ImageResult `json:"results"`
    NextPageURL string        `json:"__next"`
}

ImageResultContainer receives a slice of ImageResult and a pre-made URL for getting more results

type MetaData Uses

type MetaData struct {
    URI        string `json:"uri"`
    ResultType string `json:"type"`
}

MetaData holds meta data for each result. This is really just the URI for the specific result Metadata is describing, and the type of result. See e.g. the WebResult struct.

type NewsResult Uses

type NewsResult struct {
    MetaData    MetaData `json:"__metadata"`
    ID          string
    Title       string
    URL         string `json:"Url"`
    Source      string
    Description string
    Date        string
}

NewsResult receives a single result from the Bing search.

type NewsResultContainer Uses

type NewsResultContainer struct {
    Results     []NewsResult `json:"results"`
    NextPageURL string       `json:"__next"`
}

NewsResultContainer receives a slice of NewsResult and a pre-made URL for getting more results

type Parameters Uses

type Parameters struct {
    Query                string  // Required
    Skip                 int     // How may records to skip in the result
    Top                  int     // How may records to return (after Skip)
    Sources              string  // Composite, web+image+video+news+spell
    Adult                string  // Moderate
    ImageFilters         string  // Size:Small+Aspect:Square
    Latitude             float64 // 47.603450
    Longitude            float64 // -122.329696
    Market               string  // en-US
    NewsCategory         string  // rt_Business
    NewsLocationOverride string  // US.WA
    NewsSortBy           string  // Date
    Options              string  // EnableHighlighting
    VideoFilters         string  // Duration:Short+Resolution:High
    VideoSortBy          string  // Date
    WebFileType          string  // XLS
    WebSearchOptions     string  // DisableQueryAlterations
    RootURI              string  // Root URI for Bing search
}

Parameters holds the query parameters for all types of Bing Search API search See Bing Search API documentation for the correct combination for each type of search.

func GetParameters Uses

func GetParameters(query string) Parameters

GetParameters gets a Parameters object with default values set.

func (*Parameters) ExactSearch Uses

func (p *Parameters) ExactSearch() Parameters

ExactSearch sets the option to disable query alterations for web type search.

func (Parameters) GetURI Uses

func (p Parameters) GetURI(searchType string) string

GetURI returns the URI string based on the parameters set, and the given search type.

func (*Parameters) SetPage Uses

func (p *Parameters) SetPage(page int) Parameters

SetPage sets Skip to the correct value, based on the current Top (page size) value and the given 0-based page number.

func (*Parameters) SetSources Uses

func (p *Parameters) SetSources(sources string) Parameters

SetSources sets values for Sources. This is only used with composite search.

type RelatedResult Uses

type RelatedResult struct {
    MetaData MetaData `json:"__metadata"`
    ID       string
    Title    string
    BingURL  string `json:"BingUrl"`
}

RelatedResult receives a single result from the Bing search.

type RelatedResultContainer Uses

type RelatedResultContainer struct {
    Results     []RelatedResult `json:"results"`
    NextPageURL string          `json:"__next"`
}

RelatedResultContainer receives a slice of RelatedResult and a pre-made URL for getting more results

type SpellResult Uses

type SpellResult struct {
    MetaData MetaData `json:"__metadata"`
    ID       string
    Value    string
}

SpellResult receives a spelling suggestion from the Bing search. This struct is used with composite search.

type Thumbnail Uses

type Thumbnail struct {
    MediaURL    string `json:"MediaUrl"`
    ContentType string
    Width       int32
    Height      int32
    FileSize    int32
}

Thumbnail holds thumbnail data for an ImageResult or a VideoResult from a Bing Search API search.

type VideoResult Uses

type VideoResult struct {
    MetaData   MetaData `json:"__metadata"`
    ID         string
    Title      string
    MediaURL   string `json:"MediaUrl"`
    DisplayURL string `json:"DisplayUrl"`
    RunTime    int32
    Thumbnail  Thumbnail
}

VideoResult receives a single result from the Bing search.

type VideoResultContainer Uses

type VideoResultContainer struct {
    Results     []VideoResult `json:"results"`
    NextPageURL string        `json:"__next"`
}

VideoResultContainer receives a slice of VideoResult and a pre-made URL for getting more results

type WebResult Uses

type WebResult struct {
    MetaData    MetaData `json:"__metadata"`
    ID          string
    Title       string
    Description string
    DisplayURL  string `json:"DisplayUrl"`
    URL         string `json:"Url"`
}

WebResult receives a single result from the Bing search.

type WebResultContainer Uses

type WebResultContainer struct {
    Results     []WebResult `json:"results"`
    NextPageURL string      `json:"__next"`
}

WebResultContainer receives a slice of WebResult and a pre-made URL for getting more results

Package bingapi imports 5 packages (graph). Updated 2017-08-08. Refresh now. Tools for package owners.