Exemple #1
0
func (mg memeGen) generate(firstMsg string, secondMsg string) (string, error) {
	url := "http://version1.api.memegenerator.net/Instance_Create"
	url = url + "?username="******"&password="******"&languageCode=en"
	url = url + "&generatorID=" + strconv.Itoa(mg.generator)
	url = url + "&imageID=" + strconv.Itoa(mg.image)
	url = url + "&text0=" + neturl.QueryEscape(firstMsg)
	url = url + "&text1=" + neturl.QueryEscape(secondMsg)

	var decoded map[string]interface{}
	if err := util.UnmarshalUrl(url, &decoded); err != nil {
		return "", err
	}

	if result := decoded["result"]; result != nil {
		switch result := result.(type) {
		case map[string]interface{}:
			switch image := result["instanceImageUrl"].(type) {
			case string:
				return image, nil
			}
		}
	}
	return "", nil
}
Exemple #2
0
// Search queries google for some images, and then randomly selects one
func search(search string) (string, error) {
	searchUrl := "http://ajax.googleapis.com/ajax/services/search/images?v=1.0&q=" + neturl.QueryEscape(search)
	var gisResponse gisResponse
	if err := util.UnmarshalUrl(searchUrl, &gisResponse); err != nil {
		return "", err
	}

	if gisResponse.ResponseData.Results == nil {
		return "", fmt.Errorf("No results were returned for query %s", search)
	}

	results := *gisResponse.ResponseData.Results

	if len(results) > 0 {

		// start a goroutine to determine image info for each response result
		// we have to use buffered channels so that the senders don't hang on send after the main method exits
		imageUrlCh := make(chan string, len(results))
		errorsCh := make(chan error, len(results))
		for _, resultUrl := range results {
			go getImageInfo(resultUrl.Url, imageUrlCh, errorsCh)
		}

		// until a timeout is met, build a collection of urls
		totalResults := len(results)
		remainingResults := totalResults
		urls := make([]string, 0, totalResults)
		errors := make([]error, 0, totalResults)
		timeout := time.After(2 * time.Second)

	SEARCH:
		for remainingResults > 0 {
			select {
			case url := <-imageUrlCh:
				urls = append(urls, url)
				remainingResults--
			case err := <-errorsCh:
				errors = append(errors, err)
				remainingResults--
			case <-timeout:
				break SEARCH
			}
		}
		if len(urls) == 0 {
			return "", fmt.Errorf("No image could be found for \"%s\"", search)
		}
		return urls[rand.Intn(len(urls))], nil

	}
	return "", fmt.Errorf("No image could be found for \"%s\"", search)
}
Exemple #3
0
// Search youtube for the given query string. Returns one of the first N youtube
// results for that search at random (everyone loves entropy!)
// Returns an empty string if there were no results for that query
func search(query string, results int) (link string, err error) {
	var searchResponse youTubeResponse
	if err = util.UnmarshalUrl(buildSearchUrl(query, results), &searchResponse); err != nil {
		return
	}
	videos := searchResponse.Data.Items
	switch l := len(videos); {
	case l > 1:
		ordering := rand.Perm(len(videos))
		for _, i := range ordering {
			// Youtube adds a fragment to the end of players accessed via the API. Get
			// rid of that shit.
			link = urlCleaner.ReplaceAllLiteralString(videos[i].Player.Default, "")
		}
	case l == 1:
		link = urlCleaner.ReplaceAllLiteralString(videos[0].Player.Default, "")
	case l == 0:
		err = errors.New("No video found for search \"" + query + "\"")
	}

	return
}
Exemple #4
0
func getStashes(sensuUrl string) (error, stashList) {
	stashesUrl := fmt.Sprintf("%s/stashes", sensuUrl)
	var stashResponse stashList
	err := util.UnmarshalUrl(stashesUrl, &stashResponse)
	return err, stashResponse
}
Exemple #5
0
func getEvents(sensuUrl string) (error, eventsResponse) {
	eventsUrl := fmt.Sprintf("%s/events", sensuUrl)
	var eventsResponse eventsResponse
	err := util.UnmarshalUrl(eventsUrl, &eventsResponse)
	return err, eventsResponse
}