summaryrefslogtreecommitdiff
path: root/tumblr.go
blob: b76d18f49402dd1445b67c6c2566625297f40a59 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
package main

import (
	"crypto/hmac"
	"crypto/sha1"
	"encoding/base64"
	"encoding/json"
	"fmt"
	"io"
	"math/rand"
	"net/http"
	"net/url"
	"sort"
	"strconv"
	"strings"
	"time"
)

const apiBase = "https://api.tumblr.com/v2"
const oauthBase = "https://www.tumblr.com"

type TumblrClient struct {
	ConsumerKey    string
	ConsumerSecret string
	Token          string
	TokenSecret    string
}

func NewClient(ck, cs, token, secret string) *TumblrClient {
	return &TumblrClient{ck, cs, token, secret}
}

// --- OAuth 1.0a ---

func oauthNonce() string {
	b := make([]byte, 16)
	rand.Read(b)
	return base64.StdEncoding.EncodeToString(b)
}

func oauthTimestamp() string {
	return strconv.FormatInt(time.Now().Unix(), 10)
}

func hmacSha1(key, data string) string {
	mac := hmac.New(sha1.New, []byte(key))
	mac.Write([]byte(data))
	return base64.StdEncoding.EncodeToString(mac.Sum(nil))
}

func (c *TumblrClient) oauthHeader(method, rawURL string, extraParams map[string]string) string {
	params := map[string]string{
		"oauth_consumer_key":     c.ConsumerKey,
		"oauth_nonce":            oauthNonce(),
		"oauth_signature_method": "HMAC-SHA1",
		"oauth_timestamp":        oauthTimestamp(),
		"oauth_token":            c.Token,
		"oauth_version":          "1.0",
	}
	for k, v := range extraParams {
		params[k] = v
	}
	// Build base string
	keys := make([]string, 0, len(params))
	for k := range params {
		keys = append(keys, k)
	}
	sort.Strings(keys)
	parts := make([]string, 0, len(keys))
	for _, k := range keys {
		parts = append(parts, url.QueryEscape(k)+"="+url.QueryEscape(params[k]))
	}
	paramStr := strings.Join(parts, "&")
	baseStr := method + "&" + url.QueryEscape(rawURL) + "&" + url.QueryEscape(paramStr)
	sigKey := url.QueryEscape(c.ConsumerSecret) + "&" + url.QueryEscape(c.TokenSecret)
	params["oauth_signature"] = hmacSha1(sigKey, baseStr)

	// Build header
	headerParts := []string{}
	for k, v := range params {
		if strings.HasPrefix(k, "oauth_") {
			headerParts = append(headerParts, k+`="`+url.QueryEscape(v)+`"`)
		}
	}
	sort.Strings(headerParts)
	return "OAuth " + strings.Join(headerParts, ", ")
}

func (c *TumblrClient) get(endpoint string, params map[string]string) ([]byte, error) {
	u, _ := url.Parse(apiBase + endpoint)
	q := u.Query()
	for k, v := range params {
		q.Set(k, v)
	}
	u.RawQuery = q.Encode()
	rawURL := apiBase + endpoint
	req, _ := http.NewRequest("GET", u.String(), nil)
	req.Header.Set("Authorization", c.oauthHeader("GET", rawURL, params))
	resp, err := http.DefaultClient.Do(req)
	if err != nil {
		return nil, err
	}
	defer resp.Body.Close()
	return io.ReadAll(resp.Body)
}

func (c *TumblrClient) post(endpoint string, params map[string]string) ([]byte, error) {
	form := url.Values{}
	for k, v := range params {
		form.Set(k, v)
	}
	rawURL := apiBase + endpoint
	req, _ := http.NewRequest("POST", rawURL, strings.NewReader(form.Encode()))
	req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
	req.Header.Set("Authorization", c.oauthHeader("POST", rawURL, params))
	resp, err := http.DefaultClient.Do(req)
	if err != nil {
		return nil, err
	}
	defer resp.Body.Close()
	return io.ReadAll(resp.Body)
}

// --- OAuth token exchange (no client tokens yet) ---

func oauthRequest(method, rawURL string, consumerKey, consumerSecret, token, tokenSecret string, params map[string]string) ([]byte, error) {
	c := &TumblrClient{consumerKey, consumerSecret, token, tokenSecret}
	if method == "POST" {
		return c.post(strings.TrimPrefix(rawURL, apiBase), params)
	}
	return c.get(strings.TrimPrefix(rawURL, apiBase), params)
}

func GetRequestToken(consumerKey, consumerSecret string) (string, string, error) {
	c := &TumblrClient{ConsumerKey: consumerKey, ConsumerSecret: consumerSecret}
	rawURL := oauthBase + "/oauth/request_token"
	req, _ := http.NewRequest("POST", rawURL, nil)
	req.Header.Set("Authorization", c.oauthHeader("POST", rawURL, nil))
	resp, err := http.DefaultClient.Do(req)
	if err != nil {
		return "", "", err
	}
	defer resp.Body.Close()
	body, _ := io.ReadAll(resp.Body)
	vals, _ := url.ParseQuery(string(body))
	return vals.Get("oauth_token"), vals.Get("oauth_token_secret"), nil
}

