<commit_before>var Plugin = require('./baseplugin'),
    og = require('open-graph'),
    firstBy = require('thenBy.js');

class OpenGraph extends Plugin {
    canHandle(url) {
        return true;
    }

    // open-graph results sucks. No, really!
    processOGResult(obj) {
        if(!obj) { return; }
        if(obj.image && obj.image.url && obj.image.url.length > 0) {
            var urls = obj.image.url,
                heights = obj.image.height || [],
                widths = obj.image.width || [],
                types = obj.image.type || [],
                images = [];
            for(var i = 0; i < urls.length; i++) {
                var r = {
                    url: urls[i],
                    type: types[i],
                    width: widths[i],
                    height: heights[i]
                };
                if(widths[i] && heights[i]) {
                    r.size = `${widths[i]}x${heights[i]}`;
                }
                images.push(r);
            }
            delete obj['image'];
            obj.images = images;
        }
        return obj;
    }

    arrayOrValue(o) {
        return Array.isArray(o) ? o[0] : o;
    }

    process(url, callback) {
        this.logger.verbose('openGraph', `processing ${url}`);
        og(url, (err, meta) => {
            if(err) {
                this.logger.warning('openGraph', `Cannot process url ${url}: `, err);
                callback(null);
                return;
            } else {
                meta = this.processOGResult(meta);
                var images = meta.images;
                if(images) {
                    images = images.sort(
                        firstBy('width')
                        .thenBy('height')
                    ).reverse();
                }
                if(!meta.site_name && !meta.title && !meta.description) {
                    callback(null);
                } else {
                    callback({
                        type: 'rich-link',
                        name: this.arrayOrValue(meta.site_name),
                        title: this.arrayOrValue(meta.title),
                        description: this.arrayOrValue(meta.description),
                        image: images ? images[0] : null
                    });
                }
            }
        });
    }
}

OpenGraph.priority = -1;
module.exports = OpenGraph<commit_msg>Update OG plugin<commit_after>var Plugin = require('./baseplugin'),
    og = require('open-graph'),
    firstBy = require('thenBy.js');

class OpenGraph extends Plugin {
    canHandle(url) {
        return true;
    }

    // open-graph results sucks. No, really!
    processOGResult(obj) {
        if(!obj) { return; }
        if(obj.image && obj.image.url && obj.image.url.length > 0) {
            var urls = obj.image.url,
                heights = obj.image.height || [],
                widths = obj.image.width || [],
                types = obj.image.type || [],
                images = [];
            for(var i = 0; i < urls.length; i++) {
                var r = {
                    url: urls[i],
                    type: types[i],
                    width: widths[i],
                    height: heights[i]
                };
                if(widths[i] && heights[i]) {
                    r.size = `${widths[i]}x${heights[i]}`;
                }
                images.push(r);
            }
            delete obj['image'];
            obj.images = images;
        }
        return obj;
    }

    arrayOrValue(o) {
        return Array.isArray(o) ? o[0] : o;
    }

    process(url, callback) {
        this.logger.verbose('openGraph', `processing ${url}`);
        og(url, (err, meta) => {
            if(err) {
                this.logger.warning('openGraph', `Cannot process url ${url}: `, err);
                callback(null);
                return;
            } else {
                meta = this.processOGResult(meta);
                var images = meta.images;
                if(images) {
                    images = images.sort(
                        firstBy('width')
                        .thenBy('height')
                    ).reverse();
                }
                if(!meta.site_name && !meta.title && !meta.description) {
                    callback(null);
                } else {
                    callback({
                        type: 'rich-link',
                        ogType: this.arrayOrValue(meta.type),
                        site_name: this.arrayOrValue(meta.site_name),
                        title: this.arrayOrValue(meta.title),
                        description: this.arrayOrValue(meta.description),
                        image: images ? images[0] : null
                    });
                }
            }
        });
    }
}

OpenGraph.priority = -1;
module.exports = OpenGraph
{'fraction_non_alphanumeric': 0.13165497317375155, 'fraction_numerical': 0.0020635575732562937, 'mean_word_length': 1.1212253829321663, 'pattern_counts': {'":': 0, '<': 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': '3199481', 'n_tokens_mistral': 1342, 'n_tokens_neox': 1254, 'n_words': 243}