Skip to content

Commit

Permalink
refactor: wk cookie
Browse files Browse the repository at this point in the history
  • Loading branch information
dvgamerr committed Mar 6, 2024
1 parent ae6800f commit 472f0bf
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 17 deletions.
40 changes: 35 additions & 5 deletions collector/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,15 @@ const args = arg({
});

const enableGithub = args['--github'] && process.env.GH_TOKEN
const enableWakatime =args['--wakatime'] && process.env.WK_TOKEN
const enableWakatime =args['--wakatime'] && process.env.WK_COOKIE

// Initializes the application based on provided arguments and environment variables.
const Initializes = async () => {

if (process.env.GH_TOKEN) logger.info(`'GH_TOKEN' is available`)
if (enableGithub) logger.info(`API github.com collector starting...`)

if (process.env.WK_TOKEN) logger.info(`'WK_TOKEN' is available`)
if (process.env.WK_COOKIE) logger.info(`'WK_COOKIE' is available`)
if (enableWakatime) logger.info(`API Wakatime.com collector starting...`)
}

Expand Down Expand Up @@ -151,8 +151,8 @@ const collectWakaTime = async () => {
// https://wakatime.com/api/v1/users/current/durations?api_key=f389e6d1-207e-4c49-9526-d2623ce7b6d1&date=2022-03-26

const coding = {
weekly_seconds: 0,
average_seconds: 0,
weekly_seconds: 0,
best_seconds: 0,
daytime: [],
weektime: []
Expand All @@ -165,7 +165,7 @@ const collectWakaTime = async () => {
'accept': 'application/json, text/javascript, */*; q=0.01',
'referer': 'https://wakatime.com/@dvgamerr',
'user-agent': fetchUserAgent(0),
'cookie': process.env.WK_TOKEN,
'cookie': process.env.WK_COOKIE,
'authority': 'wakatime.com'
}
})
Expand All @@ -175,6 +175,36 @@ const collectWakaTime = async () => {
}

const body = await res.json()

const { days: dayOfYear } = body.data

const idx = dayOfYear.length - 1
for (let i = idx; i >= 0; i--) {
const totalDuration = dayOfYear[i].total
const dateDuration = dayjs(dayOfYear[i].date, 'YYYY-MM-DD')
coding.average_seconds += totalDuration
if (i > idx - 7) {
coding.weekly_seconds += totalDuration

dateDuration
}
if (coding.best_seconds < totalDuration) coding.best_seconds = totalDuration

// weekday
const dayInWeek = dateDuration.day()
const shiftWeek = dayInWeek - 1 < 0 ? 6 : dayInWeek - 1
if (!coding.weektime[shiftWeek]) coding.weektime[shiftWeek] = { count: 0, duration: 0 }
coding.weektime[shiftWeek] = {
count: coding.weektime[shiftWeek].count + 1,
duration: coding.weektime[shiftWeek].duration + totalDuration,
}
}
coding.average_seconds = coding.average_seconds / dayOfYear.length
coding.weekly_seconds = coding.weekly_seconds / 7
coding.weektime = coding.weektime.map(e => e.duration / e.count)

logger.debug(coding)

await mergeJsonResponse(body, './src/i18n/insights.json')

// for (let i = totalDay; i >= 0; i--) {
Expand All @@ -185,7 +215,7 @@ const collectWakaTime = async () => {
// const day = currentDate.add(i * -1, 'd').day()
// wakaTask.push((async () => {
// const { status, data: { data: durations } } = await apiWaka.request('GET /users/current/durations', {
// api_key: process.env.WK_TOKEN,
// api_key: process.env.WK_COOKIE,
// date: dateFormat
// })
// if (status !== 200) return logger.error(status)
Expand Down
16 changes: 8 additions & 8 deletions src/i18n/coding.json
Original file line number Diff line number Diff line change
Expand Up @@ -84,12 +84,12 @@
95
],
"weektime": [
125.65,
112.12,
111.32,
124.95,
135.05,
53.68,
52.85
7387.844356711541,
10491.952919365383,
8996.180162358487,
9303.651937615385,
6895.8477763461515,
3726.3331686538468,
2789.1322143653842
]
}
}
4 changes: 2 additions & 2 deletions src/i18n/experience.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
"interview": false,
"currencry": {
"ccy": "USD",
"sell": 36.06,
"buy": 35.44
"sell": 35.92,
"buy": 35.41
},
"salary": {
"base": 71950,
Expand Down
4 changes: 2 additions & 2 deletions src/i18n/insights.json
Original file line number Diff line number Diff line change
Expand Up @@ -5099,11 +5099,11 @@
},
{
"date": "2024-03-06",
"total": 28070.39642,
"total": 30262.969648,
"categories": [
{
"name": "Coding",
"total": 28070.39642
"total": 30262.969648
}
]
}
Expand Down

0 comments on commit 472f0bf

Please sign in to comment.