-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerate_html.py
More file actions
460 lines (373 loc) · 16.5 KB
/
generate_html.py
File metadata and controls
460 lines (373 loc) · 16.5 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
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
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
# AliExpress Order Tracker - HTML Generator
# Copyright (C) 2026 rx422 <ad.birnaz@gmail.com>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
"""
AliExpress Order Tracker - HTML Generator
Extracts orders from a saved AliExpress orders page and generates
a standalone HTML file with sorting, filtering, and state persistence.
"""
import re
import os
import json
import base64
import urllib.request
from pathlib import Path
from dataclasses import dataclass
from typing import List, Optional
# =============================================================================
# Configuration
# =============================================================================
BASE_DIR = Path(__file__).parent
TEMPLATES_DIR = BASE_DIR / 'templates'
ACTIVE_DIR = BASE_DIR / 'active'
ARCHIVE_DIR = BASE_DIR / 'archive'
# Output settings
OUTPUT_FILENAME = 'AliExpress_Orders.html'
# Exchange rate cache
EXCHANGE_RATE_CACHE = BASE_DIR / 'exchange_rate_cache.json'
DEFAULT_EXCHANGE_RATE = 0.92
# =============================================================================
# Data Models
# =============================================================================
@dataclass
class Order:
"""Represents an AliExpress order."""
order_number: str
status: str
delivery_info: str
delivery_date: str
description: str
is_delayed: bool
price: float
local_image: str
is_archived: bool = False
is_usd: bool = False
# =============================================================================
# Exchange Rate
# =============================================================================
def load_rate_cache() -> dict:
"""Load exchange rate cache from disk."""
try:
with open(EXCHANGE_RATE_CACHE, 'r') as f:
data = json.load(f)
# Migrate old format {"rate": X} to new format
if 'rate' in data and 'current' not in data:
return {'current': data['rate'], 'historical': {}}
return data
except:
return {'current': None, 'historical': {}}
def save_rate_cache(cache: dict):
"""Save exchange rate cache to disk."""
with open(EXCHANGE_RATE_CACHE, 'w') as f:
json.dump(cache, f)
def get_current_rate(cache: dict) -> float:
"""Fetch current USD to EUR exchange rate."""
try:
url = 'https://api.exchangerate-api.com/v4/latest/USD'
req = urllib.request.Request(url, headers={'User-Agent': 'Mozilla/5.0'})
with urllib.request.urlopen(req, timeout=5) as response:
data = json.loads(response.read().decode())
rate = data['rates']['EUR']
cache['current'] = rate
print(f"Current USD/EUR rate: {rate} (live)")
return rate
except Exception as e:
print(f"Could not fetch current rate: {e}")
if cache.get('current'):
print(f"Current USD/EUR rate: {cache['current']} (cached)")
return cache['current']
print(f"Current USD/EUR rate: {DEFAULT_EXCHANGE_RATE} (default)")
return DEFAULT_EXCHANGE_RATE
def fetch_historical_rates(dates: set, cache: dict) -> dict:
"""Fetch historical USD→EUR rates for given dates via frankfurter API."""
historical = cache.setdefault('historical', {})
missing = sorted(d for d in dates if d not in historical)
if not missing:
return historical
min_date, max_date = missing[0], missing[-1]
try:
url = f'https://api.frankfurter.app/{min_date}..{max_date}?from=USD&to=EUR'
req = urllib.request.Request(url, headers={'User-Agent': 'Mozilla/5.0'})
with urllib.request.urlopen(req, timeout=15) as response:
data = json.loads(response.read().decode())
for date_str, rates in data.get('rates', {}).items():
historical[date_str] = rates['EUR']
print(f"Fetched historical rates: {min_date} to {max_date} ({len(missing)} dates)")
except Exception as e:
print(f"Could not fetch historical rates: {e}")
return historical
def get_rate_for_date(date_str: str, historical: dict, current_rate: float) -> float:
"""Get USD→EUR rate for a specific date, finding nearest if exact not available."""
if not date_str:
return current_rate
if date_str in historical:
return historical[date_str]
# Find nearest available date (weekends/holidays fall back to closest business day)
available = sorted(historical.keys())
if not available:
return current_rate
# Binary search for nearest date
import bisect
idx = bisect.bisect_left(available, date_str)
if idx == 0:
return historical[available[0]]
if idx >= len(available):
return historical[available[-1]]
before, after = available[idx - 1], available[idx]
return historical[before] # Use the rate from before (conservative)
# =============================================================================
# Source File Discovery
# =============================================================================
def find_source_files(directory: Path):
"""Find the HTML file and corresponding _files folder in a directory."""
if not directory.exists():
return None, None
html_files = list(directory.glob('*.html'))
if not html_files:
return None, None
html_file = html_files[0]
# Browser saves companion folder as <name>_files
files_folder = directory / (html_file.stem + '_files')
if not files_folder.exists():
# Fallback: find any _files folder
files_folders = [d for d in directory.iterdir() if d.is_dir() and d.name.endswith('_files')]
files_folder = files_folders[0] if files_folders else None
return html_file, files_folder
# =============================================================================
# Order Parsing
# =============================================================================
def parse_price(price_str: str) -> tuple:
"""Parse price string, return (amount, is_usd)."""
is_usd = '$' in price_str
price_num = re.sub(r'[$€\s]', '', price_str).replace(',', '.')
try:
return float(price_num), is_usd
except:
return 0.0, False
def parse_delivery_date(delivery_info: str) -> str:
"""Extract delivery date from delivery info string."""
date_match = re.search(
r'(\d{1,2})\s+(January|February|March|April|May|June|July|August|September|October|November|December)(?:\s+(\d{4}))?',
delivery_info, re.IGNORECASE
)
if date_match:
day = int(date_match.group(1))
month_name = date_match.group(2).capitalize()
months = {
'January': 1, 'February': 2, 'March': 3, 'April': 4,
'May': 5, 'June': 6, 'July': 7, 'August': 8,
'September': 9, 'October': 10, 'November': 11, 'December': 12
}
month = months.get(month_name, 1)
year = int(date_match.group(3)) if date_match.group(3) else 2026
return f'{year}-{month:02d}-{day:02d}'
return ''
def parse_orders(html_content: str, is_archived: bool = False) -> List[Order]:
"""Parse orders from AliExpress HTML content."""
order_blocks = re.split(r'RedOrderList_OrderList__item__a2315', html_content)
orders = []
for block in order_blocks[1:]:
# Order number
order_match = re.search(
r'RedOrderList_OrderItem__number__1tjf5">(\d{2,4}\s\d{4}\s\d{4}\s\d{4})</div>',
block
)
if not order_match:
continue
order_number = order_match.group(1).strip()
# Status
status_match = re.search(r'RedOrderList_OrderItem__tag__1tjf5[^"]*">([^<]+)</div>', block)
status = status_match.group(1).strip() if status_match else "Unknown"
# Delivery info
delivery_match = re.search(r'RedOrderList_OrderItem__title__1tjf5">([^<]+)</h4>', block)
delivery_info = delivery_match.group(1).strip() if delivery_match else "N/A"
# Description
desc_match = re.search(r'RedOrderList_OrderItem__description__1tjf5[^"]*">([^<]+)</div>', block)
description = desc_match.group(1).strip() if desc_match else ""
# Skip cancelled/expired orders (check status, delivery info, and description)
combined = f'{status} {delivery_info} {description}'.upper()
if 'CANCELLED' in combined or 'CANCELED' in combined or 'TIME FOR PAYMENT' in combined:
continue
# Check if delayed
is_delayed = 'descriptionDangerous' in block and bool(description)
# Price
price_match = re.search(r'totalPrice__1tjf5">([^<]+)</div>', block)
price, is_usd = parse_price(price_match.group(1)) if price_match else (0.0, False)
# Image
image_match = re.search(r'src="[^"]*_files/([^"]+\.jpg)"', block)
local_image = image_match.group(1) if image_match else ""
# Delivery date
delivery_date = parse_delivery_date(delivery_info)
orders.append(Order(
order_number=order_number,
status=status,
delivery_info=delivery_info,
delivery_date=delivery_date,
description=description,
is_delayed=is_delayed,
price=price,
local_image=local_image,
is_archived=is_archived,
is_usd=is_usd,
))
return orders
# =============================================================================
# Image Processing
# =============================================================================
def get_image_base64(image_filename: str, images_folder: Path) -> Optional[str]:
"""Load image and convert to base64 data URL."""
if not image_filename:
return None
image_path = images_folder / image_filename
try:
with open(image_path, 'rb') as f:
img_data = base64.b64encode(f.read()).decode('utf-8')
return f'data:image/jpeg;base64,{img_data}'
except:
return None
def create_image_html(image_filename: str, images_folder: Path) -> str:
"""Create HTML for order image."""
base64_src = get_image_base64(image_filename, images_folder)
if base64_src:
return f'<img src="{base64_src}" alt="Product" class="product-image">'
return '<div class="product-image" style="background:#f0f0f0;display:flex;align-items:center;justify-content:center;color:#ccc;font-size:12px;">No image</div>'
# =============================================================================
# Template Rendering
# =============================================================================
def load_template(name: str) -> str:
"""Load template from templates directory."""
template_path = TEMPLATES_DIR / name
with open(template_path, 'r', encoding='utf-8') as f:
return f.read()
def render_template(template: str, **kwargs) -> str:
"""Simple template rendering with {{variable}} syntax."""
result = template
for key, value in kwargs.items():
result = result.replace('{{' + key + '}}', str(value))
return result
def get_status_class(status: str) -> str:
"""Determine CSS class for order status."""
status_upper = status.upper()
if 'READY' in status_upper or 'PICKUP' in status_upper:
return 'status-ready'
elif 'TRANSIT' in status_upper:
return 'status-transit'
return 'status-unknown'
def render_order_card(order: Order, images_folder: Optional[Path]) -> str:
"""Render a single order card."""
template = load_template('order_card.html')
order_id = order.order_number.replace(' ', '')
order_url = f"https://aliexpress.ru/order-list/{order_id}"
return render_template(
template,
price=order.price,
order_number=order.order_number,
delivery_date=order.delivery_date,
order_url=order_url,
img_html=create_image_html(order.local_image, images_folder) if images_folder else '',
status_class='status-ready' if order.is_archived else get_status_class(order.status),
status='Received' if order.is_archived else order.status,
delivery_info=order.delivery_info,
desc_class='delivery-desc delayed' if order.is_delayed else 'delivery-desc',
description=order.description,
price_formatted=f'{order.price:.2f}'.replace('.', ','),
card_extra_class=' archived' if order.is_archived else '',
checkbox_attrs=' checked disabled' if order.is_archived else '',
)
def generate_html(orders: List[Order], images_map: dict) -> str:
"""Generate complete HTML document."""
# Load templates
base_template = load_template('base.html')
styles = load_template('styles.css')
script = load_template('script.js')
# Render order cards
order_cards = '\n'.join(
render_order_card(order, images_map.get(order.order_number))
for order in orders
)
# Calculate totals
total_price = sum(o.price for o in orders)
total_formatted = f'{total_price:.2f}'.replace('.', ',')
# Render final HTML
return render_template(
base_template,
styles=styles,
script=script,
order_count=len(orders),
total_price=total_formatted,
order_cards=order_cards
)
# =============================================================================
# Main
# =============================================================================
def load_orders_from_dir(directory: Path, is_archived: bool = False):
"""Load and parse orders from a directory containing a saved AliExpress HTML."""
html_file, images_folder = find_source_files(directory)
if not html_file:
label = 'archive' if is_archived else 'active'
print(f"No HTML file found in {label}/ folder, skipping.")
return [], None
with open(html_file, 'r', encoding='utf-8') as f:
html_content = f.read()
orders = parse_orders(html_content, is_archived=is_archived)
label = 'archive' if is_archived else 'active'
print(f"Found {len(orders)} orders in {label}/ ({html_file.name})")
return orders, images_folder
def convert_usd_prices(orders: List[Order], current_rate: float, historical: dict):
"""Convert USD prices to EUR using date-appropriate exchange rates."""
for order in orders:
if not order.is_usd:
continue
rate = get_rate_for_date(order.delivery_date, historical, current_rate)
order.price = round(order.price * rate, 2)
def main():
"""Main entry point."""
# Load rate cache
cache = load_rate_cache()
current_rate = get_current_rate(cache)
# Load orders from active and archive folders
active_orders, active_images = load_orders_from_dir(ACTIVE_DIR)
archive_orders, archive_images = load_orders_from_dir(ARCHIVE_DIR, is_archived=True)
all_orders = active_orders + archive_orders
if not all_orders:
print("No orders found in active/ or archive/ folders.")
return
# Fetch historical rates for USD orders that have dates
usd_dates = {o.delivery_date for o in all_orders if o.is_usd and o.delivery_date}
historical = fetch_historical_rates(usd_dates, cache) if usd_dates else {}
# Convert USD prices using date-specific rates
convert_usd_prices(all_orders, current_rate, historical)
# Save updated cache
save_rate_cache(cache)
# Build images lookup: map each order to its images folder
images_map = {}
for order in active_orders:
images_map[order.order_number] = active_images
for order in archive_orders:
images_map[order.order_number] = archive_images
# Generate HTML
output_html = generate_html(all_orders, images_map)
# Save output
output_path = BASE_DIR / OUTPUT_FILENAME
with open(output_path, 'w', encoding='utf-8') as f:
f.write(output_html)
# Summary
total_price = sum(o.price for o in all_orders)
active_count = len(active_orders)
archive_count = len(archive_orders)
print(f"\nHTML file saved to: {output_path}")
print(f"{len(all_orders)} orders total ({active_count} active, {archive_count} archived), total: €{total_price:.2f}")
if __name__ == '__main__':
main()