all repos — navani @ twitter-api

forlater's primary mail processing service

reader/twitter/twitter.go (view raw)

 1package twitter
 2
 3import (
 4	"fmt"
 5	"net/url"
 6	"path"
 7	"strconv"
 8
 9	"git.icyphox.sh/forlater/navani/reader"
10	"github.com/dghubble/go-twitter/twitter"
11	"github.com/dghubble/oauth1"
12)
13
14type Creds struct {
15	AccessToken       string
16	AccessTokenSecret string
17	APIKey            string
18	APISecret         string
19}
20
21func GetClient(creds *Creds) *twitter.Client {
22	config := oauth1.NewConfig(creds.APIKey, creds.APISecret)
23	token := oauth1.NewToken(creds.AccessToken, creds.AccessTokenSecret)
24
25	httpClient := config.Client(oauth1.NoContext, token)
26	client := twitter.NewClient(httpClient)
27
28	return client
29}
30
31func parseSnowflake(url string) (int64, error) {
32	strid := path.Base(url)
33	id, err := strconv.ParseInt(strid, 10, 64)
34	if err != nil {
35		return 0, fmt.Errorf("snowflake: %w\n", err)
36	}
37
38	return id, nil
39}
40
41func GetTweet(client *twitter.Client, url string) (*twitter.Tweet, error) {
42	id, err := parseSnowflake(url)
43	if err != nil {
44		return nil, err
45	}
46
47	tweet, _, err := client.Statuses.Show(id, nil)
48	return tweet, err
49}
50
51func MakeTweetArticle(tweet *twitter.Tweet, u *url.URL) *reader.Article {
52	article := reader.Article{}
53	article.Content = tweet.Text
54	article.Title = fmt.Sprintf("Tweet: %s @%s", tweet.User.Name, tweet.User.ScreenName)
55	article.Byline = fmt.Sprintf("@%s tweeted on %s", tweet.User.ScreenName, tweet.CreatedAt)
56	article.URL = u
57	return &article
58}