|
1 | | -from typing import Iterable, TypedDict |
| 1 | +from typing import Iterable, Optional, TypedDict |
2 | 2 |
|
3 | 3 | from bs4 import BeautifulSoup |
4 | 4 |
|
5 | 5 |
|
6 | 6 | class DownloadFile(TypedDict): |
7 | 7 | client_entropy: str |
8 | 8 | encrypted: str |
9 | | - encrypted_size: int |
| 9 | + encrypted_size: Optional[int] |
10 | 10 | fileaead: str |
11 | 11 | fileiv: str |
12 | 12 | id: int |
13 | 13 | key_salt: str |
14 | | - key_version: int |
| 14 | + key_version: Optional[int] |
15 | 15 | mime: str |
16 | 16 | #: filename |
17 | 17 | name: str |
18 | 18 | password_encoding: str |
19 | | - password_hash_iterations: int |
20 | | - password_version: int |
| 19 | + password_hash_iterations: Optional[int] |
| 20 | + password_version: Optional[int] |
21 | 21 | size: int |
22 | 22 | transferid: int |
23 | 23 |
|
| 24 | +def _opt_int(value: str) -> Optional[int]: |
| 25 | + return int(value) if value else None |
| 26 | + |
24 | 27 | def files_from_page(content: bytes) -> Iterable[DownloadFile]: |
25 | 28 | """ |
26 | 29 | Yields dictionaries describing the files listed on a FileSender web page |
27 | 30 |
|
28 | 31 | Params: |
29 | | - content: The HTML content of the FileSender download page |
| 32 | + content: The HTML content of the FileSender download page |
30 | 33 | """ |
31 | 34 | for file in BeautifulSoup(content, "html.parser").find_all( |
32 | 35 | class_="file" |
33 | 36 | ): |
34 | 37 | yield { |
35 | 38 | "client_entropy": file.attrs[f"data-client-entropy"], |
36 | 39 | "encrypted": file.attrs["data-encrypted"], |
37 | | - "encrypted_size": int(file.attrs["data-encrypted-size"]), |
| 40 | + "encrypted_size": _opt_int(file.attrs["data-encrypted-size"]), |
38 | 41 | "fileaead": file.attrs["data-fileaead"], |
39 | 42 | "fileiv": file.attrs["data-fileiv"], |
40 | 43 | "id": int(file.attrs["data-id"]), |
41 | 44 | "key_salt": file.attrs["data-key-salt"], |
42 | | - "key_version": int(file.attrs["data-key-version"]), |
| 45 | + "key_version": _opt_int(file.attrs["data-key-version"]), |
43 | 46 | "mime": file.attrs["data-mime"], |
44 | 47 | "name": file.attrs["data-name"], |
45 | 48 | "password_encoding": file.attrs["data-password-encoding"], |
46 | | - "password_hash_iterations": int(file.attrs["data-password-hash-iterations"]), |
47 | | - "password_version": int(file.attrs["data-password-version"]), |
| 49 | + "password_hash_iterations": _opt_int(file.attrs["data-password-hash-iterations"]), |
| 50 | + "password_version": _opt_int(file.attrs["data-password-version"]), |
48 | 51 | "size": int(file.attrs["data-size"]), |
49 | 52 | "transferid": int(file.attrs["data-transferid"]), |
50 | 53 | } |
0 commit comments