-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathMastodonLib.applescript
More file actions
166 lines (133 loc) · 8.27 KB
/
MastodonLib.applescript
File metadata and controls
166 lines (133 loc) · 8.27 KB
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
use AppleScript version "2.4" -- Yosemite (10.10) or later
use framework "Foundation"
use scripting additions
-- Mastodon App Configuration - https://mastodon.example/settings/applications (replace with your instance's domain)
property APIDOMAIN : "https://mastodon.example" -- your Mastodon instance's URL
property APICLIENT_KEY : "client key" -- your Mastodon App's client key
property APICLIENT_SECRET : "client secret" -- your Mastodon App's client secret
property APIACCESS_TOKEN : missing value -- your Mastodon App's access token OR missing value (get new access token for each use)
-- classes, constants, and enums used
property NSJSONSerialization : a reference to current application's NSJSONSerialization
property NSData : a reference to current application's NSData
property NSString : a reference to current application's NSString
property NSURL : a reference to current application's NSURL
property NSURLRequestReloadIgnoringLocalCacheData : a reference to current application's NSURLRequestReloadIgnoringLocalCacheData
property NSMutableURLRequest : a reference to current application's NSMutableURLRequest
property NSURLConnection : a reference to current application's NSURLConnection
property NSURLComponents : a reference to current application's NSURLComponents
property NSURLQueryItem : a reference to current application's NSURLQueryItem
property NSUTF8StringEncoding : a reference to current application's NSUTF8StringEncoding
-- state
property accessToken : missing value
-- testing...
--getHomeTimeline()
--getHashtagTimeline("lego")
--postStatus("Test post from AppleScript", missing value, "private") -- use "public" to make the post visible
--
--set maxStatusID to missing value
--repeat
-- set statusItems to getHomeTimeline(maxStatusID, missing value, missing value, missing value)
-- set statusItemsCount to count of statusItems
-- if statusItemsCount = 0 then
-- exit repeat
-- else
-- set maxStatusID to ((last item of statusItems)'s objectForKey:"id") as text
--
-- -- do something with the statusItems array of status entries
-- end if
--end repeat
on getAccessToken()
-- If we have an accesss token for the Mastodon App, use that
if APIACCESS_TOKEN is not missing value then
return APIACCESS_TOKEN
end if
-- Otherwiae, request a new access token
if accessToken is missing value then
-- Get OAuth2 access token
--
-- See https://docs.joinmastodon.org/client/token/#flow
local theRequest, theResult, theJSONData, theJSON -- so SD can see them
-- Construct a URL containing all the query parameters needed to create an access token
set clientIdParam to NSURLQueryItem's queryItemWithName:"client_id" value:APICLIENT_KEY
set clientSecretParam to NSURLQueryItem's queryItemWithName:"client_secret" value:APICLIENT_SECRET
set redirectURIParam to NSURLQueryItem's queryItemWithName:"redirect_uri" value:"urn:ietf:wg:oauth:2.0:oob"
set grantTypeParam to NSURLQueryItem's queryItemWithName:"grant_type" value:"client_credentials"
set urlComponents to NSURLComponents's componentsWithString:(APIDOMAIN & "/oauth/token")
urlComponents's setQueryItems:{clientIdParam, clientSecretParam, redirectURIParam, grantTypeParam}
-- Send the create post request to the Discourse site
set theRequest to NSMutableURLRequest's requestWithURL:(urlComponents's |URL|) ¬
cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:10
theRequest's setHTTPMethod:"POST"
set theResult to NSURLConnection's sendSynchronousRequest:theRequest ¬
returningResponse:(reference) |error|:(missing value)
set theJSONData to item 1 of theResult
set theJSON to NSJSONSerialization's JSONObjectWithData:theJSONData options:0 |error|:(missing value)
set accessToken to (theJSON's objectForKey:"access_token") as text
end if
return accessToken
end getAccessToken
on getHomeTimeline(maxID, minID, sinceID, limit)
-- See https://docs.joinmastodon.org/methods/timelines/#home
local theJSON, params -- so SD can see them
set params to {}
if maxID is not missing value then set end of params to NSURLQueryItem's queryItemWithName:"max_id" value:maxID
if minID is not missing value then set end of params to NSURLQueryItem's queryItemWithName:"min_id" value:minID
if sinceID is not missing value then set end of params to NSURLQueryItem's queryItemWithName:"since_id" value:sinceID
if limit is not missing value then set end of params to NSURLQueryItem's queryItemWithName:"limit" value:limit
set urlComponents to NSURLComponents's componentsWithString:(APIDOMAIN & "/api/v1/timelines/home")
urlComponents's setQueryItems:params
-- Send the get catagories request to the Discourse site
set theRequest to NSMutableURLRequest's requestWithURL:(urlComponents's |URL|) ¬
cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:10
theRequest's setValue:("Bearer " & getAccessToken()) forHTTPHeaderField:"Authorization"
theRequest's setValue:"application/json;charset=utf-8" forHTTPHeaderField:"Content-Type"
set theResult to NSURLConnection's sendSynchronousRequest:theRequest ¬
returningResponse:(reference) |error|:(missing value)
set theJSONData to item 1 of theResult
set theJSON to NSJSONSerialization's JSONObjectWithData:theJSONData options:0 |error|:(missing value)
return theJSON -- list of status (up to 20 by default)
end getHomeTimeline
on getHashtagTimeline(hashtag, maxID, minID, sinceID, limit)
-- See https://docs.joinmastodon.org/methods/timelines/#tag
local theJSON -- so SD can see them
set params to {}
if maxID is not missing value then set end of params to NSURLQueryItem's queryItemWithName:"max_id" value:maxID
if minID is not missing value then set end of params to NSURLQueryItem's queryItemWithName:"min_id" value:minID
if sinceID is not missing value then set end of params to NSURLQueryItem's queryItemWithName:"since_id" value:sinceID
if limit is not missing value then set end of params to NSURLQueryItem's queryItemWithName:"limit" value:limit
set urlComponents to NSURLComponents's componentsWithString:(APIDOMAIN & "/api/v1/timelines/home")
urlComponents's setQueryItems:params
-- Send the get catagories request to the Discourse site
set theRequest to NSMutableURLRequest's requestWithURL:(urlComponents's |URL|) ¬
cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:10
theRequest's setValue:("Bearer " & getAccessToken()) forHTTPHeaderField:"Authorization"
theRequest's setValue:"application/json;charset=utf-8" forHTTPHeaderField:"Content-Type"
set theResult to NSURLConnection's sendSynchronousRequest:theRequest ¬
returningResponse:(reference) |error|:(missing value)
set theJSONData to item 1 of theResult
set theJSON to NSJSONSerialization's JSONObjectWithData:theJSONData options:0 |error|:(missing value)
return theJSON -- list of status (up to 20 by default)
end getHashtagTimeline
on postStatus(status, contentWarning, visibility)
-- See https://docs.joinmastodon.org/methods/statuses/#create
local theRequest, theResult, theJSONData, theJSON -- so SD can see them
if visibility is missing value then set visibility to "public"
-- Construct a URL containing all the query parameters needed to create a Dicourse post
set params to {}
set end of params to NSURLQueryItem's queryItemWithName:"status" value:status
set end of params to NSURLQueryItem's queryItemWithName:"visibility" value:visibility
if contentWarning is not missing value then set end of params to NSURLQueryItem's queryItemWithName:"spoiler_text" value:contentWarning
set urlComponents to NSURLComponents's componentsWithString:(APIDOMAIN & "/api/v1/statuses")
urlComponents's setQueryItems:params
-- Send the create post request to the Discourse site
set theRequest to NSMutableURLRequest's requestWithURL:(urlComponents's |URL|) ¬
cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:10
theRequest's setHTTPMethod:"POST"
theRequest's setValue:("Bearer " & getAccessToken()) forHTTPHeaderField:"Authorization"
theRequest's setValue:"application/json;charset=utf-8" forHTTPHeaderField:"Content-Type"
set theResult to NSURLConnection's sendSynchronousRequest:theRequest ¬
returningResponse:(reference) |error|:(missing value)
set theJSONData to item 1 of theResult
set theJSON to NSJSONSerialization's JSONObjectWithData:theJSONData options:0 |error|:(missing value)
return theJSON -- dictionary of newly created post
end postStatus