<commit_before>const express = require('express');
const app = express();
var http = require('http').Server(app);
var bodyParser = require('body-parser');
var rp = require('request-promise-native');
const cheerio = require('cheerio');
var moment = require('moment');

app.use(bodyParser.json());
app.use(express.static('./frontend/dist'))

app.post('/api/parse/forum-page', function (req, res) {
    parseForumThreadList({}).then((threads) => {
        return res.json(threads);
    }).catch((error)=>{
        console.log(error);
        return res.status(500).json(error);
    });
});

app.post('/api/parse/forum-polls', function (req, res) {
    parseForumThreadList({polls:true}).then((threads) => {
        return res.json(threads);
    }).catch((error)=>{
        console.log(error);
        return res.status(500).json(error);
    });
});

app.post('/api/parse/entry-thread', function (req, res) {
    var uri = req.body.threadUri;
    var start = 0;

    rp(uri).then((html)=>{
        var higestsStart = 0;
        for(let i = 0; i < 1000; i += 15){
            var test = '&amp;start='+i+'">';
            if(html.includes(test)){
                higestsStart = i;
            }
        }

        var pages = [];
        for(let i = 0; i <= higestsStart; i += 15){
            pages.push(uri+'&start='+i);
        }

        var promises = pages.map((page, pageIndex)=>{
            return getInfo(page, pageIndex);
        });

        return Promise.all(promises).then((pageInfo)=>{
            var merged = [].concat.apply([], pageInfo);
            var candidates = merged.filter( (cand) => {
                return cand.postBody.images.length > 0;
            });

            return res.json(candidates);
        });

    }).catch((error) => {
        console.log(error);
        return res.status(500).json(error);
    });
});

app.post('/api/parse/poll-thread', function (req, res) {
    var uri = req.body.threadUri;

    getWinners(uri).then((winners)=>{
        return res.json(winners);
    }).catch((error) => {
        console.log(error);
        return res.status(500).json(error);
    });
});

function getWinners(uri){
    return rp(uri).then((html)=>{
        const $ = cheerio.load(html);
        var rawEntries = []
        var entries = [];

        $('td.pollbg table table tbody tr').each((i, element)=>{
            var text = $(element).first().text().trim();
            var parsed = text.match(/^\*?(.+?) in (.+?)\*?by(.+?)\*?\s/) || [];

            entries[i] = {
                dollName: (parsed[1] || '').trim(),
                caption: (parsed[2] || '').trim(),
                contestant: (parsed[3] || '').trim(),
                imageUri: $(element).find('a img').first().attr('src'),
                votes: parseInt(text.match(/\[(\d+)\]$/)[1] || 0)
            }
        });

        return entries.sort((a, b)=>{
            return b.votes - a.votes;
        });
    });
}

function getInfo(uri, pageIndex){
    return rp(uri).then((html)=>{
        const $ = cheerio.load(html);
        var authors = [];
        var postBody = [];
        $('.postauthor').each((i, element)=>{
            authors[i] = $(element).first().text();<commit_msg>fixed so contestants with spaces in them are properly included<commit_after>const express = require('express');
const app = express();
var http = require('http').Server(app);
var bodyParser = require('body-parser');
var rp = require('request-promise-native');
const cheerio = require('cheerio');
var moment = require('moment');

app.use(bodyParser.json());
app.use(express.static('./frontend/dist'))

app.post('/api/parse/forum-page', function (req, res) {
    parseForumThreadList({}).then((threads) => {
        return res.json(threads);
    }).catch((error)=>{
        console.log(error);
        return res.status(500).json(error);
    });
});

app.post('/api/parse/forum-polls', function (req, res) {
    parseForumThreadList({polls:true}).then((threads) => {
        return res.json(threads);
    }).catch((error)=>{
        console.log(error);
        return res.status(500).json(error);
    });
});

app.post('/api/parse/entry-thread', function (req, res) {
    var uri = req.body.threadUri;
    var start = 0;

    rp(uri).then((html)=>{
        var higestsStart = 0;
        for(let i = 0; i < 1000; i += 15){
            var test = '&amp;start='+i+'">';
            if(html.includes(test)){
                higestsStart = i;
            }
        }

        var pages = [];
        for(let i = 0; i <= higestsStart; i += 15){
            pages.push(uri+'&start='+i);
        }

        var promises = pages.map((page, pageIndex)=>{
            return getInfo(page, pageIndex);
        });

        return Promise.all(promises).then((pageInfo)=>{
            var merged = [].concat.apply([], pageInfo);
            var candidates = merged.filter( (cand) => {
                return cand.postBody.images.length > 0;
            });

            return res.json(candidates);
        });

    }).catch((error) => {
        console.log(error);
        return res.status(500).json(error);
    });
});

app.post('/api/parse/poll-thread', function (req, res) {
    var uri = req.body.threadUri;

    getWinners(uri).then((winners)=>{
        return res.json(winners);
    }).catch((error) => {
        console.log(error);
        return res.status(500).json(error);
    });
});

function getWinners(uri){
    return rp(uri).then((html)=>{
        const $ = cheerio.load(html);
        var rawEntries = []
        var entries = [];

        $('td.pollbg table table tbody tr').each((i, element)=>{
            var text = $(element).first().text().trim();
            var parsed = text.match(/^\*?(.+?) in (.+?)\*?by(.+?)\*?\s\s\s/) || [];

            entries[i] = {
                dollName: (parsed[1] || '').trim(),
                caption: (parsed[2] || '').trim(),
                contestant: (parsed[3] || '').trim(),
                imageUri: $(element).find('a img').first().attr('src'),
                votes: parseInt(text.match(/\[(\d+)\]$/)[1] || 0)
            }
        });

        return entries.sort((a, b)=>{
            return b.votes - a.votes;
        });
    });
}

function getInfo(uri, pageIndex){
    return rp(uri).then((html)=>{
        const $ = cheerio.load(html);
        var authors = [];
        var postBody = [];
        $('.postauthor').each((i, element)=>{
            authors[i] = $(element).first().text();
{'fraction_non_alphanumeric': 0.20344774033234975, 'fraction_numerical': 0.009318217114458767, 'mean_word_length': 2.1692913385826773, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 39, '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': '150359', 'n_tokens_mistral': 2095, 'n_tokens_neox': 1986, 'n_words': 374}