Created
March 13, 2017 11:36
-
-
Save benozol/ee411685e0386a2491f1a8d5f4b786b3 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function download(text, name, type) { | |
var a = document.createElement("a"); | |
document.body.appendChild(a) | |
var file = new Blob([text], {type: type}); | |
a.href = URL.createObjectURL(file); | |
a.download = name; | |
a.click(); | |
} | |
function getMessage(elt) { | |
return { | |
'username': elt.find('.username b').text(), | |
'user_id': elt.find('.account-group').attr('data-user-id'), | |
'timestamp': Number(elt.find('small.time a .js-short-timestamp').attr('data-time')), | |
'href': elt.find('small.time a').attr('href'), | |
'content': elt.find('p.tweet-text').text(), | |
'content_html': elt.find('p.tweet-text').html(), | |
}; | |
} | |
function toCSV(msgs) { | |
var processRow = function (row) { | |
var finalVal = ''; | |
for (var j = 0; j < row.length; j++) { | |
var innerValue = row[j] === null ? '' : row[j].toString(); | |
if (row[j] instanceof Date) { | |
innerValue = row[j].toLocaleString(); | |
}; | |
var result = innerValue.replace(/"/g, '""'); | |
if (result.search(/("|,|\n)/g) >= 0) | |
result = '"' + result + '"'; | |
if (j > 0) | |
finalVal += ','; | |
finalVal += result; | |
} | |
return finalVal + '\n'; | |
}; | |
var csvFile = processRow(['username', 'user_id', 'timestamp', 'link', 'content', 'content_html']); | |
for (var i = 0; i < msgs.length; i++) { | |
var msg = msgs[i]; | |
csvFile += processRow([msg.username, msg.user_id, new Date(1000*msg.timestamp), msg.href, msg.content, msg.content_html]); | |
} | |
return csvFile; | |
} | |
var msgs = $('.tweet .content').map(function() { return getMessage($(this)); }) | |
download(toCSV(msgs.toArray()), 'messages.csv', 'text/csv'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The entry point for obtaining Tweets is its search https://twitter.com/search-advanced.
messages.csv
).