Skip to content

Instantly share code, notes, and snippets.

@tsuzu
Last active June 7, 2018 06:01
Show Gist options
  • Save tsuzu/6aa60c7ff575b6eed052 to your computer and use it in GitHub Desktop.
Save tsuzu/6aa60c7ff575b6eed052 to your computer and use it in GitHub Desktop.
AtCoder Submission Notifier
http = require 'https'
cookie = require 'cookie'
NotCen = require('node-notifier').NotificationCenter
notifier = new NotCen {withFallback: false, customPath: null}
name = "" # Your ID
password = "" # Your Password
if process.argv.length < 3
console.log "usage: node main.js contest_name(ex. abc001.atcoder.jp=>abc001)"
process.exit 0
contest_name = process.argv[2]
hostname = contest_name + ".contest.atcoder.jp"
session = ""
submissions = {}
isFirstTime = true
startLoop = ->
req = http.get {host: hostname, path: "/submissions/me", headers: {cookie: session}}, (res)->
if res.statusCode != 200
console.log "error: connectiing to #{hostname}/submissions/me (statusCode: #{res.statusCode})"
res.setEncoding 'utf-8'
buf = ""
res.on 'data', (chunk)->
buf += chunk
res.on 'end', (chunk)->
regs = /<tbody>([\s\S]+)<\/tbody>/.exec buf
if regs?
subm = regs[1]
reg = /<tr>[\s\S]*?<time>([\s\S]+?)<\/time><\/td>[\s\S]*?<a href=.*?>([\s\S]*?)<\/a><\/td>[\s\S]*?<td class=".*">([\s\S]+?)<\/td>[\s\S]*?<td class=".*">(.*?)<\/td>[\s\S]*?data-title="([\s\S]*?)">[\s\S]*?<td class="right">([\s\S]*?)<\/td>[\s\S]*?<td class="right">([\s\S]*?)<\/td>[\s\S]*?<td><a href="([\s\S]*?)"><span[\s\S]*?<\/tr>/g
while (regs = reg.exec subm)?
if regs[5] != "Judging" && regs[5] != "Waiting for Judging"
if !(submissions[regs[8]]?)
submissions[regs[8]] = {
time: regs[1],
name: regs[2],
lang: regs[3],
score: if regs[4] == "-" then 0 else regs[4],
result: regs[5],
proctime: regs[6],
memory: regs[7]
link: regs[8]
}
if !isFirstTime
notifier.notify {
title: regs[5],
subtitle: "#{contest_name}: #{regs[2]}",
message: "Score: #{regs[4]}, Time: #{regs[6]}, Memory: #{regs[7]}",
sound: false,
open: "https://" + hostname + regs[8],
}, (err, resp)->
if err?
console.error err
isFirstTime = false
setTimeout ->
startLoop()
, 1000
req.setTimeout 10000
req.on 'error', (err)->
console.error "error: #{err}"
req.on 'timeout', ->
console.error 'error: timeout'
req.abort()
post_body = "name=#{encodeURIComponent(name)}&password=#{encodeURIComponent(password)}"
req = http.request {
host: hostname,
path: "/login",
method: "POST",
headers: {"content-type": "application/x-www-form-urlencoded", "content-length": post_body.length, "host": hostname}
}, (res)->
if res.statusCode != 302
console.error "error: failed to log in to AtCoder(#{hostname})"
process.exit
res.setEncoding 'utf-8'
arr = []
for str in res.headers["set-cookie"]
regs = /^(.*?);/.exec(str)
if regs?
arr.push(regs[1])
session = arr.join('; ')
console.log "log: succeed in logging in to AtCoder"
startLoop()
req.setTimeout 10000
req.on 'error', (err)->
console.error "error: #{err}"
req.on 'timeout', ->
req.abort()
console.error "error: timeout"
req.write post_body
req.end()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment