-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathDiscourseLib.applescript
More file actions
135 lines (109 loc) · 6.56 KB
/
DiscourseLib.applescript
File metadata and controls
135 lines (109 loc) · 6.56 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
use AppleScript version "2.4" -- Yosemite (10.10) or later
use framework "Foundation"
use scripting additions
-- Public Discourse Configuration
property APIDOMAIN : missing value -- "https://forum.domain.com"
property APIKEY : missing value -- "your Discourse API key"
property APIUSER : missing value -- "your Discourse username"
-- Discourse properties
property APIPOSTS_ENDPOINT : "/posts.json"
property APIPOST_ENDPOINT : "/posts/{id}.json"
property APICATAGORIES_ENDPOINT : "/categories.json"
property APITAGGROUPS_ENDPOINT : "/tag_groups.json"
property APITAGS_ENDPOINT : "/tags.json"
-- 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 NSArray : a reference to current application's NSArray
property NSURL : a reference to current application's NSURL
property NSURLRequest : a reference to current application's NSURLRequest
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
on getDiscourseTags()
local theJSON, theNames -- so SD can see them
-- Construct a URL containing all the query parameters needed to create a Dicourse post
set apiKeyParam to NSURLQueryItem's queryItemWithName:"api_key" value:APIKEY
set userParam to NSURLQueryItem's queryItemWithName:"api_username" value:APIUSER
set urlComponents to NSURLComponents's componentsWithString:(APIDOMAIN & APITAGS_ENDPOINT)
urlComponents's setQueryItems:{apiKeyParam, userParam}
-- Send the get tags request to the Discourse site
set theRequest to NSURLRequest's requestWithURL:(urlComponents's |URL|) ¬
cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:10
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 theNames to (theJSON's valueForKeyPath:"tags.text") as list
return theNames
end getDiscourseTags
on getDiscourseCategories()
local theJSON, theNames -- so SD can see them
-- Construct a URL containing all the query parameters needed to create a Dicourse post
set apiKeyParam to NSURLQueryItem's queryItemWithName:"api_key" value:APIKEY
set userParam to NSURLQueryItem's queryItemWithName:"api_username" value:APIUSER
set urlComponents to NSURLComponents's componentsWithString:(APIDOMAIN & APICATAGORIES_ENDPOINT)
urlComponents's setQueryItems:{apiKeyParam, userParam}
-- Send the get catagories request to the Discourse site
set theRequest to NSURLRequest's requestWithURL:(urlComponents's |URL|) ¬
cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:10
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 theNames to (theJSON's valueForKeyPath:"category_list.categories.name") as list
return theNames
end getDiscourseCategories
on getDiscoursePost(postID)
local theJSON, endpoint
set endpoint to (NSString's stringWithString:APIPOST_ENDPOINT)'s ¬
stringByReplacingOccurrencesOfString:"{id}" withString:(postID as text)
-- Construct a URL containing all the query parameters needed to create a Dicourse post
set apiKeyParam to NSURLQueryItem's queryItemWithName:"api_key" value:APIKEY
set userParam to NSURLQueryItem's queryItemWithName:"api_username" value:APIUSER
set urlComponents to NSURLComponents's componentsWithString:(APIDOMAIN & endpoint)
urlComponents's setQueryItems:{apiKeyParam, userParam}
-- Send the get post request to the Discourse site
set theRequest to NSURLRequest's requestWithURL:(urlComponents's |URL|) ¬
cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:10
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
end getDiscoursePost
on postToDiscourse(postTitle, postCategory, postTags, postMarkdown)
local theRequest, theResult, theJSONData, theJSON -- so SD can see them
-- AppleScript strings often have CR line endings while Discourse expects LF line endings. Convert all
-- CRs to LFs.
set postMarkdown to (NSString's stringWithString:postMarkdown)'s ¬
stringByReplacingOccurrencesOfString:return withString:linefeed
if postTags = missing value tjem then
postTags = (NSArray's array)
else if postTags is not array then
set postTags to (NSArray's arrayWithObjects:[postTags])
else
set postTags to (NSArray's arrayWithObjects:postTags)
end if
-- Construct a URL containing all the query parameters needed to create a Dicourse post
set apiKeyParam to NSURLQueryItem's queryItemWithName:"api_key" value:APIKEY
set userParam to NSURLQueryItem's queryItemWithName:"api_username" value:APIUSER
set titleParam to NSURLQueryItem's queryItemWithName:"title" value:postTitle
set categoryParam to NSURLQueryItem's queryItemWithName:"category" value:postCategory
set tagsParam to NSURLQueryItem's queryItemWithName:"tags" value:postTags
set rawParam to NSURLQueryItem's queryItemWithName:"raw" value:postMarkdown
set urlComponents to NSURLComponents's componentsWithString:(APIDOMAIN & APIPOSTS_ENDPOINT)
urlComponents's setQueryItems:{apiKeyParam, userParam, titleParam, categoryParam, tagsParam, rawParam}
-- 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)
return theJSON
end postToDiscourse