Spaces:
Running

File size: 25,551 Bytes
0437ed8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
db07660
0437ed8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
14c76d2
0437ed8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ba8c848
0437ed8
 
ba8c848
 
 
 
 
 
0437ed8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
14c76d2
0437ed8
7930088
0437ed8
 
 
 
14c76d2
db07660
0437ed8
 
 
 
 
 
 
 
 
 
 
 
7930088
0437ed8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
14c76d2
0437ed8
 
 
 
 
7930088
14c76d2
0437ed8
 
 
 
 
 
 
 
 
 
 
 
 
db07660
 
0437ed8
db07660
8fbd451
 
 
 
 
 
0437ed8
8fbd451
 
0437ed8
 
 
 
 
 
 
 
 
db07660
 
0437ed8
 
 
 
 
 
db07660
 
 
 
 
 
0437ed8
db07660
 
0437ed8
db07660
 
 
 
0437ed8
db07660
0437ed8
db07660
0437ed8
 
 
 
 
 
 
 
 
db07660
0437ed8
 
db07660
0437ed8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8fbd451
0437ed8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
import asyncio
import random
import shutil
import string
from typing import Dict, Optional, Tuple

from bot import logger, Vars
from pyrogram.errors import FloodWait
from .db import database, get_episode_number
from .img2pdf import thumbnali_images
from bot import Bot
from os import path as ospath
import pyrogram.errors
from io import BytesIO

import time

#from collections import OrderedDict
from threading import Lock, Timer

from Tools.img2pdf import download_and_convert_images


def igrone_error(func, sync=False):
    async def wrapper(*args, **kwargs):
        try:
            if sync:
                return await asyncio.to_thread(func, *args, **kwargs)
            else:
                tasks = await asyncio.gather(*[func(*args, **kwargs)])
                return tasks[0]
        except Exception:
            return None

    return wrapper

def retry_on_flood(func):
    async def wrapper(*args, **kwargs):
        while True:
            try:
                return await func(*args, **kwargs)
            except FloodWait as e:
                logger.warning(f'FloodWait: waiting {e.value}s')
                await asyncio.sleep(int(e.value) + 3)

            except (ValueError, pyrogram.errors.QueryIdInvalid, pyrogram.errors.MessageNotModified):
                raise


            except(pyrogram.errors.exceptions.bad_request_400.WebpageCurlFailed, pyrogram.errors.exceptions.bad_request_400.WebpageMediaEmpty, pyrogram.errors.exceptions.bad_request_400.PeerIdInvalid, pyrogram.errors.exceptions.bad_request_400.UsernameNotOccupied, pyrogram.errors.exceptions.bad_request_400.BadRequest, pyrogram.errors.exceptions.bad_request_400.MessageIdInvalid):
                raise

            except Exception as e:
                if "stat: path should be string, bytes, os.PathLike or integer, not NoneType" in str(e):
                    raise

                logger.exception(e)
                raise

    return wrapper



class MangaCard:
    __slots__ = ('url', 'title', 'poster', 'msg', 'chapters', 'webs', 'data')

    def __init__(
        self, 
        webs=None, url: str = "", 
        title: str = "", poster: str = "", 
        msg: str = "",  chapters: list = [], 
        data: dict = {}
    ):
        self.url = url
        self.title = title
        self.poster = poster
        self.msg = msg
        self.chapters = [] if chapters is None else chapters
        self.webs = webs
        self.data = {} if data is None else data

    def __repr__(self):
        return f"MangaCard({self.title})"

    def load_to_class(self, data: dict) -> 'MangaCard':
        slots = self.__slots__
        for key, val in data.items():
            if key in slots:
                setattr(self, key, val)
            else:
                self.data[key] = val
        return self


    def load_to_dict(self) -> dict:
        result = {}
        slots = self.__slots__

        for i in range(len(slots)):
            key = slots[i]
            if key == "data":
                result.update(self.data)
            else:
                result[key] = getattr(self, key)
        return result

    def update_dict(self, data: dict) -> None:
        slots = self.__slots__
        for key, val in data.items():
            if key in slots:
                setattr(self, key, val)
            else:
                self.data[key] = val

    @classmethod
    def from_dict(cls, data: dict) -> 'MangaCard':
        obj = cls.__new__(cls)

        obj.url = ""
        obj.title = ""
        obj.poster = ""
        obj.msg = ""
        obj.chapters = []
        obj.webs = None
        obj.data = {}

        obj.load_to_class(data)
        return obj


class Subscribes:
  """A class to manage user-specific subscriptions."""
  __slots__ = ('user_id', "manga_url", "webs", "lastest_chapter", "manga_title")

  def __init__(self, webs: str, manga_url: str, lastest_chapter: str, manga_title: str):
    self.manga_url: str = manga_url
    self.manga_title: str = manga_title
    self.webs: str = webs
    self.lastest_chapter: str = lastest_chapter

  def load_to_dict(self):
     return {"url": self.manga_url, "title": self.manga_title, "lastest_chapter": self.lastest_chapter}


def clean(txt, length=-1):
    remove_char = [
        "_", "&", ";", ":", "'", "|", "*", "?", ">", "<", "`", 
        "!", "@", "#", "$", "%", "^", "~", "+", "=", "/", 
        "\\", "\n",
    ]
    translator = txt.maketrans("", "", "".join(remove_char))
    txt = txt.translate(translator)

    txt = txt.replace("None", "")
    txt = txt.replace(".jpg", "")

    return txt[:length] if length != -1 else txt



def get_file_name(data: list, setting: dict = {}):
    regex = setting.get('regex', None)

    flen = setting.get('file_name_len', "30")
    try:
        flen = int(flen)
    except Exception:
        flen = 30

    if len(data) > 1:
        episode_number1 = str(get_episode_number(data[0].get("title", "None")))
        if not episode_number1 or (episode_number1 == "None"):
            episode_number1 = clean(data[0].get("title", "None"))
        else:
            episode_number1 = episode_number1.zfill(int(regex)) if regex else episode_number1

        episode_number2 = str(get_episode_number(data[-1].get("title", "None")))
        if not episode_number2 or (episode_number2 == "None"):
            episode_number2 = clean(data[-1].get("title", "None"))
        else:
            episode_number2 = episode_number2.zfill(int(regex)) if regex else episode_number2

        episode_number = f"{episode_number1} - {episode_number2}"
    else:
        episode_number = str(get_episode_number(data[0].get("title", "None")))
        if (episode_number == "None") or not episode_number:
            episode_number = clean(data[0].get("title", "None"))
        else:
            episode_number = episode_number.zfill(int(regex)) if regex else episode_number

    orginal_manga_title = data[0].get("manga_title", "")
    manga_title = clean(orginal_manga_title, flen)

    return orginal_manga_title, manga_title, episode_number


async def process_thumbnail_pdf(thumb_path):
    import os
    from PIL import Image, ImageOps

    if not thumb_path or not os.path.exists(thumb_path):
        return None

    try:
        img = await asyncio.to_thread(Image.open, thumb_path)
        img = await asyncio.to_thread(lambda: img.convert("RGB"))

        img = await asyncio.to_thread(lambda: ImageOps.contain(img, (320, 320), Image.LANCZOS))


        img = await asyncio.to_thread(lambda: ImageOps.pad(img, (320, 320), color=(0, 0, 0)))


        await asyncio.to_thread(img.save, thumb_path, "JPEG", quality=95)

    except Exception:
        pass

    return thumb_path

