pku-elective 0.1.0

北大选课网 CLI 客户端
Documentation
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
//! 所有子命令的实现

use crate::api::{ElectiveApi, ValidationResult};
use crate::captcha::{self, CaptchaConfig};
use crate::config::{AutoElectCourse, ElectiveConfig};
use crate::display;
use crate::login::APP_NAME;
use anyhow::{anyhow, Result};
use colored::Colorize;
use pkuinfo_common::session::Store;

// ─── show ─────────────────────────────────────────────────────

pub async fn cmd_show() -> Result<()> {
    let api = ElectiveApi::from_session()?;
    let results = api.get_results().await?;
    display::print_results(&results);
    Ok(())
}

// ─── list ─────────────────────────────────────────────────────

pub async fn cmd_list(page: Option<usize>) -> Result<()> {
    let api = ElectiveApi::from_session()?;
    let (total_pages, elected) = api.get_supply_cancel().await?;

    display::print_elected(&elected);

    let page = page.unwrap_or(0);
    if page >= total_pages {
        return Err(anyhow!(
            "页码 {} 超出范围(共 {} 页)",
            page + 1,
            total_pages,
        ));
    }

    let courses = api.get_supplements(page).await?;
    display::print_supplements(&courses, page, total_pages);

    println!(
        "{}",
        format!(
            "提示: 使用 `elective list --page N` 浏览其他页面 (1-{})",
            total_pages,
        )
        .dimmed()
    );
    Ok(())
}

// ─── set ──────────────────────────────────────────────────────

pub async fn cmd_set() -> Result<()> {
    let store = Store::new(APP_NAME)?;
    let mut cfg = ElectiveConfig::load(store.config_dir())?;

    let api = ElectiveApi::from_session()?;

    println!("{} 正在加载所有补退选课程...", "[*]".cyan());
    let all = api.get_all_supplements().await?;

    if all.is_empty() {
        println!("{}", "补退选列表为空".dimmed());
        return Ok(());
    }

    // 显示所有课程供选择
    for (i, c) in all.iter().enumerate() {
        let full_mark = if c.is_full() {
            " [满]".red().to_string()
        } else {
            String::new()
        };
        println!(
            "  {} {} - {} (班号:{}) [页{}]{}",
            format!("[{}]", i + 1).cyan(),
            c.base.name,
            c.base.teacher,
            c.base.class_id,
            c.page_id + 1,
            full_mark,
        );
    }

    println!("请输入要自动选课的课程编号:");
    let idx = read_index()?.saturating_sub(1);
    let course = all
        .get(idx)
        .ok_or_else(|| anyhow!("无效的编号"))?;

    // 检查是否已在配置中
    let exists = cfg.auto_elect.iter().any(|c| {
        c.name == course.base.name
            && c.teacher == course.base.teacher
            && c.class_id == course.base.class_id
    });

    if exists {
        println!("{} 该课程已在自动选课列表中", "[info]".yellow());
        return Ok(());
    }

    cfg.auto_elect.push(AutoElectCourse {
        page_id: course.page_id,
        name: course.base.name.clone(),
        teacher: course.base.teacher.clone(),
        class_id: course.base.class_id.clone(),
    });

    cfg.save(store.config_dir())?;
    println!(
        "{} 已添加: {} - {} (班号:{})",
        "".green(),
        course.base.name,
        course.base.teacher,
        course.base.class_id,
    );
    Ok(())
}

// ─── unset ────────────────────────────────────────────────────

pub fn cmd_unset() -> Result<()> {
    let store = Store::new(APP_NAME)?;
    let mut cfg = ElectiveConfig::load(store.config_dir())?;

    if cfg.auto_elect.is_empty() {
        println!("{}", "自动选课列表为空".dimmed());
        return Ok(());
    }

    display::print_auto_elect_list(&cfg.auto_elect);
    println!("请输入要移除的编号:");

    let idx = read_index()?.saturating_sub(1);
    if idx >= cfg.auto_elect.len() {
        return Err(anyhow!("无效的编号"));
    }

    let removed = cfg.auto_elect.remove(idx);
    cfg.save(store.config_dir())?;

    println!(
        "{} 已移除: {} - {}",
        "".green(),
        removed.name,
        removed.teacher,
    );
    Ok(())
}

// ─── config-captcha ───────────────────────────────────────────

pub fn cmd_config_captcha(backend: &str) -> Result<()> {
    let store = Store::new(APP_NAME)?;
    let mut cfg = ElectiveConfig::load(store.config_dir())?;

    cfg.captcha = match backend {
        "manual" => CaptchaConfig::Manual,
        "utool" => CaptchaConfig::Utool,
        "ttshitu" => {
            print!("TTShiTu 用户名: ");
            std::io::stdout().flush()?;
            let mut username = String::new();
            std::io::stdin().read_line(&mut username)?;
            let username = username.trim().to_string();

            print!("TTShiTu 密码: ");
            std::io::stdout().flush()?;
            let password = rpassword::read_password()?;

            CaptchaConfig::TTShiTu { username, password }
        }
        "yunma" => {
            print!("云码 Token (用户中心密钥): ");
            std::io::stdout().flush()?;
            let mut token = String::new();
            std::io::stdin().read_line(&mut token)?;
            let token = token.trim().to_string();

            if token.is_empty() {
                return Err(anyhow!("Token 不能为空"));
            }

            CaptchaConfig::Yunma { token }
        }
        _ => return Err(anyhow!(
            "未知后端: {backend}。可选: manual, utool, ttshitu, yunma"
        )),
    };

    cfg.save(store.config_dir())?;
    println!("{} 验证码后端已设为: {}", "".green(), cfg.captcha);
    Ok(())
}

