Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fetching data from api #3

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
94 changes: 56 additions & 38 deletions lib/desktime.js
Original file line number Diff line number Diff line change
@@ -1,35 +1,64 @@
const config = require('./config')
const util = require('./util')

const jsdom = require('jsdom')
const {JSDOM} = jsdom
const format = require('dateformat')
const request = require('request-promise')
const baseUrl = `${config.desktime.protocol}://${config.desktime.host}`

const request = require('request')
const options = {
method: 'GET',
uri: baseUrl + '/api/v2/json/employee/projects',
qs: {
apiKey: config.desktime.APIkey,
id: config.desktime.EmployeeId,
date: `${format(new Date(), 'yyyy-mm-dd')}`
},
json: true
}

const baseUrl = `${config.desktime.protocol}://${config.desktime.host}`
const urlLogin = '/app/?session=' + config.desktime.APIkey
const projectTitlePropName = 'project_title'
const timePropName = 'duration'

const req = request.defaults({baseUrl, jar: config.__jar})
function _parseTimeValue(value) {
value = (value && (value = parseInt(value))) ? (value / 60) : null

function _parseValue(content) {
content = content.trim()
if (!value) {
console.log('Warning: project time value is not valid!')
return null
}

return content === '-' ? null : content
return parseInt(value) // float to int
}

function _parseTime(html) {
const {document} = new JSDOM(html).window
const data = document.querySelectorAll('.table-projects > tbody > tr')
function _parseProjectTitleValue(value) {
if (!value || value.trim().length === 0) {
console.log('Warning: project title value is empty!')
return null
}

return value.trim()
}

function _validateValue(obj, propName) {
if (!obj.hasOwnProperty(propName)) {
console.log('Warning: Projects data property "' + propName + '" not isset!')
return null
}

return obj[propName]
}

function _parseTime(data) {
const result = []
data.forEach(function(item) {
const cells = item.cells
const project = _parseValue(cells.item(0).textContent)
const task = _parseValue(cells.item(1).textContent)
const time = _parseValue(cells.item(2).textContent)
data = data || {}
data.projects = data.projects || []

data.projects.forEach(function(item) {
const project = _parseProjectTitleValue(_validateValue(item, projectTitlePropName))
const task = ''
const time = _parseTimeValue(_validateValue(item, timePropName))
const id = util.hash(project + task + time)
result.push({
project && time && result.push({
project,
task,
time,
Expand All @@ -40,31 +69,20 @@ function _parseTime(html) {
return result
}

function _desktimeRequest(url, fn) {
if (config.__cookie.isEmpty() || config.__cookie.isExpired()) {
req(urlLogin, function(error) {
error && console.log(error)
req(url, fn)
})
} else {
req(url, fn)
}
function _desktimeRequest(fn) {
request(options).then(function(response) {
fn(response)
}).catch(function(err) {
console.log(err)
})
}

/**
* @param {Date} date
* @param {callback} fn
* @param {String} scope
*/
function fetchMyTime(date, fn, scope = 'day') {
fn = fn || date
date = typeof date === 'object' ? date : new Date()

const url = `/app/my/${format(date, 'yyyy-mm-dd')}/${scope}`

_desktimeRequest(url, function(error, response, body) {
error && console.log(error)
fn(_parseTime(body))
function fetchMyTime(fn) {
_desktimeRequest(function(response) {
fn(_parseTime(response))
})
}

Expand Down
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@
"test": "standart"
},
"dependencies": {
"assign-deep": "^0.4.7",
"assign-deep": "^1.0.1",
"betterc": "^1.3.0",
"dateformat": "^3.0.3",
"jira-connector": "^2.7.0",
"jsdom": "^11.6.2",
"request": "^2.85.0",
"request": "^2.88.2",
"request-promise": "^4.2.5",
"tough-cookie-file-store": "^1.2.0"
},
"devDependencies": {
Expand Down
3 changes: 2 additions & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ Create file `~/.integrator/config`:
}
},
"desktime": {
"APIkey": "%key%"
"APIkey": "%key%",
"EmployeeId": "%employeeId%"
}
}
```
Expand Down