Finish Twitter integration, start on web service
continuous-integration/drone/push Build is passing Details

This commit is contained in:
Tyler 2019-10-06 00:52:26 -04:00
parent a5f2d06861
commit 89990c547b
6 changed files with 213 additions and 6 deletions

View File

@ -72,7 +72,7 @@ func (api *LinkInfoApi) DefaultLinkHandler(link string) (*LinkInfo, error) {
switch contentType {
case contentTypeHtml:
if contentLength > 0 && contentLength < maxBodySizeBytes {
if contentLength >= 0 && contentLength < maxBodySizeBytes {
err = api.retrieveHtmlLinkTitle(ret, link)
break
}

7
go.mod
View File

@ -2,4 +2,9 @@ module meow.tf/go/linkinfo
go 1.12
require github.com/PuerkitoBio/goquery v1.5.0
require (
github.com/PuerkitoBio/goquery v1.5.0
github.com/caarlos0/env/v6 v6.1.0
github.com/dghubble/go-twitter v0.0.0-20190719072343-39e5462e111f
github.com/dghubble/oauth1 v0.6.0
)

25
go.sum
View File

@ -2,6 +2,31 @@ github.com/PuerkitoBio/goquery v1.5.0 h1:uGvmFXOA73IKluu/F84Xd1tt/z07GYm8X49XKHP
github.com/PuerkitoBio/goquery v1.5.0/go.mod h1:qD2PgZ9lccMbQlc7eEOjaeRlFQON7xY8kdmcsrnKqMg=
github.com/andybalholm/cascadia v1.0.0 h1:hOCXnnZ5A+3eVDX8pvgl4kofXv2ELss0bKcqRySc45o=
github.com/andybalholm/cascadia v1.0.0/go.mod h1:GsXiBklL0woXo1j/WYWtSYYC4ouU9PqHO0sqidkEA4Y=
github.com/caarlos0/env/v6 v6.1.0 h1:4FbM+HmZA/Q5wdSrH2kj0KQXm7xnhuO8y3TuOTnOvqc=
github.com/caarlos0/env/v6 v6.1.0/go.mod h1:iUA6X3VCAOwDhoqvgKlTGjjwJzQseIJaFYApUqQkt+8=
github.com/cenkalti/backoff v2.1.1+incompatible h1:tKJnvO2kl0zmb/jA5UKAt4VoEVw1qxKWjE/Bpp46npY=
github.com/cenkalti/backoff v2.1.1+incompatible/go.mod h1:90ReRw6GdpyfrHakVjL/QHaoyV4aDUVVkXQJJJ3NXXM=
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/dghubble/go-twitter v0.0.0-20190719072343-39e5462e111f h1:M2wB039zeS1/LZtN/3A7tWyfctiOBL4ty5PURBmDdWU=
github.com/dghubble/go-twitter v0.0.0-20190719072343-39e5462e111f/go.mod h1:xfg4uS5LEzOj8PgZV7SQYRHbG7jPUnelEiaAVJxmhJE=
github.com/dghubble/oauth1 v0.6.0 h1:m1yC01Ohc/eF38jwZ8JUjL1a+XHHXtGQgK+MxQbmSx0=
github.com/dghubble/oauth1 v0.6.0/go.mod h1:8pFdfPkv/jr8mkChVbNVuJ0suiHe278BtWI4Tk1ujxk=
github.com/dghubble/sling v1.3.0 h1:pZHjCJq4zJvc6qVQ5wN1jo5oNZlNE0+8T/h0XeXBUKU=
github.com/dghubble/sling v1.3.0/go.mod h1:XXShWaBWKzNLhu2OxikSNFrlsvowtz4kyRuXUG7oQKY=
github.com/google/go-querystring v1.0.0 h1:Xkwi/a1rcvNg1PPYe5vI8GbeBY/jrVuDX5ASuANWTrk=
github.com/google/go-querystring v1.0.0/go.mod h1:odCYkC5MyYFN7vkCjXpyrEuKhc/BUO6wN/zVPAxq5ck=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
golang.org/x/net v0.0.0-20180218175443-cbe0f9307d01/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20181114220301-adae6a3d119a h1:gOpx8G595UYyvj8UK4+OFyY4rx037g3fmfhe5SasG3U=
golang.org/x/net v0.0.0-20181114220301-adae6a3d119a/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=

View File

@ -16,6 +16,7 @@ type LinkInfoApi struct {
Imgur *ImgurInfoApi
Youtube *YoutubeInfoApi
Twitter *TwitterInfoApi
linkHandlers []*LinkHandler
}
@ -41,6 +42,9 @@ func New(opts ...Option) *LinkInfoApi {
api.Imgur = &ImgurInfoApi{api, opt.(*ImgurOptions)}
case *YoutubeOptions:
api.Youtube = &YoutubeInfoApi{api, opt.(*YoutubeOptions)}
case *TwitterOptions:
api.Twitter = &TwitterInfoApi{opts: opt.(*TwitterOptions)}
api.Twitter.Init()
}
}
@ -65,6 +69,13 @@ func (i *LinkInfoApi) registerDefaultHandlers() {
Handler: i.Youtube.Handler,
})
}
if i.Twitter != nil {
i.linkHandlers = append(i.linkHandlers, &LinkHandler{
Hosts: twitterHosts,
Handler: i.Twitter.Handler,
})
}
}
func (i *LinkInfoApi) AddHandler(handler *LinkHandler) {

View File

@ -1,15 +1,128 @@
package main
import (
"encoding/json"
"github.com/caarlos0/env/v6"
"log"
"meow.tf/go/linkinfo"
"net"
"net/http"
"os"
"time"
)
var (
api *linkinfo.LinkInfoApi
)
type apiConfig struct {
LocalAddress string `env:"LOCAL_ADDRESS"`
ImgurClientId string `env:"IMGUR_CLIENT_ID"`
YoutubeKey string `env:"YOUTUBE_KEY"`
TwitterConsumerKey string `env:"TWITTER_CONSUMER_KEY"`
TwitterConsumerSecret string `env:"TWITTER_CONSUMER_SECRET"`
TwitterToken string `env:"TWITTER_TOKEN"`
TwitterTokenSecret string `env:"TWITTER_TOKEN_SECRET"`
}
func main() {
config := &apiConfig{}
if err := env.Parse(config); err != nil {
log.Fatalln("Unable to load config from env")
}
opts := make([]linkinfo.Option, 0)
if config.ImgurClientId != "" {
opts = append(opts, &linkinfo.ImgurOptions{
ClientId: config.ImgurClientId,
})
}
if config.YoutubeKey != "" {
opts = append(opts, &linkinfo.YoutubeOptions{
Key: config.YoutubeKey,
})
}
if config.TwitterConsumerKey != "" {
opts = append(opts, &linkinfo.TwitterOptions{
ConsumerKey: config.TwitterConsumerKey,
ConsumerSecret: config.TwitterConsumerSecret,
Token: config.TwitterToken,
TokenSecret: config.TwitterTokenSecret,
})
}
api = linkinfo.New(opts...)
if addr := os.Getenv("LOCAL_ADDRESS"); addr != "" {
var err error
api.Client, err = constructBoundClient(addr)
if err != nil {
log.Fatalln("Unable to bind client to address:", err)
}
}
mux := http.NewServeMux()
mux.HandleFunc("/info", handleInfoRequest)
http.ListenAndServe(":8080", mux)
}
func handleInfoRequest(w http.ResponseWriter, r *http.Request) {
query := r.URL.Query()
urlStr := query.Get("url")
if urlStr == "" {
w.WriteHeader(http.StatusBadRequest)
return
}
ret, err := api.Retrieve(urlStr)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte(err.Error()))
return
}
w.Header().Set("Content-Type", "application/json")
err = json.NewEncoder(w).Encode(ret)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
}
}
func constructBoundClient(addr string) (*http.Client, error) {
localAddr, err := net.ResolveIPAddr("ip", addr)
if err != nil {
return nil, err
}
// You also need to do this to make it work and not give you a
// "mismatched local address type ip"
// This will make the ResolveIPAddr a TCPAddr without needing to
// say what SRC port number to use.
localTCPAddr := net.TCPAddr{
IP: localAddr.IP,
}
dialer := &net.Dialer{
LocalAddr: &localTCPAddr,
Timeout: 30 * time.Second,
KeepAlive: 30 * time.Second,
}
return &http.Client{
Transport: &http.Transport{
DialContext: dialer.DialContext,
},
}, nil
}

