yklibpy 0.1.11__tar.gz → 0.2.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- yklibpy-0.2.0/.claude/settings.local.json +11 -0
- {yklibpy-0.1.11 → yklibpy-0.2.0}/.gitignore +2 -0
- yklibpy-0.2.0/.python-version +1 -0
- yklibpy-0.2.0/CLAUDE.md +109 -0
- {yklibpy-0.1.11 → yklibpy-0.2.0}/PKG-INFO +4 -2
- yklibpy-0.2.0/mypy.ini +39 -0
- {yklibpy-0.1.11 → yklibpy-0.2.0}/pyproject.toml +9 -2
- yklibpy-0.2.0/src/yklibpy/common/__init__.py +22 -0
- {yklibpy-0.1.11 → yklibpy-0.2.0}/src/yklibpy/common/env.py +15 -15
- yklibpy-0.2.0/src/yklibpy/common/safedict.py +3 -0
- yklibpy-0.2.0/src/yklibpy/common/timex.py +9 -0
- {yklibpy-0.1.11 → yklibpy-0.2.0}/src/yklibpy/common/util.py +40 -66
- yklibpy-0.2.0/src/yklibpy/common/util_json.py +16 -0
- yklibpy-0.2.0/src/yklibpy/common/util_yaml.py +80 -0
- yklibpy-0.2.0/src/yklibpy/config/__init__.py +3 -0
- yklibpy-0.2.0/src/yklibpy/config/appconfig.py +65 -0
- yklibpy-0.2.0/src/yklibpy/db/__init__.py +42 -0
- yklibpy-0.2.0/src/yklibpy/db/appstore.py +120 -0
- {yklibpy-0.1.11 → yklibpy-0.2.0}/src/yklibpy/db/db_yaml.py +9 -5
- yklibpy-0.2.0/src/yklibpy/db/storex.py +56 -0
- yklibpy-0.2.0/src/yklibpy/htmlparser/__init__.py +27 -0
- {yklibpy-0.1.11 → yklibpy-0.2.0}/src/yklibpy/htmlparser/app.py +27 -40
- {yklibpy-0.1.11 → yklibpy-0.2.0}/src/yklibpy/htmlparser/htmlop.py +8 -14
- yklibpy-0.2.0/src/yklibpy/htmlparser/misc/__init__.py +6 -0
- yklibpy-0.2.0/src/yklibpy/htmlparser/misc/anchortaginfo.py +28 -0
- yklibpy-0.2.0/src/yklibpy/htmlparser/misc/anchortagx.py +26 -0
- {yklibpy-0.1.11 → yklibpy-0.2.0}/src/yklibpy/htmlparser/misc/priceinfo.py +4 -1
- yklibpy-0.2.0/src/yklibpy/htmlparser/misc/tagx.py +30 -0
- {yklibpy-0.1.11 → yklibpy-0.2.0}/src/yklibpy/htmlparser/preparex.py +17 -50
- yklibpy-0.2.0/src/yklibpy/htmlparser/scraper.py +111 -0
- yklibpy-0.2.0/src/yklibpy/tomlop/__init__.py +6 -0
- yklibpy-0.2.0/src/yklibpy/tomlop/fileitem.py +41 -0
- {yklibpy-0.1.11 → yklibpy-0.2.0}/src/yklibpy/tomlop/tomlop.py +10 -58
- {yklibpy-0.1.11 → yklibpy-0.2.0}/uv.lock +32 -10
- yklibpy-0.1.11/.python-version +0 -1
- yklibpy-0.1.11/src/yklibpy/__init__.py +0 -20
- yklibpy-0.1.11/src/yklibpy/common/__init__.py +0 -19
- yklibpy-0.1.11/src/yklibpy/common/convert_yaml_strip_tags.py +0 -114
- yklibpy-0.1.11/src/yklibpy/common/util_yaml.py +0 -32
- yklibpy-0.1.11/src/yklibpy/db/__init__.py +0 -25
- yklibpy-0.1.11/src/yklibpy/htmlparser/__init__.py +0 -40
- yklibpy-0.1.11/src/yklibpy/htmlparser/amazon.py +0 -3
- yklibpy-0.1.11/src/yklibpy/htmlparser/amazonsavedcartscraper.py +0 -111
- yklibpy-0.1.11/src/yklibpy/htmlparser/ascraper.py +0 -79
- yklibpy-0.1.11/src/yklibpy/htmlparser/fanzadoujinpurchasedscraper.py +0 -96
- yklibpy-0.1.11/src/yklibpy/htmlparser/fanzadoujinscraper.py +0 -251
- yklibpy-0.1.11/src/yklibpy/htmlparser/h3scraper.py +0 -77
- yklibpy-0.1.11/src/yklibpy/htmlparser/kuscraper.py +0 -67
- yklibpy-0.1.11/src/yklibpy/htmlparser/misc/__init__.py +0 -0
- yklibpy-0.1.11/src/yklibpy/htmlparser/misc/anchortaginfo.py +0 -13
- yklibpy-0.1.11/src/yklibpy/htmlparser/misc/anchortagx.py +0 -13
- yklibpy-0.1.11/src/yklibpy/htmlparser/misc/tagx.py +0 -26
- yklibpy-0.1.11/src/yklibpy/htmlparser/scraper.py +0 -181
- yklibpy-0.1.11/src/yklibpy/htmlparser/udemyscraper.py +0 -262
- yklibpy-0.1.11/src/yklibpy/tomlop/__init__.py +0 -19
- {yklibpy-0.1.11 → yklibpy-0.2.0}/.editorconfig +0 -0
- {yklibpy-0.1.11 → yklibpy-0.2.0}/.prettierrc +0 -0
- {yklibpy-0.1.11 → yklibpy-0.2.0}/README.md +0 -0
- {yklibpy-0.1.11 → yklibpy-0.2.0}/db.yaml +0 -0
- {yklibpy-0.1.11 → yklibpy-0.2.0}/src/yklibpy/common/info.py +0 -0
- {yklibpy-0.1.11 → yklibpy-0.2.0}/src/yklibpy/db/db_base.py +0 -0
- {yklibpy-0.1.11 → yklibpy-0.2.0}/src/yklibpy/htmlparser/configprepare.py +0 -0
- {yklibpy-0.1.11 → yklibpy-0.2.0}/src/yklibpy/htmlparser/progress.py +0 -0
- {yklibpy-0.1.11/src/yklibpy/htmlparser → yklibpy-0.2.0/src/yklibpy}/py.typed +0 -0
|
@@ -0,0 +1 @@
|
|
|
1
|
+
3.14.0
|
yklibpy-0.2.0/CLAUDE.md
ADDED
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
# CLAUDE.md
|
|
2
|
+
|
|
3
|
+
This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository.
|
|
4
|
+
|
|
5
|
+
## Project Overview
|
|
6
|
+
|
|
7
|
+
yklibpy is a Python utility library focused on HTML parsing/scraping and data format conversion (TOML/YAML). It targets Python 3.14+ and uses `uv` for dependency management with `hatchling` as the build backend.
|
|
8
|
+
|
|
9
|
+
## Development Commands
|
|
10
|
+
|
|
11
|
+
### Package Management
|
|
12
|
+
- Install dependencies: `uv sync`
|
|
13
|
+
- Add dependency: `uv add <package>`
|
|
14
|
+
- Add dev dependency: `uv add --dev <package>`
|
|
15
|
+
|
|
16
|
+
### Code Quality
|
|
17
|
+
- Format code: `black src/`
|
|
18
|
+
- Lint code: `ruff check src/`
|
|
19
|
+
- Type check: `mypy src/yklibpy`
|
|
20
|
+
- Configuration is split between `pyproject.toml` (strict mode, Python 3.14) and `mypy.ini` (module-specific settings)
|
|
21
|
+
- Type checking uses strict mode by default
|
|
22
|
+
|
|
23
|
+
### Building and Testing
|
|
24
|
+
- Build package: `uv build` or `hatch build`
|
|
25
|
+
- Run tests: `pytest` (test directory: `tests/`, configured in pyproject.toml)
|
|
26
|
+
- Run with coverage: `pytest --cov`
|
|
27
|
+
|
|
28
|
+
### Publishing
|
|
29
|
+
- Build distribution: `python -m build`
|
|
30
|
+
- Upload to PyPI: `twine upload dist/*`
|
|
31
|
+
|
|
32
|
+
## Architecture
|
|
33
|
+
|
|
34
|
+
### Module Structure
|
|
35
|
+
|
|
36
|
+
The library is organized into four main modules:
|
|
37
|
+
|
|
38
|
+
#### 1. `common/` - Core Utilities
|
|
39
|
+
- `Env`: Configuration and path management from YAML files, extracts scraper modes and base paths
|
|
40
|
+
- `Info`: Data container for parsed HTML information
|
|
41
|
+
- `Util`: General utilities including file operations and encoding detection
|
|
42
|
+
- `UtilYaml`: YAML operations with custom tag registration support
|
|
43
|
+
- `UtilJson`: JSON operations
|
|
44
|
+
- `SafeDict`: Safe dictionary wrapper with type checking
|
|
45
|
+
|
|
46
|
+
#### 2. `htmlparser/` - Web Scraping Framework
|
|
47
|
+
Base scraper pattern with site-specific implementations:
|
|
48
|
+
|
|
49
|
+
- **Base Class**: `Scraper` - provides core scraping logic with link extraction and deduplication
|
|
50
|
+
- **Concrete Scrapers**:
|
|
51
|
+
- `UdemyScraper`: Udemy course pages
|
|
52
|
+
- `KUScraper`: KU (Kindle Unlimited) content
|
|
53
|
+
- `AmazonSavedCartScraper`: Amazon saved cart items
|
|
54
|
+
- `FanzaDoujinPurchasedScraper`: Fanza purchased doujin content
|
|
55
|
+
- `FanzaDoujinBasketScraper`: Fanza basket/cart items
|
|
56
|
+
|
|
57
|
+
- **App**: Factory/orchestrator that creates appropriate scraper based on mode string
|
|
58
|
+
- Uses `Env` to load configuration and determine scraper mode
|
|
59
|
+
- Processes HTML files via BeautifulSoup (bs4 + html5lib)
|
|
60
|
+
- Maintains link collections with sequence tracking for deduplication
|
|
61
|
+
|
|
62
|
+
- **Supporting Classes**:
|
|
63
|
+
- `HtmlOp`: HTML operations and manipulation
|
|
64
|
+
- `Preparex`: Preparation/preprocessing logic
|
|
65
|
+
- `Progress`: Progress tracking
|
|
66
|
+
- `misc/`: Miscellaneous helpers like `AnchorTagInfo`
|
|
67
|
+
|
|
68
|
+
#### 3. `tomlop/` - TOML/YAML Conversion
|
|
69
|
+
- `Tomlop`: Bidirectional TOML ↔ YAML conversion with deep dictionary comparison
|
|
70
|
+
- `FileItem`: File metadata and operations wrapper
|
|
71
|
+
- Entry points: `toml2yaml`, `yaml2toml`, and various main functions
|
|
72
|
+
|
|
73
|
+
#### 4. `db/` - Database Abstraction
|
|
74
|
+
- `DbBase`: Base class for database operations
|
|
75
|
+
- `DbYaml`: YAML-backed key-value store with encoding detection
|
|
76
|
+
- Supports custom YAML tag constructors
|
|
77
|
+
- Automatic encoding detection via `chardet`
|
|
78
|
+
|
|
79
|
+
### Key Patterns
|
|
80
|
+
|
|
81
|
+
**Scraper Pattern**: All scrapers inherit from `Scraper` base class and implement site-specific parsing logic. The `App` class acts as a factory, creating the appropriate scraper based on a mode string from configuration.
|
|
82
|
+
|
|
83
|
+
**Configuration-Driven**: Uses YAML config files to specify base paths, patterns, and scraper modes. The `Env` class loads these and provides structured access.
|
|
84
|
+
|
|
85
|
+
**Link Deduplication**: Scrapers track links by ID with sequence numbers to avoid duplicates across multiple scrapes.
|
|
86
|
+
|
|
87
|
+
**Entry Points**: Multiple CLI entry points defined in pyproject.toml under `[project.scripts]` for different tools and scrapers.
|
|
88
|
+
|
|
89
|
+
## Important Notes
|
|
90
|
+
|
|
91
|
+
### Dependencies
|
|
92
|
+
- BeautifulSoup4 with html5lib parser (not lxml)
|
|
93
|
+
- PyYAML for configuration and data storage
|
|
94
|
+
- chardet for encoding detection
|
|
95
|
+
- TOML library for TOML operations
|
|
96
|
+
|
|
97
|
+
### Encoding
|
|
98
|
+
The codebase handles various encodings:
|
|
99
|
+
- Uses `chardet` to detect file encodings
|
|
100
|
+
- Falls back to default encoding when detection fails
|
|
101
|
+
- All file operations specify encoding explicitly
|
|
102
|
+
|
|
103
|
+
### Type Annotations
|
|
104
|
+
- Strict mypy checking enabled
|
|
105
|
+
- Uses modern Python 3.14+ type syntax (e.g., `dict[str, Any]` instead of `Dict`)
|
|
106
|
+
- Optional types use `| None` syntax instead of `Optional`
|
|
107
|
+
|
|
108
|
+
### YAML Custom Tags
|
|
109
|
+
The YAML utilities support custom tag registration for deserializing Python objects. Used in database operations for object persistence.
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: yklibpy
|
|
3
|
-
Version: 0.
|
|
3
|
+
Version: 0.2.0
|
|
4
4
|
Summary: Utilty Library by ykominami
|
|
5
5
|
Project-URL: Homepage, https://github.com/ykominami/yklibpy
|
|
6
6
|
Project-URL: Repository, https://github.com/ykominami/yklibpy
|
|
@@ -10,7 +10,7 @@ Classifier: Development Status :: 3 - Alpha
|
|
|
10
10
|
Classifier: Intended Audience :: Developers
|
|
11
11
|
Classifier: License :: OSI Approved :: MIT License
|
|
12
12
|
Classifier: Programming Language :: Python :: 3
|
|
13
|
-
Classifier: Programming Language :: Python :: 3.
|
|
13
|
+
Classifier: Programming Language :: Python :: 3.14
|
|
14
14
|
Classifier: Programming Language :: Python :: Implementation :: PyPy
|
|
15
15
|
Requires-Python: >=3.14
|
|
16
16
|
Requires-Dist: bs4>=0.0.2
|
|
@@ -18,3 +18,5 @@ Requires-Dist: chardet>=5.2.0
|
|
|
18
18
|
Requires-Dist: html5lib>=1.1
|
|
19
19
|
Requires-Dist: pyyaml>=6.0.3
|
|
20
20
|
Requires-Dist: toml>=0.10.2
|
|
21
|
+
Requires-Dist: types-pyyaml>=6.0.12.20250915
|
|
22
|
+
Requires-Dist: types-toml>=0.10.8.20240310
|
yklibpy-0.2.0/mypy.ini
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
[mypy]
|
|
2
|
+
mypy_path = src
|
|
3
|
+
namespace_packages = True
|
|
4
|
+
explicit_package_bases = True
|
|
5
|
+
check_untyped_defs = True
|
|
6
|
+
|
|
7
|
+
[mypy-yklibpy.htmlparser.scraper.*]
|
|
8
|
+
ignore_errors = False
|
|
9
|
+
[fanzadoujinbasketscraper.*]
|
|
10
|
+
ignore_errors = False
|
|
11
|
+
[util.*]
|
|
12
|
+
ignore_errors = False
|
|
13
|
+
[udemyscraper.*]
|
|
14
|
+
ignore_errors = False
|
|
15
|
+
[kuscraper.*]
|
|
16
|
+
ignore_errors = False
|
|
17
|
+
[amazon_saved_cart_scraper.*]
|
|
18
|
+
ignore_errors = False
|
|
19
|
+
[htmlop.*]
|
|
20
|
+
ignore_errors = False
|
|
21
|
+
[misc.*]
|
|
22
|
+
ignore_errors = False
|
|
23
|
+
[price_info.*]
|
|
24
|
+
ignore_errors = False
|
|
25
|
+
[tagx.*]
|
|
26
|
+
ignore_errors = False
|
|
27
|
+
[util_yaml.*]
|
|
28
|
+
ignore_errors = False
|
|
29
|
+
[work_info.*]
|
|
30
|
+
ignore_errors = False
|
|
31
|
+
[ymain.*]
|
|
32
|
+
ignore_errors = False
|
|
33
|
+
[zmain.*]
|
|
34
|
+
ignore_errors = False
|
|
35
|
+
[db_yaml.*]
|
|
36
|
+
ignore_errors = False
|
|
37
|
+
|
|
38
|
+
# [mypy-untyped_package.*]
|
|
39
|
+
# follow_untyped_imports = True
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
[project]
|
|
2
2
|
name = "yklibpy"
|
|
3
|
-
version = "0.
|
|
3
|
+
version = "0.2.0"
|
|
4
4
|
description = "Utilty Library by ykominami"
|
|
5
5
|
readme = "README.md"
|
|
6
6
|
authors = [
|
|
@@ -13,6 +13,8 @@ dependencies = [
|
|
|
13
13
|
"html5lib>=1.1",
|
|
14
14
|
"pyyaml>=6.0.3",
|
|
15
15
|
"toml>=0.10.2",
|
|
16
|
+
"types-pyyaml>=6.0.12.20250915",
|
|
17
|
+
"types-toml>=0.10.8.20240310",
|
|
16
18
|
]
|
|
17
19
|
license = "MIT" # ライセンス
|
|
18
20
|
# 分類の情報
|
|
@@ -21,7 +23,7 @@ classifiers = [
|
|
|
21
23
|
"Intended Audience :: Developers",
|
|
22
24
|
"License :: OSI Approved :: MIT License",
|
|
23
25
|
"Programming Language :: Python :: 3",
|
|
24
|
-
"Programming Language :: Python :: 3.
|
|
26
|
+
"Programming Language :: Python :: 3.14",
|
|
25
27
|
"Programming Language :: Python :: Implementation :: PyPy",
|
|
26
28
|
]
|
|
27
29
|
|
|
@@ -84,3 +86,8 @@ target-version = "py314"
|
|
|
84
86
|
[tool.mypy]
|
|
85
87
|
python_version = "3.14"
|
|
86
88
|
strict = true
|
|
89
|
+
mypy_path = ["src"]
|
|
90
|
+
|
|
91
|
+
# [[tool.mypy.overrides]]
|
|
92
|
+
# module = ["untyped_package.*"]
|
|
93
|
+
# follow_untyped_imports = true
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
from yklibpy.common.env import Env
|
|
2
|
+
from yklibpy.common.info import Info
|
|
3
|
+
from yklibpy.common.util import Util
|
|
4
|
+
from yklibpy.common.util_yaml import UtilYaml
|
|
5
|
+
from yklibpy.common.util_json import UtilJson
|
|
6
|
+
from yklibpy.common.safedict import SafeDict
|
|
7
|
+
|
|
8
|
+
__all__ = ["Env", "Info", "Util", "UtilYaml", "UtilJson", "SafeDict"]
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
def xmain() -> str:
|
|
12
|
+
print("Hello from yklibpy!")
|
|
13
|
+
return "Hello from yklibpy!"
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
def ymain() -> str:
|
|
17
|
+
print("Y Hello from yklibpy!")
|
|
18
|
+
return "Y Hello from yklibpy!"
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
if __name__ == "__main__":
|
|
22
|
+
xmain()
|
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
from pathlib import Path
|
|
2
|
-
from typing import List
|
|
2
|
+
from typing import Any, List
|
|
3
3
|
|
|
4
4
|
import yaml
|
|
5
5
|
|
|
6
6
|
|
|
7
7
|
class Env:
|
|
8
|
-
def __init__(self, config_path: Path = None):
|
|
8
|
+
def __init__(self, config_path: Path | None = None):
|
|
9
9
|
"""Load configuration and initialize base path/mode settings.
|
|
10
10
|
|
|
11
11
|
Args:
|
|
@@ -15,23 +15,18 @@ class Env:
|
|
|
15
15
|
Returns:
|
|
16
16
|
None
|
|
17
17
|
"""
|
|
18
|
-
self.
|
|
19
|
-
self.
|
|
20
|
-
self.
|
|
21
|
-
self.
|
|
22
|
-
|
|
18
|
+
self.sequence = -1
|
|
19
|
+
self.base_path: Path = Path(".")
|
|
20
|
+
self.pattern: str | None = None
|
|
21
|
+
self.config: dict[str, Any] = {}
|
|
22
|
+
self.assoc: dict[str, Any] = {}
|
|
23
23
|
if config_path is not None:
|
|
24
24
|
with open(config_path, "r", encoding="utf-8") as f:
|
|
25
25
|
self.assoc = yaml.load(f, Loader=yaml.FullLoader)
|
|
26
|
-
|
|
27
|
-
# list = self.assoc.keys()
|
|
28
|
-
# for key in list:
|
|
29
|
-
# print(f"key={key}")
|
|
30
|
-
|
|
31
|
-
base_path_array = self.assoc["base_path"]
|
|
26
|
+
base_path_array: list[str] = self.assoc["base_path"]
|
|
32
27
|
self.base_path = self.make_path(base_path_array)
|
|
33
28
|
|
|
34
|
-
def make_path(self, path_array: list[str]) -> Path
|
|
29
|
+
def make_path(self, path_array: list[str]) -> Path:
|
|
35
30
|
"""Convert a sequence of path components into a concrete path.
|
|
36
31
|
|
|
37
32
|
Args:
|
|
@@ -41,7 +36,7 @@ class Env:
|
|
|
41
36
|
Returns:
|
|
42
37
|
Path | None: Composed path or ``None`` when no components exist.
|
|
43
38
|
"""
|
|
44
|
-
base_path =
|
|
39
|
+
base_path = Path(".")
|
|
45
40
|
if path_array is not None:
|
|
46
41
|
top_dir = path_array.pop(0)
|
|
47
42
|
top_path = Path(top_dir)
|
|
@@ -92,10 +87,15 @@ class Env:
|
|
|
92
87
|
Returns:
|
|
93
88
|
List[Path]: Concrete file paths ready for scraping.
|
|
94
89
|
"""
|
|
90
|
+
print("env:get_files")
|
|
95
91
|
if len(self.config) == 0:
|
|
92
|
+
print("0 env:get_files")
|
|
93
|
+
self.sequence = -1
|
|
96
94
|
return []
|
|
97
95
|
else:
|
|
98
96
|
dir_path = self.base_path / Path(*self.config["dir"])
|
|
97
|
+
print(f"2 env:get_files dir_path={dir_path}")
|
|
98
|
+
self.sequence = int(dir_path.stem)
|
|
99
99
|
|
|
100
100
|
if self.config["kind"] == "file":
|
|
101
101
|
# 指定されたファイルのみを返す
|
|
@@ -5,12 +5,13 @@ import re
|
|
|
5
5
|
from io import StringIO
|
|
6
6
|
from pathlib import Path
|
|
7
7
|
from typing import List, Literal, Optional, Sequence
|
|
8
|
-
from urllib.parse import urlparse
|
|
8
|
+
from urllib.parse import ParseResult, urlparse
|
|
9
9
|
|
|
10
10
|
import chardet
|
|
11
|
-
import yaml
|
|
12
11
|
from bs4 import Tag
|
|
13
12
|
|
|
13
|
+
from .util_yaml import UtilYaml
|
|
14
|
+
|
|
14
15
|
TargetType = Literal["file", "dir", "both"]
|
|
15
16
|
|
|
16
17
|
"""
|
|
@@ -63,7 +64,9 @@ class Util:
|
|
|
63
64
|
return repr(self._list)
|
|
64
65
|
|
|
65
66
|
class Result:
|
|
66
|
-
def __init__(
|
|
67
|
+
def __init__(
|
|
68
|
+
self, success: bool, url: str, reason: str, parsed: ParseResult | None
|
|
69
|
+
):
|
|
67
70
|
self.success = success
|
|
68
71
|
self.url = url
|
|
69
72
|
self.reason = reason
|
|
@@ -79,7 +82,7 @@ class Util:
|
|
|
79
82
|
frame = inspect.currentframe()
|
|
80
83
|
if frame and frame.f_back:
|
|
81
84
|
caller = frame.f_back
|
|
82
|
-
filename = Path(caller.
|
|
85
|
+
filename = Path(caller.f_code.co_filename).name
|
|
83
86
|
lineno = caller.f_lineno
|
|
84
87
|
function = caller.f_code.co_name
|
|
85
88
|
return f"{filename}:{lineno} in {function}"
|
|
@@ -129,7 +132,7 @@ class Util:
|
|
|
129
132
|
return list
|
|
130
133
|
|
|
131
134
|
@classmethod
|
|
132
|
-
def is_valid_urls(cls, urls: [str]) ->
|
|
135
|
+
def is_valid_urls(cls, urls: List[str]) -> List["Util.Result"]:
|
|
133
136
|
result_array = []
|
|
134
137
|
for url in urls:
|
|
135
138
|
if url == "" or url is None:
|
|
@@ -155,12 +158,18 @@ class Util:
|
|
|
155
158
|
|
|
156
159
|
result = cls.Result(True, url, "URL is valid", parsed)
|
|
157
160
|
result_array.append(result)
|
|
161
|
+
|
|
158
162
|
return result_array
|
|
159
163
|
|
|
160
164
|
@classmethod
|
|
161
|
-
def extract_cid(cls, text: str) -> str
|
|
165
|
+
def extract_cid(cls, text: str) -> str:
|
|
162
166
|
m = re.search(r"cid=([^/?&]+)", text)
|
|
163
|
-
return m.group(1) if m else
|
|
167
|
+
return m.group(1) if m else ""
|
|
168
|
+
|
|
169
|
+
@classmethod
|
|
170
|
+
def extract_product_id(cls, text: str) -> str:
|
|
171
|
+
m = re.search(r"product_id=([^/?&]+)", text)
|
|
172
|
+
return m.group(1) if m else ""
|
|
164
173
|
|
|
165
174
|
@classmethod
|
|
166
175
|
def extract_base(cls, base: str, text: str) -> str | None:
|
|
@@ -178,7 +187,7 @@ class Util:
|
|
|
178
187
|
yield item
|
|
179
188
|
|
|
180
189
|
@classmethod
|
|
181
|
-
def ensure_file_path(cls, path: Path) -> Path:
|
|
190
|
+
def ensure_file_path(cls, path: Path | None) -> Path | None:
|
|
182
191
|
if path is None:
|
|
183
192
|
return None
|
|
184
193
|
if not path.exists():
|
|
@@ -189,7 +198,7 @@ class Util:
|
|
|
189
198
|
return path
|
|
190
199
|
|
|
191
200
|
@classmethod
|
|
192
|
-
def ensure_dir_path(cls, path: Path) -> Path:
|
|
201
|
+
def ensure_dir_path(cls, path: Path | None) -> Path | None:
|
|
193
202
|
if path is None:
|
|
194
203
|
return None
|
|
195
204
|
path.mkdir(parents=True, exist_ok=True)
|
|
@@ -215,7 +224,7 @@ class Util:
|
|
|
215
224
|
return flat_list
|
|
216
225
|
|
|
217
226
|
@classmethod
|
|
218
|
-
def detect_encoding(cls, input_path: Path) -> str:
|
|
227
|
+
def detect_encoding(cls, input_path: Path) -> Optional[str]:
|
|
219
228
|
if input_path is not None:
|
|
220
229
|
with open(input_path, "rb") as f:
|
|
221
230
|
raw = f.read()
|
|
@@ -278,45 +287,6 @@ class Util:
|
|
|
278
287
|
|
|
279
288
|
return common_parents
|
|
280
289
|
|
|
281
|
-
@classmethod
|
|
282
|
-
def load_yaml(cls, input_path: Path) -> dict:
|
|
283
|
-
"""Load a YAML file and return it as a dictionary.
|
|
284
|
-
|
|
285
|
-
Args:
|
|
286
|
-
input_path (Path): Path to the YAML file.
|
|
287
|
-
|
|
288
|
-
Returns:
|
|
289
|
-
dict: Parsed YAML content.
|
|
290
|
-
"""
|
|
291
|
-
data = {}
|
|
292
|
-
with open(input_path, "r", encoding="utf-8") as f:
|
|
293
|
-
data = yaml.load(f, Loader=yaml.FullLoader)
|
|
294
|
-
if data is None:
|
|
295
|
-
data = {}
|
|
296
|
-
return data
|
|
297
|
-
|
|
298
|
-
@classmethod
|
|
299
|
-
def save_yaml(cls, assoc: dict, output_path: Optional[Path] = None) -> str:
|
|
300
|
-
"""Serialize a dictionary to YAML and optionally save it to disk.
|
|
301
|
-
|
|
302
|
-
Args:
|
|
303
|
-
assoc (dict): Data to dump.
|
|
304
|
-
output_path (Path | None): Destination path. When ``None`` the YAML
|
|
305
|
-
string is merely returned.
|
|
306
|
-
|
|
307
|
-
Returns:
|
|
308
|
-
str: YAML representation of ``assoc``.
|
|
309
|
-
"""
|
|
310
|
-
yaml_str = yaml.dump(
|
|
311
|
-
assoc, default_flow_style=False, allow_unicode=True, sort_keys=False
|
|
312
|
-
)
|
|
313
|
-
|
|
314
|
-
if output_path is not None:
|
|
315
|
-
with open(output_path, "w", encoding="utf-8") as f:
|
|
316
|
-
f.write(yaml_str)
|
|
317
|
-
|
|
318
|
-
return yaml_str
|
|
319
|
-
|
|
320
290
|
@classmethod
|
|
321
291
|
def load_tsv(
|
|
322
292
|
cls, input_path: Path, fieldnames: Optional[Sequence[str]] = None
|
|
@@ -378,9 +348,9 @@ class Util:
|
|
|
378
348
|
)
|
|
379
349
|
|
|
380
350
|
if fieldnames is None:
|
|
381
|
-
headers = []
|
|
351
|
+
headers: list[str] = []
|
|
382
352
|
else:
|
|
383
|
-
headers = fieldnames
|
|
353
|
+
headers = list(fieldnames)
|
|
384
354
|
|
|
385
355
|
buffer = StringIO()
|
|
386
356
|
writer = csv.writer(buffer, delimiter="\t", lineterminator="\n")
|
|
@@ -408,21 +378,21 @@ class Util:
|
|
|
408
378
|
input_path = Path(input_file)
|
|
409
379
|
input_path_2 = Path(input_file_2)
|
|
410
380
|
output_path = Path(output_file)
|
|
411
|
-
dict =
|
|
381
|
+
dict = UtilYaml.load_yaml(input_path)
|
|
412
382
|
# print(dict)
|
|
413
383
|
|
|
414
|
-
dict_2 =
|
|
384
|
+
dict_2 = UtilYaml.load_yaml(input_path_2)
|
|
415
385
|
#
|
|
416
386
|
output_tsv_path = input_path_2.with_suffix(".tsv")
|
|
417
387
|
output_tsv_path_2 = output_path.with_suffix(".tsv")
|
|
418
388
|
# keys = dict.keys()
|
|
419
|
-
values = dict.values()
|
|
389
|
+
values = list(dict.values())
|
|
420
390
|
# print(values)
|
|
421
|
-
keys =
|
|
391
|
+
keys = values[0].keys()
|
|
422
392
|
Util.output_tsv(values, output_tsv_path, keys)
|
|
423
393
|
# exit()
|
|
424
|
-
values_2 = dict_2.values()
|
|
425
|
-
keys_2 =
|
|
394
|
+
values_2 = list(dict_2.values())
|
|
395
|
+
keys_2 = values_2[0].keys()
|
|
426
396
|
Util.output_tsv(values_2, output_tsv_path_2, keys_2)
|
|
427
397
|
|
|
428
398
|
for key_2, value_2 in dict_2.items():
|
|
@@ -434,7 +404,7 @@ class Util:
|
|
|
434
404
|
else:
|
|
435
405
|
value_2["Time"] = "0時間"
|
|
436
406
|
|
|
437
|
-
|
|
407
|
+
UtilYaml.save_yaml(dict_2, output_path)
|
|
438
408
|
|
|
439
409
|
def test_tsv(self, input_file: str, input_file_2: str, output_file: str):
|
|
440
410
|
"""Developer helper for merging Udemy TSV progress data.
|
|
@@ -443,19 +413,23 @@ class Util:
|
|
|
443
413
|
None
|
|
444
414
|
"""
|
|
445
415
|
input_path = Path(input_file)
|
|
446
|
-
|
|
416
|
+
# yaml_dict = UtilYaml.load_yaml(input_path)
|
|
447
417
|
|
|
448
418
|
# input_path = Path('output_2.tsv')
|
|
449
419
|
input_path_2 = Path(input_file_2)
|
|
450
420
|
output_path = Path(output_file)
|
|
451
|
-
|
|
421
|
+
records = Util.load_tsv(input_path)
|
|
452
422
|
dict_2 = Util.load_tsv(input_path_2)
|
|
453
423
|
for record_2 in dict_2:
|
|
454
424
|
record_2["Time"] = "0時間"
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
425
|
+
# Note: records is a list, not a dict, so this logic may need adjustment
|
|
426
|
+
# Keeping original logic structure but it may not work as intended
|
|
427
|
+
if isinstance(records, list):
|
|
428
|
+
for record in records:
|
|
429
|
+
if record.get("Course_ID") == record_2.get("Course_ID"):
|
|
430
|
+
if "Time" in record.keys():
|
|
431
|
+
record_2["Time"] = record["Time"]
|
|
432
|
+
break
|
|
459
433
|
else:
|
|
460
434
|
record_2["Time"] = "0時間"
|
|
461
435
|
Util.output_tsv(dict_2, output_path)
|
|
@@ -465,6 +439,6 @@ if __name__ == "__main__":
|
|
|
465
439
|
# test_util = Util()
|
|
466
440
|
# test_util.test_yaml(input_file="output_udemy_2.yaml", input_file_2="output_udemy_4.yaml", output_file="output_udemy_5.yaml")
|
|
467
441
|
# test_util.test_tsv(input_file="output_udemy_4.tsv", input_file_2="output_udemy_5.tsv", output_file="output_udemy_6.tsv")
|
|
468
|
-
|
|
469
|
-
ret = Util.extract_cid(
|
|
442
|
+
url_str = "https://www.dmm.co.jp/dc/doujin/-/detail/=/cid=d_573976/?dmmref=Basket&i3_ref=recommend&i3_ord=1"
|
|
443
|
+
ret = Util.extract_cid(url_str)
|
|
470
444
|
print(ret)
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import json
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
class UtilJson:
|
|
5
|
+
@classmethod
|
|
6
|
+
def load_file(cls, file_name: str):
|
|
7
|
+
data = None
|
|
8
|
+
with open(file_name, "r", encoding="utf-8") as f:
|
|
9
|
+
data = json.load(f)
|
|
10
|
+
return data
|
|
11
|
+
|
|
12
|
+
@classmethod
|
|
13
|
+
def load_string(cls, string: str):
|
|
14
|
+
data = None
|
|
15
|
+
data = json.loads(string)
|
|
16
|
+
return data
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
from pathlib import Path
|
|
2
|
+
from typing import Any, Optional
|
|
3
|
+
|
|
4
|
+
import yaml
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
class UtilYaml:
|
|
8
|
+
_constructors_registered = False
|
|
9
|
+
|
|
10
|
+
@classmethod
|
|
11
|
+
def ignore_python_object_tag(cls, loader, node):
|
|
12
|
+
"""カスタムタグを辞書として読み込む"""
|
|
13
|
+
if isinstance(node, yaml.MappingNode):
|
|
14
|
+
return loader.construct_mapping(node, deep=True)
|
|
15
|
+
elif isinstance(node, yaml.SequenceNode):
|
|
16
|
+
return loader.construct_sequence(node, deep=True)
|
|
17
|
+
else:
|
|
18
|
+
return loader.construct_scalar(node)
|
|
19
|
+
|
|
20
|
+
@classmethod
|
|
21
|
+
def _register_constructors(cls, tags: list[str]):
|
|
22
|
+
# print(f"1 _register_constructors: {tags}")
|
|
23
|
+
if not cls._constructors_registered:
|
|
24
|
+
# print(f"2 _register_constructors: {tags}")
|
|
25
|
+
tags.append("tag:yaml.org,2002:python/object")
|
|
26
|
+
# カスタムタグのコンストラクタを登録
|
|
27
|
+
for tag in tags:
|
|
28
|
+
yaml.add_constructor(
|
|
29
|
+
tag,
|
|
30
|
+
cls.ignore_python_object_tag,
|
|
31
|
+
yaml.SafeLoader,
|
|
32
|
+
)
|
|
33
|
+
# print(f'tag={tag}')
|
|
34
|
+
cls._constructors_registered = True
|
|
35
|
+
|
|
36
|
+
@classmethod
|
|
37
|
+
def safe_load(cls, f):
|
|
38
|
+
return yaml.load(f, Loader=yaml.SafeLoader)
|
|
39
|
+
|
|
40
|
+
@classmethod
|
|
41
|
+
def load_yaml(cls, input_path: Path) -> dict[str, Any]:
|
|
42
|
+
"""Load a YAML file and return it as a dictionary.
|
|
43
|
+
|
|
44
|
+
Args:
|
|
45
|
+
input_path (Path): Path to the YAML file.
|
|
46
|
+
|
|
47
|
+
Returns:
|
|
48
|
+
dict: Parsed YAML content.
|
|
49
|
+
"""
|
|
50
|
+
data = {}
|
|
51
|
+
print(f"input_path={input_path}")
|
|
52
|
+
with open(input_path, "r", encoding="utf-8") as f:
|
|
53
|
+
data = yaml.load(f, Loader=yaml.FullLoader)
|
|
54
|
+
if data is None:
|
|
55
|
+
data = {}
|
|
56
|
+
return data
|
|
57
|
+
|
|
58
|
+
@classmethod
|
|
59
|
+
def save_yaml(
|
|
60
|
+
cls, assoc: dict[Any, Any], output_path: Optional[Path] = None
|
|
61
|
+
) -> str:
|
|
62
|
+
"""Serialize a dictionary to YAML and optionally save it to disk.
|
|
63
|
+
|
|
64
|
+
Args:
|
|
65
|
+
assoc (dict): Data to dump.
|
|
66
|
+
output_path (Path | None): Destination path. When ``None`` the YAML
|
|
67
|
+
string is merely returned.
|
|
68
|
+
|
|
69
|
+
Returns:
|
|
70
|
+
str: YAML representation of ``assoc``.
|
|
71
|
+
"""
|
|
72
|
+
yaml_str = yaml.dump(
|
|
73
|
+
assoc, default_flow_style=False, allow_unicode=True, sort_keys=False
|
|
74
|
+
)
|
|
75
|
+
|
|
76
|
+
if output_path is not None:
|
|
77
|
+
with open(output_path, "w", encoding="utf-8") as f:
|
|
78
|
+
f.write(yaml_str)
|
|
79
|
+
|
|
80
|
+
return yaml_str
|