<commit_before>/*
 * Avocore : twitter/api.js
 * copyright (c) 2015 Susisu
 */

"use strict";

function end_module() {
    module.exports = Object.freeze({
        "tweetRandom"      : tweetRandom,
        "replyMentions"    : replyMentions,
        "replyHomeTimeline": replyHomeTimeline,
        "followBack"       : followBack
    });
}

var core    = require("../core.js"),
    actions = require("../actions.js"),
    api     = require("./api.js");

function showDate() {
    return "[" + (new Date()).toLocaleTimeString() + "] ";
}

function tweetRandom(keys, file, separator, encoding) {
    return actions.readFile(file, encoding)
        .map(function (content) {
            return content.toString().split(separator);
        })
        .bind(actions.pickRandom)
        .bind(function (status) {
            return api.post(keys, "statuses/update", {
                    "status": status
                })
                .then(actions.echo(showDate() + "Tweeted: " + status))
        });
}

function replyWith(keys, func) {
    return function (tweet) {
        var replyText = func(tweet);
        if (replyText !== undefined) {
            var status = "@" + tweet["user"]["screen_name"] + " " + replyText;
            return api.post(keys, "statuses/update", {
                    "status"               : status,
                    "in_reply_to_status_id": tweet["id_str"]
                })
                .then(actions.echo(showDate() + "Tweeted: " + status));
        }
        else {
            return core.pure(undefined);
        }
    };
}

function maxIdStr(x, y) {
    if (x.length > y.length) {
        return x;
    }
    else if (x.length < y.length) {
        return y;
    }
    else if (x > y) {
        return x;
    }
    else {
        return y;
    }
}

function replyMentions(keys, screenName, count, sinceId, func) {
    var params = {};
    if (count !== undefined) {
        params["count"] = count;
    }
    if (sinceId !== undefined && sinceId !== "") {
        params["since_id"] = sinceId;
    }
    return api.get(keys, "statuses/mentions_timeline", params)
        .bind(function (tweets) {
            tweets =
                tweets.filter(function (data) {
                    return data["text"]                !== undefined
                        && data["id_str"]              !== undefined
                        && data["user"]                !== undefined
                        && data["user"]["screen_name"] !== undefined
                        && data["user"]["screen_name"] !== screenName;
                });
            var latestIdStr =
                tweets.map(function (data) { return data["id_str"]; })
                .reduce(maxIdStr, "0");
            return core.pure(tweets)
                .bind(actions.split)
                .bind(replyWith(keys, func))
                .then(core.pure(latestIdStr));
        });
}

function replyHomeTimeline(keys, screenName, count, sinceId, func) {
    var params = {
        "exclude_replies": true
    };
    if (count !== undefined) {
        params["count"] = count;
    }
    if (sinceId !== undefined && sinceId !== "") {
        params["since_id"] = sinceId;
    }
    return api.get(keys, "statuses/home_timeline", params)
        .bind(function (tweets) {
            tweets =
                tweets.filter(function (data) {
                    return data["text"]                !== undefined
                        && data["id_str"]              !== undefined
                        && data["user"]                !== undefined
                        && data["user"]["screen_name"] !== undefined
                        && data["user"]["screen_name"] !== screenName;
                });
            var latestIdStr =
                tweets.map(function (data) { return data["id_str"]; })
                .reduce(maxIdStr, "0");
            return core.pure(tweets)
                .bind(actions.split)
                .bind(replyWith(keys, func))
                .then(core.pure(latestIdStr));
        });
}

