-
-
Save lgs/862694c62699d2171a3e to your computer and use it in GitHub Desktop.
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 storage | |
import ( | |
"fmt" | |
"io/ioutil" | |
"log" | |
"net/http" | |
"os" | |
"code.google.com/p/goauth2/oauth/jwt" | |
"code.google.com/p/google-api-go-client/bigquery/v2" | |
"project/name/settings" | |
) | |
const scope = bigquery.BigqueryScope | |
func getGoogleOAuth2Client() (*http.Client, error) { | |
keyBytes, err := ioutil.ReadFile(settings.Config.BigQuery.ClientPemFile) | |
if err != nil { | |
log.Fatal("OAUTH2: Error reading private key file - ", err) | |
return nil, err | |
} | |
token := jwt.NewToken(settings.Config.BigQuery.ClientEmail, scope, keyBytes) | |
transport, err := jwt.NewTransport(token) | |
if err != nil { | |
log.Fatal("OAUTH2: Error creating tranport - ", err) | |
return nil, err | |
} | |
return transport.Client(), nil | |
} | |
func GetCurrentHtmlFromBigQuery(url string) (string, error) { | |
oauthClient, err := getGoogleOAuth2Client() | |
if err != nil { | |
return "", err | |
} | |
service, err := bigquery.New(oauthClient) | |
if err != nil { | |
log.Printf("BIGQUERY: Error while connecting - ", err) | |
return "", err | |
} | |
query := &bigquery.QueryRequest{ | |
Query: fmt.Sprintf("SELECT html from storage.htmls WHERE url='%s'", url), | |
Kind: "json", | |
MaxResults: 1, | |
} | |
result, err := service.Jobs.Query(settings.Config.BigQuery.ProjectId, query).Do() | |
if err != nil { | |
log.Printf("BIGQUERY: Error while listing datasets - ", err) | |
return "", err | |
} | |
if result.TotalRows == 0 { | |
log.Printf("BIGQUERY: Url not found ", url) | |
return "", nil | |
} | |
return fmt.Sprintf("%v", result.Rows[0].F[0].V), nil | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment