tunnckoCore / blankr

:heart: tasks, todos, ideas, streaks, WIP, etc
https://i.am.charlike.online
4 stars 1 forks source link

fastfingers.js additional #37

Open tunnckoCore opened 9 years ago

tunnckoCore commented 9 years ago

generate table with for all elements

'use strict'

var mzfs = require('mz/fs')
var cheerio = require('cheerio')
var months = require('months')

var config = require('./fastfingers-config.json')

var obj = {}
var tmp = []
var res = []
var data = mzfs.readFile('./' + config.station + '.html', 'utf8')
var chee = data.then(function (html) {
  return cheerio.load(html)
})

function req (year, month) {
  return chee.then(function ($) {
    var tds = $('table#m' + year + 'm tr:nth-child(' + (month + 1) + ') td')
    var monthName = months.abbr[month - 1]

    month++

    $(tds).each(function (i, item) {
      if (i === 0) {
        tmp.push(monthName)
      } else {
        tmp.push($(item).text())
      }
    })

    res.push(tmp)
    tmp = []

    if (month > 12) {
      obj[year] = res
      res = []
      month = 1
      year++
    }

    if (year === config.lastYear) {
      return
    }

    return req(year, month)
  })
}

var output = ''

function element (elnum) {
  return req(config.firstYear, 1).then(function () {
    var arr = Object.keys(obj)
    var html = '<table>'

    arr.forEach(function (year) {
      var monts = obj[year]
      html += '<tr>'
      monts.forEach(function (item, i) {
        html += '<td>' + item[elnum] + '</td>'
      })
      html += '</tr>'
    })

    html += '</table><br>'

    return html
  })
}

element(2)
.then(function (html) {
  console.log('end2')
  output += '<h1>TM</h1>'
  output += html
  return element(3)
})
.then(function (html) {
  console.log('end3')
  output += '<h1>Tm</h1>'
  output += html
  return element(4)
})
.then(function (html) {
  console.log('end4')
  output += '<h1>SLP</h1>'
  output += html
  return element(5)
})
.then(function (html) {
  console.log('end5')
  output += '<h1>H</h1>'
  output += html
  return element(8)
})
.then(function (html) {
  console.log('end8')
  output += '<h1>V</h1>'
  output += html
  return element(11)
})
.then(function (html) {
  console.log('end11')
  output += '<h1>RA</h1>'
  output += html
  return element(12)
})
.then(function (html) {
  console.log('end12')
  output += '<h1>SN</h1>'
  output += html
  return element(13)
})
.then(function (html) {
  console.log('end13')
  output += '<h1>TS</h1>'
  output += html
  return element(14)
})
.then(function (html) {
  console.log('end14')
  output += '<h1>FG</h1>'
  output += html
  return mzfs.writeFile('./' + config.station + '-all-elements.html', output)
})
.then(function () {
  console.log('Success! Copy-paste from `./' + config.station + '-all-elements.html` now!')
})
.catch(console.error)