View File

@ -1,9 +1,62 @@
package linkinfo
var (
twitterHosts = []string{"twitter.com"}
import (
"github.com/dghubble/go-twitter/twitter"
"github.com/dghubble/oauth1"
"regexp"
"strconv"
)
func (i *LinkInfoApi) TwitterLinkHandler(link string) (*LinkInfo, error) {
return nil, nil
var (
twitterHosts = []string{"twitter.com"}
twitterStatusRegexp = regexp.MustCompile("/status/(\\d+)")
)
type TwitterOptions struct {
Option
ConsumerKey string
ConsumerSecret string
Token string
TokenSecret string
}
type TwitterInfoApi struct {
opts *TwitterOptions
client *twitter.Client
}
func (i *TwitterInfoApi) Init() {
config := oauth1.NewConfig(i.opts.ConsumerKey, i.opts.ConsumerSecret)
token := oauth1.NewToken(i.opts.Token, i.opts.TokenSecret)
i.client = twitter.NewClient(config.Client(oauth1.NoContext, token))
}
func (i *TwitterInfoApi) Handler(link string) (*LinkInfo, error) {
m := twitterStatusRegexp.FindStringSubmatch(link)
if m == nil {
return nil, nil
}
id, err := strconv.ParseInt(m[1], 10, 64)
if err != nil {
return nil, err
}
tweet, _, err := i.client.Statuses.Show(id, nil)
if err != nil {
return nil, err
}
ret := &LinkInfo{
Title: tweet.User.Name + " on Twitter: " + tweet.Text,
Description: tweet.Text,
}
return ret, nil
}