Build out structure

This commit is contained in:
Dustin Pianalto
2020-09-29 19:36:45 -08:00
parent 0258e0910f
commit 76e448f5a3
11 changed files with 165 additions and 12 deletions
+20
View File
@@ -0,0 +1,20 @@
package tweets
import (
"fmt"
"log"
"github.com/bwmarrin/discordgo"
"github.com/dustinpianalto/overpass/internal/twitter"
)
func TweetHandler(session *discordgo.Session, tweetChan <-chan *twitter.Tweet) {
for tweet := range tweetChan {
_, err := session.ChannelMessageSend("404569276012560386", fmt.Sprintf("%s\n%s - %s\n%s\n", tweet.IDStr, tweet.User.Name, tweet.User.IDStr, tweet.Text))
if err != nil {
log.Println(err)
}
log.Printf("%s\n%s - %s\n%s\n", tweet.IDStr, tweet.User.Name, tweet.User.IDStr, tweet.Text)
}
}
+21
View File
@@ -0,0 +1,21 @@
package oauth1
import (
"net/http"
"os"
"github.com/dghubble/oauth1"
)
func GetClient() *http.Client {
apiKey := os.Getenv("TWITTER_API_KEY")
apiSecret := os.Getenv("TWITTER_API_SECRET")
accessKey := os.Getenv("TWITTER_ACCESS_KEY")
accessSecret := os.Getenv("TWITTER_ACCESS_SECRET")
config := oauth1.NewConfig(apiKey, apiSecret)
token := oauth1.NewToken(accessKey, accessSecret)
// OAuth1 http.Client will automatically authorize Requests
httpClient := config.Client(oauth1.NoContext, token)
return httpClient
}
+1 -1
View File
@@ -1,4 +1,4 @@
package oauth
package oauth2
import (
"net/http"
+21
View File
@@ -0,0 +1,21 @@
package postgres
import (
"database/sql"
"fmt"
_ "github.com/lib/pq"
)
func ConnectDatabase(dbConnString string) *sql.DB {
db, err := sql.Open("postgres", dbConnString)
if err != nil {
panic(fmt.Sprintf("Can't connect to the database. %v", err))
} else {
fmt.Println("Database Connected.")
}
db.SetMaxOpenConns(75) // The RDS instance has a max of 75 open connections
db.SetMaxIdleConns(5)
db.SetConnMaxLifetime(300)
return db
}
+11 -6
View File
@@ -4,19 +4,24 @@ import (
"log"
"github.com/dghubble/go-twitter/twitter"
oauth "github.com/dustinpianalto/overpass/internal/oauth2"
"github.com/dustinpianalto/overpass/internal/oauth1"
)
type Tweet struct {
twitter.Tweet
}
func Connect() *twitter.Client {
httpClient := oauth.GetClient()
httpClient := oauth1.GetClient()
return twitter.NewClient(httpClient)
}
func StartUserScanner(client *twitter.Client, userID string) (chan *twitter.Tweet, *twitter.Stream) {
func StartUserScanner(client *twitter.Client, userID string, tweetChan chan *Tweet) *twitter.Stream {
demux := twitter.NewSwitchDemux()
tweetChan := make(chan *twitter.Tweet, 10)
demux.Tweet = func(tweet *twitter.Tweet) {
tweetChan <- tweet
if tweet.User.IDStr == userID {
tweetChan <- &Tweet{*tweet}
}
}
demux.StatusDeletion = func(deletion *twitter.StatusDeletion) {
log.Printf("%#v\n", deletion)
@@ -40,5 +45,5 @@ func StartUserScanner(client *twitter.Client, userID string) (chan *twitter.Twee
log.Println(err)
}
go demux.HandleChan(stream.Messages)
return tweetChan, stream
return stream
}