garoon api

u.go
2016-02-14T05:08:00Z

garoon.js

| const Promise = require('bluebird')
| const request = require('request')
| const cheerio = require('cheerio')
| const moment = require('moment')
| const _ = require('lodash')
|
| class GaroonClient {
| constructor () {
| this.jar = request.jar()
| this.url = {
| base: 'Garoon_BASE_URL',
| login: 'Garoon_login_URL',
| kaneSchedule: 'Garoon_group_schedule_URL'
| }
| this.username = 'USER_NAME'
| this.password = 'PASSWORD'
| }
|
| async login () {
| return new Promise((resolve, reject) => {
| request.post({
| url: this.url.login,
| jar: this.jar,
| formData: {
| _system: 1, _account: this.username, _password: this.password, "login-submit": "Login"
| }
| }, (err, res, body) => {
| if (!err && !_.startsWith(res.statusCode.toString(), '4')) resolve(body)
| else reject(err || new Error(` [GaroonClient.login : bad status]
| status: ${res.statusCode}`))
| }
| )
| })
| }
|
| async fetchGroupSchedulePage() {
| return new Promise ((resolve, reject) => {
| request.get({
| url: this.url.kaneSchedule,
| jar: this.jar
| }, (err,res, body) => {
| if (!err && res.statusCode === 200) resolve(body)
| else reject(err || new Error(` [GaroonClient.fetchGroupSchedulePage : bad status]
| status: ${res.statusCode}`))
| }
| )
| })
| }
|
| parseGroupSchedulePage (htmlBody) {
| let $ = cheerio.load(htmlBody)
| let allUserSchedules = []
| $('tr')
| .filter((i, el) => {
| return $(el).find('td.userBox').length > 0
| })
| .each((i, el)=> {
| //uid
| let userSpan = $(el).find('span.span_user')
| let username = userSpan.text().trim()
| let uid = $(userSpan).find('a').attr('href')
|
| //td tag of today's schedule
| let todayTd = $(el).children('td.normalEvent').first()
| let schedules = []
| todayTd.children('div.normalEventElement').each( (j, e) => {
| if($(e).find('div.groupWeekEventTitle').length > 0) {
| let timeRangeStr = $(e).find('div.listTime').text()
| let startMoment = timeRangeStr.indexOf('-') !== -1
| ? moment(timeRangeStr.slice(0, timeRangeStr.indexOf('-')), 'h:mm a')
| : undefined
| if(startMoment) {
| schedules.push(
| {
| moment: startMoment
| , allDay: false
| , title: $(e).find('div.groupWeekEventTitle').text().trim()
| , link: this.url.base + $(e).find('a').attr('href')
| , error: null
| }
| )
| } else {
| schedules.push(
| {
| moment: undefined
| , allDay: false
| , title: $(e).find('div.groupWeekEventTitle').text().trim()
| , link: this.url.base + $(e).find('a').attr('href')
| , error: new Error(`can't parse time: ${timeRangeStr}`)
| }
| )
| }
|
|
| } else if($(e).find('div.groupWeekEventTitleAllday').length > 0){
| schedules.push(
| {
| moment : undefined
| , allDay: true
| , title: $(e).find('div.groupWeekEventTitleAllday').text().trim()
| , link: this.url.base + $(e).find('a').attr('href')
| , error: null
| }
| )
| } else {
| //logger
| console.log('[Error: groupWeekEventTitle else called]');
| }
|
| })
| allUserSchedules.push({
| username: username,
| uid: uid,
| schedules: schedules
| })
| })
| return allUserSchedules
| }
|
| async pushSheduleToUser(grJson, grUid, bot, slackUser) {
| let u = _.filter(grJson, {uid: grUid})
| if(u) {
|
| }
| }
|
| }
|
| export {
| GaroonClient as default
| }