-
Notifications
You must be signed in to change notification settings - Fork 5
/
save-to-db.coffee
executable file
·37 lines (29 loc) · 1.21 KB
/
save-to-db.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
fs = require('fs')
mysql = require('mysql')
user = process.argv[2] ? "zuck"
_env = require('system').env
feed = JSON.parse(fs.readFileSync("parsed/#{user}.json"))
# Function to dump an object to console w/ indentation. Based off casperjs's utils lib
dump = (value, indent = 4) ->
if Array.isArray(value)
value = value.map (prop) ->
if typeof(prop) is 'function' then prop.toString().replace(/\s{2,}/, '') else prop
console.log JSON.stringify(value, null, indent)
unless _env.hasOwnProperty("mysql_user") and _env.hasOwnProperty("mysql_pass")
console.log "Missing environment variables. Run `source .env` first."
process.exit(1)
connection = mysql.createConnection({
host : 'localhost',
user : _env.mysql_user,
password : _env.mysql_user,
database : 'statuses'
});
connection.connect()
for key, val of feed
# dump val
connection.query('INSERT INTO statuses(id, user, content, permalink, time, timestamp, likes, shares, comments, is_friend_post) VALUES (null, ?, ?, ?, ?, ?, ?, ?, ?, ?)', [user, val.content, val.permalink, val.time, val.timestamp, val.likes, val.shares, val.comments, val.isFriendPost], (err, results) ->
dump err
dump results
)
connection.end()
process.exit()