-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
226 lines (180 loc) · 5.71 KB
/
index.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
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
221
222
223
224
225
226
var Form = document.querySelector('Form');
var Input = document.querySelector('[name="url"]');
var Result = document.querySelector('.result');
var server = 'wss://steemd-int.steemit.com';
steem.api.setOptions({
url: server
});
var generate = function (author, permlink) {
return new Promise(function (resolve) {
console.log('call api');
Promise.all([
steem.api.getContent(author, permlink),
steem.api.getContentReplies(author, permlink)
]).then(function (result) {
var i, len, entry, votes, link, Calc;
var data = [];
var List = [];
var postData = result[0];
var replies = result[1];
for (i = 0, len = replies.length; i < len; i++) {
console.log('Parse ...' + i);
entry = replies[i];
votes = '';
link = '';
if (entry.author === 'minnowpond') {
continue;
}
if (entry.body.indexOf('https://') === -1) {
continue;
}
link = getLink(entry.body);
if (!link) {
continue;
}
Calc = getTagsFromPost(entry.author, link).then(function (results) {
return results.indexOf('beersaturday');
}).then(function (hasBeersaturday) {
if (hasBeersaturday === -1) {
return;
}
var voteSelf = this;
return getVote(this.author, getPermalinkFromUrl(this.link)).then(function (res) {
var author = voteSelf.author;
var beerVote = postData.active_votes.filter(function (entry) {
return author === entry.voter;
});
if (beerVote.length) {
beerVote = beerVote[0].percent;
} else {
beerVote = 0;
}
data.push({
'Author' : author,
'Link' : voteSelf.link,
'Post Votes' : res.votes,
'Post Weight' : res.weight,
'Beer Vote' : beerVote,
'Beer Vote %' : (beerVote / 100) + '%',
'Beer Comment Votes': voteSelf.net_votes
});
});
}.bind({
author : entry.author,
net_votes: entry.net_votes,
link : link
}));
List.push(Calc);
}
Promise.all(List).then(function () {
console.warn(data);
downloadCSV(data);
resolve();
});
});
});
};
/**
*
* @param author
* @param permlink
* @return {Promise}
*/
var getVote = function (author, permlink) {
return new Promise(function (resolve) {
steem.api.getContent(author, permlink, function (err, result) {
var weight = 0;
var votes = result.active_votes;
for (var i = 0, len = votes.length; i < len; i++) {
weight = weight + votes[i].weight;
}
resolve({
votes : result.active_votes.length,
weight: weight
});
});
});
};
/**
* Return the Beer Link
*
* @param body
* @return {String|Boolean}
*/
var getLink = function (body) {
var exp = /(\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/ig;
body = body.replace(exp, "<a href='$1'>$1</a>");
var Ghost = document.createElement('div');
Ghost.innerHTML = body;
var result = [];
var links = Ghost.querySelectorAll('a');
// beer filter :D
links.forEach(function (Link) {
if (Link.href.indexOf('https://steemit.com/') !== -1) {
result.push(Link.href);
}
});
if (result.length) {
return result[0];
}
return false;
};
/**
* Return all tags from a steemit post
*
* @param {String} author
* @param {String} link
* @return {Promise}
*/
var getTagsFromPost = function (author, link) {
var permlink = getPermalinkFromUrl(link);
return steem.api.getContent(author, permlink).then(function (res) {
var metaData = {};
try {
metaData = JSON.parse(res.json_metadata);
} catch (e) {
}
if ("tags" in metaData) {
return metaData.tags;
}
return [];
});
};
/**
* Return the permalink part from a steemit url
*
* @param value
* @return {String}
*/
var getPermalinkFromUrl = function (value) {
var Url = new URI(value);
if (value.indexOf('https://steemit.com') === -1) {
console.error('No Steemit Url');
return '';
}
var path = Url.path(),
parts = path.split('/');
return parts[3];
};
/**
* Form
*/
Form.addEventListener('submit', function (event) {
event.preventDefault();
var value = Input.value,
Url = new URI(value);
if (value.indexOf('https://steemit.com') === -1) {
console.error('No Steemit Url');
return;
}
var path = Url.path(),
parts = path.split('/');
var author = parts[2].replace('@', '');
var permlink = parts[3];
console.warn(author);
console.warn(permlink);
Result.style.display = '';
generate(author, permlink).then(function () {
Result.style.display = 'none';
});
});