-
Notifications
You must be signed in to change notification settings - Fork 0
/
RedWall.coffee
220 lines (187 loc) · 5.89 KB
/
RedWall.coffee
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
# Rx = require("./bower_components/rxjs/rx.all.js")
Rx = require("rx")
cheerio = require("cheerio")
fs = require('fs')
Observable = Rx.Observable
Array.prototype.contains = (what) ->
return this.indexOf(what) != -1
Array::randomized = -> @sort -> 0.5 - Math.random()
class RedWall
helpSpace = null
bigDance = null
logger = null
constructor: (whichHelpSpaceToUse) ->
helpSpace = whichHelpSpaceToUse
logger =
log: (whatToLog) ->
console.log(whatToLog)
go = () ->
bigDance.subscribe(
((emitted) ->
logger.log("Successfully set #{emitted} as today's wallpaper")
),
((error) ->
logger.log("Uncaught error: #{error}")
),
(() ->
logger.log("Finished")
)
)
test = () ->
null
# test()
go()
dayIndexStream = Observable.from([(new Date()).getDay()])
dayStream = dayIndexStream.map \
(whichIndex) ->
{
0: "sunday",
1: "monday",
2: "tuesday",
3: "wednesday",
4: "thursday",
5: "friday",
6: "saturday",
}[whichIndex]
redditUrlStream = dayStream.map \
(whichDay) ->
{
"monday": "http://www.reddit.com/r/bikeporn",
"tuesday": "http://www.reddit.com/r/spaceporn",
"wednesday": "http://www.reddit.com/r/roomporn",
"thursday": "http://www.reddit.com/r/cityporn",
"friday": "http://www.reddit.com/r/earthporn",
"saturday": "http://www.reddit.com/r/bikeporn",
"sunday": "http://www.reddit.com/r/bikeporn",
}[whichDay]
redditPageStream = redditUrlStream.flatMap \
(redditUrl) ->
return Observable.create \
(observer) ->
helpSpace.download(
redditUrl,
(successfullyDownloaded) ->
logger.log("Successfully downloaded #{redditUrl}")
observer.onNext(successfullyDownloaded)
observer.onCompleted()
(failedDownload) ->
logger.log("Couldn't download #{redditUrl}")
observer.onError(failedDownload)
)
# return Observable.catch(redditPageStream, Observable.empty())
redditPageStreamGivenThreeChances = \
Observable.amb([
Observable.catch(redditPageStream, Observable.empty()).delay(0 * 1000),
Observable.catch(redditPageStream, Observable.empty()).delay(30 * 1000),
Observable.catch(redditPageStream, Observable.empty()).delay(90 * 1000),
# Observable.throw(new Error('Failed to retrieve reddit page content')).delay(10000)
# Observable.create(
# (observer) ->
# throw new Error('Failed to retrieve reddit page content')
# )
]).defaultIfEmpty(Observable.throw(new Error('Failed to retrieve reddit page content')))
jqueryDocStream = redditPageStreamGivenThreeChances.map \
(docText) ->
return cheerio.load(docText);
imageUrlsStream = jqueryDocStream.map \
(jqueryDoc) ->
return jqueryDoc("div.entry.unvoted")
.find("a.title.may-blank")
.filter(
(i, eachLink) ->
linkFileExtension = helpSpace.getFileExtension(eachLink)
return (linkFileExtension == "jpg") or
(linkFileExtension == "jpeg") or
(linkFileExtension == "png")
).toArray() \
.randomized()
.map(
(eachLink) ->
return eachLink.attribs.href
)
imageContentStreamTriedSuccessively = imageUrlsStream.flatMap \
(imageUrls) ->
successiveTries = []
howMuchDelay = -3000
for imageUrl in imageUrls
howMuchDelay += 3000
fileExtension = helpSpace.getFileExtension(imageUrl)
successiveTries.push (Observable.create \
(observer) ->
helpSpace.downloadForBinary(
imageUrl,
(successfullyDownloaded) ->
logger.log("Successfully downloaded #{imageUrl}")
observer.onNext([successfullyDownloaded, fileExtension])
observer.onCompleted()
(failedDownload) ->
logger.log("Couldn't download #{imageUrl}")
null
)
).delay(howMuchDelay)
# successiveTries.push Rx.Observable.throw(new Error('Failed to retrieve reddit image content')).delay(100)
return Observable.amb(successiveTries) \
.defaultIfEmpty(Observable.throw(new Error('Failed to retrieve reddit image content')))
.first()
imageContentSaved = imageContentStreamTriedSuccessively.map(
([whichImageContent, whichFileExtenstion]) ->
filename = "redwall.#{whichFileExtenstion}"
fs.writeFile(filename, whichImageContent, {encoding: 'binary'},
(err) ->
if (err)
throw new Error("image content not saved")
)
return filename
).take(1)
imageContentHeard = imageContentSaved.map(
(whichFilename) ->
helpSpace.informOS(whichFilename)
return whichFilename
)
bigDance = imageContentHeard
class HelpSpace
http = require("http");
# http://www.storminthecastle.com/2013/08/25/use-node-js-to-extract-data-from-the-web-for-fun-and-profit/
@downloadForBinary: (url, successCallback, badnewsCallback) ->
http.get(url, (res) ->
data = ""
res.setEncoding('binary')
res.on('data', (chunk) ->
data += chunk
)
res.on("end", () ->
successCallback(data)
)
).on("error", (e) ->
badnewsCallback(e);
)
request = require("request")
# http://www.sitepoint.com/web-scraping-in-node-js/
@download: (url, successCallback, badnewsCallback) ->
console.log("@ HelpSpace.download")
# thisUrl = url
request({uri: url,
},
(error, response, body) ->
if (error)
badnewsCallback(error)
else
successCallback(body)
)
@getFileExtension: (fromLinkorText) ->
linkHref = if fromLinkorText.attribs then fromLinkorText.attribs.href else fromLinkorText
return linkHref[(linkHref.lastIndexOf('.') + 1)..]
exec = require('child_process').exec
# http://stackoverflow.com/questions/20643470/execute-a-command-line-binary-with-node-js
@informOS: (whichFilename) ->
preciseFilename = require('path').resolve(
__dirname,
whichFilename)
cmd = """
gsettings set org.gnome.desktop.background picture-uri file://#{preciseFilename} &&
gsettings set org.gnome.desktop.background picture-options scaled
"""
exec(cmd, (error, stdout, stderr) ->
null
) # todo: error handling
new RedWall(HelpSpace)