From fd3e2b081c8b2af3bfdcb3b2421f44a521e5afa7 Mon Sep 17 00:00:00 2001 From: Hamcha Date: Fri, 9 Sep 2016 17:25:19 +0200 Subject: [PATCH] friday: Use KYM instead of Bing Images --- friday/main.go | 48 ++++++++++++++++++------------------------------ 1 file changed, 18 insertions(+), 30 deletions(-) diff --git a/friday/main.go b/friday/main.go index 8713d21..01bc969 100644 --- a/friday/main.go +++ b/friday/main.go @@ -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)