-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
121 lines (114 loc) · 3.25 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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
var nomnom = require("nomnom");
var request = require("request");
now = new Date();
CLI_OPTIONS = {
year: {
full: 'year',
abbr: 'y',
help: 'Target year',
default: now.getFullYear(),
},
month: {
full: 'month',
abbr: 'm',
help: 'Target month',
default: now.getMonth() + 1,
},
task: {
full: 'task',
abbr: 't',
default: '6771643',
help: 'the id of the task. This can be found by hovering the mouse over the given task on Tickspot GUI.'
},
authToken: {
full: 'auth',
abbr: 'a',
help: 'the API authorization token. This can be found at: https://tickspot.com/users',
required: true
},
email: {
full: 'email',
abbr: 'e',
help: 'Your tickspot e-mail address',
required: true
},
subscription: {
full: 'subscription',
abbr: 's',
help: 'Tickspot subscription id. This can be found at: https://tickspot.com/users',
default: "12420"
},
hours: {
full: 'hours',
abbr: 'o',
help: 'Number of worked hours',
required: true,
default: 8
}
}
var options = nomnom.options(CLI_OPTIONS).parse();
var getEntry = function (date, options, callback) {
var url = "https://www.tickspot.com/" + options["subscription"] + "/api/v2/entries.json"
var headers = {
"Authorization": "Token token=" + options["authToken"],
"User-Agent": "tickspot-fill " + options["email"]
}
var data = {
"start_date": date,
"end_date": date
}
request({
headers: headers,
url: url,
body: data,
json: true,
method: "GET",
}, function(error, response, body) {
callback(error, response, date);
});
}
var createEntry = function (date, options) {
var url = "https://www.tickspot.com/" + options["subscription"] + "/api/v2/entries.json"
var headers = {
"Authorization": "Token token=" + options["authToken"],
"User-Agent": "tickspot-fill " + options["email"]
}
var data = {
"date": date,
"hours": options["hours"],
"notes": "",
"task_id": options["task"]
};
request({
headers: headers,
url: url,
body: data,
json: true,
method: "POST",
}, function(error, response, body) {
if (error !== null) {
console.log("Error happened:");
console.dir(error);
}
if (response.statusCode === 201) {
console.log("Entry created on date: " + date);
} else {
console.log("Couldn't create entry on date:", date, "Status code:", response.statusCode);
}
});
}
var year = options["year"];
var month = options["month"] - 1;
date = new Date(year, month, 1);
while(date.getMonth() === month) {
weekDay = date.getDay();
if (weekDay !== 0 && weekDay !== 6) {
var dateToGet = year + "-" + (month+1) + "-" + date.getDate();
getEntry(dateToGet, options, function(error, response, dateToPost) {
if (!error && response.toJSON().body.length == 0) {
createEntry(dateToPost, options);
}
});
}
date.setDate(date.getDate() + 1);
}