function followBack(keys, screenName, count) {
    var params = {
        "screen_name": screenName
    };
    if (count !== undefined) {
        params["count"] = count;
    }
    return api.get(keys, "followers/list", params)
        .filter(function (data) {
            return data["users"] !== undefined;
        })
        .map(function (data) {
            return data["users"];
        })
        .bind(actions.split)
        .filter(function (data) {
            return data["screen_name"] !== undefined
                && data["following"]   !== undefined;
        })
        .filter(function (data) {
            return !data["following"];
        })
        .bind(function (user) {
            return api.post(keys, "friendships/create", {
                    "screen_name": user["screen_name"],<commit_msg>Fix twitter/utils<commit_after>/*
 * Avocore : twitter/api.js
 * copyright (c) 2015 Susisu
 */

"use strict";

function end_module() {
    module.exports = Object.freeze({
        "tweetRandom"      : tweetRandom,
        "replyMentions"    : replyMentions,
        "replyHomeTimeline": replyHomeTimeline,
        "followBack"       : followBack
    });
}

var core    = require("../core.js"),
    actions = require("../actions.js"),
    api     = require("./api.js");

function showDate() {
    return "[" + (new Date()).toLocaleTimeString() + "] ";
}

function tweetRandom(keys, file, separator, encoding) {
    return actions.readFile(file, encoding)
        .map(function (content) {
            return content.toString().split(separator);
        })
        .bind(actions.pickRandom)
        .bind(function (status) {
            return api.post(keys, "statuses/update", {
                    "status": status
                })
                .then(actions.echo(showDate() + "Tweeted: " + status))
        });
}

function replyWith(keys, func) {
    return function (tweet) {
        var replyText = func(tweet);
        if (replyText !== undefined) {
            var status = "@" + tweet["user"]["screen_name"] + " " + replyText;
            return api.post(keys, "statuses/update", {
                    "status"               : status,
                    "in_reply_to_status_id": tweet["id_str"]
                })
                .then(actions.echo(showDate() + "Tweeted: " + status));
        }
        else {
            return core.pure(undefined);
        }
    };
}

function maxIdStr(x, y) {
    if (x.length > y.length) {
        return x;
    }
    else if (x.length < y.length) {
        return y;
    }
    else if (x > y) {
        return x;
    }
    else {
        return y;
    }
}

function replyMentions(keys, screenName, count, sinceId, func) {
    var params = {};
    if (count !== undefined) {
        params["count"] = count;
    }
    if (sinceId !== undefined && sinceId !== "") {
        params["since_id"] = sinceId;
    }
    return api.get(keys, "statuses/mentions_timeline", params)
        .bind(function (tweets) {
            tweets =
                tweets.filter(function (data) {
                    return data["text"]                !== undefined
                        && data["id_str"]              !== undefined
                        && data["user"]                !== undefined
                        && data["user"]["screen_name"] !== undefined
                        && data["user"]["screen_name"] !== screenName;
                });
            var latestIdStr =
                tweets.map(function (data) { return data["id_str"]; })
                .reduce(maxIdStr, "0");
            return actions.split(tweets)
                .bind(replyWith(keys, func))
                .discard()
                .then(core.pure(latestIdStr));
        });
}

function replyHomeTimeline(keys, screenName, count, sinceId, func) {
    var params = {
        "exclude_replies": true
    };
    if (count !== undefined) {
        params["count"] = count;
    }
    if (sinceId !== undefined && sinceId !== "") {
        params["since_id"] = sinceId;
    }
    return api.get(keys, "statuses/home_timeline", params)
        .bind(function (tweets) {
            tweets =
                tweets.filter(function (data) {
                    return data["text"]                !== undefined
                        && data["id_str"]              !== undefined
                        && data["user"]                !== undefined
                        && data["user"]["screen_name"] !== undefined
                        && data["user"]["screen_name"] !== screenName;
                });
            var latestIdStr =
                tweets.map(function (data) { return data["id_str"]; })
                .reduce(maxIdStr, "0");
            return actions.split(tweets)
                .bind(replyWith(keys, func))
                .discard()
                .then(core.pure(latestIdStr));
        });
}

function followBack(keys, screenName, count) {
    var params = {
        "screen_name": screenName
    };
    if (count !== undefined) {
        params["count"] = count;
    }
    return api.get(keys, "followers/list", params)
        .filter(function (data) {
            return data["users"] !== undefined;
        })
        .map(function (data) {
            return data["users"];
        })
        .bind(actions.split)
        .filter(function (data) {
            return data["screen_name"] !== undefined
                && data["following"]   !== undefined;
        })
        .filter(function (data) {
            return !data["following"];
        })
        .bind(function (user) {
            return api.post(keys, "friendships/create", {
                    "screen_name": user["screen_name"],
{'fraction_non_alphanumeric': 0.14710726786081943, 'fraction_numerical': 0.0012353304508956147, 'mean_word_length': 1.583089603828769, 'pattern_counts': {'":': 12, '<': 5, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2717000', 'n_tokens_mistral': 2767, 'n_tokens_neox': 2701, 'n_words': 522}