Update nn
Add special parsing for Telegram Chat exports
This commit is contained in:
@@ -9,13 +9,19 @@ import sys
|
|||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
|
||||||
PATTERN = re.compile(
|
PHONE_PATTERN = re.compile(
|
||||||
r'^[A-Za-z]+[_-]'
|
r'^[A-Za-z]+[_-]'
|
||||||
r'(\d{4})-?(\d{2})-?(\d{2})'
|
r'(\d{4})-?(\d{2})-?(\d{2})'
|
||||||
r'[_-]'
|
r'[_-]'
|
||||||
r'(.+)$'
|
r'(.+)$'
|
||||||
)
|
)
|
||||||
|
|
||||||
|
# Telegram photo/video export, e.g. "photo_94@16-07-2026_22-09-43.jpg"
|
||||||
|
# (photo|video)_<n>@<DD>-<MM>-<YYYY>_<HH>-<MM>-<SS>
|
||||||
|
TELEGRAM_PATTERN = re.compile(
|
||||||
|
r'^(?:photo|video)_(\d+)@(\d{2})-(\d{2})-(\d{4})_(\d{2})-(\d{2})-(\d{2})$'
|
||||||
|
)
|
||||||
|
|
||||||
NORMALIZED = re.compile(r'^\d{4}-\d{2}-\d{2} .+')
|
NORMALIZED = re.compile(r'^\d{4}-\d{2}-\d{2} .+')
|
||||||
|
|
||||||
STATUS_RENAME = 'rename'
|
STATUS_RENAME = 'rename'
|
||||||
@@ -56,9 +62,14 @@ def plan_default_rename(filepath):
|
|||||||
if NORMALIZED.match(filepath.name):
|
if NORMALIZED.match(filepath.name):
|
||||||
return None, STATUS_ALREADY
|
return None, STATUS_ALREADY
|
||||||
|
|
||||||
match = PATTERN.match(stem)
|
tg_match = TELEGRAM_PATTERN.match(stem)
|
||||||
|
if tg_match:
|
||||||
|
number, day, month, year, hour, minute, second = tg_match.groups()
|
||||||
|
return f'{year}-{month}-{day} {hour}{minute}{second} {number}{ext}', STATUS_RENAME
|
||||||
|
|
||||||
|
match = PHONE_PATTERN.match(stem)
|
||||||
if not match:
|
if not match:
|
||||||
match = PATTERN.match(filepath.name)
|
match = PHONE_PATTERN.match(filepath.name)
|
||||||
if match:
|
if match:
|
||||||
year, month, day, rest = match.groups()
|
year, month, day, rest = match.groups()
|
||||||
new_name = f'{year}-{month}-{day} {rest}'
|
new_name = f'{year}-{month}-{day} {rest}'
|
||||||
@@ -118,10 +129,16 @@ def get_date_for_file(filepath, parse_mode):
|
|||||||
if norm_match:
|
if norm_match:
|
||||||
return f'{norm_match.group(1)}-{norm_match.group(2)}-{norm_match.group(3)}'
|
return f'{norm_match.group(1)}-{norm_match.group(2)}-{norm_match.group(3)}'
|
||||||
|
|
||||||
|
# Telegram export pattern (date is DD-MM-YYYY)
|
||||||
|
tg_match = TELEGRAM_PATTERN.match(stem)
|
||||||
|
if tg_match:
|
||||||
|
_, day, month, year = tg_match.groups()[:4]
|
||||||
|
return f'{year}-{month}-{day}'
|
||||||
|
|
||||||
# Try the standard pattern
|
# Try the standard pattern
|
||||||
match = PATTERN.match(stem)
|
match = PHONE_PATTERN.match(stem)
|
||||||
if not match:
|
if not match:
|
||||||
match = PATTERN.match(filepath.name)
|
match = PHONE_PATTERN.match(filepath.name)
|
||||||
if match:
|
if match:
|
||||||
year, month, day, _ = match.groups()
|
year, month, day, _ = match.groups()
|
||||||
return f'{year}-{month}-{day}'
|
return f'{year}-{month}-{day}'
|
||||||
|
|||||||
Reference in New Issue
Block a user