func GetAccessToken(consumerKey, consumerSecret, tmpToken, tmpSecret, verifier string) (string, string, error) {
	c := &TumblrClient{ConsumerKey: consumerKey, ConsumerSecret: consumerSecret, Token: tmpToken, TokenSecret: tmpSecret}
	rawURL := oauthBase + "/oauth/access_token"
	req, _ := http.NewRequest("POST", rawURL, strings.NewReader("oauth_verifier="+url.QueryEscape(verifier)))
	req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
	req.Header.Set("Authorization", c.oauthHeader("POST", rawURL, map[string]string{"oauth_verifier": verifier}))
	resp, err := http.DefaultClient.Do(req)
	if err != nil {
		return "", "", err
	}
	defer resp.Body.Close()
	body, _ := io.ReadAll(resp.Body)
	vals, _ := url.ParseQuery(string(body))
	return vals.Get("oauth_token"), vals.Get("oauth_token_secret"), nil
}

// --- API response types ---

type Post struct {
	ID         json.Number `json:"id"`
	IDString   string      `json:"id_string"`
	Type       string      `json:"type"`
	BlogName   string      `json:"blog_name"`
	PostURL    string      `json:"post_url"`
	ReblogKey  string      `json:"reblog_key"`
	NoteCount  int         `json:"note_count"`
	Liked      bool        `json:"liked"`
	Photos     []struct {
		OriginalSize struct {
			URL string `json:"url"`
		} `json:"original_size"`
	} `json:"photos"`
	VideoURL     string `json:"video_url"`
	ThumbnailURL string `json:"thumbnail_url"`
	Caption      string `json:"caption"`
}

type UserInfo struct {
	Response struct {
		User struct {
			Blogs []struct {
				Name string `json:"name"`
			} `json:"blogs"`
		} `json:"user"`
	}
}

type PostsResponse struct {
	Response struct {
		Posts []Post `json:"posts"`
	}
}

type TaggedResponse struct {
	Response []Post
}

// --- API methods ---

func (c *TumblrClient) GetUserInfo() (*UserInfo, error) {
	body, err := c.get("/user/info", nil)
	if err != nil {
		return nil, err
	}
	var result UserInfo
	if err := json.Unmarshal(body, &result); err != nil {
		return nil, err
	}
	return &result, nil
}

func (c *TumblrClient) GetDashboard(offset, limit int, postType string) ([]Post, error) {
	params := map[string]string{
		"offset": strconv.Itoa(offset),
		"limit":  strconv.Itoa(limit),
		"type":   postType,
	}
	body, err := c.get("/user/dashboard", params)
	if err != nil {
		return nil, err
	}
	var result PostsResponse
	if err := json.Unmarshal(body, &result); err != nil {
		return nil, fmt.Errorf("parse error: %w — body: %s", err, string(body))
	}
	return result.Response.Posts, nil
}

func (c *TumblrClient) GetBlogPosts(blogName string, offset, limit int, postType string) ([]Post, error) {
	params := map[string]string{
		"offset": strconv.Itoa(offset),
		"limit":  strconv.Itoa(limit),
		"type":   postType,
	}
	body, err := c.get("/blog/"+blogName+".tumblr.com/posts", params)
	if err != nil {
		return nil, err
	}
	var result PostsResponse
	if err := json.Unmarshal(body, &result); err != nil {
		return nil, fmt.Errorf("parse error: %w — body: %s", err, string(body))
	}
	return result.Response.Posts, nil
}

func (c *TumblrClient) GetTagged(tag string) ([]Post, error) {
	body, err := c.get("/tagged", map[string]string{"tag": tag})
	if err != nil {
		return nil, err
	}
	// Tagged endpoint returns array directly under response
	var raw struct {
		Response []Post `json:"response"`
	}
	if err := json.Unmarshal(body, &raw); err != nil {
		return nil, err
	}
	return raw.Response, nil
}

func (c *TumblrClient) LikePost(id, reblogKey string) error {
	_, err := c.post("/user/like", map[string]string{"id": id, "reblog_key": reblogKey})
	return err
}

func (c *TumblrClient) UnlikePost(id, reblogKey string) error {
	_, err := c.post("/user/unlike", map[string]string{"id": id, "reblog_key": reblogKey})
	return err
}

func (c *TumblrClient) ReblogPost(nativeBlog, id, reblogKey, sourceBlog string) error {
	_, err := c.post("/blog/"+nativeBlog+".tumblr.com/post/reblog", map[string]string{
		"id":         id,
		"reblog_key": reblogKey,
	})
	return err
}