/* 
Copyright 2021 Huan Truong <htruong@tnhh.net> & Chisomo Sakala

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

Full text of license: GPLv2: https://www.gnu.org/licenses/old-licenses/gpl-2.0.en.html
*/

import { Readability } from '@mozilla/readability'
import striptags from 'striptags'
import request from 'request'
import { JSDOM } from 'jsdom'
import Feed from 'rss-to-json'
import jsonFilter from 'node-json-filter'
import express from 'express'

import template from './template.js'

const app = express()
const port = 3000
const filter = new jsonFilter({
  items: [{ url: '$string', title: '$string' }],
})


function make_readable(body, url) {
  const doc = new JSDOM(body, {url})
  const reader = new Readability(doc.window.document)

  return reader.parse()
}

function article(req, res) {
  //      console.log(req);
  //res.send('Hello World!')
        
  const url = req.query.url
  const contentType = req.query.contentType
  console.log(`GET /article/ url=${url}`)
  
  request.get(url, (error, response, body) => {
    // callback(make_readable(body), true)
    const { content, title } = make_readable(body)

    //console.log(obj);
    if (contentType !== 'html') {
      res.setHeader('Content-Type', 'text/plain')
      let txt = 'ERROR!'
      if (content !== null) {
        txt = content
      }

      //txt = striptags(striptags(txt, ['p']), '', '\n');
      //txt = striptags(txt, ['p']);
      txt = txt.replace(/\n/gm, ' ')
      txt = striptags(txt, ['p'], '')
      //console.log(txt);
      txt = striptags(txt, [], '\n')
      const blankLines = new RegExp(/(^[ \t]*)/, 'gm')
      txt = txt.replace(blankLines, '')
      txt = txt.replace(/^(\n){2,}/gm, '\n')
      //txt = txt.replace(/ /g, '-');

      res.send(`${title}\n==============\n\n${txt}`)
    } else {
      res.send(template(title, content))
    }  
  })
  
}


///////////////
app.get('/article/', article)

app.get('/feed/', (req, res) => {
  const url = req.query.url
  console.log(`GET /feed/ url=${url}`)
  Feed.load(url, (err, { items }) => {
    const rss_trunc_items = items.slice(0, 15)
    const rss_trunc = {}
    rss_trunc.items = rss_trunc_items
    res.setHeader('Content-Type', 'text/json')
    res.send(JSON.stringify(filter.apply(rss_trunc), null, 3))
  })
})

app.listen(port, '0.0.0.0', () => {
  console.log(`Example app listening at http://localhost:${port}`)
})