// ─── launch ───────────────────────────────────────────────────

pub async fn cmd_launch(interval_secs: u64) -> Result<()> {
    let store = Store::new(APP_NAME)?;
    let cfg = ElectiveConfig::load(store.config_dir())?;

    if cfg.auto_elect.is_empty() {
        return Err(anyhow!(
            "未配置自动选课目标。使用 `elective set` 添加课程"
        ));
    }

    println!(
        "{} 开始监控 {} 门课程,间隔 {}s",
        "[launch]".green().bold(),
        cfg.auto_elect.len(),
        interval_secs,
    );

    display::print_auto_elect_list(&cfg.auto_elect);
    println!("验证码后端: {}", cfg.captcha);
    println!();

    let api = ElectiveApi::from_session()?;
    let mut targets = cfg.auto_elect.clone();

    loop {
        if targets.is_empty() {
            println!("{} 所有目标课程已选上!", "".green().bold());
            break;
        }

        // 检查已选课程,移除已成功的
        match api.get_supply_cancel().await {
            Ok((_, elected)) => {
                targets.retain(|t| {
                    let already = elected.iter().any(|e| {
                        e.name == t.name && e.teacher == t.teacher && e.class_id == t.class_id
                    });
                    if already {
                        println!(
                            "  {} 已选上: {} - {}",
                            "".green(),
                            t.name,
                            t.teacher,
                        );
                    }
                    !already
                });
            }
            Err(e) => {
                println!(
                    "  {} 查询已选课程失败: {e:#}",
                    "[warn]".yellow(),
                );
            }
        }

        if targets.is_empty() {
            println!("{} 所有目标课程已选上!", "".green().bold());
            break;
        }

        // 逐个检查目标课程
        for target in &targets {
            match api.get_supplements(target.page_id).await {
                Ok(supplements) => {
                    let found = supplements.iter().find(|s| {
                        s.base.name == target.name
                            && s.base.teacher == target.teacher
                            && s.base.class_id == target.class_id
                    });

                    if let Some(course) = found {
                        if course.is_full() {
                            println!(
                                "  {} {} - {} ({})",
                                "×".red(),
                                target.name,
                                target.teacher,
                                course.base.status.dimmed(),
                            );
                            continue;
                        }

                        // 有名额!尝试选课
                        println!(
                            "  {} {} - {} 有名额!正在尝试选课...",
                            "!".yellow().bold(),
                            target.name,
                            target.teacher,
                        );

                        match try_elect(&api, &cfg.captcha, &store, course.elect_url.clone())
                            .await
                        {
                            Ok(true) => {
                                println!(
                                    "  {} 选课成功: {} - {}",
                                    "".green().bold(),
                                    target.name,
                                    target.teacher,
                                );
                            }
                            Ok(false) => {
                                println!(
                                    "  {} 选课失败(服务器拒绝)",
                                    "[fail]".red(),
                                );
                            }
                            Err(e) => {
                                println!(
                                    "  {} 选课出错: {e:#}",
                                    "[error]".red(),
                                );
                            }
                        }
                    } else {
                        println!(
                            "  {} {} - {} 在第 {} 页未找到",
                            "?".yellow(),
                            target.name,
                            target.teacher,
                            target.page_id + 1,
                        );
                    }
                }
                Err(e) => {
                    println!(
                        "  {} 获取第 {} 页失败: {e:#}",
                        "[warn]".yellow(),
                        target.page_id + 1,
                    );
                }
            }
        }

        let now = chrono::Local::now().format("%H:%M:%S");
        println!(
            "  {} 下次检查于 {}s 后 ({})",
            "[wait]".dimmed(),
            interval_secs,
            now,
        );
        tokio::time::sleep(std::time::Duration::from_secs(interval_secs)).await;
    }

    Ok(())
}

/// 尝试通过验证码并选课
async fn try_elect(
    api: &ElectiveApi,
    captcha_cfg: &CaptchaConfig,
    store: &Store,
    elect_url: String,
) -> Result<bool> {
    // 最多重试 3 次验证码
    for attempt in 0..3 {
        // 获取验证码图片
        let image = api.get_captcha_image().await?;

        // 识别验证码
        let code = captcha::recognize(
            api.client(),
            captcha_cfg,
            &image,
            store.config_dir(),
        )
        .await?;

        println!("    验证码识别结果: {code}");

        // 验证
        match api.validate_captcha(&code).await? {
            ValidationResult::Success => {
                // 验证通过,提交选课
                let msg = api.elect(&elect_url).await?;
                if let Some(m) = &msg {
                    println!("    选课结果: {m}");
                }
                return Ok(true);
            }
            ValidationResult::Wrong => {
                println!(
                    "    {} 验证码错误 ({}/3)",
                    "[retry]".yellow(),
                    attempt + 1,
                );
                tokio::time::sleep(std::time::Duration::from_secs(1)).await;
            }
            ValidationResult::Empty => {
                println!("    {} 验证码未填写", "[error]".red());
            }
        }
    }

    Ok(false)
}

// ─── helpers ──────────────────────────────────────────────────

use std::io::Write;

fn read_index() -> Result<usize> {
    print!("> ");
    std::io::stdout().flush()?;
    let mut buf = String::new();
    std::io::stdin().read_line(&mut buf)?;
    buf.trim()
        .parse::<usize>()
        .map_err(|_| anyhow!("请输入有效数字"))
}