Skip to content

Instantly share code, notes, and snippets.

@lokedhs
Created February 14, 2021 16:17
Show Gist options
  • Save lokedhs/496b75782585df7c8264e555ffb797a1 to your computer and use it in GitHub Desktop.
Save lokedhs/496b75782585df7c8264e555ffb797a1 to your computer and use it in GitHub Desktop.
use("standard-lib.kap")
namespace("mastodon")
∇ urlEscape (s) {
s
}
∇ mkParams (p) {
↑,/¯1↓,((⊂"="),(⊂"&"),urlEscape¨ p)[;2 0 3 1]
}
∇ register (server) {
url ← "https://",server,"/"
params ← 3 2 ⍴ `
"client_name" "foo" `
"redirect_uris" "urn:ietf:wg:oauth:2.0:oob" `
"scopes" "read+write+follow"
json ← json:readString io:httpPost (url,"api/v1/apps" ; mkParams params)
map 3 2 ⍴ :url url :clientId json["client_id"] :clientSecret json["client_secret"]
}
∇ (srv) authenticate (user;password) {
url ← srv[:url],"oauth/token"
params ← 7 2 ⍴ `
"client_id" srv[:clientId] `
"client_secret" srv[:clientSecret] `
"grant_type" "password" `
"username" user `
"password" password `
"scope" "read+write+follow" `
"redirect_uri" "urn:ietf:wg:oauth:2.0:oob"
json ← json:readString io:httpPost (url ; mkParams params)
map 2 2 ⍴ :url srv[:url] :token json["access_token"]
}
∇ loadToken (file) {
lines ← io:read file
map 2 2 ⍴ :url (0⌷lines) :token (1⌷lines)
}
∇ (token) postStatus (text) {
url ← token[:url],"api/v1/statuses"
params ← 1 2 ⍴ "status" text
headers ← 1 2 ⍴ "Authorization" ("Bearer ",token[:token])
json ← json:readString io:httpPost (url ; mkParams params ; headers)
}
∇ readFortunes (file) {
lines ← (⊂"%") , io:read file
indexes ← ((⊂"%")≡¨lines) / ⍳⍴lines
indexes (1↓lines)
}
∇ randomFortune (x) {
(indexes lines) ← readFortunes "fortune-netbsd.dat"
z ← indexes[?⍴indexes] ↓ lines
z ↑⍨ 1 ⍳⍨ z≡¨⊂"%"
}
∇ postFortune (token) {
lines ← randomFortune ⍬
token postStatus ↑ {⍺,(unicode:fromCodepoints 10),⍵}/lines
}
∇ foo (x) {
mastodon:postFortune mastodon:loadToken "token.txt"
}
∇ postLoop (interval) {
while (1) {
io:println "Posting new message to server"
foo 0
io:println "Sleeping ",(⍕interval)," seconds"
time:sleep interval
}
}
postLoop 7200
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment