|
let fs = require ("fs") |
|
let _ = require("lodash") |
|
|
|
function getOthers(tweet) { |
|
let text = tweet.full_text.replace(/@arvidkahl/g, '').replace("@"+tweet.user.screen_name, '') |
|
let m = text.match(/@[\S]+/); |
|
if (m) {m = m[0].replace(/[\.'].*/,"")} |
|
return m || "" |
|
} |
|
|
|
|
|
function isAlreadyWinner(all, it) { |
|
if (!it || !it.user) return false; |
|
return _.some(all, (i)=> { |
|
return i.user.screen_name == it.user.screen_name || i.full_text.indexOf(it.user.screen_name)>-1 |
|
}) |
|
} |
|
|
|
let data = _.map(JSON.parse(fs.readFileSync("./all.json", {encoding: 'utf-8'})), (i) => _.extend(i, {type: i.is_quote_status==true?'retweet':'reply'})) |
|
const max_winners = 50 |
|
|
|
let candidates = [] |
|
candidates = _.shuffle(data) |
|
candidates = _.filter(candidates, (i)=>{return i.user.screen_name!='arvidkahl'}) // filter out my own account |
|
candidates = _.filter(candidates, (i)=>{return (getOthers(i))}) // only those who mention other accounts |
|
candidates = _.uniqBy(candidates, (i) => (i.user.screen_name + getOthers(i)).split('').sort().join('')) // filter out replies to replies |
|
|
|
winners = [] |
|
|
|
while (winners.length < max_winners) { |
|
let candidate = candidates.shift(); |
|
if (!isAlreadyWinner(winners, candidate)) winners.push(candidate) |
|
} |
|
|
|
console.log("=== Long ===\n\n") |
|
|
|
_.forEach(winners, (v, i)=>{ |
|
console.log(`Winner #${i+1}: @${v.user.screen_name} (and ${getOthers(v)}) [${v.type}] https://twitter.com/${v.user.screen_name}/status/${v.id_str}`) |
|
}) |
|
|
|
console.log("\n\n=== Tweetable ===\n\n") |
|
|
|
_.forEach(winners, (v, i)=>{ |
|
console.log(`Winner #${i+1}: @${v.user.screen_name} and ${getOthers(v)}`) |
|
}) |
|
|
|
console.log("\n\n=== Accounts ===\n\n") |
|
|
|
let names = [] |
|
_.forEach(winners, (v, i)=>{ |
|
names.push(v.user.screen_name) |
|
names.push(getOthers(v).replace("@",'')) |
|
}) |
|
_.forEach(_.uniq(names.sort()), (v) => console.log(v)) |
I’m not sure if this is a feature, but right now if a name appear in multiple tweets its odds of being picked are increased. Maybe you should extract the list of twitter handles somehow, discard duplicates, and use the remaining list in order to pick a winner ?