コード例 #1
0
ファイル: fcdg.go プロジェクト: sergiodmn/tgbot
// getCard returns a random card from the 4cdg
func (cmd *cmdFcdg) randomCard() (filePath string, err error) {
	// Get random pic ID
	resp, err := http.Get(fcdgUrl + "?card")
	if err != nil {
		return "", err
	}
	defer resp.Body.Close()

	if resp.StatusCode != http.StatusOK {
		return "", fmt.Errorf("HTTP error: %v (%v)", resp.Status, resp.StatusCode)
	}

	body, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		return "", err
	}

	matches := imgRe.FindStringSubmatch(string(body))
	if len(matches) != 2 {
		return "", errors.New("regexp error")
	}

	// Download pic
	filePath, err = utils.Download(cmd.tempDir, "", fcdgUrl+matches[1])
	if err != nil {
		return "", err
	}
	return filePath, nil
}
コード例 #2
0
ファイル: voice.go プロジェクト: sergiodmn/tgbot
func (cmd *cmdVoice) Run(title, from, text string) error {
	var (
		path string
		err  error
	)

	if cmd.tempDir == "" {
		cmd.tempDir, err = ioutil.TempDir("", "tgbot-voice-")
		if err != nil {
			fmt.Fprintf(cmd.w, "msg %v error: internal command error\n", title)
			return err
		}
		log.Println("Created VOICE sounds dir:", cmd.tempDir)
	}

	// Get language and text
	matches := cmd.re.FindStringSubmatch(text)
	lang := matches[1]
	msg := matches[2]

	// Download sound
	path, err = utils.Download(cmd.tempDir, ".mp3", setResourceUrl(lang, msg))

	if err != nil {
		fmt.Fprintf(cmd.w, "msg %v error: cannot get sound\n", title)
		return err
	}

	// Send to tg as audio
	fmt.Fprintf(cmd.w, "send_audio %v %v\n", title, path)
	return nil
}
コード例 #3
0
ファイル: ano.go プロジェクト: sergiodmn/tgbot
// searchTag returns a pic from ANO with a given tag.
func (cmd *cmdAno) searchTag(title string, tags []string) (filePath string, err error) {
	var data struct {
		Pics []struct {
			ID string
		}
	}
	client := &http.Client{}

	// Get random pic ID
	searchStr := fmt.Sprintf("{ \"method\" : \"searchRelated\", \"tags\" : [%v], \"limit\" : 10 }",
		tagsString(tags))

	methodSearch := strings.NewReader(searchStr)
	req, err := http.NewRequest("POST", "http://ano.lolcathost.org/json/tag.json", methodSearch)
	if err != nil {
		return "", err
	}
	req.Header.Add("Content-Type", "application/json")
	res, err := client.Do(req)
	if err != nil {
		return "", err
	}
	defer res.Body.Close()

	if res.StatusCode != http.StatusOK {
		return "", fmt.Errorf("HTTP error: %v (%v)", res.Status, res.StatusCode)
	}

	// Receive data
	bdata, err := ioutil.ReadAll(res.Body)
	if err != nil {
		return "", err
	}
	err = json.Unmarshal(bdata, &data)
	if err != nil {
		return "", err
	}
	if len(data.Pics) <= 1 {
		return "", errors.New("no pics")
	}

	rndInt := rand.Intn(len(data.Pics) - 1)
	rndData := data.Pics[rndInt]

	// Download pic
	filePath, err = utils.Download(cmd.tempDir, "", picsURL+rndData.ID)
	if err != nil {
		return "", err
	}
	return filePath, nil
}
コード例 #4
0
ファイル: ano.go プロジェクト: sergiodmn/tgbot
// randomPic returns a random pic from ANO
func (cmd *cmdAno) randomPic(title string) (filePath string, err error) {
	var data struct {
		Pic struct {
			ID string
		}
	}
	client := &http.Client{}

	// Get random pic ID
	methodRandom := strings.NewReader(`{ "method" : "random" }`)
	req, err := http.NewRequest("POST", "http://ano.lolcathost.org/json/pic.json", methodRandom)
	if err != nil {
		return "", err
	}
	req.Header.Add("Content-Type", "application/json")
	res, err := client.Do(req)
	if err != nil {
		return "", err
	}
	defer res.Body.Close()

	if res.StatusCode != http.StatusOK {
		return "", fmt.Errorf("HTTP error: %v (%v)", res.Status, res.StatusCode)
	}

	// Receive data
	bdata, err := ioutil.ReadAll(res.Body)
	if err != nil {
		return "", err
	}
	err = json.Unmarshal(bdata, &data)
	if err != nil {
		return "", err
	}

	// Download pic
	filePath, err = utils.Download(cmd.tempDir, "", picsURL+data.Pic.ID)
	if err != nil {
		return "", err
	}
	return filePath, nil
}
コード例 #5
0
ファイル: bing.go プロジェクト: sergiodmn/tgbot
// search returns a pic from Bing after a search using the given query.
func (cmd *cmdBing) search(query string) (filePath string, err error) {
	c := bing.NewClient(cmd.config.Key)
	if cmd.config.Limit > 0 {
		c.Limit = cmd.config.Limit
	}

	results, err := c.Query(bing.Image, query)
	if err != nil {
		return "", err
	}
	if len(results) == 0 {
		return "", errors.New("no pics")
	}
	rndInt := rand.Intn(len(results))

	// Download pic
	filePath, err = utils.Download(cmd.tempDir, "", results[rndInt].MediaUrl)
	if err != nil {
		return "", err
	}
	return filePath, nil
}