Spaces:
Running

File size: 11,161 Bytes
0437ed8
4bff127
 
0437ed8
 
 
 
 
 
 
 
bf33aaf
0437ed8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bf33aaf
0437ed8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4bff127
0437ed8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ba8c848
21a6739
ba8c848
21a6739
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ba8c848
21a6739
ba8c848
 
 
 
21a6739
ba8c848
 
 
 
 
 
21a6739
ba8c848
 
0437ed8
ba8c848
0437ed8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bf33aaf
0437ed8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ba8c848
 
0437ed8
 
 
 
 
 
 
ba8c848
0437ed8
 
 
 
 
4bff127
 
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

import gc

from pyrogram.errors import FileReferenceEmpty, FileReferenceExpired, FileReferenceInvalid, UsernameInvalid

from TG.storage import retry_on_flood, igrone_error, queue
from bot import Bot, Vars, logger


from Tools.img2cbz import images_to_cbz
from Tools.img2pdf import ( 
    download_and_convert_images, convert_images_to_pdf_async,
    ImageDownloadError
)

import os
import shutil
import asyncio

from time import time
from pyrogram.types import InputMediaDocument

from Tools.base import TaskCard, database
from .uploaders import get_target_auto_channel, Uploader


LOGS_MESSAGE = """
{caption}

{url}

`Downloaded By`: `{user_id}`  [{mention}]
`PDF Password`: `{password}`
`Time Taken`: `{time_taken}`"""




class NormalError(Exception):
  def __init__(self):
    pass


async def send_error(task_card: TaskCard, error_text):
  if task_card.sts:
    docs = await retry_on_flood(task_card.sts.edit)(
      f"{task_card.url} : `{error_text}`"
    )
  else:
    docs = await retry_on_flood(Bot.send_message)(
      int(task_card.user_id), f"{task_card.url} : `{error_text}`"
    )

  await retry_on_flood(docs.copy)(Vars.LOG_CHANNEL) if Vars.LOG_CHANNEL else None



def create_file_async(process):
  """Async wrapper for File creation"""
  async def wrapper(*args, **kwargs):
    return await asyncio.to_thread(process, *args, **kwargs)

  return wrapper



def clean_system(tasks_card: TaskCard, thumb: str| None, values: list):
    """Clean up files and directories based on task settings."""

    def safe_remove(path):
        """Safely remove file or directory."""
        if not path:
            return

        try:
            if os.path.isdir(path):
                shutil.rmtree(path, ignore_errors=True)
            elif os.path.exists(path):
                os.remove(path)
        except OSError:
            pass

    if tasks_card.setting.get("thumb") == "constant":
        safe_remove(thumb)

    if queue.check_queue(tasks_card.user_id) is False:
      process_dir = f"Process/{tasks_card.user_id}"
      shutil.rmtree(process_dir, ignore_errors=True)


    for value in values:
        safe_remove(value)

    gc.collect()


async def copy_media(channel_id, doc, caption):
  try:
    await retry_on_flood(Bot.copy_media_group)(
      channel_id, doc[-1].chat.id,
      doc[-1].id, captions=caption,
    )
  except Exception:

    try:
      await retry_on_flood(Bot.copy_message)(
        channel_id, doc[-1].chat.id,
        doc[-1].id, caption=caption
      )
    except UsernameInvalid:
      pass
    except Exception as e:
      logger.exception(f"Error copying media: {e}")



async def send_manga_chapter(
  tasks_card: TaskCard,
):
  #tasks_card.update_mode = True
  start_time = time()
  error_msg = None

  download_dir = None
  compressed_dir = None

  pdf_output_path = None
  cbz_output_path = None

  password = None
  banner_setting = await tasks_card.get_banner()
  thumb = banner_setting.get("thumb_file_name", None)
  banner1 = banner_setting.get("banner1_file_path", None)
  banner2 = banner_setting.get("banner2_file_path", None)

  media_docs = []

  if not getattr(tasks_card, "picturesList", None):
    # Deferred parallel scraping for bulk downloads
    try:
      semaphore = asyncio.Semaphore(5)
      async def scrape_chapter(chapter):
        async with semaphore:
          try:
            return await asyncio.wait_for(
                tasks_card.webs.get_pictures(chapter['url'], data=chapter),
                timeout=120
            )
          except Exception as e:
            logger.error(f"Chapter scrape failed: {chapter['url']} - {e}")
            return []

      scrape_tasks = [scrape_chapter(c) for c in tasks_card.data_list]
      results = await asyncio.gather(*scrape_tasks)
      
      picturesList = []
      for pics in results:
        if pics:
          picturesList.extend(pics)
      
      if not picturesList:
        raise Exception("No pictures found after parallel scraping")
        
      tasks_card.picturesList = picturesList
      # Start the download/convert process immediately since TaskCard.run_process() was 
      # skipped in queue.put() because picturesList was initially empty.
      tasks_card.run_process()
    except Exception as e:
      logger.error(f"Deferred parallel scraping failed: {e}")
      await igrone_error(send_error)(tasks_card, f"Scraping Error: {str(e)}")
      raise NormalError()

  # Fallback for single chapters or if run_process was not triggered yet
  if getattr(tasks_card, "processsing", None) is None:
    tasks_card.run_process()

  main_dir = tasks_card.main_dir
  download_dir = tasks_card.download_dir
  compressed_dir = tasks_card.compressed_dir

  main_dir = f"Process/{tasks_card.tasks_id}"
  download_dir = f"{main_dir}/pictures"
  compressed_dir = f"{main_dir}/compress"


  try:
    file_name = tasks_card.setting.get('file_name', None)
    if not file_name:
      file_name = "Chapter {episode_number} {manga_title}"

    if tasks_card.webs.sf == "mf" and "Vol" in tasks_card.manga_title or "Volume" in tasks_card.manga_title:
      file_name = file_name.replace("Chapter", "Vol")

    caption = tasks_card.setting.get('caption', "<blockquote>{file_name}</blockquote>")
    if not caption:
      caption = "<blockquote>{file_name}</blockquote>"

    replacements = {
      "{file_name}": file_name,
      "{episode_number}": str(tasks_card.episode_number),
      "{chapter_num}": str(tasks_card.episode_number),
      "{manga_title}": tasks_card.manga_title,
    }

    for key, value in replacements.items():
      file_name = file_name.replace(key, value)

    for key, value in replacements.items():
      caption = caption.replace(key, value)

    downloads_list = await tasks_card.processsing


    await igrone_error(tasks_card.sts.edit)("<code>Converting.....</code>") if tasks_card.sts else None

    file_type = tasks_card.setting.get('type', ['PDF'])
    if not file_type:
      file_type = ['PDF']


    processing_tasks = []
    if "PDF" in file_type:
      pdf_output_path = f"{main_dir}/{file_name}.pdf"

      password = tasks_card.setting.get('password', None)
      try:
        compress = int(tasks_card.setting.get("compress", "80"))
      except Exception:
        try:
          compress = int(await database.get_value(str(tasks_card.user_id), "compress"))
        except Exception:
          compress = 80

      if not compress:
        compress = 80

      hyperLink = tasks_card.setting.get("hyper", None)

      ## from here
      processing_tasks.append(convert_images_to_pdf_async(
        downloads_list, pdf_output_path,
        compressed_dir, password, compress, hyperLink,
        banner1, banner2
      ))


    if "CBZ" in file_type:
      cbz_output_path = f"{main_dir}/{file_name}.cbz"
      processing_tasks.append(create_file_async(images_to_cbz)(
        downloads_list, cbz_output_path
      ))


    processing_tasks = await asyncio.gather(*processing_tasks)
    for task in processing_tasks:
      if task is not None:
        await igrone_error(send_error)(tasks_card, str(task))
        raise NormalError()


    if pdf_output_path:
      media_docs.append(InputMediaDocument(pdf_output_path, caption=caption, thumb=thumb))

    if cbz_output_path:
      media_docs.append(InputMediaDocument(cbz_output_path, caption=caption, thumb=thumb))

    await igrone_error(tasks_card.sts.edit)("<code>Uploading.....</code>") if tasks_card.sts else None

    if len(media_docs) == 0:
      await igrone_error(send_error)(tasks_card, "<i> Not Any File Type Found </i>")
      raise NormalError()


    doc = await retry_on_flood(Bot.send_media_group)(int(tasks_card.chat_id), media_docs)

    await igrone_error(tasks_card.sts.edit)("<code>Uploading to targets.....</code>") if tasks_card.sts else None


    await Uploader().upload_to_targets_channels(
      doc[-1], original_ep_num=str(tasks_card.episode_number),
      search_name=tasks_card.orginal_manga_title, user_id=str(tasks_card.user_id),
    )

    dump = tasks_card.setting.get('dump', None)
    try:
      dump = int(dump)
    except Exception: 
      dump = dump

    if Vars.CONSTANT_DUMP_CHANNEL:
        await copy_media(Vars.CONSTANT_DUMP_CHANNEL, doc, caption)

    if dump:
        await copy_media(dump, doc, caption)

    if Vars.LOG_CHANNEL:
      time_taken = time() - start_time
      minutes, seconds = divmod(int(time_taken), 60)
      time_taken = f"{minutes}m, {seconds}s"

      user = await igrone_error(Bot.get_users)(int(tasks_card.user_id))

      log_caption = LOGS_MESSAGE.format(
        caption=caption,
        url=tasks_card.url,
        user_id=tasks_card.user_id,
        mention=user.mention() if user else "[None]",
        password=password,
        time_taken=time_taken
      )
      caption = ["", log_caption] if len(doc) > 1 else log_caption
      await copy_media(Vars.LOG_CHANNEL, doc, caption)

  except NormalError:
    error_msg = True
    pass

  except ImageDownloadError:
    error_msg = True
    await igrone_error(send_error)(tasks_card, f"Error at Downloading Picture at {tasks_card.url}")
    if tasks_card.sts:
      await retry_on_flood(tasks_card.sts.edit_text)(f"<code>Error at Downloading Picture</code> : {tasks_card.url} ")
    else:
      await retry_on_flood(Bot.send_message)(int(tasks_card.user_id), f"<code>Error at Downloading Picture</code> : {tasks_card.url} ")
  except (FileReferenceExpired, FileReferenceEmpty, FileReferenceInvalid):
    error_msg = True
    if tasks_card.sts:
      await retry_on_flood(tasks_card.sts.edit_text)("<b><i>Change Your Thumb or Banner and Try Again... </i></b> ")
    else:
      await retry_on_flood(Bot.send_message)(int(tasks_card.user_id), "<b><i>Change Your Thumb or Banner and Try Again... </i></b> ")

  except Exception as e:
    error_msg = True
    await igrone_error(send_error)(
      tasks_card, str(e)
    )

    logger.exception(f"Error processing task: {e}")

  finally:
    clean_system(
      tasks_card, thumb, 
      [

        pdf_output_path, cbz_output_path, 
        download_dir, compressed_dir, main_dir
      ]
    )

    if not error_msg:
      await igrone_error(tasks_card.sts.delete)() if tasks_card.sts else None



async def worker(worker_id: int = 1):
  while True:
    tasks_card, _ = await queue.get(worker_id)
    logger.info(f"Worker {worker_id} processing task {tasks_card.tasks_id}")
    await retry_on_flood(tasks_card.sts.edit)("<code>Processing.....</code>") if tasks_card.sts else None
    try:
      # We allow processing if picturesList is present OR if we have the data_list to scrape it later
      if tasks_card.picturesList or tasks_card.data_list:
        try: 
          await send_manga_chapter(tasks_card)
        except Exception as error:
          logger.exception(f"Worker {worker_id} encountered an error: {error}")
          await send_error(tasks_card, error)
      else:
        await igrone_error(send_error)(
          tasks_card, "Error: No chapters to process",
        )

    except Exception as err:
      logger.exception(f"Worker {worker_id} encountered an error: {err}")
    finally:
      await queue.task_done(tasks_card)
      gc.collect()