0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

online-judge-tools (oj) と atcoder-cli (acc) のログインでbot判定・エラーが出るときの解決手順

0
Posted at

背景

AtCoder のセキュリティ対策(reCAPTCHA / Cloudflare)強化に伴い、oj login や acc login で以下の問題が発生してログインできないケースの解決手順です。

  • oj login --use-browser で起動したブラウザで「ロボットではありません」に弾かれる
  • 拡張機能で取得した cookies.txt を渡すと does not look like a Set-Cookie3 (LWP) format file エラーになる
  • oj はログインできているのに acc session が not login のまま同期されない

ブラウザから Cookie をエクスポートし、Python スクリプトで oj と acc の両方に直接セッションを適用します。

前提環境

  • Python 3.x
  • online-judge-tools (oj)
  • atcoder-cli (acc)

解決手順

1. ブラウザから Cookie をエクスポート

  1. Chrome 拡張機能 Get cookies.txt LOCALLY などをインストール。
  2. 通常の Chrome で AtCoder にログイン。
  3. AtCoder のページ上で拡張機能を開き、Cookie を cookies.txt としてダウンロード。

2. Python スクリプトの実行

ターミナルで cookies.txt を配置したディレクトリへ移動し、以下のワンライナーを実行します。

python3 -c '
import http.cookiejar
import json
from pathlib import Path

# 1. Netscape形式 (cookies.txt) を読み込み
mozilla_jar = http.cookiejar.MozillaCookieJar("cookies.txt")
mozilla_jar.load(ignore_discard=True, ignore_expires=True)

# 2. oj 用に LWP形式 (Set-Cookie3) へ変換して保存
oj_dir = Path.home() / ".local/share/online-judge-tools"
oj_dir.mkdir(parents=True, exist_ok=True)
oj_cookie_path = oj_dir / "cookie.jar"

lwp_jar = http.cookiejar.LWPCookieJar(str(oj_cookie_path))
for c in mozilla_jar:
    lwp_jar.set_cookie(c)
lwp_jar.save(ignore_discard=True, ignore_expires=True)
print(f"[+] oj: Saved to {oj_cookie_path}")

# 3. acc 用に session.json (REVEL_SESSION) を生成して保存
revel_session = None
for c in mozilla_jar:
    if c.name == "REVEL_SESSION":
        revel_session = c.value
        break

if not revel_session:
    print("[!] Error: REVEL_SESSION cookie not found in cookies.txt")
    exit(1)

acc_dir = Path.home() / ".config/atcoder-cli-nodejs"
acc_dir.mkdir(parents=True, exist_ok=True)
acc_session_path = acc_dir / "session.json"

with open(acc_session_path, "w") as f:
    json.dump({"cookies": [f"REVEL_SESSION={revel_session}"]}, f, indent=2)

print(f"[+] acc: Saved to {acc_session_path}")
print("[+] All setup completed successfully!")
'

Notes: 本スクリプトの配置先は Linux 環境を前提としています。

3. ログイン状態の確認

# oj の確認
oj login --check https://atcoder.jp/
# => [SUCCESS] You have already signed in.

# acc の確認
acc session

どちらも認証が通っていれば完了です。不要になった cookies.txt にはセッション情報が含まれるため、安全のためささっと削除しておいてください。

スクリプトの解説・仕組み

import http.cookiejar
import json
from pathlib import Path

# 1. Netscape形式 (cookies.txt) を読み込み
mozilla_jar = http.cookiejar.MozillaCookieJar("cookies.txt")
mozilla_jar.load(ignore_discard=True, ignore_expires=True)

# 2. oj 用に LWP形式 (Set-Cookie3) へ変換して保存
oj_dir = Path.home() / ".local/share/online-judge-tools"
oj_dir.mkdir(parents=True, exist_ok=True)
oj_cookie_path = oj_dir / "cookie.jar"

lwp_jar = http.cookiejar.LWPCookieJar(str(oj_cookie_path))
for c in mozilla_jar:
    lwp_jar.set_cookie(c)
lwp_jar.save(ignore_discard=True, ignore_expires=True)
print(f"[+] oj: Saved to {oj_cookie_path}")

# 3. acc 用に session.json (REVEL_SESSION) を生成して保存
revel_session = None
for c in mozilla_jar:
    if c.name == "REVEL_SESSION":
        revel_session = c.value
        break

if not revel_session:
    print("[!] Error: REVEL_SESSION cookie not found in cookies.txt")
    exit(1)

acc_dir = Path.home() / ".config/atcoder-cli-nodejs"
acc_dir.mkdir(parents=True, exist_ok=True)
acc_session_path = acc_dir / "session.json"

with open(acc_session_path, "w") as f:
    json.dump({"cookies": [f"REVEL_SESSION={revel_session}"]}, f, indent=2)

print(f"[+] acc: Saved to {acc_session_path}")
print("[+] All setup completed successfully!")
[cookies.txt (Netscape形式)]
         │
         ▼ (MozillaCookieJar で解析)
   Cookie オブジェクト群
   ├── ① 全Cookieを LWP形式へ変換 ──▶ ~/.local/share/online-judge-tools/cookie.jar (oj用)
   └── ② REVEL_SESSION を抽出 ─────▶ ~/.config/atcoder-cli-nodejs/session.json  (acc用)

  1. cookies.txt のパース (MozillaCookieJar)
    拡張機能が出力する Netscape 形式(タブ区切り)のテキストを、Python 標準ライブラリで Cookie オブジェクトへ復元します。
  2. oj 向けの LWP 形式変換 (LWPCookieJar)
    online-judge-tools は http.cookiejar.LWPCookieJar(Set-Cookie3 形式)を期待しているため、そのまま cookies.txt を配置するとパースエラーになります。読み込んだ Cookie を LWP 形式に変換して cookie.jar に保存しています。
  3. acc 向けの session.json 生成
    AtCoder のセッション情報は主に REVEL_SESSION Cookie に格納されています。acc が参照する ~/.config/atcoder-cli-nodejs/session.json に直接書き出すことで、CLI からのログイン試行(bot判定)を回避しています。
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?