import json
import os
from typing import Callable

import aiofiles
import aiohttp
import ffmpeg
import requests
import wget
from PIL import Image, ImageDraw, ImageFont
from pyrogram import Client, filters
from pyrogram.errors import UserAlreadyParticipant
from pyrogram.types import InlineKeyboardButton, InlineKeyboardMarkup, Message
from Python_ARQ import ARQ
from youtube_search import YoutubeSearch

from musikku.config import BOT_NAME as bn
from musikku.config import DURATION_LIMIT
from musikku.config import UPDATES_CHANNEL as updateschannel
from musikku.config import que
from musikku.function.admins import admins as a
from musikku.helpers.admins import get_administrators
from musikku.helpers.channelmusic import get_chat_id
from musikku.helpers.decorators import authorized_users_only
from musikku.helpers.filters import command, other_filters
from musikku.helpers.gets import get_file_name
from musikku.services.callsmusic import callsmusic, queues
from musikku.services.callsmusic.callsmusic import client as USER
from musikku.services.converter.converter import convert
from musikku.services.downloaders import youtube

chat_id = None

def cb_admin_check(func: Callable) -> Callable:
    async def decorator(client, cb):
        admemes = a.get(cb.message.chat.id)
        if cb.from_user.id in admemes:
            return await func(client, cb)
        else:
            await cb.answer("You ain't allowed!", show_alert=True)
            return

    return decorator

def transcode(filename):
    ffmpeg.input(filename).output(
        "input.raw", format="s16le", acodec="pcm_s16le", ac=2, ar="48k"
    ).overwrite_output().run()
    os.remove(filename)

# Convert seconds to mm:ss
def convert_seconds(seconds):
    seconds = seconds % (24 * 3600)
    seconds %= 3600
    minutes = seconds // 60
    seconds %= 60
    return "%02d:%02d" % (minutes, seconds)

# Convert hh:mm:ss to seconds
def time_to_seconds(time):
    stringt = str(time)
    return sum(int(x) * 60 ** i for i, x in enumerate(reversed(stringt.split(":"))))

# Change image size
def changeImageSize(maxWidth, maxHeight, image):
    widthRatio = maxWidth / image.size[0]
    heightRatio = maxHeight / image.size[1]
    newWidth = int(widthRatio * image.size[0])
    newHeight = int(heightRatio * image.size[1])
    newImage = image.resize((newWidth, newHeight))
    return newImage

async def generate_cover(requested_by, title, views, duration, thumbnail):
    async with aiohttp.ClientSession() as session:
        async with session.get(thumbnail) as resp:
            if resp.status == 200:
                f = await aiofiles.open("background.png", mode="wb")
                await f.write(await resp.read())
                await f.close()

    image1 = Image.open("./background.png")
    image2 = Image.open("./etc/foreground.png")
    image3 = changeImageSize(1280, 720, image1)
    image4 = changeImageSize(1280, 720, image2)
    image5 = image3.convert("RGBA")
    image6 = image4.convert("RGBA")
    Image.alpha_composite(image5, image6).save("temp.png")
    img = Image.open("temp.png")
    draw = ImageDraw.Draw(img)
    font = ImageFont.truetype("etc/font.otf", 32)
    draw.text((205, 550), f"Title: {title}", (51, 215, 255), font=font)
    draw.text((205, 590), f"Duration: {duration}", (255, 255, 255), font=font)
    draw.text((205, 630), f"Views: {views}", (255, 255, 255), font=font)
    draw.text(
        (205, 670),
        f"Added By: {requested_by}",
        (255, 255, 255),
        font=font,
    )
    img.save("final.png")
    os.remove("temp.png")
    os.remove("background.png")

@Client.on_message(filters.command("playlist") & filters.group & ~filters.edited)
async def playlist(client, message):
    global que
    queue = que.get(message.chat.id)
    if not queue:
        await message.reply_text("Pemain menganggur")
    temp = []
    for t in queue:
        temp.append(t)
    now_playing = temp[0][0]
    by = temp[0][1].mention(style="md")
    msg = "**Sedang dimainkan** in {}".format(message.chat.title)
    msg += "\n- " + now_playing
    msg += "\n- Req by " + by
    temp.pop(0)
    if temp:
        msg += "\n\n"
        msg += "**Antre**"
        for song in temp:
            name = song[0]
            usr = song[1].mention(style="md")
            msg += f"\n- {name}"
            msg += f"\n- Req by {usr}\n"
    await message.reply_text(msg)

# ============================= Settings =========================================

def updated_stats(chat, queue, vol=100):
    if chat.id in callsmusic.pytgcalls.active_calls:
        # if chat.id in active_chats:
        stats = "Pengaturan di **{}**".format(chat.title)
        if len(que) > 0:
            stats += "\n\n"
            stats += "Volume : {}%\n".format(vol)
            stats += "Lagu dalam antrian : `{}`\n".format(len(que))
            stats += "Sedang dimainkan : **{}**\n".format(queue[0][0])
            stats += "Permintaan dari : {}".format(queue[0][1].mention)
    else:
        stats = None
    return stats

def r_ply(type_):
    if type_ == "play":
        pass
    else:
        pass
    mar = InlineKeyboardMarkup(
        [
            [
                InlineKeyboardButton("⏹", "leave"),
                InlineKeyboardButton("⏸", "puse"),
                InlineKeyboardButton("▶", "resume"),
                InlineKeyboardButton("⏭", "skip"),
            ],
            [
                InlineKeyboardButton("Playlist 📖", "playlist"),
            ],
            [InlineKeyboardButton("❌ Close", "cls")],
        ]
    )
    return mar

@Client.on_message(filters.command("current") & filters.group & ~filters.edited)
async def ee(client, message):
    queue = que.get(message.chat.id)
    stats = updated_stats(message.chat, queue)
    if stats:
        await message.reply(stats)
    else:
        await message.reply("Tidak ada instans VC yang berjalan dalam obrolan ini")

@Client.on_message(filters.command("player") & filters.group & ~filters.edited)
@authorized_users_only
async def settings(client, message):
    playing = None
    chat_id = get_chat_id(message.chat)
    if chat_id in callsmusic.pytgcalls.active_calls:
        playing = True
    queue = que.get(chat_id)
    stats = updated_stats(message.chat, queue)
    if stats:
        if playing:
            await message.reply(stats, reply_markup=r_ply("pause"))

        else:
            await message.reply(stats, reply_markup=r_ply("play"))
    else:
        await message.reply("Tidak ada instans VC yang berjalan dalam obrolan ini")

@Client.on_callback_query(filters.regex(pattern=r"^(playlist)$"))
async def p_cb(b, cb):
    global que
    que.get(cb.message.chat.id)
    type_ = cb.matches[0].group(1)
    cb.message.chat.id
    cb.message.chat
    cb.message.reply_markup.inline_keyboard[1][0].callback_data
    if type_ == "playlist":
        queue = que.get(cb.message.chat.id)
        if not queue:
            await cb.message.edit("Pemain menganggur")
        temp = []
        for t in queue:
            temp.append(t)
        now_playing = temp[0][0]
        by = temp[0][1].mention(style="md")
        msg = "**Sedang dimainkan** in {}".format(cb.message.chat.title)
        msg += "\n- " + now_playing
        msg += "\n- Req by " + by
        temp.pop(0)
        if temp:
            msg += "\n\n"
            msg += "**Antre**"
            for song in temp:
                name = song[0]
                usr = song[1].mention(style="md")
                msg += f"\n- {name}"
                msg += f"\n- Req by {usr}\n"
        await cb.message.edit(msg)

@Client.on_callback_query(
    filters.regex(pattern=r"^(play|pause|skip|leave|puse|resume|menu|cls)$")
)
@cb_admin_check
async def m_cb(b, cb):
    global que
    if (
        cb.message.chat.title.startswith("Channel Music: ")
        and chat.title[14:].isnumeric()
    ):
        chet_id = int(chat.title[13:])
    else:
        chet_id = cb.message.chat.id
    qeue = que.get(chet_id)
    type_ = cb.matches[0].group(1)
    cb.message.chat.id
    m_chat = cb.message.chat

    the_data = cb.message.reply_markup.inline_keyboard[1][0].callback_data
    if type_ == "pause":
        if (chet_id not in callsmusic.pytgcalls.active_calls) or (
            callsmusic.pytgcalls.active_calls[chet_id] == "paused"
        ):
            await cb.answer("Obrolan tidak terhubung!", show_alert=True)
        else:
            callsmusic.pytgcalls.pause_stream(chet_id)

            await cb.answer("Music Paused!")
            await cb.message.edit(
                updated_stats(m_chat, qeue), reply_markup=r_ply("play")
            )

    elif type_ == "play":
        if (chet_id not in callsmusic.pytgcalls.active_calls) or (
            callsmusic.pytgcalls.active_calls[chet_id] == "playing"
        ):
            await cb.answer("Obrolan tidak terhubung!", show_alert=True)
        else:
            callsmusic.pytgcalls.resume_stream(chet_id)
            await cb.answer("Music Resumed!")
            await cb.message.edit(
                updated_stats(m_chat, qeue), reply_markup=r_ply("pause")
            )

    elif type_ == "playlist":
        queue = que.get(cb.message.chat.id)
        if not queue:
            await cb.message.edit("Pemain menganggur")
        temp = []
        for t in queue:
            temp.append(t)
        now_playing = temp[0][0]
        by = temp[0][1].mention(style="md")
        msg = "**Sedang dimainkan** in {}".format(cb.message.chat.title)
        msg += "\n- " + now_playing
        msg += "\n- Req by " + by
        temp.pop(0)
        if temp:
            msg += "\n\n"
            msg += "**Antre**"
            for song in temp:
                name = song[0]
                usr = song[1].mention(style="md")
                msg += f"\n- {name}"
                msg += f"\n- Req by {usr}\n"
        await cb.message.edit(msg)

    elif type_ == "resume":
        if (chet_id not in callsmusic.pytgcalls.active_calls) or (
            callsmusic.pytgcalls.active_calls[chet_id] == "playing"
        ):
            await cb.answer("Obrolan tidak terhubung or already playng", show_alert=True)
        else:
            callsmusic.pytgcalls.resume_stream(chet_id)
            await cb.answer("Music Resumed!")
    elif type_ == "puse":
        if (chet_id not in callsmusic.pytgcalls.active_calls) or (
            callsmusic.pytgcalls.active_calls[chet_id] == "paused"
        ):
            await cb.answer("Obrolan tidak terhubung atau sudah dijeda", show_alert=True)
        else:
            callsmusic.pytgcalls.pause_stream(chet_id)

            await cb.answer("Music Paused!")
    elif type_ == "cls":
        await cb.answer("Closed menu")
        await cb.message.delete()

    elif type_ == "menu":
        stats = updated_stats(cb.message.chat, qeue)
        await cb.answer("Menu opened")
        marr = InlineKeyboardMarkup(
            [
                [
                    InlineKeyboardButton("⏹", "leave"),
                    InlineKeyboardButton("⏸", "puse"),
                    InlineKeyboardButton("▶", "resume"),
                    InlineKeyboardButton("⏭", "skip"),
                ],
                [
                    InlineKeyboardButton("Playlist 📖", "playlist"),
                ],
                [InlineKeyboardButton("❌ Close", "cls")],
            ]
        )
        await cb.message.edit(stats, reply_markup=marr)
    elif type_ == "skip":
        if qeue:
            qeue.pop(0)
        if chet_id not in callsmusic.pytgcalls.active_calls:
            await cb.answer("Obrolan tidak terhubung!", show_alert=True)
        else:
            callsmusic.queues.task_done(chet_id)

            if callsmusic.queues.is_empty(chet_id):
                callsmusic.pytgcalls.leave_group_call(chet_id)

                await cb.message.edit("- Tidak Ada Lagi Daftar Putar..\n- Keluar dari VC!")
            else:
                callsmusic.pytgcalls.change_stream(
                    chet_id, callsmusic.queues.get(chet_id)["file"]
                )
                await cb.answer("Skipped")
                await cb.message.edit((m_chat, qeue), reply_markup=r_ply(the_data))
                await cb.message.reply_text(
                    f"- Skipped track\n- Sedang dimainkan **{qeue[0][0]}**"
                )

    else:
        if chet_id in callsmusic.pytgcalls.active_calls:
            try:
                callsmusic.queues.clear(chet_id)
            except QueueEmpty:
                pass

            callsmusic.pytgcalls.leave_group_call(chet_id)
            await cb.message.edit("Berhasil Meninggalkan Obrolan!")
        else:
            await cb.answer("Obrolan tidak terhubung!", show_alert=True)

@Client.on_message(command("play") & other_filters)
async def play(_, message: Message):
    global que
    lel = await message.reply("🔄 **Sabar aja cuk**")
    administrators = await get_administrators(message.chat)
    chid = message.chat.id

    try:
        user = await USER.get_me()
    except:
        user.first_name = "helper"
    usar = user
    wew = usar.id
    try:
        # chatdetails = await USER.get_chat(chid)
        await _.get_chat_member(chid, wew)
    except:
        for administrator in administrators:
            if administrator == message.from_user.id:
                if message.chat.title.startswith("Channel Music: "):
                    await lel.edit(
                        "Ingatlah untuk menambahkan pembantu ke saluran Anda",
                    )
                    pass
                try:
                    invitelink = await _.export_chat_invite_link(chid)
                except:
                    await lel.edit(
                        "Tambahkan saya sebagai admin grup Anda terlebih dahulu",
                    )
                    return

                try:
                    await USER.join_chat(invitelink)
                    await USER.send_message(
                        message.chat.id, "Saya bergabung dengan grup ini untuk memutar musik di VC"
                    )
                    await lel.edit(
                        "helper userbot joined your chat",
                    )

                except UserAlreadyParticipant:
                    pass
                except Exception:
                    # print(e)
                    await lel.edit(
                        f"🔴 Kesalahan permintaan banjir 🔴 \nUser {user.first_name} tidak dapat bergabung dengan grup Anda karena banyak permintaan untuk userbot! Pastikan pengguna tidak diblokir di grup."
                        "\n\nAtau tambahkan asisten ke Grup Anda secara manual dan coba lagi",
                    )
    try:
        await USER.get_chat(chid)
        # lmoa = await client.get_chat_member(chid,wew)
    except:
        await lel.edit(
            f" {user.first_name} Userbot tidak ada dalam obrolan ini, Minta admin untuk mengirim perintah /play untuk pertama kalinya atau menambahkan {user.first_name} secara manual"
        )
        return
    message.from_user.id
    message.from_user.first_name
    await lel.edit("🔎 **Menemukan**")
    message.from_user.id
    user_id = message.from_user.id
    message.from_user.first_name
    user_name = message.from_user.first_name
    rpk = "[" + user_name + "](tg://user?id=" + str(user_id) + ")"
    audio = (
        (message.reply_to_message.audio or message.reply_to_message.voice)
        if message.reply_to_message
        else None
    )
    if audio:
        if round(audio.duration / 60) > DURATION_LIMIT:
            raise DurationLimitError(
                f"❌ Video lebih panjang dari {DURATION_LIMIT} minute(s) tidak diperbolehkan diputar!"
            )
        keyboard = InlineKeyboardMarkup(
            [
                [
                    InlineKeyboardButton("📖 Playlist", callback_data="playlist"),
                    InlineKeyboardButton("Menu ⏯ ", callback_data="menu"),
                ],
                [InlineKeyboardButton(text="Support Channel", url="https://t.me/levinachannel")],
                [InlineKeyboardButton(text="Close", callback_data="cls")],
            ]
        )
        file_name = get_file_name(audio)
        title = file_name
        thumb_name = "https://telegra.ph/file/f6086f8909fbfeb0844f2.png"
        thumbnail = thumb_name
        duration = round(audio.duration / 60)
        views = "Locally added"
        requested_by = message.from_user.first_name
        await generate_cover(requested_by, title, views, duration, thumbnail)
        file_path = await converter.convert(
            (await message.reply_to_message.download(file_name))
            if not path.isfile(path.join("downloads", file_name))
            else file_name
        )
    else:
        query = ""
        for i in message.command[1:]:
            query += " " + str(i)
        print(query)
        await lel.edit("🎵 **Sabar aja cuk**")
        ydl_opts = {"format": "bestaudio[ext=m4a]"}
        try:
            results = YoutubeSearch(query, max_results=1).to_dict()
            url = f"https://youtube.com{results[0]['url_suffix']}"
            # print(results)
            title = results[0]["title"][:40]
            thumbnail = results[0]["thumbnails"][0]
            thumb_name = f"thumb{title}.jpg"
            thumb = requests.get(thumbnail, allow_redirects=True)
            open(thumb_name, "wb").write(thumb.content)
            duration = results[0]["duration"]
            results[0]["url_suffix"]
            views = results[0]["views"]

        except Exception as e:
            await lel.edit(
                "Song not found.Try another song or maybe spell it properly."
            )
            print(str(e))
            return

        keyboard = InlineKeyboardMarkup(
            [
                [
                    InlineKeyboardButton("📖 Playlist", callback_data="playlist"),
                    InlineKeyboardButton("Menu ⏯ ", callback_data="menu"),
                ],
                [InlineKeyboardButton(text="Support Channel", url="https://t.me/levinachannel")],
                [InlineKeyboardButton(text="Close", callback_data="cls")],
            ]
        )
        requested_by = message.from_user.first_name
        await generate_cover(requested_by, title, views, duration, thumbnail)
        file_path = await convert(youtube.download(url))
    chat_id = get_chat_id(message.chat)
    if chat_id in callsmusic.pytgcalls.active_calls:
        position = await queues.put(chat_id, file=file_path)
        qeue = que.get(chat_id)
        s_name = title
        r_by = message.from_user
        loc = file_path
        appendable = [s_name, r_by, loc]
        qeue.append(appendable)
        await message.reply_photo(
            photo="final.png",
            caption=f"#⃣ Your requested song **queued** at position {position}!",
            reply_markup=keyboard,
        )
        os.remove("final.png")
        return await lel.delete()
    else:
        chat_id = get_chat_id(message.chat)
        que[chat_id] = []
        qeue = que.get(chat_id)
        s_name = title
        r_by = message.from_user
        loc = file_path
        appendable = [s_name, r_by, loc]
        qeue.append(appendable)
        try:
            callsmusic.pytgcalls.join_group_call(chat_id, file_path)
        except:
            message.reply("Group Call is not connected or I can't join it")
            return
        await message.reply_photo(
            photo="final.png",
            reply_markup=keyboard,
            caption="▶ **Playing** Permintaan dari {} via Youtube Music 😜".format(
                message.from_user.mention()
            ),
        )
        os.remove("final.png")
        return await lel.delete()

@Client.on_message(filters.command("dplay") & filters.group & ~filters.edited)
async def deezer(client: Client, message_: Message):
    global que
    lel = await message_.reply("🔄 **Sabar aja cuk**")
    administrators = await get_administrators(message_.chat)
    chid = message_.chat.id
    try:
        user = await USER.get_me()
    except:
        user.first_name = "MY-VCG"
    usar = user
    wew = usar.id
    try:
        # chatdetails = await USER.get_chat(chid)
        await client.get_chat_member(chid, wew)
    except:
        for administrator in administrators:
            if administrator == message_.from_user.id:
                if message_.chat.title.startswith("Channel Music: "):
                    await lel.edit(
                        "Ingatlah untuk menambahkan pembantu ke saluran Anda",
                    )
                    pass
                try:
                    invitelink = await client.export_chat_invite_link(chid)
                except:
                    await lel.edit(
                        "Tambahkan saya sebagai admin grup Anda terlebih dahulu",
                    )
                    return

                try:
                    await USER.join_chat(invitelink)
                    await USER.send_message(
                        message_.chat.id, "Saya bergabung dengan grup ini untuk memutar musik di VC"
                    )
                    await lel.edit(
                        "helper userbot joined your chat",
                    )

                except UserAlreadyParticipant:
                    pass
                except Exception:
                    # print(e)
                    await lel.edit(
                        f"🔴 Kesalahan permintaan banjir 🔴 \nUser {user.first_name} Sudo kagak sabi gabung."
                        "\n\nAtau tambahin secara manual",
                    )
    try:
        await USER.get_chat(chid)
        # lmoa = await client.get_chat_member(chid,wew)
    except:
        await lel.edit(
            f" {user.first_name} Kagak sabi gabung cuk, coba admin play dulu atau tambahin {user.first_name} manual"
        )
        return
    requested_by = message_.from_user.first_name

    text = message_.text.split(" ", 1)
    queryy = text[1]
    res = lel
    await res.edit(f"Mencari untuk `{queryy}` di deezer")
    try:
        r = await arq.deezer(query=queryy, limit=1)
        title = r[0]["title"]
        duration = int(r[0]["duration"])
        thumbnail = r[0]["thumbnail"]
        artist = r[0]["artist"]
        url = r[0]["url"]
    except:
        await res.edit("Tidak Menemukan Apa-apa, Coba pake bahasa inggris!")
        return
    keyboard = InlineKeyboardMarkup(
        [
            [
                InlineKeyboardButton("📖 Playlist", callback_data="playlist"),
                InlineKeyboardButton("Menu ⏯ ", callback_data="menu"),
            ],
            [InlineKeyboardButton(text="Support Channel", url=f"Https://t.me/levinachannel")],
            [InlineKeyboardButton(text="❌ Close", callback_data="cls")],
        ]
    )
    file_path = await convert(wget.download(url))
    await res.edit("Membuat Gambar Mini")
    await generate_cover(requested_by, title, artist, duration, thumbnail)
    chat_id = get_chat_id(message_.chat)
    if chat_id in callsmusic.pytgcalls.active_calls:
        await res.edit("menambahkan dalam antrian")
        position = await queues.put(chat_id, file=file_path)
        qeue = que.get(chat_id)
        s_name = title
        r_by = message_.from_user
        loc = file_path
        appendable = [s_name, r_by, loc]
        qeue.append(appendable)
        await res.edit_text(f"#⃣ Antri di posisi {position}")
    else:
        await res.edit_text(f"▶ Playing.....")

        que[chat_id] = []
        qeue = que.get(chat_id)
        s_name = title
        r_by = message_.from_user
        loc = file_path
        appendable = [s_name, r_by, loc]
        qeue.append(appendable)
        try:
            callsmusic.pytgcalls.join_group_call(chat_id, file_path)
        except:
            res.edit("Panggilan grup tidak terhubung karena saya tidak dapat bergabung")
            return

    await res.delete()

    m = await client.send_photo(
        chat_id=message_.chat.id,
        reply_markup=keyboard,
        photo="final.png",
        caption=f"Playing [{title}]({url}) Via Deezer",
    )
    os.remove("final.png")

@Client.on_message(filters.command("splay") & filters.group & ~filters.edited)
async def jiosaavn(client: Client, message_: Message):
    global que
    lel = await message_.reply("🔄 **Sabar aja cuk**")
    administrators = await get_administrators(message_.chat)
    chid = message_.chat.id
    try:
        user = await USER.get_me()
    except:
        user.first_name = "Geez-MusicProject"
    usar = user
    wew = usar.id
    try:
        # chatdetails = await USER.get_chat(chid)
        await client.get_chat_member(chid, wew)
    except:
        for administrator in administrators:
            if administrator == message_.from_user.id:
                if message_.chat.title.startswith("Channel Music: "):
                    await lel.edit(
                        "Ingatlah untuk menambahkan pembantu ke saluran Anda",
                    )
                    pass
                try:
                    invitelink = await client.export_chat_invite_link(chid)
                except:
                    await lel.edit(
                        "Tambahkan saya sebagai admin grup Anda terlebih dahulu",
                    )
                    return

                try:
                    await USER.join_chat(invitelink)
                    await USER.send_message(
                        message_.chat.id, "Saya bergabung dengan grup ini untuk memutar musik di VC"
                    )
                    await lel.edit(
                        "helper userbot joined your chat",
                    )

                except UserAlreadyParticipant:
                    pass
                except Exception:
                    # print(e)
                    await lel.edit(
                        f"🔴 Permintaan banjir 🔴 \nUser {user.first_name} tidak dapat bergabung dengan grup Anda karena banyaknya permintaan untuk userbot! Pastikan pengguna tidak dilarang di grup."
                        "\n\nAtau tambahkan BOT Asisten secara manual ke Grup Anda dan coba lagi",
                    )
    try:
        await USER.get_chat(chid)
        # lmoa = await client.get_chat_member(chid,wew)
    except:
        await lel.edit(
            " helper Userbot tidak ada dalam obrolan ini, Minta admin untuk mengirim / memutar perintah untuk pertama kalinya atau menambahkan asisten secara manual"
        )
        return
    requested_by = message_.from_user.first_name
    chat_id = message_.chat.id
    text = message_.text.split(" ", 1)
    query = text[1]
    res = lel
    await res.edit(f"Searching for `{query}` on jio saavn")
    try:
        async with aiohttp.ClientSession() as session:
            async with session.get(
                f"https://jiosaavnapi.bhadoo.uk/result/?query={query}"
            ) as resp:
                r = json.loads(await resp.text())
        sname = r[0]["song"]
        slink = r[0]["media_url"]
        ssingers = r[0]["singers"]
        sthumb = r[0]["image"]
        sduration = int(r[0]["duration"])
    except Exception as e:
        await res.edit("Found Literally Nothing!, You Should Work On Your English.")
        print(str(e))
        return
    keyboard = InlineKeyboardMarkup(
        [
            [
                InlineKeyboardButton("📖 Playlist", callback_data="playlist"),
                InlineKeyboardButton("Menu ⏯ ", callback_data="menu"),
            ],
            [
                InlineKeyboardButton(
                    text="Join Updates Channel", url=f"{updateschannel}"
                )
            ],
            [InlineKeyboardButton(text="❌ Close", callback_data="cls")],
        ]
    )
    file_path = await convert(wget.download(slink))
    chat_id = get_chat_id(message_.chat)
    if chat_id in callsmusic.pytgcalls.active_calls:
        position = await queues.put(chat_id, file=file_path)
        qeue = que.get(chat_id)
        s_name = sname
        r_by = message_.from_user
        loc = file_path
        appendable = [s_name, r_by, loc]
        qeue.append(appendable)
        await res.delete()
        m = await client.send_photo(
            chat_id=message_.chat.id,
            reply_markup=keyboard,
            photo="final.png",
            caption=f"#⃣ Antri di posisi {position}",
        )

    else:
        await res.edit_text(f"▶ Playing.....")
        que[chat_id] = []
        qeue = que.get(chat_id)
        s_name = sname
        r_by = message_.from_user
        loc = file_path
        appendable = [s_name, r_by, loc]
        qeue.append(appendable)
        try:
            callsmusic.pytgcalls.join_group_call(chat_id, file_path)
        except:
            res.edit("Panggilan grup tidak terhubung karena saya tidak dapat bergabung")
            return
    await res.edit("Membuat Gambar Mini.")
    await generate_cover(requested_by, sname, ssingers, sduration, sthumb)
    await res.delete()
    m = await client.send_photo(
        chat_id=message_.chat.id,
        reply_markup=keyboard,
        photo="final.png",
        caption=f"Playing {sname} Via Jiosaavn",
    )
    os.remove("final.png")

# Have u read all. If read RESPECT :-)


import logging

from zelos.handles import SocketHandle
from zelos.manager import IManager

from .base_socket import BaseSelect, BaseSocket

class Network(IManager):
    def __init__(self, helpers, file_manager, tracer):
        super().__init__(helpers)
        self.file_manager = file_manager
        self.trace = tracer
        self.logger = logging.getLogger(__name__)
        self.attempted_connections = set()
        self.num_sockets = 0

        self.ignore_whitelist = False
        self.whitelist = {
            "127.0.0.53": [53]
            # '8.8.8.8': [53]
        }

        # Entries in the DNS whitelist will always result in a
        # successful DNS response. If real networking is used, and the
        # domain is non-existant, the response will be replaced with a
        # default IP. This is useful for ensuring network checks for
        # common domains succeed.
        self.dns_whitelist = {"google.com", "pastebin.com", "virustotal.com"}
        # Entries in the DNS blacklist will always result in an NXDOMAIN
        # result, whether using real networking or Base sockets. This
        # is useful making it appear as if hardcoded C2 hosts are
        # unavailable, which often invokes DGA functionality.
        self.dns_blacklist = {}

        # When returning a fake DNS response, use this IP address in the
        # answer. This could be pointed to a network simulator.
        self.dns_default_ip = "45.45.45.45"

        # By default, return non-existant (NX) responses for all
        # domains, except those listed in the whitelist.
        self.dns_default_to_nx = True

        self.socket_class = BaseSocket
        self.select = BaseSelect(self)

    @property
    def sockets(self):
        return self.handles.get_by_type(SocketHandle)

    def set_socket_class(self, socket_class):
        """
        Allows network activity to be handled by a different socket
        class.
        """
        self.socket_class = socket_class

    def set_select_class(self, select_class):
        """
        Allows select/poll to be handled by a different class.
        """
        self.select = select_class(self)

    def add_attempted_connection(self, string, method):
        self.attempted_connections.add(string)
        self.triggers.tr_contacts_domain(string, method)

        if len(self.attempted_connections) > 10:
            self.triggers.tr_contacts_many_domains(self.attempted_connections)

    def create_socket_handle(self, domain, type, protocol=0):
        sock = self.create_socket(domain, type, protocol)
        socket_handle_num = self.handles.new_socket(
            f"sock#{self.num_sockets:03d}", sock
        )
        self.num_sockets += 1
        return socket_handle_num

    def create_socket(self, domain, type, protocol=0):
        return self.socket_class(self, domain, type, protocol)

    def enable_whitelist(self):
        self.ignore_whitelist = False

    def disable_whitelist(self):
        self.ignore_whitelist = True

    def is_whitelisted(self, host, port):
        """
        Returns if the host and port to connect is whitelisted
        """
        if host in self.whitelist:
            if self.whitelist[host] == -1:
                return True
            if port in self.whitelist[host]:
                return True
        return self.ignore_whitelist


from django.urls import path
from .views import (cargar_inicio,politicas,LoginView, LogoutView)

urlpatterns = [
    path('', cargar_inicio, name = 'inicio'),
    path('politicas/',politicas,name ='politicas'),

    path('login/', LoginView.as_view(template_name='accounts/login.html'),name='login'),
    path('logoutsesion', LogoutView.as_view(template_name='accounts/logout.html'),name='logout'),

]


import glob
import xml.etree.ElementTree as ET
from PIL import Image

#adding images in a folder to list
li=[]
for j in glob.glob("E:/gokul/PascalVOC-to-Images/data/*.jpg"): #path to data folder(should contain image and xml in same folder)
    li.append(j)

#for cropping images from a list by using bounding box in pascal voc(xml)
count=0
for i in glob.glob("E:/gokul/PascalVOC-to-Images/data/*.xml"): #path to data folder(should contain image and xml in same folder)
    # print('name',j)
    tree = ET.parse(i)
    # print('tree',tree)

    root = tree.getroot()
    # print('root',root)

    objects = root.findall('object')
    # print('objects',objects)
    temp=0
    for o in objects:
        bndbox = o.find('bndbox') # reading bound box
        xmin = int(bndbox.find('xmin').text)
        ymin = int(bndbox.find('ymin').text)
        xmax = int(bndbox.find('xmax').text)
        ymax = int(bndbox.find('ymax').text)
        print(xmin,ymin,xmax,ymax)
        bbox=(xmin,ymin,xmax,ymax)
        # print('fin',bbox)
        image=li[count]
        im=Image.open(image)
        im=im.crop(bbox)
        im.show()
        im.save(f"E:/gokul/PascalVOC-to-Images/output/cropped_image{count}_{temp}.jpg") #set save directory(E:/gokul/PascalVOC-to-Images/output/)
        temp=temp+1
    count=count+1


import torch
import torch.nn as nn
import torch.nn.functional as F

""" From: https://github.com/kevinzakka/one-shot-siamese/blob/master/model.py """

class SiameseNetwork(nn.Module):
    def __init__(self):

        super(SiameseNetwork, self).__init__()

        # Koch et al.
        # Conv2d(input_channels, output_channels, kernel_size)
        #Input should be 105x105x1
        self.conv1 = nn.Conv2d(1, 64, 10)
        self.conv2 = nn.Conv2d(64, 128, 7)
        self.conv3 = nn.Conv2d(128, 128, 4)
        self.conv4 = nn.Conv2d(128, 256, 4)
        self.fc1 = nn.Linear(9216, 4096)
        # self.fc2 = nn.Linear(4096, 1)
        self.extraL = nn.Linear(4096, 1)

        # 3 channels 150x150
        #self.conv1 = nn.Conv2d(3, 64, 10)
        #self.conv2 = nn.Conv2d(64, 128, 7)
        #self.conv3 = nn.Conv2d(128, 128, 4)
        #self.conv4 = nn.Conv2d(128, 256, 4)
        #self.fc1 = nn.Linear(30976, 4096)
        #self.fc2 = nn.Linear(4096, 1)

        # 1 channel 200x200
        # self.conv1 = nn.Conv2d(1, 64, 10)
        # self.conv2 = nn.Conv2d(64, 128, 7)
        # self.conv3 = nn.Conv2d(128, 128, 4)
        # self.conv4 = nn.Conv2d(128, 256, 4)
        # self.fc1 = nn.Linear(30976, 4096)

        # using kaiming intialization
        # random source- https://adityassrana.github.io/blog/theory/2020/08/26/Weight-Init.html
        for m in self.modules():
            if isinstance(m, nn.Conv2d):
                nn.init.kaiming_normal_(m.weight, mode='fan_in')

        # distanceLayer = True
        # defines if the last layer uses a distance metric or a neuron output

    def forward_once(self, x):

        out = F.relu(F.max_pool2d(self.conv1(x), 2))
        out = F.relu(F.max_pool2d(self.conv2(out), 2))
        out = F.relu(F.max_pool2d(self.conv3(out), 2))
        out = F.relu(self.conv4(out))

        out = out.view(out.shape[0], -1)
        out = self.fc1(out)
        out = self.extraL(out)
        # out = torch.sigmoid(self.fc1(out)) # #sigmoid as we use BCELoss
        return out

    def forward(self, input1, input2):
        output1 = self.forward_once(input1)
        output2 = self.forward_once(input2)

        # if self.lastLayer:
        #     # compute l1 distance
        #     diff = torch.abs(output1 - output2)
        #     # score the similarity between the 2 encodings
        #     scores = self.extraL(diff)
        #     return scores
        # else:
        return output1, output2

########### +++++++++++++++ ###########

#create the Siamese Neural Network
# class SiameseNetwork(nn.Module):

#     def __init__(self):
#         super(SiameseNetwork, self).__init__()

#         # Setting up the Sequential of CNN Layers
#         self.cnn1 = nn.Sequential(
#             nn.Conv2d(3, 96, kernel_size=11,stride=4),
#             nn.ReLU(inplace=True),
#             nn.MaxPool2d(3, stride=2),

#             nn.Conv2d(96, 256, kernel_size=5, stride=1),
#             nn.ReLU(inplace=True),
#             nn.MaxPool2d(2, stride=2),

#             nn.Conv2d(256, 384, kernel_size=3,stride=1),
#             nn.ReLU(inplace=True)
#         )

#         # Setting up the Fully Connected Layers
#         self.fc1 = nn.Sequential(
#             nn.Linear(384, 1024),
#             nn.ReLU(inplace=True),

#             nn.Linear(1024, 256),
#             nn.ReLU(inplace=True),

#             nn.Linear(256,2)
#         )

#     def forward_once(self, x):
#         # This function will be called for both images
#         # Its output is used to determine the similiarity
#         output = self.cnn1(x)
#         output = output.view(output.size()[0], -1)
#         output = self.fc1(output)
#         return output

#     def forward(self, input1, input2):
#         # In this function we pass in both images and obtain both vectors
#         # which are returned
#         output1 = self.forward_once(input1)
#         output2 = self.forward_once(input2)

#         return output1, output2


import pygame
import random
import numpy as np

buffer = 5
screen_width, screen_height = 1000, 600

config = {

    "large": {
        "number": 5,
        "location": int(.25*screen_height),
        "scale": int(.15*screen_width),
        "points": 10
        },
    "medium": {
        "number": 5,
        "location": int(.35*screen_height),
        "scale": int(.07*screen_width),
        "points": 25
        },
    "small": {
        "number": 5,
        "location": int(.45*screen_height),
        "scale": int(.05*screen_width),
        "points": 30
        }

}

class Target(pygame.sprite.Sprite):
    def __init__(self, image, x, y, points):
        super().__init__()
        self.image = image
        self.points = points
        self.rect = self.image.get_rect()
        self.rect.x = x
        self.rect.y = y

    def get_score(self):
        return self.points

def generate_targets(screen, group):
    xs, ys = get_offsets(screen, group, buffer)
    return pygame.sprite.Group(
        [Target(load_image('target.png', group['scale']), xs[i], ys[i], group['points']) for i in range(group['number'])]
    )

def ranks(sample):
    indices = sorted(range(len(sample)), key=lambda i: sample[i])
    return sorted(indices, key=lambda i: indices[i])

def sample(n=40, k=4, d=10):
    sample = random.sample(range(n-(k-1)*(d-1)), k)
    return np.array([s + (d-1)*r for s, r in zip(sample, ranks(sample))])

def get_offsets(screen, group, buffer=10):
    return (sample(screen.get_width() - group['scale'] - buffer, group['number'], group['scale']) + buffer,
            np.full(group['number'], screen.get_height() - group['location'] - buffer))

def load_image(path, scale):
    return pygame.transform.scale(pygame.image.load(path).convert_alpha(), (scale, scale))

def generate_groups(screen, config):
    return [generate_targets(screen, params) for group, params in config.items()]

def check_hit(target_group, position, score):
    for target in target_group:
        if target.rect.collidepoint(position[0], position[1]):
            score += target.get_score()
            target_group.remove(target)
    return score

def get_count(targets):
    count = 0
    for group in targets:
        count += len(group.sprites())
    return count

# Initialize Pygame
pygame.init()
clock = pygame.time.Clock()
screen = pygame.display.set_mode((screen_width, screen_height))

background_image = pygame.transform.scale(pygame.image.load("background.jpg").convert(), (screen_width, screen_height))

crosshair_image = pygame.transform.scale(pygame.image.load('crosshair.png'), (int(.3*screen_width), int(.3*screen_width)))
crosshair_rect = crosshair_image.get_rect()

pygame.mouse.set_visible(False)

score = 0
targets = generate_groups(screen, config)

running = True
while running:
    # check for user events
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
        elif event.type == pygame.MOUSEBUTTONDOWN:
            if event.button == 1:
                for group in targets:
                    score = check_hit(group, event.pos, score)

    screen.blit(background_image, (0, 0))
    for group in targets:
        group.draw(screen)

    crosshair_rect.center = pygame.mouse.get_pos()
    screen.blit(crosshair_image, crosshair_rect)
    screen.blit(pygame.font.Font(None, 36).render("Score: " + str(score), True, (255, 255, 255)), (10, 10))

    if get_count(targets) == 0:
        screen.blit(pygame.font.Font(None, 36).render("YOU WIN!", True, (255, 255, 255)), (.4*screen_height, 0.4*screen_width))

    pygame.display.update()
    clock.tick(60)

# Quit Pygame
pygame.quit()


import json
import os
import requests

class Region():

    root = os.path.dirname(os.path.abspath(__file__))

    def files(self, fileName):
        with open(self.root+"/"+fileName+".json", encoding="utf8") as jsonData:
            data = jsonData.read()

        return data

    def callCountry(self, country=None):
        countries = json.loads(self.files('countries'))

        countriesDict = {}
        for i in range(len(countries)):
            countriesDict[countries[i]['iso2']] = countries[i]['name']

        return countriesDict

    def callState(self, countryID):
        states = json.loads(self.files('states'))
        statesDict = {}
        for i in range(len(states)):
            if (states[i]['country_code'].upper() == countryID.upper()):
                statesDict[states[i]['state_code']] = states[i]['name']

        return statesDict

    def callCity(self, stateID):
        cities = json.loads(self.files('cities'))

        citiesDict = {}
        for i in range(len(cities)):
            if (cities[i]['state_code'] == stateID.upper()):
                citiesDict[cities[i]['name']] = cities[i]['name']

        return citiesDict

    # def remoteCities(self, hi):
    #     data = requests.get('https://raw.githubusercontent.com/dr5hn/countries-states-cities-database/master/cities.json').json()
    #     with open(self.root+"/cities.json", 'w', encoding='utf-8') as f:
    #         json.dump(data, f, ensure_ascii=False, indent=4)


import random
from torchvision import transforms

transform = transforms.Compose(
    [
        transforms.Resize((224, 224)),
        transforms.RandomRotation(10),
        transforms.RandomHorizontalFlip(),
        transforms.ToTensor(),
        transforms.Normalize((0.5,), (0.5,)),
    ]
)

def shuffle_files(img_files: list[str]) -> tuple[list[str], list[str]]:
    """
    Args:
        img_files (list[str]):

    Returns:
        tuple[list[str], list[str]]: train_files, valid_files
    """
    random.shuffle(img_files)
    img_files_len = len(img_files)
    train_files = img_files[: int(img_files_len * 0.8)]
    valid_files = img_files[int(img_files_len * 0.8) :]
    return train_files, valid_files


from skbuild import setup
import torch
from torch.utils import cpp_extension
import re
from os import path as osp
import sys

def _s(s):
    match = re.fullmatch(r"([^:]*\:)(.+)", s)
    if " " in s and match is not None:
        return match[1] + '"' + match[2] + '"'
    else:
        return s

if torch.__version__.startswith("1.0"):
    pytorch_base_include_path_suffix = osp.sep + osp.join("lib", "include")
elif torch.__version__.startswith("1.1"):
    pytorch_base_include_path_suffix = osp.sep + osp.join("include")
elif torch.__version__.startswith("1.2"):
    pytorch_base_include_path_suffix = osp.sep + osp.join("include")
else:
    raise ValueError("unsupported PyTorch version {}".format(torch.__version__))

pytorch_install_prefix = None
for current_include_path in cpp_extension.include_paths(False):
    if ("torch" + pytorch_base_include_path_suffix) in current_include_path:
        offset_of_search_string = current_include_path.rfind("torch" + pytorch_base_include_path_suffix)
        current_include_path = current_include_path[:offset_of_search_string + len("torch")]
        if osp.isdir(osp.join(current_include_path, "share")):
            pytorch_install_prefix = current_include_path
            break

if pytorch_install_prefix is None:
    raise ValueError("could not determine pytorch install prefix")

cmake_args = [
        "-DPYTORCH_INCLUDES:PATH=" + osp.pathsep.join(cpp_extension.include_paths(False)),
        "-DPYTORCH_LDFLAGS:STRING=" + " ".join(map(_s, cpp_extension._prepare_ldflags([], False, False))),
        "-DTORCH_INSTALL_PREFIX:PATH=" + pytorch_install_prefix
    ]
try:
    cmake_args.extend([
        "-DPYTORCH_CUDA_INCLUDES:PATH=" + osp.pathsep.join(cpp_extension.include_paths(True)),
         "-DPYTORCH_CUDA_LDFLAGS:STRING=" + " ".join(map(_s, cpp_extension._prepare_ldflags([], True, False)))
    ])
except OSError:
    pass

setup(
    name="pytorch_register_op_minimal",
    version="0.0.0",
    author="Leopold Walkling",
    author_email="leopold_walkling@web.de",
    cmake_args=cmake_args,
    packages=["pytorch_register_op_minimal"],
    package_dir={"pytorch_register_op_minimal": "pytorch_register_op_minimal"}
)


#!/usr/bin/env python3
# -*- coding: utf-8 -*-
from refinery.units import Arg, Unit

class netbios(Unit):
    """
    Encodes and decodes strings using the same algorithm that is used for NetBIOS
    labels. Each byte 0xUL is encoded as two bytes, which are the sum of 0xU and
    0xL with an offset character, respectively. The default offset is the capital
    letter A.
    """

    def __init__(self, key: Arg(help="Provide a single letter to use as the offset.") = B'A'):
        if len(key) != 1:
            raise ValueError("The key must be a binary string of length exactly 1")
        super().__init__(key=key[0])

    def reverse(self, data):
        result = bytearray(2 * len(data))
        for k, byte in enumerate(data):
            hi, lo = byte >> 4, byte & 15
            result[2 * k + 0] = hi + self.args.key
            result[2 * k + 1] = lo + self.args.key
        return result

    def process(self, data):
        def merge(it):
            while True:
                try:
                    hi = next(it) - self.args.key
                    lo = next(it) - self.args.key
                    if hi not in range(16) or lo not in range(16):
                        raise ValueError(F'Invalid character encoding detected: hi={hi:X}, lo={lo:X}.')
                    yield (hi << 4) | lo
                except StopIteration:
                    break
        return bytearray(merge(iter(data)))


import os
import csv
import json
import glob
import appdirs
from jupyter_core.paths import jupyter_runtime_dir
from jupyter_client import BlockingKernelClient
from permon.backend import Stat
from permon import exceptions

class JupyterRAMUsage(Stat):
    """
    tag: ``jupyter.ram_usage``

    settings:

    .. code-block:: javascript

        {
            "connection info": "",
            "query interval [s]": 1
        }

    Tracks the RAM usage of all variables in
    a user-specified jupyter notebook. If no connection info is given in the
    settings, take the kernel with the latest start date.

    ``connection info`` must be a string containing the info displayed when
    running ``%connect_info`` in a jupyter notebook
    (you can directly copy-paste it).

    ``query interval [s]`` specifies how often the thread running in the
    jupyter notebook should read the variables. The lower this is, the higher
    the resolution of the stat but it might start affecting the speed of
    your notebook when too low.

    Note that RAM tracked in this way is not equal to the actual RAM
    the OS needs because some further optimization is done by e. g. numpy
    to reduce the OS memory usage.
    """
    name = 'RAM Usage of objects in a Python Jupyter Notebook [MB]'
    base_tag = 'ram_usage'
    default_settings = {
        'connection info': '',
        # how often the memory usage is read in the jupyter notebook
        'query interval [s]': 1.
    }

    @classmethod
    def _read_latest_connection_file(cls):
        """
        Reads the latest jupyter kernel connection file.
        https://jupyter.readthedocs.io/en/latest/projects/jupyter-directories.html.
        """
        runtime_dir = jupyter_runtime_dir()
        files = glob.glob(os.path.join(runtime_dir, 'kernel-*.json'))
        if len(files) == 0:
            return None

        # use the latest connection file
        connection_file = max(files, key=os.path.getctime)
        with open(connection_file, 'r') as f:
            return json.load(f)

    @classmethod
    def get_connection_info(cls):
        """
        Get the target kernel connection info.
        Returns a dictionary of the connection info supplied
        in the settings, or the latest started kernel if none is given.
        Retuns `None` if no kernel has been found.
        """
        if len(cls.settings['connection info']) == 0:
            return cls._read_latest_connection_file()
        return json.loads(cls.settings['connection info'])

    @classmethod
    def check_availability(cls):
        # the stat is not available if no suitable connection info
        # can be found
        if cls.get_connection_info() is None:
            raise exceptions.StatNotAvailableError(
                'Could not find any running kernel.')

    def __init__(self, fps):
        self.config = self.get_connection_info()
        data_dir = appdirs.user_data_dir('permon', 'bminixhofer')
        os.makedirs(data_dir, exist_ok=True)

        self.usage_file = os.path.join(data_dir, 'jupyter_ram_usage.csv')
        open(self.usage_file, 'w').close()

        # self.setup_code is the code that is run in the notebook when the
        # stat is instantiated. It starts a thread which reads the memory
        # usage of all public variables in a set interval and saves it to a
        # csv file in the user data directory
        self.setup_code = f"""
if '_permon_running' not in globals() or not _permon_running:
    import threading
    import csv
    import sys
    import time
    from pympler import asizeof
    from types import ModuleType

    def _permon_get_ram_usage_per_object():
        while _permon_running:
            ram_usage = []
            global_vars = [key for key in globals() if not key.startswith('_')]
            for name in global_vars:
                value = globals()[name] if name in globals() else None
                if isinstance(value, ModuleType):
                    continue

                try:
                    ram_usage.append((name, asizeof.asizeof(value)))
                except TypeError:
                    continue

            with open('{self.usage_file}', 'w') as f:
                writer = csv.writer(f, delimiter=',')
                for name, ram in ram_usage:
                    writer.writerow([name, ram])
            time.sleep({self.settings['query interval [s]']})

    _permon_thread = threading.Thread(target=_permon_get_ram_usage_per_object)
    _permon_running = True
    _permon_thread.start()
"""
        self.teardown_code = """
_permon_running = False
"""
        self.client = BlockingKernelClient()
        self.client.load_connection_info(self.config)
        self.client.start_channels()
        self.client.execute(self.setup_code)
        super(JupyterRAMUsage, self).__init__(fps=fps)

    def __del__(self):
        # stop the thread running in the jupyter notebook
        # and stop the connection to the kernel upon deletion
        self.client.execute(self.teardown_code)
        self.client.stop_channels()

    def get_stat(self):
        # reads the csv file the setup code has written to
        ram_usage = []
        with open(self.usage_file, 'r') as f:
            reader = csv.reader(f)
            for row in reader:
                ram_usage.append(
                    (row[0], float(row[1]) / 1000**2)
                )
        # sort the ram_usage list so that the largest variables come first
        ram_usage = sorted(ram_usage, key=lambda x: x[1], reverse=True)

        # return the sum of RAM usage and the variables taking up the most RAM
        return sum(x[1] for x in ram_usage), ram_usage[:5]

    @property
    def minimum(self):
        return 0

    @property
    def maximum(self):
        return None


# -*- coding: utf-8 -*-
"""
Created on Sun Apr 26 19:10:13 2020

@author: Dell_1
"""

import nltk
import string

from os import listdir
from os.path import isfile, join

from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.metrics.pairwise import cosine_similarity

# All text entries to compare
BASE_INPUT_DIR = "C:\\Users\\arjav\\Desktop\\Text_Mining\\Project\\testfiles1"

#returns File list
def returnFilePathsList(folderPath):
    fileInfo = []
    FileNamesList = [fileName for fileName in listdir(folderPath) if isfile(join(folderPath, fileName))]
    FilePathsList = [join(folderPath, fileName) for fileName in listdir(folderPath) if isfile(join(folderPath, fileName))]
    fileInfo.append(FileNamesList)
    fileInfo.append(FilePathsList)
    return fileInfo

fileNames, filePaths = returnFilePathsList(BASE_INPUT_DIR)
#print(fileNames, "\n", filePaths)

# Get document contents
def create_docContentDict(filePaths):
    rawContentDict = {}
    for filePath in filePaths:
        with open(filePath, "r") as f:
            fileContent = f.read()
        rawContentDict[filePath] = fileContent
    return rawContentDict
rawContentDict = create_docContentDict(filePaths)
#print(rawContentDict)

def tokenizeContent(contentsRaw):
    tokenized = nltk.tokenize.word_tokenize(contentsRaw)
    return tokenized

def removeStopWordsFromTokenized(contentsTokenized):
    stop_word_set = set(nltk.corpus.stopwords.words("english"))
    filteredContents = [word for word in contentsTokenized if word not in stop_word_set]
    return filteredContents

def performPorterStemmingOnContents(contentsTokenized):
    porterStemmer = nltk.stem.PorterStemmer()
    filteredContents = [porterStemmer.stem(word) for word in contentsTokenized]
    return filteredContents

def stemming(contentsTokenized):
    stemmer= nltk.LancasterStemmer()
    filteredContents = [stemmer.stem(word) for word in contentsTokenized]
    return filteredContents

def removePunctuationFromTokenized(contentsTokenized):
    excludePuncuation = set(string.punctuation)
    filteredContents = [word for word in contentsTokenized if word not in excludePuncuation]
    return filteredContents

def convertItemsToLower(contentsRaw):
    filteredContents = [term.lower() for term in contentsRaw]
    return filteredContents

# process data using PorterStemmer
def processData(raw_data):
    cleaned = tokenizeContent(raw_data)
    cleaned = removeStopWordsFromTokenized(cleaned)
    cleaned = performPorterStemmingOnContents(cleaned)
    cleaned = removePunctuationFromTokenized(cleaned)
    cleaned = convertItemsToLower(cleaned)
    return cleaned

# process data using LancasterStemmer
def processData2(raw_data):
    cleaned = tokenizeContent(raw_data)
    cleaned = removeStopWordsFromTokenized(cleaned)
    cleaned = stemming(cleaned)
    cleaned = removePunctuationFromTokenized(cleaned)
    cleaned = convertItemsToLower(cleaned)
    return cleaned

# print TFIDF values
def print_TFIDF_values(term, values, fileNames):
    values = values.transpose()
    numValues = len(values[0])
    print('                ', end="")   #bank space for formatting output
    for n in range(len(fileNames)):
        print('{0:18}'.format(fileNames[n]), end="")
    print()
    for i in range(len(term)):
        print('{0:8}'.format(term[i]), end='\t|  ')
        for j in range(numValues):
            print('{0:.12f}'.format(values[i][j]), end='   ')
        print()

def print_Jaccard_Similarity(query, document):
    intersection = set(query).intersection(set(document))
    union = set(query).union(set(document))
    print("\n===============Jaccard Similarity=============\n", len(intersection)/len(union))
    print('===============================================\n')

def print_CosineSimilarity(tfs, fileNames):
    #print(cosine_similarity(tfs[0], tfs[1]))
    print("\n\n\n========COSINE SIMILARITY=====================\n")
    numFiles = len(fileNames)
    names = []
    print('                   ', end="")    #formatting
    for i in range(numFiles):
        if i == 0:
            for k in range(numFiles):
                print(fileNames[k], end='   ')
            print()

        print(fileNames[i], end='   ')
        for n in range(numFiles):
            matrixValue = cosine_similarity(tfs[i], tfs[n])
            numValue = matrixValue[0][0]
            names.append(fileNames[n])
            print(" {0:.8f}".format(numValue), end='         ')
            #(cosine_similarity(tfs[i], tfs[n]))[0][0]
        print()
    print("\n\n=====================================\n")

def main():
    baseFolderPath = "C:\\Users\\arjav\\Desktop\\Text_Mining\\Project\\testfiles1"

    fileNames, filePathList = returnFilePathsList(baseFolderPath)

    rawContentDict = create_docContentDict(filePathList)

    query_doc1 = ([key for key in rawContentDict.values()][0])
    compare_doc1 = ([key for key in rawContentDict.values()][1])

    query_doc = processData2(query_doc1)
    compare_doc = processData2(compare_doc1)

    #Jaccard Similarity
    print_Jaccard_Similarity(query_doc,compare_doc)

    # calculate tfidf
    tfidf = TfidfVectorizer(tokenizer=processData)
    tfs = tfidf.fit_transform(rawContentDict.values())
    tfs_Values = tfs.toarray()
    tfs_Term = tfidf.get_feature_names()
    # print results
    print_TFIDF_values(tfs_Term, tfs_Values, fileNames)
    print_CosineSimilarity(tfs, fileNames)

main()


import gym
from keras.models import Sequential
from keras.layers import Dense, Activation, Flatten
from keras.optimizers import Adam
from keras.callbacks import TensorBoard
from keras.models import load_model
from time import time
from rl.agents.dqn import DQNAgent
from rl.policy import BoltzmannQPolicy, EpsGreedyQPolicy
from rl.memory import SequentialMemory
import os.path
from environment import Environment
import numpy as np

WINDOW_SIZE = 10
EPISODES = 3
DATA_SET_PATH = "datasets/NVDA Aug 24 2018.csv"
WEIGHTS_NAME = "23.08.18/dqn_weights.h5f"

env = Environment(dataSetPath=DATA_SET_PATH)
model = Sequential()
model.add(Flatten(input_shape=(WINDOW_SIZE,) + env.observation_space.shape))
model.add(Dense(64))
model.add(Activation('relu'))
model.add(Dense(32))
model.add(Activation('relu'))
model.add(Dense(8))
model.add(Activation('relu'))
model.add(Dense(env.action_space.n))
model.add(Activation('linear'))

# model.summary()

memory = SequentialMemory(
  limit=env.dataLength,
  window_length=WINDOW_SIZE
)
policy = BoltzmannQPolicy()
dqn = DQNAgent(
  model=model,
  nb_actions=env.action_space.n,
  memory=memory,
  nb_steps_warmup=100,
  target_model_update=1e-2,
  policy=policy
)
dqn.compile(
  Adam(lr=0.001),
  metrics=['mae']
)

if(os.path.exists(WEIGHTS_NAME)):
  dqn.load_weights(WEIGHTS_NAME)
  print("saved weight loaded")

def getPredictionAt(index=0):
  state = env.getLatestState(index, window=10)
  state = np.reshape(state, (-1, 1))
  state = np.expand_dims(state, axis=0)
  prediction = model.predict(state)[0];
  index_of_maximum = np.where(prediction == np.max(prediction))
  return index_of_maximum[0]

# dqn.test(env, nb_episodes=1, visualize=False)

prediction = getPredictionAt(0)
predictionName = env.getActionNameByIndex(prediction[0]);

print("Today, I should " + predictionName)


import sys
from datetime import date
from os import system

sys.path.append('./classes')

from classes.Card import Card
from classes.Person import Person
from classes.bankomat import ATM
from loader import load

if __name__ =='__main__':
    karta=Card(input("Karta nomi:"),int(input("Balansi:")),int(input("Parol o'ylang:")))
    odam=Person(input("Ismingiz:"),karta,int(input("Naxt pulingiz qancha:")))
    bank1=ATM(1400000,'NBU')
    bank1.parol_tekshir(odam)
    load()
    print('''[1]--> Balansni tekshirish
[2]--> Naqd pul yechish
[3]--> Parolni o'zgartirish
[4]--> Kartaga pul o'tkazish
[5]--> Naqd pul balansi
[6]--> Chiqish''')
    while True:
        tanlov=int(input(">>>"))
        if tanlov ==1:
            print(karta.get_balans())
        elif tanlov==2:
            qancha=int(input("Qancha pul yechasiz:"))
            if qancha <=karta.get_balans() and qancha<=bank1.get_naxt_pul():
                karta.set_balans_minus(qancha)
                bank1.naxt_pul_minus(qancha)
                odam.karmon_plus(qancha)
                print(f'''           Chek
    Pul yechilgan sana:{date.today()}
    {qancha} pul yechildi
    Balans:{karta.get_balans()} so'm''')
            else:
                print("Mablag' yetarli emas")
        elif tanlov==3:
            karta.set_parol(input("Yangi parol:"))

        elif tanlov==4:
            qiymat=int(input('Qancha pul tashlamoqchisiz:'))
            if qiymat<=odam.karmon:
                bank1.naxt_pul_plus(qiymat)
                odam.karmon_minus(qiymat)
                karta.set_balans_plus(qiymat)
            else:
                print("Mablag' yetarli emas")
        elif tanlov==5:
            print(odam.karmon)
        elif tanlov==6:
            system('clear')
            exit()
        else:
            print("Tushunarsiz buyruq!!")


from django.urls import path

from . import views     # it means - 'from all import views'

urlpatterns = [
    path('',views.index, name='index'),
    path('add',views.addition, name='add'),
    path("register", views.register_request, name="register"),
    path("login", views.login_request, name="login"),
    path("logout", views.logout_request, name= "logout"),

]

# whenever 'localhost:8000' will be called, function named 'index' will be called that is present in 'views.py' file
# This will happen for all other routes as well


from qulacs import QuantumCircuit, QuantumState
from qulacs.gate import *
import random
from pwn import *
from Crypto.Cipher import AES
import base64

# CNOT gate
def ADD_CNOT(a, b):
    return "CNOT " + str(a) + "," + str(b) + "; "

# single qubit gates
def ADD_single(s, a):
    return s + " " + str(a) + "; "

# toffoli
def ADD_toffoli(a, b, c):
    ret = ""
    ret += ADD_single("H", c)
    ret += ADD_CNOT(b, c)

    ret += ADD_single("TDAG", c)
    ret += ADD_CNOT(a, c)

    ret += ADD_single("T", c)
    ret += ADD_CNOT(b, c)

    ret += ADD_single("TDAG", c)
    ret += ADD_CNOT(a, c)

    ret += ADD_single("T", b)
    ret += ADD_single("T", c)

    ret += ADD_CNOT(a, b)
    ret += ADD_single("H", c)

    ret += ADD_single("T", a)
    ret += ADD_single("TDAG", b)

    ret += ADD_CNOT(a, b)

    return ret

N = 128
xi, xip = 0.98, 0.98
p = (xi * (1 + xi))**0.5 - xi
Np = int(N * (1 + 2*xi + 2*(xi*(1+xi))**0.5 + xip))

# shor error correction
decoder = ""
decoder += ADD_CNOT(0, 1)
decoder += ADD_CNOT(3, 4)
decoder += ADD_CNOT(6, 7)
decoder += ADD_CNOT(0, 2)
decoder += ADD_CNOT(3, 5)
decoder += ADD_CNOT(6, 8)
decoder += ADD_toffoli(1, 2, 0)
decoder += ADD_toffoli(4, 5, 3)
decoder += ADD_toffoli(7, 8, 6)
decoder += ADD_single("H", 0)
decoder += ADD_single("H", 3)
decoder += ADD_single("H", 6)
decoder += ADD_CNOT(0, 3)
decoder += ADD_CNOT(0, 6)
decoder += ADD_toffoli(3, 6, 0)

r = remote('others.ctf.zer0pts.com', '11099')

def get_bin():
    s = r.recvline()
    s = s.split()[-1].decode()
    return int(s, 2)

r.recvline()
for i in range(0, 860):
    r.sendline(decoder)

measure = get_bin()
bb = (1 << 400) - 1
r.sendline(bin(bb))

ba = get_bin()
xa = get_bin()
m = get_bin()

l = []
for i in range(Np):
    if (ba >> i) & 1 == (bb >> i) & 1 == 0:
        l.append(i)

chosen = []
for i in range(Np):
    if (m & (1 << i)) > 0:
        chosen.append(i)

k = 0
for i in sorted(list(set(l) - set(chosen))):
    k = (k << 1) | ((measure >> i) & 1)