async def load_images_(u_id, p, b_url=None, p_fn=None):
    import base64
    import os

    s = {}
    us = await database.get_settings(u_id)

    md = f"Process/{u_id}"
    os.makedirs(md, exist_ok=True)

    prs = [
        ("banner1_file_path", us.get("banner1", None)), 
        ("banner2_file_path", us.get("banner2", None)), 
        ("thumb_file_name", us.get("thumb", None))
    ]

    def s64(b6, fp):
        try:
            if b6.startswith("data:"):
                b6 = b6.split(",", 1)[1]

            d = base64.b64decode(b6)
            with open(fp, "wb") as f:
                f.write(d)

            return fp
        except Exception:
            return None

    for k, v in prs:
        t = None
        fp = f"{md}/{k}.jpg"
        if ospath.exists(fp):
            t = fp
            s[k] = t

        elif v and v.startswith("http"):
            t = await igrone_error(thumbnali_images)(image_url=v, download_dir=md, file_name=k)
            t = await process_thumbnail_pdf(thumb_path=t)
            s[k] = t

        elif v and v.startswith("data:image"):
            t = s64(v, fp)
            s[k] = t

        elif v and (v.startswith("/9j") or v.startswith("iVBOR") or len(v) > 500):
            try:
                t = s64(v, fp)
                s[k] = t
            except Exception:
                t = await igrone_error(Bot.download_media)(v, file_name=fp)
                t = await process_thumbnail_pdf(thumb_path=t)
                s[k] = t

        elif v == "constant":
            t = await igrone_error(thumbnali_images)(image_url=p, download_dir=md, file_name=p_fn, base_url=b_url)
            t = await process_thumbnail_pdf(thumb_path=t)

            s[k] = t
        elif v:
            t = await igrone_error(Bot.download_media)(v, file_name=fp)
            t = await process_thumbnail_pdf(thumb_path=t)
            s[k] = t

    return s




class TaskCard:
    """ A class to manage user-specific tasks. """
    def __init__(
        self, webs, sts, picturesList, 
        user_id, chat_id, priority, 
        tasks_id=None, data_list: list = [],
        update_mode: bool = False,
        settings: dict = {}
    ):
        self.picturesList = picturesList or []
        self.data_list = data_list
        self.poster: str = data_list[0].get("poster", "") # data

        self.webs = webs
        self.sts = sts
        self.setting = settings
        self.orginal_manga_title, self.manga_title, self.episode_number = get_file_name(data_list, self.setting)

        if len(data_list) == 1:
            self.url = data_list[0].get("url", "") # data
        else:
            self.url = f"{data_list[0].get('url', '')} - {data_list[-1].get('url', '')}"

        self.tasks_id = tasks_id
        self.user_id = user_id
        self.chat_id = chat_id
        self.priority = priority
        self.update_mode = update_mode

    def run_process(self) -> None:
        if not self.tasks_id or not self.webs:
            return 

        if not self.picturesList:
            return

        if hasattr(self, "processsing") and self.processsing and not self.processsing.done():
            self.processsing.cancel()

        cs = getattr(self.webs, "cs", False)
        self.main_dir = f"Process/{self.tasks_id}"
        self.download_dir = f"{self.main_dir}/pictures"
        self.compressed_dir = f"{self.main_dir}/compress"

        self.processsing = asyncio.create_task(
            download_and_convert_images(
                self.picturesList, self.download_dir, 
                self.webs.url, cs=cs
            )
        )


    async def close(self) -> None:
        if self.processsing:
            self.processsing.cancel()

        shutil.rmtree(self.main_dir, ignore_errors=True)
        if self.sts:
            await igrone_error(self.sts.delete)()


    async def get_banner(self) -> dict:
        tasks = await asyncio.gather(
            load_images_(
                self.user_id, 
                self.poster, 
                self.webs.url, 
                self.manga_title
            )
        )
        return tasks[0]




class AQueue:
    __slots__ = ('storage_data', 'data_users', 'ongoing_tasks', 'maxsize', 'max_concurrent_users', '_lock')

    def __init__(self, maxsize: Optional[int] = None, max_concurrent_users: int = 0):
        self.storage_data: Dict[str, Tuple[TaskCard, bool]] = {}  # {task_id: TaskCard, True}
        #self.data_users: Dict[int, List[str]] = {}  # {user_id: [task_ids]}
        self.ongoing_tasks: Dict[str, TaskCard] = {} # {user_id: TaskCard}
        self.maxsize = maxsize
        self.max_concurrent_users = max_concurrent_users
        self._lock = asyncio.Lock()

    async def get_random_id(self) -> str:
        """Generate unique 7-char task ID"""
        chars = string.ascii_letters + string.digits + "s"
        while True:
            task_id = ''.join(random.choices(chars, k=7))
            if task_id not in self.storage_data and task_id not in self.ongoing_tasks:
                return task_id

    async def put(self, tasks: TaskCard, updates: bool = False) -> str:
        """Add task to queue"""

        # No queue size limit

        await database.ensure_user(tasks.user_id)
        tasks.tasks_id = await self.get_random_id()

        if not tasks.setting:
            tasks.setting = await database.get_settings(tasks.user_id)

        self.storage_data[tasks.tasks_id] = (tasks, updates)

        if Vars.IS_PRIVATE:
            tasks.run_process()

        return tasks.tasks_id

    def get_available_tasks(self, user_id=None):
        def extract_sort_value(item):
            if item is None:
                return (2, 0)  # Put None at end
            if isinstance(item, int):
                return (0, item)  # Type 0 for integers
            elif isinstance(item, str):
                s = str(item).lower()
                if s == 'none':
                    return (2, 0)  # Put 'None' at end
                elif '-' in s:
                    # For "10-11", take the first number
                    try:
                        num = int(s.split('-')[0])
                        return (1, num)  # Type 1 for hyphenated strings
                    except Exception:
                        return (3, s)  # Type 3 for other strings
                else:
                    # For other strings like "222", "333"
                    try:
                        num = int(s)
                        return (0, num)  # Same as integers
                    except Exception:
                        return (3, s)  # Can't convert, sort alphabetically
            else:
                return (3, str(item))  # Other types

        # Get all tasks
        all_tasks = []
        for task, is_completed in self.storage_data.values():
            if user_id is None or task.user_id == user_id:
                all_tasks.append((task, is_completed))

        # Filter out ongoing tasks (1 chapter at a time per user)
        available_tasks = [
            (task, is_completed) for task, is_completed in all_tasks
            if task.user_id not in self.ongoing_tasks
        ]

        # No concurrent users limit - process all available tasks

        # Sort by episode_number
        available_tasks.sort(key=lambda x: extract_sort_value(x[0].episode_number))

        return available_tasks[0] if available_tasks else None

    async def get(self, worker_id: int) -> Tuple[TaskCard, bool]:
        """Get next available task with better error handling"""
        while True:
            try:
                if not self.storage_data:
                    await asyncio.sleep(0.9)
                    continue

                async with self._lock:
                    available_task = self.get_available_tasks()

                    if not available_task:
                        # Release lock before sleeping
                        pass
                    else:
                        self.ongoing_tasks[available_task[0].user_id] = available_task[0]
                        self.storage_data.pop(str(available_task[0].tasks_id), None)
                        return available_task

                # Sleep outside the lock when no task was available
                await asyncio.sleep(1.0)

            except Exception as e:
                logger.exception(f"Error in queue get: {e}")
                await asyncio.sleep(1)


    async def delete_task(self, task_id: str) -> bool:
        """Delete specific task"""
        if task_id in self.storage_data:
            task_card, _ = self.storage_data.pop(task_id)
            await igrone_error(task_card.close)()
            return True

        return False

    async def delete_tasks(self, user_id: int) -> int:
        """Delete all tasks for user"""
        # Collect task IDs first to avoid modifying dict during iteration
        tasks_to_delete = [
            (task_id, task_tuple)
            for task_id, task_tuple in list(self.storage_data.items())
            if str(task_tuple[0].user_id) == str(user_id)
        ]

        for task_id, task_tuple in tasks_to_delete:
            self.storage_data.pop(task_id, None)

        # Close tasks after removal
        for task_id, task_tuple in tasks_to_delete:
            try:
                await asyncio.wait_for(igrone_error(task_tuple[0].close)(), 20)
            except Exception as e:
                logger.exception(f"Error deleting task {task_id}: {e}")

        return len(tasks_to_delete)

    def get_count(self, user_id: int = 0) -> int:
        """Get user's pending task count or total Users in queue"""
        try:
            if user_id == 0:
                # Count unique users with pending tasks
                unique_users = set()

                for task, is_completed in self.storage_data.values():
                    unique_users.add(int(task.user_id))

                for task in self.ongoing_tasks.values():
                    unique_users.add(int(task.user_id))

                return len(unique_users)
            else:
                user_id_str = str(user_id)  # Convert to string if stored as string

                ongoing_tasks = sum(1 for task in self.ongoing_tasks.values() if str(task.user_id) == user_id_str)
                total_tasks = ongoing_tasks + sum(1 for task, completed in self.storage_data.values() if str(task.user_id) == user_id_str)
                return total_tasks



        except (ValueError, TypeError, AttributeError) as e:
            logger.error(f"Error counting tasks for user {user_id}: {e}")
            return 0

    def task_exists(self, task_id: str) -> bool:
        """Check if task exists"""
        return task_id in self.storage_data or task_id in self.ongoing_tasks

    def qsize(self) -> int:
        return len(self.storage_data) + len(self.ongoing_tasks)

    def empty(self) -> bool:
        return not self.storage_data

    async def task_done(self, tasks_card: TaskCard) -> bool:
        """Mark task as done"""
        if tasks_card.user_id in self.ongoing_tasks:
            del self.ongoing_tasks[tasks_card.user_id]
            if tasks_card.tasks_id in self.storage_data:
                 self.storage_data.pop(str(tasks_card.tasks_id))
            return True

        return False

    def get_ongoing_count(self, user_id: int) -> int:
        """Get user's ongoing task count"""
        return sum(1 for t in self.ongoing_tasks.values() if int(t.user_id) == user_id)


    def check_queue(self, user_id) -> bool:
        return any(int(task[0].user_id) == user_id for task in list(self.storage_data.values()))


queue = AQueue()


""" Make By AI """
class TTLCache:
    """
    A dictionary-like class that automatically removes entries after a specified time.

    Features:
    - Behaves like a regular dictionary (supports get, set, delete, membership tests)
    - Each entry has its own expiration time
    - Automatic cleanup of expired entries
    - Thread-safe operations
    """

    def __init__(self, default_timeout=60, cleanup_interval=30):
        """
        Initialize the TimedDict.

        Args:
            default_timeout (int): Default time in seconds after which entries expire
            cleanup_interval (int): How often to run cleanup in seconds (0 to disable)
        """
        self._dict = {}  # Main storage: key -> (value, expiry_time)
        self._default_timeout = default_timeout
        self._lock = Lock()

        # Start periodic cleanup if interval > 0
        self._cleanup_interval = cleanup_interval
        if cleanup_interval > 0:
            self._start_cleanup_timer()

    def _start_cleanup_timer(self):
        """Start the periodic cleanup timer."""
        self._cleanup_timer = Timer(self._cleanup_interval, self._cleanup)
        self._cleanup_timer.daemon = True
        self._cleanup_timer.start()

    def _cleanup(self):
        """Remove all expired entries."""
        current_time = time.time()
        with self._lock:
            # Collect keys to delete
            keys_to_delete = [
                key for key, (_, expiry) in self._dict.items()
                if expiry <= current_time
            ]
            # Delete expired keys
            for key in keys_to_delete:
                del self._dict[key]

        # Restart timer if still needed
        if self._cleanup_interval > 0:
            self._start_cleanup_timer()

    def __getitem__(self, key):
        """Get value for key, raising KeyError if expired or not found."""
        with self._lock:
            if key not in self._dict:
                raise KeyError(key)

            value, expiry = self._dict[key]
            if time.time() > expiry:
                # Entry has expired
                del self._dict[key]
                raise KeyError(f"Key '{key}' has expired")

            return value

    def __setitem__(self, key, value):
        """Set value with default timeout."""
        self.set(key, value, self._default_timeout)

    def set(self, key, value, timeout=None):
        """
        Set a key with optional custom timeout.

        Args:
            key: The key to set
            value: The value to store
            timeout: Custom timeout in seconds (uses default_timeout if None)
        """
        expiry = time.time() + (timeout if timeout is not None else self._default_timeout)
        with self._lock:
            self._dict[key] = (value, expiry)

    def __delitem__(self, key):
        """Delete a key."""
        with self._lock:
            del self._dict[key]

    def __contains__(self, key):
        """Check if key exists and is not expired."""
        try:
            self.__getitem__(key)
            return True
        except KeyError:
            return False

    def get(self, key, default=None):
        """Get value with default if key doesn't exist or is expired."""
        try:
            return self.__getitem__(key)
        except KeyError:
            return default

    def pop(self, key, default=None):
        """
        Remove and return value for key.
        Returns default if key doesn't exist or is expired.
        """
        try:
            value = self.__getitem__(key)
            with self._lock:
                del self._dict[key]
            return value
        except KeyError:
            return default

    def keys(self):
        """Return list of non-expired keys."""
        current_time = time.time()
        with self._lock:
            # First clean up expired entries
            keys_to_delete = [
                key for key, (_, expiry) in self._dict.items()
                if expiry <= current_time
            ]
            for key in keys_to_delete:
                del self._dict[key]

            return list(self._dict.keys())

    def values(self):
        """Return list of non-expired values."""
        current_time = time.time()
        with self._lock:
            values = []
            keys_to_delete = []

            for key, (value, expiry) in self._dict.items():
                if expiry > current_time:
                    values.append(value)
                else:
                    keys_to_delete.append(key)

            # Clean up expired entries
            for key in keys_to_delete:
                del self._dict[key]

            return values

    def items(self):
        """Return list of (key, value) pairs for non-expired entries."""
        current_time = time.time()
        with self._lock:
            items = []
            keys_to_delete = []

            for key, (value, expiry) in self._dict.items():
                if expiry > current_time:
                    items.append((key, value))
                else:
                    keys_to_delete.append(key)

            # Clean up expired entries
            for key in keys_to_delete:
                del self._dict[key]

            return items

    def __len__(self):
        """Return number of non-expired entries."""
        return len(self.keys())

    def __iter__(self):
        """Iterate over non-expired keys."""
        return iter(self.keys())

    def clear(self):
        """Remove all entries."""
        with self._lock:
            self._dict.clear()

    def update(self, other_dict):
        """Update with another dictionary, using default timeout."""
        with self._lock:
            for key, value in other_dict.items():
                self[key] = value

    def setdefault(self, key, default=None):
        """
        Get value if key exists, otherwise set to default and return default.
        """
        try:
            return self.__getitem__(key)
        except KeyError:
            self[key] = default
            return default

    def get_expiry_time(self, key):
        """
        Get expiry time for a key as timestamp.
        Returns None if key doesn't exist.
        """
        with self._lock:
            if key in self._dict:
                return self._dict[key][1]
            return None

    def refresh_key(self, key, timeout=None):
        """
        Reset the expiry time for an existing key.
        Returns True if successful, False if key doesn't exist.
        """
        with self._lock:
            if key in self._dict:
                value, _ = self._dict[key]
                expiry = time.time() + (timeout if timeout is not None else self._default_timeout)
                self._dict[key] = (value, expiry)
                return True
            return False

    def __repr__(self):
        """String representation."""
        items = self.items()
        return f"TimedDict({dict(items)})"

    def __enter__(self):
        """Context manager entry."""
        return self

    def __exit__(self, exc_type, exc_val, exc_tb):
        """Context manager exit - clean up."""
        self.clear()
        if hasattr(self, '_cleanup_timer'):
            self._cleanup_timer.cancel()