-
Notifications
You must be signed in to change notification settings - Fork 8
/
tiny-rss-updater.groovy
75 lines (64 loc) · 1.99 KB
/
tiny-rss-updater.groovy
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
@Grab(group='org.codehaus.groovy.modules.http-builder', module='http-builder', version='0.7.2' )
import groovyx.net.http.*
import static groovyx.net.http.ContentType.*
import static groovyx.net.http.Method.*
//====================================================================
def env = System.getenv()
def tinyrss = env["TINY_RSS_URL"]?:"https://tiny-man.rhcloud.com" +"/api/"
def username = env["TINY_RSS_USERNAME"]
def password = env["TINY_RSS_PASSWORD"]
def http = new groovyx.net.http.HTTPBuilder(tinyrss)
//Set JSON parser according to server response
http.parser.'text/json' = http.parser.'application/json'
def sessionId = login(http, username, password)
try {
getFeeds(http, sessionId).each { feed ->
updateFeed ( http, sessionId, feed)
}
} finally {
logout(http, sessionId)
}
//====================================================================
def login(http, username, password) {
def sessId
http.request( POST, JSON ){ req ->
body = ["op":"login","user": username, "password": password]
response.success = { resp, json ->
sessId = json?.content?.session_id
}
}
if (!sessId) {
throw new RuntimeException("Failed to log to Tiny RSS")
}
println "Successfully logged in"
sessId
}
def getFeeds(http, sid) {
def feeds = []
http.request( POST, JSON ){ req ->
body = ["op":"getFeeds","sid": sid, "cat_id": -3]
response.success = { resp, json ->
feeds = json.content
}
}
feeds
}
def updateFeed(http, sid, feed) {
http.request( POST, JSON ){ req ->
body = ["op":"updateFeed","sid": sid, "feed_id": feed.id]
response.success = { resp, json ->
println "Updated feed ${feed.title}"
}
}
}
def logout(http, sid) {
if (!sid) {
return
}
http.request( POST, JSON ){ req ->
body = ["op":"logout","sid": sid]
response.success = { resp, json ->
println "Successfully logged out"
}
}
}