mirror of
https://github.com/taigrr/wtf
synced 2025-01-18 04:03:14 -08:00
74
modules/twitter/client.go
Normal file
74
modules/twitter/client.go
Normal file
@@ -0,0 +1,74 @@
|
||||
package twitter
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"os"
|
||||
"strconv"
|
||||
|
||||
"github.com/wtfutil/wtf/wtf"
|
||||
)
|
||||
|
||||
/* NOTE: Currently single application ONLY
|
||||
* bearer tokens are only supported for applications, not single-users
|
||||
*/
|
||||
|
||||
// Client represents the data required to connect to the Twitter API
|
||||
type Client struct {
|
||||
apiBase string
|
||||
bearerToken string
|
||||
count int
|
||||
screenName string
|
||||
}
|
||||
|
||||
// NewClient creates and returns a new Twitter client
|
||||
func NewClient() *Client {
|
||||
client := Client{
|
||||
apiBase: "https://api.twitter.com/1.1/",
|
||||
count: wtf.Config.UInt("wtf.mods.twitter.count", 5),
|
||||
screenName: "",
|
||||
}
|
||||
|
||||
client.loadAPICredentials()
|
||||
|
||||
return &client
|
||||
}
|
||||
|
||||
/* -------------------- Public Functions -------------------- */
|
||||
|
||||
// Tweets returns a list of tweets of a user
|
||||
func (client *Client) Tweets() []Tweet {
|
||||
tweets, err := client.tweets()
|
||||
if err != nil {
|
||||
return []Tweet{}
|
||||
}
|
||||
|
||||
return tweets
|
||||
}
|
||||
|
||||
/* -------------------- Private Functions -------------------- */
|
||||
|
||||
// tweets is the private interface for retrieving the list of user tweets
|
||||
func (client *Client) tweets() (tweets []Tweet, err error) {
|
||||
apiURL := fmt.Sprintf(
|
||||
"%s/statuses/user_timeline.json?screen_name=%s&count=%s",
|
||||
client.apiBase,
|
||||
client.screenName,
|
||||
strconv.Itoa(client.count),
|
||||
)
|
||||
|
||||
data, err := Request(client.bearerToken, apiURL)
|
||||
if err != nil {
|
||||
return tweets, err
|
||||
}
|
||||
err = json.Unmarshal(data, &tweets)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
func (client *Client) loadAPICredentials() {
|
||||
client.bearerToken = wtf.Config.UString(
|
||||
"wtf.mods.twitter.bearerToken",
|
||||
os.Getenv("WTF_TWITTER_BEARER_TOKEN"),
|
||||
)
|
||||
}
|
||||
42
modules/twitter/request.go
Normal file
42
modules/twitter/request.go
Normal file
@@ -0,0 +1,42 @@
|
||||
package twitter
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
func Request(bearerToken string, apiURL string) ([]byte, error) {
|
||||
req, err := http.NewRequest("GET", apiURL, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Expected authorization format for single-application twitter dev accounts
|
||||
req.Header.Add("Authorization",
|
||||
fmt.Sprintf("Bearer %s", bearerToken))
|
||||
|
||||
client := &http.Client{}
|
||||
resp, err := client.Do(req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
data, err := ParseBody(resp)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return data, err
|
||||
}
|
||||
|
||||
func ParseBody(resp *http.Response) ([]byte, error) {
|
||||
var buffer bytes.Buffer
|
||||
_, err := buffer.ReadFrom(resp.Body)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return buffer.Bytes(), nil
|
||||
}
|
||||
32
modules/twitter/tweet.go
Normal file
32
modules/twitter/tweet.go
Normal file
@@ -0,0 +1,32 @@
|
||||
package twitter
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
)
|
||||
|
||||
type Tweet struct {
|
||||
User User `json:"user"`
|
||||
Text string `json:"text"`
|
||||
CreatedAt string `json:"created_at"`
|
||||
}
|
||||
|
||||
func (tweet *Tweet) String() string {
|
||||
return fmt.Sprintf("Tweet: %s at %s by %s", tweet.Text, tweet.CreatedAt, tweet.User.ScreenName)
|
||||
}
|
||||
|
||||
/* -------------------- Exported Functions -------------------- */
|
||||
|
||||
func (tweet *Tweet) Username() string {
|
||||
return tweet.User.ScreenName
|
||||
}
|
||||
|
||||
func (tweet *Tweet) Created() time.Time {
|
||||
newTime, _ := time.Parse(time.RubyDate, tweet.CreatedAt)
|
||||
return newTime
|
||||
}
|
||||
|
||||
func (tweet *Tweet) PrettyCreatedAt() string {
|
||||
newTime := tweet.Created()
|
||||
return fmt.Sprint(newTime.Format("Jan 2, 2006"))
|
||||
}
|
||||
6
modules/twitter/user.go
Normal file
6
modules/twitter/user.go
Normal file
@@ -0,0 +1,6 @@
|
||||
package twitter
|
||||
|
||||
// User is used as part of the Tweet struct to get user information
|
||||
type User struct {
|
||||
ScreenName string `json:"screen_name"`
|
||||
}
|
||||
168
modules/twitter/widget.go
Normal file
168
modules/twitter/widget.go
Normal file
@@ -0,0 +1,168 @@
|
||||
package twitter
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"html"
|
||||
"regexp"
|
||||
|
||||
"github.com/dustin/go-humanize"
|
||||
"github.com/gdamore/tcell"
|
||||
"github.com/rivo/tview"
|
||||
"github.com/wtfutil/wtf/wtf"
|
||||
)
|
||||
|
||||
const HelpText = `
|
||||
Keyboard commands for Textfile:
|
||||
|
||||
/: Show/hide this help window
|
||||
h: Previous Twitter name
|
||||
l: Next Twitter name
|
||||
|
||||
arrow left: Previous Twitter name
|
||||
arrow right: Next Twitter name
|
||||
`
|
||||
|
||||
type Widget struct {
|
||||
wtf.HelpfulWidget
|
||||
wtf.MultiSourceWidget
|
||||
wtf.TextWidget
|
||||
|
||||
client *Client
|
||||
idx int
|
||||
sources []string
|
||||
}
|
||||
|
||||
func NewWidget(app *tview.Application, pages *tview.Pages) *Widget {
|
||||
widget := Widget{
|
||||
HelpfulWidget: wtf.NewHelpfulWidget(app, pages, HelpText),
|
||||
MultiSourceWidget: wtf.NewMultiSourceWidget("twitter", "screenName", "screenNames"),
|
||||
TextWidget: wtf.NewTextWidget(app, "Twitter", "twitter", true),
|
||||
|
||||
idx: 0,
|
||||
}
|
||||
|
||||
widget.HelpfulWidget.SetView(widget.View)
|
||||
|
||||
widget.LoadSources()
|
||||
widget.SetDisplayFunction(widget.display)
|
||||
|
||||
widget.client = NewClient()
|
||||
|
||||
widget.View.SetBorderPadding(1, 1, 1, 1)
|
||||
widget.View.SetWrap(true)
|
||||
widget.View.SetWordWrap(true)
|
||||
widget.View.SetInputCapture(widget.keyboardIntercept)
|
||||
|
||||
return &widget
|
||||
}
|
||||
|
||||
/* -------------------- Exported Functions -------------------- */
|
||||
|
||||
// Refresh is called on the interval and refreshes the data
|
||||
func (widget *Widget) Refresh() {
|
||||
widget.display()
|
||||
}
|
||||
|
||||
/* -------------------- Unexported Functions -------------------- */
|
||||
|
||||
func (widget *Widget) display() {
|
||||
widget.client.screenName = widget.CurrentSource()
|
||||
tweets := widget.client.Tweets()
|
||||
|
||||
widget.View.SetTitle(widget.ContextualTitle(fmt.Sprintf("Twitter - [green]@%s[white]", widget.CurrentSource())))
|
||||
|
||||
if len(tweets) == 0 {
|
||||
str := fmt.Sprintf("\n\n\n%s", wtf.CenterText("[blue]No Tweets[white]", 50))
|
||||
widget.View.SetText(str)
|
||||
return
|
||||
}
|
||||
|
||||
str := wtf.SigilStr(len(widget.Sources), widget.Idx, widget.View) + "\n"
|
||||
for _, tweet := range tweets {
|
||||
str = str + widget.format(tweet)
|
||||
}
|
||||
|
||||
widget.View.SetText(str)
|
||||
}
|
||||
|
||||
// If the tweet's Username is the same as the account we're watching, no
|
||||
// need to display the username
|
||||
func (widget *Widget) displayName(tweet Tweet) string {
|
||||
if widget.CurrentSource() == tweet.User.ScreenName {
|
||||
return ""
|
||||
}
|
||||
return tweet.User.ScreenName
|
||||
}
|
||||
|
||||
func (widget *Widget) formatText(text string) string {
|
||||
result := text
|
||||
|
||||
// Convert HTML entities
|
||||
result = html.UnescapeString(result)
|
||||
|
||||
// RT indicator
|
||||
rtRegExp := regexp.MustCompile(`^RT`)
|
||||
result = rtRegExp.ReplaceAllString(result, "[olive]${0}[white::-]")
|
||||
|
||||
// @name mentions
|
||||
atRegExp := regexp.MustCompile(`@[0-9A-Za-z_]*`)
|
||||
result = atRegExp.ReplaceAllString(result, "[blue]${0}[white]")
|
||||
|
||||
// HTTP(S) links
|
||||
linkRegExp := regexp.MustCompile(`http[s:\/.0-9A-Za-z]*`)
|
||||
result = linkRegExp.ReplaceAllString(result, "[lightblue::u]${0}[white::-]")
|
||||
|
||||
// Hash tags
|
||||
hashRegExp := regexp.MustCompile(`#[0-9A-Za-z_]*`)
|
||||
result = hashRegExp.ReplaceAllString(result, "[yellow]${0}[white]")
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
func (widget *Widget) format(tweet Tweet) string {
|
||||
body := widget.formatText(tweet.Text)
|
||||
name := widget.displayName(tweet)
|
||||
|
||||
var attribution string
|
||||
if name == "" {
|
||||
attribution = humanize.Time(tweet.Created())
|
||||
} else {
|
||||
attribution = fmt.Sprintf(
|
||||
"%s, %s",
|
||||
name,
|
||||
humanize.Time(tweet.Created()),
|
||||
)
|
||||
}
|
||||
|
||||
return fmt.Sprintf("%s\n[grey]%s[white]\n\n", body, attribution)
|
||||
}
|
||||
|
||||
func (widget *Widget) keyboardIntercept(event *tcell.EventKey) *tcell.EventKey {
|
||||
switch string(event.Rune()) {
|
||||
case "/":
|
||||
widget.ShowHelp()
|
||||
return nil
|
||||
case "h":
|
||||
widget.Prev()
|
||||
return nil
|
||||
case "l":
|
||||
widget.Next()
|
||||
return nil
|
||||
case "o":
|
||||
wtf.OpenFile(widget.CurrentSource())
|
||||
return nil
|
||||
}
|
||||
|
||||
switch event.Key() {
|
||||
case tcell.KeyLeft:
|
||||
widget.Prev()
|
||||
return nil
|
||||
case tcell.KeyRight:
|
||||
widget.Next()
|
||||
return nil
|
||||
default:
|
||||
return event
|
||||
}
|
||||
|
||||
return event
|
||||
}
|
||||
Reference in New Issue
Block a user