Last active
October 7, 2016 23:32
-
-
Save IndianGuru/8d0bed934ae0328a6b5b760e8c9dbc91 to your computer and use it in GitHub Desktop.
Program to access tags
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 ClientSecret" | |
) | |
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"` | |
} | |
func main() { | |
accessToken, err := requestAccessToken() | |
if err != nil { | |
fmt.Println(err) | |
} | |
tags, err := getTags(accessToken) | |
if err != nil { | |
fmt.Println(err) | |
} | |
for i, v := range tags { | |
fmt.Printf("Tag%d = %s\n", i, v) | |
} | |
} | |
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 getTags(token string) ([]string, error) { | |
// Analyze the image at https://samples.clarifai.com/metro-north.jpg | |
url := fmt.Sprintf("https://api.clarifai.com/v1/tag/?url=https://samples.clarifai.com/metro-north.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 TagResp | |
if err := json.NewDecoder(resp.Body).Decode(&record); err != nil { | |
return nil, err | |
} | |
return record.Results[0].Result.Tag.Classes, nil | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment