Exemplo n.º 1
0
def banchecker(user_id):
    try:
        casurl = "https://api.cas.chat/check?user_id={}".format(user_id)
        data = get(casurl).json()
    except Exception as e:
        LOGS.info(e)
        data = None
    return bool(data and data["ok"])
Exemplo n.º 2
0
def extract_w_h(file):
    """ Get width and height of media """
    command_to_run = [
        "ffprobe",
        "-v",
        "quiet",
        "-print_format",
        "json",
        "-show_format",
        "-show_streams",
        file,
    ]
    # https://stackoverflow.com/a/11236144/4723940
    try:
        t_response = subprocess.check_output(command_to_run,
                                             stderr=subprocess.STDOUT)
    except subprocess.CalledProcessError as exc:
        LOGS.warning(exc)
    else:
        x_reponse = t_response.decode("UTF-8")
        response_json = json.loads(x_reponse)
        width = int(response_json["streams"][0]["width"])
        height = int(response_json["streams"][0]["height"])
        return width, height
Exemplo n.º 3
0
 async def anti_spambot(event):
     if not event.user_joined and not event.user_added:
         return
     chat = event.chat_id
     user = await event.get_user()
     freakadmin = await is_admin(bot, chat, bot.uid)
     if not freakadmin:
         return
     freakbanned = None
     adder = None
     ignore = None
     if event.user_added:
         try:
             adder = event.action_message.sender_id
         except AttributeError:
             return
     async for admin in event.client.iter_participants(
             event.chat_id, filter=ChannelParticipantsAdmins):
         if admin.id == adder:
             ignore = True
             break
     if ignore:
         return
     if is_gbanned(user.id):
         freakgban = get_gbanuser(user.id)
         if freakgban.reason:
             hmm = await event.reply(
                 f"[{user.first_name}](tg://user?id={user.id}) was gbanned by you for the reason `{freakgban.reason}`"
             )
         else:
             hmm = await event.reply(
                 f"[{user.first_name}](tg://user?id={user.id}) was gbanned by you"
             )
         try:
             await bot.edit_permissions(chat, user.id, view_messages=False)
             freakbanned = True
         except Exception as e:
             LOGS.info(e)
     if spamwatch and not freakbanned:
         ban = spamwatch.get_ban(user.id)
         if ban:
             hmm = await event.reply(
                 f"[{user.first_name}](tg://user?id={user.id}) was banned by spamwatch for the reason `{ban.reason}`"
             )
             try:
                 await bot.edit_permissions(chat,
                                            user.id,
                                            view_messages=False)
                 freakbanned = True
             except Exception as e:
                 LOGS.info(e)
     if not freakbanned:
         try:
             casurl = "https://api.cas.chat/check?user_id={}".format(
                 user.id)
             data = get(casurl).json()
         except Exception as e:
             LOGS.info(e)
             data = None
         if data and data["ok"]:
             reason = (
                 f"[Banned by Combot Anti Spam](https://cas.chat/query?u={user.id})"
             )
             hmm = await event.reply(
                 f"[{user.first_name}](tg://user?id={user.id}) was banned by Combat anti-spam service(CAS) for the reason check {reason}"
             )
             try:
                 await bot.edit_permissions(chat,
                                            user.id,
                                            view_messages=False)
                 freakbanned = True
             except Exception as e:
                 LOGS.info(e)
     if BOTLOG and freakbanned:
         await event.client.send_message(
             BOTLOG_CHATID,
             "#ANTISPAMBOT\n"
             f"**User :** [{user.first_name}](tg://user?id={user.id})\n"
             f"**Chat :** {event.chat.title} (`{event.chat_id}`)\n"
             f"**Reason :** {hmm.text}",
         )
