-
Notifications
You must be signed in to change notification settings - Fork 2
/
cli-args.js
149 lines (122 loc) · 5.18 KB
/
cli-args.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
const login_isa = require('./lib/login-isa')
const connect_isa = require('./lib/connect-isa')
const auto_logout_disabler = require('./lib/auto-logout-disabler')
const Watcher = require('./Watcher')
const notify = require('./util/notify')
const fail = reason => {
console.error(`Error: ${reason}`)
process.exit(1)
}
const print_help = () => {
console.log(`shs-notifier (https://github.com/Maeeen/shs-notifier)
Usage:
-h, --help : Prints this
--cookie=<cookie> : Log in with this isa-cookie (mandatory or --creds)
--creds=<user>*<pass> : Log in with the given credentials (mandatory or --cookie)
--watch=<course_ids> : Watch courses with the given courses' id, separated by commas
--discord-webhook-url=<url>: Will trigger the given discord's webhook when the course is available
--spam-discord : Spam the webhook instead of only one send. Default=false
--disable-desktop-notify : Disables desktop notification
--polling-interval=<int> : The polling interval, in milliseconds. Default=5000
--tg-creds=<token>*<id> : Bot token and dst chat id, update and activate telegram bot.
This tool is not endorsed by any organization.
`)
}
module.exports = async (ISA_ADDR, args) => {
if (args.includes('-h') || args.includes('--help')) {
print_help()
return
}
let find_arg = begins_with => args.filter(d => d.startsWith(begins_with)).map(d => d.substring(begins_with.length)).find(d => true)
let cookie
const credsArg = find_arg('--creds=')
if (credsArg) {
let split = credsArg.split('*')
let username = split.shift()
let password = split.join('*') // in the case where the password contains weird ass characters
console.log(process.argv)
if (username && password) {
try {
cookie = await login_isa(ISA_ADDR, { username, password })
} catch(e) {
return fail('Invalid credentials')
}
}
}
const cookieArg = find_arg('--cookie=')
if (cookieArg) {
cookie = cookieArg
}
if (!cookie)
return fail('No credentials or cookie given. Can not login to IS-Academia.')
let course_reg_url, courses, auto_logout_date
try {
// Verifying access
const home_data = await connect_isa.fetch_home(ISA_ADDR, cookie)
course_reg_url = home_data.course_reg_url
auto_logout_date = new Date(Date.now() + home_data.logout_delay)
courses = await connect_isa.fetch_courses(ISA_ADDR, cookie, course_reg_url)
} catch(e) {
return fail('Invalid cookie')
}
const coursesIdArg = find_arg('--watch=')
if (!coursesIdArg)
return fail('No given courses to watch')
const watched_courses = coursesIdArg.split(',')
watched_courses.forEach(id => {
if (courses.hasOwnProperty(id))
console.log(`Watching ${courses[id].text}`)
else
console.warn(`Warning: can not find course with the id ${id}`)
})
let polling_interval = 5000
const pollingIntervalArg = find_arg('--polling-interval=')
if (pollingIntervalArg) {
let temp = parseInt(pollingIntervalArg)
if (!Number.isNaN(temp))
polling_interval = temp
else
console.warn(`Warning: The given polling interval is invalid.`)
}
const notification_settings = {
do_discord_webhook: false,
discord_webhook_url: '',
desktop_notification: !args.includes('--disable-desktop-notify'),
discord_webhook_spam: args.includes('--spam-discord'),
do_telegram_bot: false,
bot_token: '',
chat_id: 0
}
const discordWebhookUrlArg = find_arg('--discord-webhook-url=')
if (discordWebhookUrlArg) {
if (/[(http(s)?):\/\/(www\.)?a-zA-Z0-9@:%._\+~#=]{2,256}\.[a-z]{2,6}\b([-a-zA-Z0-9@:%_\+.~#?&//=]*)/ig.test(discordWebhookUrlArg)) {
notification_settings.do_discord_webhook = true
notification_settings.discord_webhook_url = discordWebhookUrlArg
} else {
console.warn(`Warning: The given discord webhook url is invalid.`)
}
}
const tgCreds = find_arg('--tg-args=')
if (tgCreds) {
let split = credsArg.split('*')
let telegramBotTokenArg = split.shift()
let telegramChatIdArg = split.join('*')
notification_settings.do_telegram_bot = true //TODO: no input check
notification_settings.bot_token = telegramBotTokenArg
notification_settings.chat_id = telegramChatIdArg
}
const watcher = new Watcher()
auto_logout_disabler({token: cookie, auto_logout_date})
watcher.on('available-course', courses => {
notify(courses.map(d => d.text), notification_settings)
})
if (args.includes('--debug')) {
setTimeout(() => notify('ptdrrr', notification_settings), 10000)
}
watcher.on('checked', () => {
process.stdout.clearLine()
process.stdout.cursorTo(0)
process.stdout.write(`Last update: ${new Date().toLocaleString()}. Watching ${watched_courses.length} courses.`)
})
watcher.watch(ISA_ADDR, { token: cookie, course_reg_url }, { watched_courses, polling_interval })
}