friday: Use KYM instead of Bing Images
This commit is contained in:
parent
3b3fbbac26
commit
fd3e2b081c
1 changed files with 18 additions and 30 deletions
|
@ -1,32 +1,20 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"flag"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"math/rand"
|
||||
"net/http"
|
||||
"regexp"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/hamcha/clessy/tg"
|
||||
)
|
||||
|
||||
const APIUrl = "https://api.datamarket.azure.com/Bing/Search/v1/Image?$format=json&Query=%27jojo%20bizarre%20adventure%20friday%20know%20your%20meme%27"
|
||||
|
||||
type BingAPISearchResult struct {
|
||||
MediaUrl string
|
||||
}
|
||||
|
||||
type BingAPIResponseData struct {
|
||||
Results []BingAPISearchResult `json:"results"`
|
||||
}
|
||||
|
||||
type BingAPIResponse struct {
|
||||
Data BingAPIResponseData `json:"d"`
|
||||
}
|
||||
const KYMURL = "http://knowyourmeme.com/memes/it-s-finally-a-friday/photos/page/2"
|
||||
const Search = "data-src=\"([^\"]*)masonry([^\"]*)\""
|
||||
|
||||
func assert(err error) {
|
||||
if err != nil {
|
||||
|
@ -36,7 +24,6 @@ func assert(err error) {
|
|||
|
||||
func main() {
|
||||
brokerAddr := flag.String("broker", "localhost:7314", "Broker address:port")
|
||||
apiKey := flag.String("apiKey", "", "Bing Search API key")
|
||||
chatId := flag.Int64("chatId", -1001063099772, "Chat to write to")
|
||||
flag.Parse()
|
||||
|
||||
|
@ -45,26 +32,27 @@ func main() {
|
|||
log.Println("Connected to broker")
|
||||
defer broker.Close()
|
||||
|
||||
req, err := http.NewRequest("GET", APIUrl, nil)
|
||||
resp, err := http.Get(KYMURL)
|
||||
assert(err)
|
||||
req.SetBasicAuth(*apiKey, *apiKey)
|
||||
resp, err := http.DefaultClient.Do(req)
|
||||
assert(err)
|
||||
log.Println("Connecting to Bing API")
|
||||
log.Println("Got KYM page")
|
||||
|
||||
var body BingAPIResponse
|
||||
err = json.NewDecoder(resp.Body).Decode(&body)
|
||||
data, err := ioutil.ReadAll(resp.Body)
|
||||
assert(err)
|
||||
resp.Body.Close()
|
||||
assert(err)
|
||||
log.Println("Successfully obtained JSON of miracles")
|
||||
|
||||
images := body.Data.Results
|
||||
imgcount := len(body.Data.Results)
|
||||
rand.Seed(time.Now().Unix())
|
||||
r := regexp.MustCompile(Search)
|
||||
urls := r.FindAllString(string(data), -1)
|
||||
// Process urls
|
||||
for i := range urls {
|
||||
// Take out prefix/suffix and get original instead of thumb
|
||||
urls[i] = strings.Replace(urls[i][10:len(urls[i])-1], "masonry", "original", 1)
|
||||
}
|
||||
|
||||
urlcount := len(urls)
|
||||
|
||||
for attempt := 0; attempt < 10; attempt += 1 {
|
||||
imgindex := rand.Int() % imgcount
|
||||
chosen := images[imgindex].MediaUrl
|
||||
imgindex := rand.Int() % urlcount
|
||||
chosen := urls[imgindex]
|
||||
log.Println("Trying " + chosen)
|
||||
|
||||
resp, err := http.Get(chosen)
|
||||
|
|
Reference in a new issue