key = int.to_bytes(k, N // 8, 'big')
s = r.recvline()
vv = s.split()[-1]

fin = base64.b64decode(vv)
iv = fin[:16]
ct = fin[16:]

cipher = AES.new(key, AES.MODE_CBC, iv)
print(cipher.decrypt(ct))


import json
from flask import Flask, request
from database.tables import AbstractTable

app = Flask(__name__)

@app.route('/', methods=['POST', 'GET'])
def run_post():
    try:
        data = json.loads(request.get_data(as_text=True))

        table_data = AbstractTable().transaction(data['request'], data['table'], data['data'])

        return json.dumps(dict({
                'table': data['table'],
                'data': table_data
            }))

    except Exception as e:
        print(e)
        return e.__str__()

if __name__ == '__main__':
    app.run(host='0.0.0.0',
            port=8080)


import json
import re

import pandas as pd

with open('20191116_wawa.json', 'r') as file:
    data = json.load(file)

data_clean = []

raw_any_num = re.compile(r"(\d+,\d+) m|\d+ m")
raw_number = re.compile(r"\d+")
raw_inside_strong = re.compile(r"(\w+|\w+\s\w+)")
raw_inside_strong_prop = re.compile(r"(...+)")
raw_district = re.compile(r".*?,\s*(w*?)(...+)")

for record in data:

    #Cleaning the title data
    title_clean = record["title"]

    #Cleaning the district data
    district = record["district"]

    #Cleaning the prices data
    price = record["price"].strip(" zł").replace(" ", "").replace(",", ".")
    if price == "Zapytajocenę":
         price_clean = None
    elif price.endswith("€"):
         price_clean = float(price.strip("€")) * 4.288
    else:
         price_clean = float(price)

    #Clean the rent data
    rent = list(filter(re.compile(r"Czynsz: ").search, record["bag"]))
    if (len(rent) < 1) or (rent is None):
        rent_clean = None
    else:
        rent_clean = (float(raw_number.search(rent[-1]).group().strip(" zł").replace(",", ".")))

    #Clean the area data
    area = list(filter(re.compile(r"Powierzchnia: ").search, record["bag"]))
    if (len(area) < 1) or (area is None):
        area_clean = None
    else:
        area_clean = (float(raw_any_num.search(area[-1]).group().strip(" m").replace(",", ".")))

    #Clean the room data
    chamber = list(filter(re.compile(r"Liczba pokoi: ").search, record["bag"]))
    if (len(chamber) < 1) or (chamber is None):
        chamber_clean = None
    else:
        chamber_clean = int(raw_number.search(chamber[-1]).group())

    #Clean the market data
    market = list(filter(re.compile(r"Rynek: ").search, record["bag"]))
    if (len(market) < 1) or (market is None):
        market_clean = None
    else:
       market_clean = raw_inside_strong.search(market[-1]).group(1)

    #Clean the building type data
    building = list(filter(re.compile(r"Rodzaj zabudowy: ").search, record["bag"]))
    if (len(building) < 1) or (building is None):
        building_clean = None
    else:
        building_clean = raw_inside_strong.search(building[-1]).group(1)

    #Clean the floor data
    floor = list(filter(re.compile(r"Piętro: ").search, record["bag"]))
    if (len(floor) < 1) or (floor is None) or ("poddasze" in floor[0]):
        floor_clean = None
    else:
        if ">" in floor[0]:
            floor_clean = 11
        elif "parter" in floor[0]:
            floor_clean = 0
        elif "suterena" in floor[0]:
            floor_clean = 0
        else:
            floor_clean = int(raw_inside_strong.search(floor[-1]).group(1))

    #Clean the building floor number data
    building_floors = list(filter(re.compile(r"Liczba pięter: ").search, record["bag"]))
    if (len(building_floors) < 1) or (building_floors is None):
        building_floors_clean = None
    else:
        building_floors_clean = int(raw_inside_strong.search(building_floors[-1]).group(1))

    #Clean the construction year data
    construction_year = list(filter(re.compile(r"Rok budowy: ").search, record["bag"]))
    if (len(construction_year) < 1) or (construction_year is None):
        construction_year_clean = None
    else:
        construction_year_clean = int(raw_inside_strong.search(construction_year[-1]).group(1))

    #Clean the standard data
    standard = list(filter(re.compile(r"Stan wykończenia: ").search, record["bag"]))
    if (len(standard) < 1) or (standard is None):
        standard_clean = None
    else:
        standard_clean = raw_inside_strong.search(standard[-1]).group(1)

    #Clean the property data
    property = list(filter(re.compile(r"Forma własności: ").search, record["bag"]))
    if (len(property) < 1) or (property is None):
        property_clean = None
    else:
        property_clean = raw_inside_strong_prop.search(property[-1]).group(1)

    #Clean the window data
    window = list(filter(re.compile(r"Okna: ").search, record["bag"]))
    if (len(window) < 1) or (window is None):
        window_clean = None
    else:
        window_clean = raw_inside_strong.search(window[-1]).group(1)

    #Clean the heating data
    heating = list(filter(re.compile(r"Ogrzewanie: ").search, record["bag"]))
    if (len(heating) < 1) or (heating is None):
        heating_clean = None
    else:
        heating_clean = raw_inside_strong.search(heating[-1]).group(1)

    #Colect the data
    item = {
    "district": district,
    "area": area_clean,
    "chamber": chamber_clean,
    "market": market_clean,
    "building": building_clean,
    "floor": floor_clean,
    "building_floors": building_floors_clean,
    "construction_year": construction_year_clean,
    "standard": standard_clean,
    "property": property_clean,
    "window": window_clean,
    "heating": heating_clean,
    "url": record["url"],
    "rent": rent_clean,
    "price": price_clean,
    }

    data_clean.append(item)

clean_df = pd.DataFrame(data_clean)

clean_df.to_csv(f"20191117_data_clean_wawa.csv", index=False)

print("Processing task finished")


import sys
import os
from PIL import Image

img_folder = sys.argv[1]
op_folder = sys.argv[2]

def jpg2png(a, b):
    if not os.path.exists(b):
        os.makedirs(b)

    for filename in os.listdir(a):
        img = Image.open(f'{a}/{filename}')
        name = os.path.splitext()[0]
        img.save(f'{b}/{name}.png', 'png')

    return 'all done!'

# Run Module
print(jpg2png(img_folder, op_folder))


import io
import pickle

import numpy as np
import os.path
import sqlite3
from sqlite3 import Connection
from typing import Generator, Optional

from galleries import files_utils
from galleries.collections.stream_dictionary import StreamDictionary
from galleries.data_read_write.idata_reader_writer import IDataReaderWriter
from galleries.data_read_write.utils_file_data_readers_writers import *

def adapt_array(arr):
    """
    https://stackoverflow.com/a/18622264/9464297
    """
    out = io.BytesIO()
    np.save(out, arr)
    out.seek(0)
    return sqlite3.Binary(out.read())

def convert_array(text):
    out = io.BytesIO(text)
    out.seek(0)
    return np.load(out)

# Converts np.array to TEXT when inserting
sqlite3.register_adapter(np.ndarray, adapt_array)
# Converts TEXT to np.array when selecting
sqlite3.register_converter("array", convert_array)

class SqliteDataReaderWriter(IDataReaderWriter):
    TABLE_NAME = "Data"

    def __init__(self, data_identifier, root_folder: str, file_path: str = None, batch_size=100000):
        super().__init__(data_identifier)
        self._root_folder = root_folder
        if file_path is None:
            self.set_data_identifier(data_identifier)
        else:
            self._file_path = file_path

        self._batch_size = batch_size
        self._connection: Optional[Connection] = None

    def set_data_identifier(self, data_identifier: DataIdentifier):
        indices = read_index_list(self._root_folder, self._data_identifier)
        add_generator_to_indices_if_not_exists(self._data_identifier, indices)
        write_indices(self._root_folder, self._data_identifier, indices)
        file_path = get_data_path(self._root_folder, self._data_identifier, indices)
        files_utils.create_file_if_doesnt_exist(file_path)
        self._file_path = file_path

    def read_all_data(self) -> Generator:
        self.release()
        self._connect(self._file_path)
        cur = self._connection.cursor()
        try:
            data = cur.execute("SELECT * FROM Data")
            for d in data:
                yield pickle.loads(d[0])
        except:
            pass
        finally:
            self.release()

    def read_data(self, indices) -> Generator:
        sd = StreamDictionary(self.read_all_data, self._batch_size)
        for index in indices:
            data, success = sd.try_get_item(index)
            yield index, data

    def write_data(self, data: Generator, notify_function=None, notify_rate=100):
        self.release()
        exists = os.path.exists(self._file_path)
        if not exists:
            files_utils.create_dir_of_file(self._file_path)

        try:
            self._connect(self._file_path)
            cur = self._connection.cursor()
            exists_table = False
            try:
                tables = cur.execute("""SELECT name FROM sqlite_master WHERE type='table' AND name='Data'; """).fetchall()
                exists_table = tables != []
            except sqlite3.OperationalError:
                pass
            if not exists_table:
                cur.execute("CREATE TABLE Data (data BLOB)")

            def write(d):
                bd = pickle.dumps(d)
                cur.execute("INSERT INTO Data (data) VALUES(?)", [bd])
            self.write_data_with_notifications(data, write, notify_function, notify_rate)

            self._connection.commit()
        finally:
            self.release()

    def clear_data(self):
        self._connect(self._file_path)
        cur = self._connection.cursor()
        cur.execute("DELETE FROM Data")

    def release(self):
        if self._connection is not None:
            self._connection.close()
            self._connection = None

    def _connect(self, file_path):
        self.release()
        self._connection = sqlite3.connect(file_path, detect_types=sqlite3.PARSE_DECLTYPES)


#!/usr/bin/env python3

import click
import json

def get_conversations(data):
    """Return dict of unique conversation IDs and their participants."""

    convos = {}
    for conversation in data["conversation_state"]:
        conversation_id = conversation["conversation_id"]["id"]
        convo_participants = []
        for participant in conversation["conversation_state"]["conversation"][
            "participant_data"
        ]:
            try:
                convo_participants.append(participant["fallback_name"])
            except KeyError:
                pass
        convos[conversation_id] = convo_participants

    return convos

def generate_corpus(data, convo_id):
    """Return list containing all of the messages for the supplied conversation ID."""
    corpus = []
    states = data["conversation_state"]
    for state in states:
        conversation_state = state["conversation_state"]
        if "event" in conversation_state:
            conversations = conversation_state["event"]
            for conversation in conversations:
                if "chat_message" in conversation:
                    message_content = conversation["chat_message"]["message_content"]
                    if "segment" in message_content:
                        segment = message_content["segment"]
                        for line in segment:
                            conversation_id = conversation["conversation_id"]["id"]
                            if conversation_id == convo_id:
                                # timestamp_us = int(conversation['timestamp'])  # epoch time in microseconds
                                # timestamp = dt.datetime.fromtimestamp(timestamp_us/1000000)  # convert to epoch secs
                                # timestamp_str = timestamp.strftime("%Y%m%d_%Hh%Mm%Ss.%f")
                                if "text" in line:
                                    message_text = line["text"].strip()
                                    # If empty message then skip this pass of the loop
                                    if not message_text:
                                        continue
                                    # Append period if sentence is not otherwise punctuated
                                    if not message_text.endswith((".", "!", "?")):
                                        message_text += "."
                                    # Capitalise first letter
                                    message_text = (
                                        message_text[0].upper() + message_text[1:]
                                    )
                                    corpus.append(message_text)
    return corpus

@click.command()
@click.argument("jsonfile", type=click.Path(exists=True))
def main(jsonfile):
    """Extracts a specific conversation from Hangouts data exported from Google Takeout, and readies it for use by Wynbot."""
    with open(jsonfile, "rb") as file:
        data = json.load(file)

    # Get list of conversations and choose which one to use for data extraction. This may take some time.
    convos = get_conversations(data)
    selection_choices = {}
    print("{:<3} {:<35} {:<50}".format("No.", "Convo ID", "Participants"))
    for index, (key, value) in enumerate(convos.items(), start=1):
        print(
            "{num:<3} {convo_id:<35} {participants}".format(
                num=index, convo_id=key, participants=", ".join(value)
            )
        )
        selection_choices[index] = key
    selection = int(input("Enter no. of conversation to use: "))
    selected_convo_id = selection_choices[selection]

    # Generate corpus of message text using the chosen conversation
    corpus = generate_corpus(data, selected_convo_id)

    # Output text file with each message on a new line.
    with open("corpus.txt", "w", encoding="utf-8") as file:
        for line in corpus:
            file.write("{0}\n".format(line))

    print('Corpus extracted to "corpus.txt".')

if __name__ == "__main__":
    main()


#!/usr/bin/env python

# Faster script + multiprocessing
# Script that calculates cfd score for targets with bulge 'X', and save the accumulated cfd score for the input guide
# Also calculates the Doench score if the targets has bulge 'X' and 0 mms (doench = 0 if no such target exists)

# argv 1 = target file
# argv 2 is genome_directory (eg ../../Genomes/hg19/)
# argv 3 is pam file -> to check if len is 23 and pam is NGG
# argv 4 is guide file

# Block scikit warnings
import multiprocessing
import itertools
import string
import azimuth.model_comparison
import subprocess
import numpy as np
from os.path import isfile, join
from os import listdir
import os
import sys
import re
import pickle
import time
import warnings
warnings.filterwarnings("ignore", category=FutureWarning)
warnings.filterwarnings('ignore', category=UserWarning)

SIZE_DOENCH = 10000
N_THR = 3
# doench_string.append(seq)
# doench_score =  azimuth.model_comparison.predict(np.asarray(doench_string), None, None, model= model, pam_audit=False)
# doench_score = np.around(doench_score * 100)

def doenchParallel(targets, model, result):
    start_time = time.time()
    doench_score = azimuth.model_comparison.predict(
        targets, None, None, model=model, pam_audit=False)
    doench_score = [np.around(i * 100) for i in doench_score]
    max_doench = int(max(doench_score))
    result.append(max_doench)

def doenchForIupac(sequence_doench, guide_seq):
    pos_iupac = []
    var = []
    for pos, c in enumerate(sequence_doench):
        if c in iupac_code:
            pos_iupac.append(pos)
            var.append(iupac_code[c])

    if var:
        for i in itertools.product(*var):
            t = list(sequence_doench)
            for p, el in enumerate(pos_iupac):
                t[el] = i[p]
            targets_for_doench[guide_seq].append(''.join(t))
    else:
        targets_for_doench[guide_seq].append(sequence_doench)

def get_mm_pam_scores():
    try:
        mm_scores = pickle.load(open(os.path.dirname(
            os.path.realpath(__file__)) + '/mismatch_score.pkl', 'rb'))
        pam_scores = pickle.load(open(os.path.dirname(
            os.path.realpath(__file__)) + '/PAM_scores.pkl', 'rb'))
        return (mm_scores, pam_scores)
    except:
        raise Exception(
            "Could not find file with mismatch scores or PAM scores")

def revcom(s):
    basecomp = {'A': 'T', 'C': 'G', 'G': 'C', 'T': 'A', 'U': 'A', '-': '-'}
    letters = list(s[::-1])
    try:
        letters = [basecomp[base] for base in letters]
    except:
        return None
    return ''.join(letters)

# Calculates CFD score
def calc_cfd(guide_seq, sg, pam, mm_scores, pam_scores):

    score = 1
    sg = sg.replace('T', 'U')
    guide_seq = guide_seq.replace('T', 'U')
    s_list = list(sg)
    guide_seq_list = list(guide_seq)

    for i, sl in enumerate(s_list):
        if guide_seq_list[i] == sl:
            score *= 1

        else:
            try:  # Catch exception if IUPAC character
                key = 'r' + guide_seq_list[i] + \
                    ':d' + revcom(sl) + ',' + str(i + 1)
            except Exception as e:
                score = 0
                break
            try:
                score *= mm_scores[key]
            except Exception as e:  # If '-' is in first position, i do not have the score for that position
                pass

    score *= pam_scores[pam]
    return score

tab = str.maketrans("ACTGRYSWMKHDBVactgryswmkhdbv",
                    "TGACYRSWKMDHVBtgacyrswkmdhvb")

def reverse_complement_table(seq):
    return seq.translate(tab)[::-1]
# if __name__ == '__main__':

mm_scores, pam_scores = get_mm_pam_scores()
guides_dict = dict()
guides_dict_doench = dict()
targets_for_doench = dict()

score_filename = sys.argv[1].strip().split('.targets.txt')[0] + '.scores.txt'
targets_score_file = sys.argv[1].strip().split('.targets.txt')[
    0] + '.targets.CFD.txt'
bedfile_tmp_name = sys.argv[1].strip().split('.targets.txt')[
    0] + '.bedfile_tmp.bed'
remove_bedfile_tmp = False
with open(sys.argv[3]) as pamfile:
    line = pamfile.readline().strip().split(' ')
    if len(line[0]) != 23:
        with open(score_filename, 'w+') as result:
            result.write('NO SCORES')
            exit()
    if 'NGG' not in line[0]:
        print('WARNING: The model used for the CFD and Doench scores are based on the NGG PAM, the scores may not be valid for other PAMs')

N_THR = multiprocessing.cpu_count() // 2

iupac_code = {
    "R": ("A", "G"),
    "Y": ("C", "T"),
    "S": ("G", "C"),
    "W": ("A", "T"),
    "K": ("G", "T"),
    "M": ("A", "C"),
    "B": ("C", "G", "T"),
    "D": ("A", "G", "T"),
    "H": ("A", "C", "T"),
    "V": ("A", "C", "G"),
    "r": ("A", "G"),
    "y": ("C", "T"),
    "s": ("G", "C"),
    "w": ("A", "T"),
    "k": ("G", "T"),
    "m": ("A", "C"),
    "b": ("C", "G", "T"),
    "d": ("A", "G", "T"),
    "h": ("A", "C", "T"),
    "v": ("A", "C", "G"),
}

start = time.time()

# Get list of chromosomes file to set enr_str
chromosome_files = [f for f in listdir(
    sys.argv[2]) if isfile(join(sys.argv[2], f))]
# enr = sys.argv[2].split('/')
enr_str = ''
chr_ext = '.fa'
if '.enriched.' in chromosome_files[0]:
    enr_str = '.enriched'
elif '.indels.' in chromosome_files[0]:
    enr_str = '.indels'
if '.fasta' in chromosome_files[0]:
    chr_ext = '.fasta'
# if enr[-1]:
#   if'+' in enr[-1]:
#     enr_str = '.enriched'
# else:
#   if'+' in enr[-2]:
#     enr_str = '.enriched'

with open(os.path.dirname(os.path.realpath(__file__)) + "/azimuth/saved_models/V3_model_nopos.pickle", 'rb') as f:
    model = pickle.load(f)
max_doench = 0
sum_cfd = 0

# NOTE uncomment for progress bas
# process = subprocess.Popen(['wc', '-l', sys.argv[1]], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
# out, err = process.communicate()
# total_line = int(out.decode('UTF-8').split(' ')[0])
# if total_line < 2:
#     print('WARNING! Input file has no targets')
#     sys.exit()
# if total_line < 10:
#     mod_tot_line = 1
# else:
#     mod_tot_line = int(total_line/90)
# lines_processed = 0

bulge_pos = 8
all_word = []
with open(sys.argv[1]) as result, open(targets_score_file, 'w+') as save_targ_scor:
    save_targ_scor.write(next(result).strip() + '\tCFD\n')  # Add header
    # Calc CDF score
    for target in result:
        target = target.strip().split('\t')
        # print(target)

        guide_seq = target[1]
        off = target[2].upper()
        m_guide_seq = re.search('[^ATCGN-]', guide_seq)
        m_off = re.search('[^ATCG-]', off)
        iup_off = []
        first = True
        start_iup_off = 1

        if (m_guide_seq is None) and (m_off is None):

            # Calc CFD

            pam = off[-2:]
            sg = off[:-3]
            #print("off. ", off)
            #print ("sg: ", sg)
            #print ("guide_seq: ", guide_seq)
            if target[0] == 'DNA':
                # print('tests', guide_seq[int(target[bulge_pos]):], off[int(
                # target[bulge_pos]):-3])
                cfd_score = calc_cfd(guide_seq[int(target[bulge_pos]):], off[int(
                    target[bulge_pos]):-3], pam, mm_scores, pam_scores)
            else:
                cfd_score = calc_cfd(guide_seq, sg, pam, mm_scores, pam_scores)
            if (target[bulge_pos + 1] == '0'):
                # estraggo sequenza
                with open(bedfile_tmp_name, 'w+') as bedfile:
                    remove_bedfile_tmp = True
                    if target[6] == '+':
                        bedfile.write(
                            target[3] + '\t' + str(int(target[4]) - 4) + '\t' + str(int(target[4]) + 23 + 3))
                    else:
                        bedfile.write(
                            target[3] + '\t' + str(int(target[4]) - 3) + '\t' + str(int(target[4]) + 23 + 4))

                extr = subprocess.Popen(['bedtools getfasta -fi ' + sys.argv[2] + '/' + target[3] +
                                         enr_str + chr_ext + ' -bed ' + bedfile_tmp_name], shell=True, stdout=subprocess.PIPE)
                extr.wait()
                out, err = extr.communicate()
                out = out.decode('UTF-8')
                if target[6] == '+':
                    sequence_doench = out.strip().split('\n')[-1].upper()
                else:
                    sequence_doench = reverse_complement_table(
                        out.strip().split('\n')[-1].upper())

                if target[1] not in targets_for_doench:
                    targets_for_doench[target[1]] = []
                # Get all possible targets with iupac itertools for doench
                doenchForIupac(sequence_doench, target[1])

            save_targ_scor.write('\t'.join(target) +
                                 '\t' + str(cfd_score) + '\n')
            sum_cfd = sum_cfd + cfd_score
            try:
                guides_dict[target[1]] = guides_dict[target[1]] + cfd_score
            except:
                guides_dict[target[1]] = cfd_score

        else:
            if "N" in off:
                continue
            if (target[bulge_pos + 1] == '0'):
                with open(bedfile_tmp_name, 'w+') as bedfile:
                    remove_bedfile_tmp = True
                    if target[6] == '+':
                        bedfile.write(
                            target[3] + '\t' + str(int(target[4]) - 4) + '\t' + str(int(target[4]) + 23 + 3))
                    else:
                        bedfile.write(
                            target[3] + '\t' + str(int(target[4]) - 3) + '\t' + str(int(target[4]) + 23 + 4))

                extr = subprocess.Popen(['bedtools getfasta -fi ' + sys.argv[2] + '/' + target[3] +
                                         enr_str + chr_ext + ' -bed ' + bedfile_tmp_name], shell=True, stdout=subprocess.PIPE)
                extr.wait()
                out, err = extr.communicate()
                out = out.decode('UTF-8')
                if target[6] == '+':
                    sequence_doench = out.strip().split('\n')[-1].upper()
                else:
                    sequence_doench = reverse_complement_table(
                        out.strip().split('\n')[-1].upper())

                if target[1] not in targets_for_doench:
                    targets_for_doench[target[1]] = []
                # Get all possible targets with iupac itertools for doench
                doenchForIupac(sequence_doench, target[1])

            i = 0
            for char in off:
                if char in iupac_code:
                    n = len(iup_off)
                    for list_char in iupac_code[char]:
                        if not first:
                            for test in range(n - start_iup_off, n):
                                iup_off.append(
                                    iup_off[test][:i] + list_char + iup_off[test][i+1:])

                        else:
                            iup_off.append(off[:i] + list_char + off[i+1:])
                    first = False
                    start_iup_off = start_iup_off * len(iupac_code[char])

                i += 1
            dna_gap_removal = True
            for no_iup_str in range(len(iup_off) - start_iup_off, len(iup_off)):

                # se non ci sono gap passo la stringa non modificata al calc_cfd
                no_iup_gap_srt = iup_off[no_iup_str]

                # Calc CFD

                pam = no_iup_gap_srt[-2:]
                sg = no_iup_gap_srt[:-3]
                if target[0] == 'DNA':
                    cfd_score = calc_cfd(guide_seq[int(target[bulge_pos]):], sg[int(
                        target[bulge_pos]):], pam, mm_scores, pam_scores)
                else:
                    cfd_score = calc_cfd(
                        guide_seq, sg, pam, mm_scores, pam_scores)
                sum_cfd = sum_cfd + cfd_score
                try:
                    guides_dict[target[1]] = guides_dict[target[1]] + cfd_score
                except:
                    guides_dict[target[1]] = cfd_score

                # Recalculate mismatch value, only for pam at end with length 3 (eg NGG)
                mm_new_t = 0
                guide_no_pam = guide_seq[:-3]
                list_t = list(no_iup_gap_srt)
                for position_t, char_t in enumerate(no_iup_gap_srt[:-3]):
                    if char_t.upper() != guide_no_pam[position_t]:
                        mm_new_t += 1
                        if guide_no_pam[position_t] != '-':
                            list_t[position_t] = char_t.lower()

                target[2] = ''.join(list_t)
                target[bulge_pos - 1] = str(mm_new_t - int(target[bulge_pos]))
                # total differences between targets and guide (mismatches + bulges)
                target[bulge_pos + 1] = str(mm_new_t)
                save_targ_scor.write('\t'.join(target) +
                                     '\t' + str(cfd_score) + '\n')
        # NOTE decomment for progress bar
        # lines_processed +=1
        # if lines_processed % (mod_tot_line) == 0:
        #     print('Scoring: Total progress ' + str(round(lines_processed /total_line *100, 2)) + '%')

job_id = sys.argv[1].split('/')[-1].split('.')[0]

all_scores = []
with open(score_filename, 'w+') as res, open(sys.argv[4], 'r') as guides:
    man = multiprocessing.Manager()
    shared_doench = man.list()  # list containing max doech for each thread
    guides = guides.read().strip().split('\n')
    for g in guides:
        guides_dict_doench[g] = 0
        if g not in guides_dict:
            guides_dict[g] = 0
    # for k in guides_dict.keys():
        if g not in targets_for_doench:
            guides_dict_doench[g] = 0
        else:
            if len(targets_for_doench[g]) > SIZE_DOENCH:
                jobs = []
                remaining_splits = (
                    len(targets_for_doench[g])//SIZE_DOENCH) + 1
                for i in range((len(targets_for_doench[g])//SIZE_DOENCH) + 1):
                    for thr in range(min(N_THR, remaining_splits)):
                        p = multiprocessing.Process(target=doenchParallel, args=(np.asarray(targets_for_doench[g][i*N_THR*SIZE_DOENCH + thr*SIZE_DOENCH: min(
                            i*N_THR*SIZE_DOENCH + (thr+1)*SIZE_DOENCH, len(targets_for_doench[g]))]), model, shared_doench,))
                        remaining_splits -= 1
                        p.start()
                        jobs.append(p)
                    for i in jobs:
                        i.join()

                guides_dict_doench[g] = max(shared_doench)
                shared_doench = man.list()
            else:
                start_time = time.time()
                doench_score = azimuth.model_comparison.predict(np.asarray(
                    targets_for_doench[g]), None, None, model=model, pam_audit=False)
                doench_score = [np.around(i * 100) for i in doench_score]
                guides_dict_doench[g] = int(max(doench_score))
        if guides_dict[g] == 0:  # NO CFD WAS CALCULATED
            all_scores.append([g, 0, str(guides_dict_doench[g])])
        else:
            all_scores.append(
                [g, int(round((100/(100 + guides_dict[g]))*100)), str(guides_dict_doench[g])])
    all_scores.sort(key=lambda x: x[1], reverse=True)
    res.write('#crRNA\tCFD\tDoench 2016\n')
    for guide_scored in all_scores:
        res.write('\t'.join([str(el) for el in guide_scored]) + '\n')

if remove_bedfile_tmp:
    os.remove(bedfile_tmp_name)
end = time.time()


import unittest
import os
from appium import webdriver
from time import sleep

PATH = lambda p: os.path.abspath(
    os.path.dirname(__file__), p)

class test_leroy(unittest.TestCase):

    def setUp(self):
        desired_caps = {}
        desired_caps['platformName'] = 'Android'
        desired_caps['deviceName'] = 'LG K50'
        # desired_caps['app'] = PATH('leroymerlin.apk')
        desired_caps['udid'] = 'LMX5209PCAB6S8R8BA'
        desired_caps['appPackage'] = 'com.mec.leroy'
        desired_caps['appActivity'] = 'com.mec.leroy.MainActivity'

        self.driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_caps)
        self.driver.implicitly_wait(5)

    def tearDown(self):
        self.driver.quit()

    def testApp1(self):
        # getting to item site
        driver = self.driver
        button = driver.find_element_by_xpath(
                    "//android.widget.TextView[@bounds='[91,54][182,128]']")
        button.click()
        sleep(3)
        search = driver.find_element_by_xpath("//*[@text='Szukaj']")
        search.send_keys("beton architektoniczny")
        driver.find_element_by_xpath(
            "//android.widget.TextView[@bounds='[28,153][95,222]']").click()
        sleep(3)
        driver.find_element_by_xpath(
            "//android.widget.ImageView[@bounds='[18,508][193,683]']").click()
        sleep(3)

        # getting price of an item
        priceField = driver.find_element_by_xpath(
            "//android.widget.TextView[@bounds='[28,891][312,985]']")
        price = priceField.text
        print(price)

        # Assert
        self.assertEqual("119 zł/opak. ", price)

if __name__ == 'main':
    suite = unittest.TestLoader().loadTestsFromTestCase(test_leroy)
    unittest.TextTestRunner(verbosity=2).run(suite)


import pytest

@pytest.mark.math
def test():
    a = 1
    b = 2
    c = 3
    assert a+b == c

@pytest.mark.math
def test1():
    a = 1
    b = 2
    c = 3
    assert a+b == c

@pytest.mark.math
def test_devide_by_zero():
    with pytest.raises(ZeroDivisionError) as e:
        num = 1/0
    assert 'division by zero' in str(e.value)

#         Parametrize test functions
product = [
    (2,3,6),
    (1,99,99),
    (0,99,0),
    (3,-2,-6),
    (-2,-2,4),
    (2,2,4)
]

@pytest.mark.math
@pytest.mark.parametrize('a,b,product',product)
def test_parameterize_func(a,b,product):
    assert a*b == product

@pytest.mark.parametrize("a,b,add",([(1,2,3),(2,1,3)]))
def test_parametrize_add(a,b,add):
    assert a+b == add

def test_divide_by_zero():
  with pytest.raises(ZeroDivisionError) as e:
    num = 1 / 0
  assert 'division by zero' in str(e.value)


""" EoM of 3D double pendulum with the second mass distributed """
from sympy import symbols, Function, Eq, Matrix
from sympy import sin, cos
from sympy import diff, simplify, expand, solve, factor
from sympy import init_printing, pprint, latex

init_printing()

""" Define Symbols """
l1, l2, m1, m2 = symbols("l1 l2 m1 m2")
Ixx, Iyy, Izz = symbols("Ixx, Iyy, Izz")
t = symbols("t")
g = symbols("g")
L, Pot, Kin = symbols("L Pot Kin")
theta11 = Function("theta11")(t)
theta12 = Function("theta12")(t)
theta21 = Function("theta21")(t)
theta22 = Function("theta22")(t)
theta23 = Function("theta23")(t)
u11, u12, udot11, udot12 = symbols("u11 u12 udot11 udot12")
u21, u22, u23, udot21, udot22, udot23 = symbols("u21 u22 u23 udot21 udot22 udot23")
x1, y1, z1, x2, y2, z2 = symbols("x1 y1 z1 x2 y2 z1")
q = [theta11, theta12, theta21, theta22, theta23]
substitutions = [
    (theta11.diff(t).diff(t), udot11),
    (theta12.diff(t).diff(t), udot12),
    (theta21.diff(t).diff(t), udot21),
    (theta22.diff(t).diff(t), udot22),
    (theta23.diff(t).diff(t), udot23),
    (theta11.diff(t), u11),
    (theta12.diff(t), u12),
    (theta21.diff(t), u21),
    (theta22.diff(t), u22),
    (theta23.diff(t), u23),
]

""" Define Kinematic Relations """
x1 = l1 * sin(theta11)
y1 = l1 * cos(theta11) * sin(theta12)
z1 = -l1 * cos(theta11) * cos(theta12)
x2 = x1 + l2 * sin(theta21)
y2 = y1 + l2 * cos(theta21) * sin(theta22)
z2 = z1 - l2 * cos(theta21) * cos(theta22)

""" Equations for omega from cog-rotations.py """
omega_x = -theta21.diff(t) * (
    -sin(theta21) * sin(theta22) * cos(theta23) - sin(theta23) * cos(theta21)
) + theta22.diff(t) * (
    -sin(theta21) * sin(theta22) * sin(theta23) + cos(theta21) * cos(theta23)
)
omega_y = -theta21.diff(t) * cos(theta22) * cos(theta23) + theta22.diff(t) * sin(
    theta23
) * cos(theta22)
omega_z = (
    -theta21.diff(t)
    * (-sin(theta21) * sin(theta23) + sin(theta22) * cos(theta21) * cos(theta23))
    + theta22.diff(t)
    * (sin(theta21) * cos(theta23) + sin(theta22) * sin(theta23) * cos(theta21))
    + theta23.diff(t)
)

""" Langrangian """
Pot = m1 * g * z1 + m2 * g * z2
Kin = (
    1 / 2 * m1 * (x1.diff(t) ** 2 + y1.diff(t) ** 2 + z1.diff(t) ** 2)
    + 1 / 2 * m2 * (x2.diff(t) ** 2 + y2.diff(t) ** 2 + z2.diff(t) ** 2)
    + 1 / 2 * Ixx * omega_x ** 2
    + 1 / 2 * Iyy * omega_y ** 2
    + 1 / 2 * Izz * omega_z ** 2
)
L = Kin - Pot

euleq = [Eq(0, L.diff(qi.diff(t)).diff(t) - L.diff(qi)) for qi in q]
euleq = [euleqi.subs(substitutions).expand() for euleqi in euleq]

solvedeuleq = solve(
    euleq, [udot11, udot12, udot21, udot22, udot23], simplify=True, rational=False
)

for ddot, eqn in solvedeuleq.items():
    print(ddot, eqn)