SearchMore

Search in Bunker @ footboom.com forum

2018-05-26 या दिनांकाला. सर्वात नवीन आवृत्ती पाहा.

// ==UserScript==
// @name        SearchMore
// @namespace	novhna
// @description	Search in Bunker @ footboom.com forum
// @include     https://www.footboom.com/forum/*
// @include     http://www.footboom.com/forum/*
// @version     0.0.3
// @grant       GM_getValue
// @grant       GM_setValue
// ==/UserScript==

const headers = {
    "content-type": "application/json",
    "x-apikey": "5b0953381af1a2243f0b9b1c",
    "cache-control": "no-cache",
    'Accept': 'application/json',
    'Content-Type': 'application/json',
}

const db = (collection, method, data = '') => new Promise((resolve, reject) => {
    fetch(`https://bunker-b54a.restdb.io/rest/${collection}`, { 
        method,
        headers, 
        mode: 'cors',
        body: data,
    })
        .then(res => res.json())
        .then(resolve)
        .catch(console.warn)
}

const updatingTimeout = 60*1000

let meta = {}

let topics = []

/* Setting up metadata *
fetch('https://bunker-b54a.restdb.io/rest/metadata', {
        method: 'POST',
        mode: 'cors',
        headers,
        body: JSON.stringify({ lastDepth: 0, lastUpdate: 0 }),
    })
        .then(res => res.json())
        .then(res => {
            console.log('meta--->', res)
        })
        .catch(console.warn)
/* */

const fetchMetadata = () => fetch('https://bunker-b54a.restdb.io/rest/metadata', { headers })
    .then(res => res.json())
    .then(docs => {
        console.log(docs[0])
        meta = docs[0]
        
        if (Date.now() - meta.lastUpdate < updatingTimeout) return false
        console.log('Updating topics in Database!')
        updateDB(1)
    })
    .catch(console.warn)

const grabPage = page => new Promise((resolve, reject) => {
	fetch(`/forum/bunker?page=${page}`)
        .then(res => res.text())
        .then(doc => { 
            const rows = doc.match(/<tr> <td class="m-10">[\s\S]*?<\/tr>/g)
            const info = rows.map(row => ({
                date: row.match(/datetime="(.+?)"/)[1], 
                topic: row.match(/<a[\s\S]*?>([\s\S]+?)<\/a>/)[1].trim(),
                author: row.match(/<td[\s\S]*?>(.+?)<\/td>/g)[2].replace(/<[\s\S]+?>/g, ''),
                link: row.match(/href="(.+?)"/)[1],
            }))
            resolve(info)
        })
        .catch(err => reject(err))
})

const updateMetadata = lastDepth => {
    meta = {
        lastDepth,
        lastUpdate: Date.now(),
    }
    
    fetch('https://bunker-b54a.restdb.io/rest/metadata', {
        method: 'PUT',
        mode: 'cors',
        headers,
        body: JSON.stringify(meta),
    })
        .then(res => res.json())
        .then(res => {
            console.log('Metadata has been updated!', res)
        })
        .catch(console.warn)
}

const updateDB = depth => {
    const pages = Array(depth).fill().map((_, i) => i + 1)
    
    Promise.all(pages.map(grabPage)).then(res => {
        const docs = res.reduce(($, arr) => [...$, ...arr], [])    
        console.log(docs)
        topics = docs
    
        fetch('https://bunker-b54a.restdb.io/rest/topics', {
            method: 'POST',
            mode: 'cors',
            headers,
            // headers: {
            //     "content-type": "application/json",
            //     "x-apikey": "5b0953381af1a2243f0b9b1c",
            //     "cache-control": "no-cache",
            //     'Accept': 'application/json',
            //     'Content-Type': 'application/json',
            // },
            body: JSON.stringify(topics),
        })
            .then(res => res.json())
            .then(res => {
                updateMetadata(depth)
                
                console.log('Topics has been updated!', res)
            })
            .catch(console.warn)
    })
}

const getAllTopics = db('topics', 'GET').then(console.log)

const _searchFor = query => topics.filter(({ topic }) => topic.match(new RegExp(query, "i")))

const searchFor = word => fetch(`https://bunker-b54a.restdb.io/rest/topics?q={"_tags":{"$regex":"${word}"}}`, {
    method: 'GET',
    mode: 'cors',
    headers: {
        "content-type": "application/json",
        "x-apikey": "5b0953381af1a2243f0b9b1c",
        "cache-control": "no-cache",
        'Accept': 'application/json',
        'Content-Type': 'application/json',
    },
})
    .then(res => res.json())
    .then(docs => {
        console.log(docs)
        topics = docs
    })
    .catch(console.warn)