-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
35 lines (30 loc) · 1.15 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
const getCookie = require('./src/cookie')
const download = require('./src/download')
const readUnzip = require('./src/unzip')
function getLatestElanYear() {
const date = new Date()
const year = date.getFullYear()
const month = date.getMonth()
// The Elan Download portal is generally updated on the 1st of September
if (month >= 8) return year.toString()
else return (year-1).toString()
}
module.exports = async function getInvekos(farmno, pass, options) {
if (!farmno || !pass) throw new Error('No credentials provided')
options = options || {}
options.year = options.year || getLatestElanYear()
options.type = options.type ||'Verzeichnis'
let filename = `${options.type}_${farmno}_${options.year}.zip`
if (options.year === 2016) filename = `${options.type}_${farmno}.zip`
try {
// login into ELAN Download Portal and save cookie
const cookie = await getCookie(farmno, pass)
// download zip file and unzip in buffer
const zip = await download(cookie, filename)
const unzipped = await readUnzip(zip)
// return unzipped file
return unzipped
} catch (e) {
throw new Error(e)
}
}