-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
148 lines (109 loc) · 4.12 KB
/
Copy pathutils.py
File metadata and controls
148 lines (109 loc) · 4.12 KB
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
"""
Utility functions for working with scraped data.
"""
import json
from pathlib import Path
from typing import Dict, List, Optional
import logging
import config
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
def load_metadata() -> Optional[Dict]:
"""Load metadata file."""
metadata_path = Path(config.METADATA_FILE)
if not metadata_path.exists():
logger.warning(f"Metadata file not found: {metadata_path}")
return None
with open(metadata_path, 'r', encoding='utf-8') as f:
return json.load(f)
def load_book(handle: str) -> Optional[Dict]:
"""Load a single book by handle."""
book_path = Path(config.BOOKS_DIR) / f"{handle}.json"
if not book_path.exists():
logger.warning(f"Book not found: {handle}")
return None
with open(book_path, 'r', encoding='utf-8') as f:
return json.load(f)
def load_all_books() -> List[Dict]:
"""Load all scraped books."""
books_dir = Path(config.BOOKS_DIR)
books = []
for book_file in books_dir.glob('*.json'):
try:
with open(book_file, 'r', encoding='utf-8') as f:
books.append(json.load(f))
except Exception as e:
logger.error(f"Error loading {book_file}: {e}")
return books
def search_books(query: str, field: str = 'title') -> List[Dict]:
"""Search books by field."""
books = load_all_books()
query_lower = query.lower()
results = [
book for book in books
if query_lower in str(book.get(field, '')).lower()
]
return results
def get_statistics() -> Dict:
"""Get statistics about scraped data."""
metadata = load_metadata()
books_dir = Path(config.BOOKS_DIR)
images_dir = Path(config.IMAGES_DIR)
stats = {
'total_books': len(list(books_dir.glob('*.json'))),
'total_images': len(list(images_dir.glob('*'))),
'data_directory': str(Path(config.DATA_DIR).absolute()),
}
if metadata:
stats['scraping_stats'] = metadata.get('statistics', {})
stats['scraped_at'] = metadata.get('scraped_at', 'Unknown')
return stats
def print_book_info(book: Dict):
"""Print formatted book information."""
print("\n" + "=" * 60)
print(f"Title: {book.get('title')}")
print(f"Publisher: {book.get('vendor')}")
print(f"Format: {book.get('product_type')}")
variants = book.get('variants', [])
if variants:
variant = variants[0]
print(f"ISBN: {variant.get('sku')}")
print(f"Price: {variant.get('price')} EUR")
if variant.get('compare_at_price'):
print(f"Original Price: {variant.get('compare_at_price')} EUR")
tags = book.get('tags', [])
if tags:
print(f"Tags: {', '.join(tags[:5])}" + ("..." if len(tags) > 5 else ""))
metadata = book.get('_metadata', {})
print(f"URL: {metadata.get('product_url', 'N/A')}")
print("=" * 60)
def main():
"""CLI for exploring scraped data."""
import sys
stats = get_statistics()
print("\n" + "=" * 60)
print("Kirja.fi Scraper - Data Explorer")
print("=" * 60)
print(f"Total Books: {stats['total_books']}")
print(f"Total Images: {stats['total_images']}")
print(f"Data Directory: {stats['data_directory']}")
if 'scraped_at' in stats:
print(f"Last Scraped: {stats['scraped_at']}")
if len(sys.argv) > 1:
# Search mode
query = ' '.join(sys.argv[1:])
print(f"\nSearching for: '{query}'")
results = search_books(query)
print(f"Found {len(results)} results:\n")
for book in results[:10]: # Show first 10
print(f"- {book.get('title')} ({book.get('vendor')})")
variants = book.get('variants', [])
if variants:
print(f" ISBN: {variants[0].get('sku')}")
if len(results) > 10:
print(f"\n... and {len(results) - 10} more results")
else:
print("\nUsage: python utils.py [search query]")
print("Example: python utils.py 'Harry Potter'")
if __name__ == "__main__":
main()