Exemplo n.º 4
0
async def mega_downloader(megadl):
    await megadl.edit("`Collecting information...`")
    if not os.path.isdir(TEMP_DOWNLOAD_DIRECTORY):
        os.makedirs(TEMP_DOWNLOAD_DIRECTORY)
    msg_link = await megadl.get_reply_message()
    link = megadl.pattern_match.group(1)
    if link:
        pass
    elif msg_link:
        link = msg_link.text
    else:
        return await megadl.edit("Usage: `.megadl` **<MEGA.nz link>**")
    try:
        link = re.findall(r"\bhttps?://.*mega.*\.nz\S+", link)[0]
        """ - Mega changed their URL again - """
        if "file" in link:
            link = link.replace("#", "!").replace("file/", "#!")
        elif "folder" in link or "#F" in link or "#N" in link:
            await megadl.edit("`folder download support are removed...`")
            return
    except IndexError:
        await megadl.edit("`MEGA.nz link not found...`")
        return None
    cmd = f"bin/megadown -q -m {link}"
    result = await subprocess_run(megadl, cmd)
    try:
        data = json.loads(result[0])
    except json.JSONDecodeError:
        await megadl.edit("**JSONDecodeError**: `failed to extract link...`")
        return None
    except (IndexError, TypeError):
        return
    file_name = data["file_name"]
    file_url = data["url"]
    hex_key = data["hex_key"]
    hex_raw_key = data["hex_raw_key"]
    temp_file_name = file_name + ".temp"
    temp_file_path = TEMP_DOWNLOAD_DIRECTORY + temp_file_name
    file_path = TEMP_DOWNLOAD_DIRECTORY + file_name
    if os.path.isfile(file_path):
        try:
            raise FileExistsError(errno.EEXIST, os.strerror(errno.EEXIST),
                                  file_path)
        except FileExistsError as e:
            await megadl.edit(f"`{str(e)}`")
            return None
    downloader = SmartDL(file_url, temp_file_path, progress_bar=False)
    display_message = None
    try:
        downloader.start(blocking=False)
    except HTTPError as e:
        await megadl.edit(f"**HTTPError**: `{str(e)}`")
        return None
    start = time.time()
    while not downloader.isFinished():
        status = downloader.get_status().capitalize()
        total_length = downloader.filesize if downloader.filesize else None
        downloaded = downloader.get_dl_size()
        percentage = int(downloader.get_progress() * 100)
        speed = downloader.get_speed(human=True)
        estimated_total_time = round(downloader.get_eta())
        progress_str = "`{0}` | [{1}{2}] `{3}%`".format(
            status,
            "".join(["█" for i in range(math.floor(percentage / 10))]),
            "".join(["░" for i in range(10 - math.floor(percentage / 10))]),
            round(percentage, 2),
        )
        diff = time.time() - start
        try:
            current_message = (
                f"`{file_name}`\n"
                f"{progress_str}\n"
                f"`Size:` {humanbytes(downloaded)} of {humanbytes(total_length)}\n"
                f"`Speed:` {speed}\n"
                f"`ETA:` {time_formatter(estimated_total_time)}\n"
                f"`Duration:` {time_formatter(round(diff))}")
            if round(diff % 15.00) == 0 and (display_message != current_message
                                             or total_length == downloaded):
                await megadl.edit(current_message)
                await asyncio.sleep(0.2)
                display_message = current_message
        except Exception:
            pass
        finally:
            if status == "Combining":
                wait = round(downloader.get_eta())
                await asyncio.sleep(wait)
    if downloader.isSuccessful():
        download_time = round(downloader.get_dl_time() + wait)
        try:
            P = multiprocessing.Process(
                target=await decrypt_file(megadl, file_path, temp_file_path,
                                          hex_key, hex_raw_key),
                name="Decrypt_File",
            )
            P.start()
            P.join()
        except FileNotFoundError as e:
            await megadl.edit(f"`{str(e)}`")
            return None
        else:
            await megadl.edit(
                f"`{file_name}`\n\n"
                f"Successfully downloaded in: '`{file_path}`'.\n"
                f"Download took: {time_formatter(download_time)}.")
            return None
    else:
        await megadl.edit("`Failed to download, "
                          "check heroku Logs for more details.`")
        for e in downloader.get_errors():
            LOGS.info(str(e))
    return
Exemplo n.º 5
0
async def download(target_file):
    """ For .dl command, download files to the FreakyUserbot's server. """
    Freaky = await edit_or_reply(target_file, "`Processing ...`")
    await Freaky.edit("Processing using FreakyUserbot server ( ◜‿◝ )♡")
    input_str = target_file.pattern_match.group(1)
    if not os.path.isdir(TEMP_DOWNLOAD_DIRECTORY):
        os.makedirs(TEMP_DOWNLOAD_DIRECTORY)
    if "|" in input_str:
        url, file_name = input_str.split("|")
        url = url.strip()
        # https://stackoverflow.com/a/761825/4723940
        file_name = file_name.strip()
        head, tail = os.path.split(file_name)
        if head:
            if not os.path.isdir(os.path.join(TEMP_DOWNLOAD_DIRECTORY, head)):
                os.makedirs(os.path.join(TEMP_DOWNLOAD_DIRECTORY, head))
                file_name = os.path.join(head, tail)
        downloaded_file_name = TEMP_DOWNLOAD_DIRECTORY + "" + file_name
        downloader = SmartDL(url, downloaded_file_name, progress_bar=False)
        downloader.start(blocking=False)
        c_time = time.time()
        display_message = None
        while not downloader.isFinished():
            status = downloader.get_status().capitalize()
            total_length = downloader.filesize if downloader.filesize else None
            downloaded = downloader.get_dl_size()
            now = time.time()
            diff = now - c_time
            percentage = downloader.get_progress() * 100
            downloader.get_speed()
            round(diff) * 1000
            progress_str = "[{0}{1}] {2}%".format(
                "".join(["▰" for i in range(math.floor(percentage / 10))]),
                "".join(["▱"
                         for i in range(10 - math.floor(percentage / 10))]),
                round(percentage, 2),
            )
            estimated_total_time = downloader.get_eta(human=True)
            try:
                current_message = f"{status}..\
                \nFOR : F.R.I.D.A.Y™ AND INDIANBOT™\
                \nURL: {url}\
                \nFile Name: {file_name}\
                \n{progress_str}\
                \n{humanbytes(downloaded)} of {humanbytes(total_length)}\
                \nETA: {estimated_total_time}"

                if round(diff %
                         10.00) == 0 and current_message != display_message:
                    await Freaky.edit(current_message)
                    display_message = current_message
            except Exception as e:
                LOGS.info(str(e))
        if downloader.isSuccessful():
            await Freaky.edit("Downloaded to `{}` successfully !!".format(
                downloaded_file_name))
        else:
            await Freaky.edit("Incorrect URL\n{}".format(url))
    elif target_file.reply_to_msg_id:
        try:
            c_time = time.time()
            downloaded_file_name = await target_file.client.download_media(
                await target_file.get_reply_message(),
                TEMP_DOWNLOAD_DIRECTORY,
                progress_callback=lambda d, t: asyncio.get_event_loop().
                create_task(
                    progress(d, t, target_file, c_time, "Downloading...")),
            )
        except Exception as e:  # pylint:disable=C0103,W0703
            await Freaky.edit(str(e))
        else:
            await Freaky.edit("Downloaded to `{}` successfully !!".format(
                downloaded_file_name))
    else:
        await Freaky.edit("Reply to a message to download to my local server.")
Exemplo n.º 6
0
async def uploadir(udir_event):
    """ For .uploadir command, allows you to upload everything from a folder in the server"""
    input_str = udir_event.pattern_match.group(1)
    if os.path.exists(input_str):
        await udir_event.edit("Downloading Using Userbot Server....")
        lst_of_files = []
        for r, d, f in os.walk(input_str):
            for file in f:
                lst_of_files.append(os.path.join(r, file))
            for file in d:
                lst_of_files.append(os.path.join(r, file))
        LOGS.info(lst_of_files)
        uploaded = 0
        await udir_event.edit(
            "Found {} files. Uploading will start soon. Please wait!".format(
                len(lst_of_files)))
        for single_file in lst_of_files:
            if os.path.exists(single_file):
                # https://stackoverflow.com/a/678242/4723940
                caption_rts = os.path.basename(single_file)
                c_time = time.time()
                if not caption_rts.lower().endswith(".mp4"):
                    await udir_event.client.send_file(
                        udir_event.chat_id,
                        single_file,
                        caption=caption_rts,
                        force_document=False,
                        allow_cache=False,
                        reply_to=udir_event.message.id,
                        progress_callback=lambda d, t: asyncio.get_event_loop(
                        ).create_task(
                            progress(
                                d,
                                t,
                                udir_event,
                                c_time,
                                "Uploading in Progress.......",
                                single_file,
                            )),
                    )
                else:
                    thumb_image = os.path.join(input_str, "thumb.jpg")
                    c_time = time.time()
                    metadata = extractMetadata(createParser(single_file))
                    duration = 0
                    width = 0
                    height = 0
                    if metadata.has("duration"):
                        duration = metadata.get("duration").seconds
                    if metadata.has("width"):
                        width = metadata.get("width")
                    if metadata.has("height"):
                        height = metadata.get("height")
                    await udir_event.client.send_file(
                        udir_event.chat_id,
                        single_file,
                        caption=caption_rts,
                        thumb=thumb_image,
                        force_document=False,
                        allow_cache=False,
                        reply_to=udir_event.message.id,
                        attributes=[
                            DocumentAttributeVideo(
                                duration=duration,
                                w=width,
                                h=height,
                                round_message=False,
                                supports_streaming=True,
                            )
                        ],
                        progress_callback=lambda d, t: asyncio.get_event_loop(
                        ).create_task(
                            progress(d, t, udir_event, c_time, "Uploading...",
                                     single_file)),
                    )
                os.remove(single_file)
                uploaded = uploaded + 1
        await udir_event.edit(
            "Uploaded {} files successfully !!".format(uploaded))
    else:
        await udir_event.edit("404: Directory Not Found")