20
20
var longjohn = require('./rcl/node_modules/longjohn')
22
22
//stdin.setEncoding('utf8');
24
// Declare utility functions
25
function get_url(doc, from_url, to_html, status_flag){
26
http.get(doc[from_url], function(res){
28
res.on('data', function(chunk){
31
res.on('end', function(){
32
// TODO: Check to see if we got a 404 response
33
// Write the contents of the html variable back to the database
34
doc[to_html] = pageData
35
doc[status_flag] = 'gotten'
36
// TODO: Use Backbone here instead of cradle
37
db.save(doc._id, doc._rev, doc, function(err, res){
38
// TODO: Do anything more that needs to be done here
43
function get_url_set(doc, from_urls, to_html, status_flag){
44
for (var i=0; i<doc[from_urls].length; i++){
45
doc[status_flag] = 'getting'
46
http.get(doc[from_urls][i], function(res){
48
res.on('data', function(chunk){
51
res.on('end', function(){
52
// TODO: Check to see if we got a 404 response
53
// Write the contents of the html variable back to the database
54
if (!doc[to_html] || doc[to_html] == ''){
55
doc[to_html] = new Array() // declare as array
57
var temp_items = doc[to_html]
58
var num_items = temp_items.push(pageData)
59
doc[to_html] = temp_items
61
if (num_items==doc[from_urls].length){
62
// We've gotten all the items' html!
63
doc[status_flag] = 'gotten'
65
// TODO: Use Backbone here instead of cradle
66
db.save(doc._id, doc._rev, doc, function(err, res){
67
// TODO: Do anything more that needs to be done here
24
75
process.on('message', function(doc){
27
77
// Watch for requests to get the contents of a URL for a church directory
28
78
// TODO: Check to see if the URL is valid
29
if (doc.collection == 'directory' && doc.get_url_contents==true && doc.url){
79
if (doc.collection == 'directory' && doc.get_url_html=='requested' && doc.url){
30
80
// E.g., when a user enters "opc.org/locator.html" into the church directory configuration page,
31
81
// then go get the contents of that URL.
32
http.get(doc.url, function(res){
34
res.on('data', function(chunk){
37
res.on('end', function(){
38
// TODO: Check to see if we got a 404 response
39
// Write the contents of the html variable back to the database
40
doc.url_html = pageData
41
doc.get_url_contents = false
42
// TODO: Use Backbone here instead of cradle
43
db.save(doc._id, doc._rev, doc, function(err, res){
44
// TODO: Do anything more that needs to be done here
82
get_url(doc, 'url', 'url_html', 'get_url_html')
84
// Watch for requests to get the contents of a state page URL
85
if (doc.collection == 'directory' && doc.get_state_url_html=='requested' && doc.state_url){
86
get_url(doc, 'state_page_values', 'state_url_html', 'get_state_url_html')