-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
executable file
·156 lines (122 loc) · 4.66 KB
/
app.js
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
#!/usr/bin/env node
/* jshint node: true, evil: true */
'use strict';
var program = require('commander');
var packagejs = require('./package.json');
program.version(packagejs.version)
.option('-i, --in [path]', 'directory where twitter JSON resides')
.option('-o, --out [path]', 'directory where markdown files will be saved')
.parse(process.argv);
var dirIn = program.in;
var dirOut = program.out;
if (!dirIn || !dirOut) {
console.log('\n Error: You must specify the in and out parameters');
program.help();
}
var fs = require('fs');
var path = require('path');
var mkdirp = require('mkdirp');
var _ = require('lodash');
var moment = require('moment');
var Grailbird = { data: {} };
var TweetDown = function TweetDown(inDir, outDir) {
this.inDir = inDir;
this.outDir = outDir;
};
TweetDown.prototype.init = function init() {
this.getFileList();
this.evaluateFiles();
};
TweetDown.prototype.getFileList = function getFileList() {
this.fileList = fs.readdirSync(this.inDir);
};
TweetDown.prototype.evaluateFiles = function evaluateFiles() {
this.fileList.map((function fileListMap(fileName) {
var filePath = path.join(this.inDir, fileName);
var fileData = fs.readFileSync(filePath, { encoding: 'utf8' });
eval(fileData);
}).bind(this));
};
TweetDown.prototype.writeFile = function writeFile(tweet, text) {
var createdAt = new Date(tweet.created_at);
var momDate = moment(createdAt);
var dateString = momDate.format('YYYYMMDDHHmmss');
var yearString = momDate.format('YYYY');
var monthString = momDate.format('MM');
var outPath = path.join(this.outDir, yearString, monthString);
mkdirp.sync(outPath);
var outFilePath = path.join(outPath, dateString + '.md');
fs.writeFileSync(outFilePath, text, { encoding: 'utf8' });
};
TweetDown.prototype.escape = function escapeMarkdown(text) {
return text.replace(/([\\_\-*+.!`{}()\[\]#])/g, '\\$1');
};
TweetDown.prototype.processFile = function processFile(tweet) {
var origTweet = tweet;
var rt = '';
if (tweet.retweeted_status) {
tweet = tweet.retweeted_status;
rt = '[@' + this.escape(origTweet.user.screen_name) + '](http://twitter.com/' + origTweet.user.screen_name + '): RT ';
}
var text = tweet.text;
var createdAt = new Date(tweet.created_at);
// Generate links from entities
if (tweet.entities) {
var offset = 0;
var userMentions = tweet.entities.user_mentions || [];
var hashTags = tweet.entities.hashtags || [];
var media = tweet.entities.media || [];
var urls = tweet.entities.urls || [];
var entities = userMentions.concat(hashTags).concat(media).concat(urls);
entities = entities.sort(function (a, b) {
return a.indices[0] - b.indices[0];
});
var previousIndex = 0;
var split = [];
entities.map((function (entity) {
split.push({ text: this.escape(tweet.text.substring(previousIndex, entity.indices[0])) });
split.push({ entity: entity });
previousIndex = entity.indices[1];
}).bind(this));
split.push({ text: this.escape(tweet.text.substring(previousIndex)) });
text = '';
split.map((function (val) {
if (val.entity) {
var entity = val.entity;
if (entity.screen_name) {
text += '[@' + this.escape(entity.screen_name) + '](http://twitter.com/' + entity.screen_name + ')';
} else if (entity.text) {
text += '[' + this.escape('#' + entity.text) + '](http://twitter.com/search?q=%23' + entity.text + ')';
} else if (entity.sizes) {
text += '[' + this.escape(entity.display_url) + '](' + entity.media_url + ')';
} else if (entity.display_url) {
text += '[' + this.escape(entity.display_url) + '](' + entity.expanded_url + ')';
}
} else {
text += val.text;
}
}).bind(this));
}
text = '> ' + rt + '[@' + this.escape(tweet.user.screen_name) + '](http://twitter.com/' + tweet.user.screen_name + '): ' + text;
text = text.replace(/\n/g, '\n> ') + '\n';
if (tweet.geo.type) {
if (tweet.geo.type.toLowerCase() === 'point') {
text += '\nLocation: [' + this.escape(tweet.geo.coordinates[0] + ', ' + tweet.geo.coordinates[1]) + '](https://www.google.com/maps/@' + tweet.geo.coordinates[0] + ',' + tweet.geo.coordinates[1] + ',13z)';
}
}
text += '\nCreated At: ' + this.escape(createdAt.toString());
return text;
};
TweetDown.prototype.process = function proc() {
function monthDataMap(tweet) {
var text = tweetDown.processFile(tweet);
tweetDown.writeFile(tweet, text);
}
for (var key in Grailbird.data) {
var monthData = Grailbird.data[key];
monthData.map(monthDataMap);
}
};
var tweetDown = new TweetDown(dirIn, dirOut);
tweetDown.init();
tweetDown.process();