func getBinary(u url.URL, binName string) { log.Println("Request:", u.String()) res, err := goreq.Request{ Method: "POST", Uri: u.String(), Compression: goreq.Gzip(), }.Do() if err != nil { log.Fatal(err) } if res.StatusCode != http.StatusOK { log.Println("StatusCode:", res.StatusCode) io.Copy(os.Stdout, res.Body) return } defer res.Body.Close() saveDir := saveFolder() savePath := filepath.Join(saveDir, binName) log.Println("Save to", savePath) filefd, err := os.Create(savePath) if err != nil { log.Fatal(err) } io.Copy(filefd, res.Body) filefd.Close() os.Chmod(savePath, 0755) return }
func main() { res, _ := goreq.Request{ Method: "GET", Uri: "http://blog.cyeam.com", Compression: goreq.Gzip(), Proxy: "http://localhost:8888", }.Do() body, _ := res.Body.ToString() fmt.Println(body) }
// Get map PNG from StreetDirectory func getLocationMap(location LocationInfo) (*telebot.Photo, error) { filepath := fmt.Sprintf("%s/%f_%f.png", MAP_CACHE_DIR, location.Lng, location.Lat) // if map does not exist in our cache, retrieve! if _, err := os.Stat(filepath); err != nil { qs := SDMapQuery{ Level: 14, SizeX: 500, SizeY: 500, Lon: location.Lng, Lat: location.Lat, Star: 1, } res, err := goreq.Request{ Uri: "http://www.streetdirectory.com/api/map/world.cgi", QueryString: qs, Compression: goreq.Gzip(), }.Do() if err != nil { return nil, err } data, err := res.Body.ToString() if err != nil { return nil, err } err = ioutil.WriteFile(filepath, []byte(data), 0644) if err != nil { return nil, err } } file, err := telebot.NewFile(filepath) if err != nil { return nil, err } thumbnail := telebot.Thumbnail{ File: file, Width: 500, Height: 500, } photo := telebot.Photo{ Thumbnail: thumbnail, Caption: location.Name, } return &photo, nil }
// Get location (name, lat and lng) // via StreetDirectory func getLocationInfo(query string) ([]LocationInfo, error) { qs := SDApiQuery{ Mode: "search", Act: "all", Output: "json", Limit: 1, Country: "sg", Profile: "template_1", Q: fmt.Sprintf("%s nus", query), } res, err := goreq.Request{ Uri: "http://www.streetdirectory.com/api/", QueryString: qs, Compression: goreq.Gzip(), ShowDebug: true, }.Do() if err != nil { return nil, err } var resJSON []map[string]interface{} err = res.Body.FromJsonTo(&resJSON) if err != nil { return nil, err } var locations []LocationInfo if resJSON[0]["total"].(float64) > 0 { for ind, obj := range resJSON { // Skip first result if ind == 0 { continue } location := LocationInfo{ Name: obj["t"].(string), Lng: obj["x"].(float64), Lat: obj["y"].(float64), } locations = append(locations, location) } } return locations, nil }