<commit_before>'use strict';

module.exports = function(handle, request) {
    const Twitter = require("twit-promise")({
        consumer_key: handle.twitter.consumer_key,
        consumer_secret: handle.twitter.consumer_secret,
        access_token: handle.twitter.access_token,
        access_token_secret: handle.twitter.access_token_secret,
        timeout_ms: 1000 * 15
    });

    function encodeImageFromUrl(url) {
        return new Promise(function(resolve, reject) {
            request({ url: url, encoding: null, resolveWithFullResponse: true })
                .then(function(response) {
                    if(response.statusCode == 200) resolve(response.body.data.toString("base64"));
                    else reject(new Error("Response code " + response.statusCode));
                })
                .catch(reject);
        });
    }

    function uploadMedia(imageUrl) {
        console.log("Uploading media...");
        return new Promise(function(resolve, reject) {
            encodeImageFromUrl(imageUrl)
                .then(function(response) {
                    return Promise.all([ response, Twitter.post("media/upload", { media_data: response }) ]);
                })
                .then(function(responses) {
                    return Promise.all([ responses, Twitter.post("media/metadata/create", { media_id: responses[1].data.media_id, alt_text: { text: "An image" } }) ]);
                })
                .then(function(responses) {
                    resolve(responses[1].data.media_id_string);
                })
                .catch(reject);
        });
    }

    return {

        tweet: function(text, imageUrl, state) {
            console.log("Tweeting...");
            return new Promise(function(resolve, reject) {
                if (state.isRawImage()) {
                    console.log("Raw image");
                    uploadMedia(imageUrl)<commit_msg>Wrap the string in a buffer to base64 it<commit_after>'use strict';

module.exports = function(handle, request) {
    const Twitter = require("twit-promise")({
        consumer_key: handle.twitter.consumer_key,
        consumer_secret: handle.twitter.consumer_secret,
        access_token: handle.twitter.access_token,
        access_token_secret: handle.twitter.access_token_secret,
        timeout_ms: 1000 * 15
    });

    function encodeImageFromUrl(url) {
        return new Promise(function(resolve, reject) {
            request({ url: url, encoding: null, resolveWithFullResponse: true })
                .then(function(response) {
                    if(response.statusCode == 200) resolve(new Buffer(response.body.data).toString("base64"));
                    else reject(new Error("Response code " + response.statusCode));
                })
                .catch(reject);
        });
    }

    function uploadMedia(imageUrl) {
        console.log("Uploading media...");
        return new Promise(function(resolve, reject) {
            encodeImageFromUrl(imageUrl)
                .then(function(response) {
                    return Promise.all([ response, Twitter.post("media/upload", { media_data: response }) ]);
                })
                .then(function(responses) {
                    return Promise.all([ responses, Twitter.post("media/metadata/create", { media_id: responses[1].data.media_id, alt_text: { text: "An image" } }) ]);
                })
                .then(function(responses) {
                    resolve(responses[1].data.media_id_string);
                })
                .catch(reject);
        });
    }

    return {

        tweet: function(text, imageUrl, state) {
            console.log("Tweeting...");
            return new Promise(function(resolve, reject) {
                if (state.isRawImage()) {
                    console.log("Raw image");
                    uploadMedia(imageUrl)
{'fraction_non_alphanumeric': 0.1209614887567847, 'fraction_numerical': 0.00723701214784182, 'mean_word_length': 1.9541984732824427, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '6668057', 'n_tokens_mistral': 1044, 'n_tokens_neox': 975, 'n_words': 182}