Created
May 24, 2016 04:29
-
-
Save IndianGuru/e86f1222b508fd9f8befc25b56338ce6 to your computer and use it in GitHub Desktop.
Go and Clarifai API
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"encoding/json" | |
"fmt" | |
"net/http" | |
"net/url" | |
"strings" | |
) | |
const ( | |
clientID = "Your ClientID" | |
clientSecret = "Your Client Secret" | |
) | |
type TokenResp struct { | |
AccessToken string `json:"access_token"` | |
ExpiresIn int `json:"expires_in"` | |
Scope string `json:"scope"` | |
TokenType string `json:"token_type"` | |
} | |
type TagResp struct { | |
StatusCode string `json:"status_code"` | |
StatusMsg string `json:"status_msg"` | |
Meta struct { | |
Tag struct { | |
Timestamp float64 `json:"timestamp"` | |
Model string `json:"model"` | |
Config string `json:"config"` | |
} `json:"tag"` | |
} `json:"meta"` | |
Results []struct { | |
Docid uint64 `json:"docid"` | |
URL string `json:"url"` | |
StatusCode string `json:"status_code"` | |
StatusMsg string `json:"status_msg"` | |
LocalID string `json:"local_id"` | |
Result struct { | |
Tag struct { | |
ConceptIds []string `json:"concept_ids"` | |
Classes []string `json:"classes"` | |
Probs []float64 `json:"probs"` | |
} `json:"tag"` | |
} `json:"result"` | |
DocidStr string `json:"docid_str"` | |
} `json:"results"` | |
} | |
type NSFWResp struct { | |
StatusCode string `json:"status_code"` | |
StatusMsg string `json:"status_msg"` | |
Meta struct { | |
Tag struct { | |
Timestamp float64 `json:"timestamp"` | |
Model string `json:"model"` | |
Config string `json:"config"` | |
} `json:"tag"` | |
} `json:"meta"` | |
Results []struct { | |
Docid float64 `json:"docid"` | |
URL string `json:"url"` | |
StatusCode string `json:"status_code"` | |
StatusMsg string `json:"status_msg"` | |
LocalID string `json:"local_id"` | |
Result struct { | |
Tag struct { | |
Classes []string `json:"classes"` | |
Probs []float64 `json:"probs"` | |
} `json:"tag"` | |
} `json:"result"` | |
DocidStr string `json:"docid_str"` | |
} `json:"results"` | |
} | |
func main() { | |
accessToken, err := requestAccessToken() | |
if err != nil { | |
fmt.Println(err) | |
} | |
nsfw, err := getNSFW(accessToken) | |
if err != nil { | |
fmt.Println(err) | |
} | |
if nsfw[0] > nsfw[1] { | |
fmt.Println("Safe for work, you can trust Bob again (for now)!") | |
} else { | |
fmt.Println("Not safe for work. Classic Bob.") | |
} | |
} | |
func requestAccessToken() (string, error) { | |
form := url.Values{} | |
form.Set("grant_type", "client_credentials") | |
form.Set("client_id", clientID) | |
form.Set("client_secret", clientSecret) | |
formData := strings.NewReader(form.Encode()) | |
url := fmt.Sprintf("https://api.clarifai.com/v1/token/") | |
req, err := http.NewRequest("POST", url, formData) | |
if err != nil { | |
return "", err | |
} | |
req.Header.Set("Content-Type", "application/x-www-form-urlencoded") | |
httpClient := &http.Client{} | |
resp, err := httpClient.Do(req) | |
if err != nil { | |
return "", err | |
} | |
defer resp.Body.Close() | |
var record TokenResp | |
if err := json.NewDecoder(resp.Body).Decode(&record); err != nil { | |
return "", err | |
} | |
return record.AccessToken, nil | |
} | |
func getNSFW(token string) ([]float64, error) { | |
// Analyze the image at https://samples.clarifai.com/nsfw.jpg | |
url := fmt.Sprintf("https://api.clarifai.com/v1/tag/?model=nsfw-v1.0&url=https://samples.clarifai.com/nsfw.jpg") | |
req, err := http.NewRequest("GET", url, nil) | |
if err != nil { | |
return nil, err | |
} | |
req.Header.Set("Content-Type", "application/json") | |
req.Header.Set("Authorization", "Bearer "+token) | |
httpClient := &http.Client{} | |
resp, err := httpClient.Do(req) | |
if err != nil { | |
return nil, err | |
} | |
defer resp.Body.Close() | |
var record NSFWResp | |
if err := json.NewDecoder(resp.Body).Decode(&record); err != nil { | |
return nil, err | |
} | |
return record.Results[0].Result.Tag.Probs, nil | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment