from django_mongoengine.queryset import QuerySet

from .config import (
    HARD_DELETE,
    DELETED_INVISIBLE,
    DELETED_ONLY_VISIBLE,
    DELETED_VISIBLE,
    DELETED_VISIBLE_BY_FIELD,
)

class SafeDeletionQuerySet(QuerySet):
    """Default queryset for the SafeDeletionQuerySetManager.

    Takes care of "lazily evaluating" safedeletion QuerySets. QuerySets passed
    within the ``SafeDeletionQuerySetManager`` will have all of the models available.
    The deleted policy is evaluated at the very end of the chain when the
    QuerySet itself is evaluated.
    """

    _safedeletion_visibility = DELETED_INVISIBLE
    _safedeletion_visibility_field = "pk"
    _safedeletion_filter_applied = False

    # pylint: disable=arguments-differ
    # pylint: disable=inconsistent-return-statements
    def delete(
        self,
        force_policy=None,
        signal_kwargs=None,
        write_concern=None,
        _from_doc_delete=False,
        cascade_refs=None,
    ):
        doc = self._document
        # pylint: disable=protected-access
        current_policy = (
            doc._safedeletion_policy if (force_policy is None) else force_policy
        )

        if current_policy == HARD_DELETE:
            return super().delete(write_concern, _from_doc_delete, cascade_refs)

        if write_concern is None:
            write_concern = {}

        for obj in self.all():
            obj.delete(
                force_policy=force_policy, signal_kwargs=signal_kwargs, **write_concern
            )

    def undelete(self, force_policy=None, **write_concern):
        for obj in self.all():
            obj.undelete(force_policy, **write_concern)

    def __call__(self, q_obj=None, **query):
        force_visibility = getattr(self, "_safedeletion_force_visibility", None)
        visibility = (
            force_visibility
            if force_visibility is not None
            else self._safedeletion_visibility
        )
        if not self._safedeletion_filter_applied and visibility in (
            DELETED_INVISIBLE,
            DELETED_VISIBLE_BY_FIELD,
            DELETED_ONLY_VISIBLE,
        ):
            visibility_query = (
                {"deleted": None}
                if visibility in (DELETED_INVISIBLE, DELETED_VISIBLE_BY_FIELD)
                else {"deleted__ne": None}
            )
            query.update(visibility_query)
            self._safedeletion_filter_applied = True

        return super().__call__(q_obj, **query)

    # pylint: disable=arguments-differ
    def all(self, force_visibility=None):
        """Override so related managers can also see the deleted models.

        A model's m2m field does not easily have access to `all_objects` and
        so setting `force_visibility` to True is a way of getting all of the
        models. It is not recommended to use `force_visibility` outside of related
        models because it will create a new queryset.

        Args:
            force_visibility: Force a deletion visibility. (default: {None})
        """

        if force_visibility is not None:
            # pylint: disable=attribute-defined-outside-init
            self._safedeletion_force_visibility = force_visibility
        return super().all()

    def _check_field_filter(self, **kwargs):
        """Check if the visibility for DELETED_VISIBLE_BY_FIELD needs t be put into effect.

        DELETED_VISIBLE_BY_FIELD is a temporary visibility flag that changes
        to DELETED_VISIBLE once asked for the named parameter defined in
        `_safedeletion_force_visibility`. When evaluating the queryset, it will
        then filter on all models.
        """

        if (
            self._safedeletion_visibility == DELETED_VISIBLE_BY_FIELD
            and self._safedeletion_visibility_field in kwargs
        ):
            # pylint: disable=attribute-defined-outside-init
            self._safedeletion_force_visibility = DELETED_VISIBLE

    def filter(self, *q_objs, **query):
        queryset = self.clone()

        # pylint: disable=protected-access
        queryset._check_field_filter(**query)
        return super(SafeDeletionQuerySet, queryset).filter(*q_objs, **query)

    def get(self, *q_objs, **query):
        queryset = self.clone()
        # pylint: disable=protected-access
        queryset._check_field_filter(**query)
        return super(SafeDeletionQuerySet, queryset).get(*q_objs, **query)

    def clone(self):
        clone = super().clone()
        # pylint: disable=protected-access
        clone._safedeletion_visibility = self._safedeletion_visibility
        clone._safedeletion_visibility_field = self._safedeletion_visibility_field
        clone._safedeletion_filter_applied = self._safedeletion_filter_applied
        if hasattr(self, "_safedeletion_force_visibility"):
            # pylint: disable=protected-access
            clone._safedeletion_force_visibility = self._safedeletion_force_visibility
        return clone


"""empty message

Revision ID: dde24bfed677
Revises: 0c89b9c0a9cd
Create Date: 2020-04-07 16:50:44.747914

"""
from alembic import op
import sqlalchemy as sa

# revision identifiers, used by Alembic.
revision = 'dde24bfed677'
down_revision = '0c89b9c0a9cd'
branch_labels = None
depends_on = None

def upgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.create_table('workout',
    sa.Column('user_id', sa.Integer(), nullable=True),
    sa.Column('program', sa.Integer(), nullable=True),
    sa.Column('name', sa.String(length=120), nullable=False),
    sa.Column('id', sa.Integer(), nullable=False),
    sa.Column('description', sa.String(length=1200), nullable=False),
    sa.ForeignKeyConstraint(['program'], ['program.id'], ),
    sa.ForeignKeyConstraint(['user_id'], ['users.id'], ),
    sa.PrimaryKeyConstraint('id'),
    sa.UniqueConstraint('name')
    )
    op.create_table('exercise',
    sa.Column('user_id', sa.Integer(), nullable=True),
    sa.Column('workout', sa.Integer(), nullable=True),
    sa.Column('name', sa.String(length=40), nullable=False),
    sa.Column('id', sa.Integer(), nullable=False),
    sa.Column('description', sa.String(length=300), nullable=False),
    sa.Column('frequency_lower', sa.Integer(), nullable=True),
    sa.Column('frequency_upper', sa.Integer(), nullable=True),
    sa.Column('completetion_type', sa.Integer(), nullable=True),
    sa.ForeignKeyConstraint(['user_id'], ['users.id'], ),
    sa.ForeignKeyConstraint(['workout'], ['workout.id'], ),
    sa.PrimaryKeyConstraint('id'),
    sa.UniqueConstraint('name')
    )
    # ### end Alembic commands ###

def downgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_table('exercise')
    op.drop_table('workout')
    # ### end Alembic commands ###


import Gui
import networkx as nx
import random

def ant(graph:nx.Graph, psize:int, node:tuple, pgraph, pherom, pheromcoef, update):
    ants = []
    for i in range(psize):
        ants.append([set(),0])
        findway(graph, pgraph , ants[-1], node, pheromcoef)
        update()
        s = node
        for n in ants[-1][0]:
            if n != s:
                pgraph[n][s]["weight"] += float(pherom)/ants[-1][1]
                s = n
    ants = sorted(ants,key=lambda q: q[1])
    return ants[0][0],ants[0][1]

def findway(graph:nx.Graph, pgraph:nx.Graph, ant, node, pheromcoef):
    s = [node]
    visited = set([node])
    while len(s) < graph.number_of_nodes()+1 and s:
        sm = 0
        for nei in graph[s[-1]].items():
            if nei[0] not in visited:
                sm += float(pheromcoef)*pgraph[s[-1]][nei[0]]["weight"]+nei[1]["weight"]
        c = random.uniform(0,1)
        p = 0
        for nei in graph[s[-1]].items():
            if nei[0] not in visited:
                p += float(pheromcoef)*pgraph[s[-1]][nei[0]]["weight"]+nei[1]["weight"]
                if c <= p/sm:
                    s.append(nei[0])
                    visited.add(nei[0])
                    ant[1] += nei[1]["weight"]
                    break
            if nei[0] == node and len(s) == graph.number_of_nodes():
                s.append(nei[0])
    if len(s) == graph.number_of_nodes()+1:
        ant[0] = s
    else:
        ant[0] = None

if __name__ == '__main__':
    w = Gui.Window(ant, 0)
    w.run()


from gevent import monkey

monkey.patch_all()
from bs4 import BeautifulSoup
import requests
from fake_useragent import UserAgent
import gevent
from gevent.queue import Queue
import os

def getPictures():
    while not urls.empty():
        item = urls.get_nowait()
        if not os.path.exists(f'F://meiziba//{item[1]}'):
            os.mkdir(f'F://meiziba//{item[1]}')
        headers = {'User-Agent': UserAgent().random}
        response = requests.get(item[0], headers=headers)
        picSoup = BeautifulSoup(response.text, 'lxml')
        pictures = picSoup.find(
            'div', class_='entry-content u-clearfix text_indent_2').find_all('a')
        i = 1
        for pic in pictures:
            with open(f'F://meiziba//{item[1]}//{i}.jpg', 'wb') as f:
                f.write(requests.get(pic['href']).content)
            i += 1

if __name__ == '__main__':
    urls = Queue()
    URL = 'http://www.mziba.cn/page_1.html'
    headers = {'User-Agent': UserAgent().random}
    res = requests.get(URL, headers=headers)
    soup = BeautifulSoup(res.text, 'html.parser')
    articles = soup.find_all('article', class_='col-md-6 col-lg-4 col-xl-3 grid-item')
    for article in articles:
        t = article.find('h2', class_='entry-title').find('a')
        print(t)
        title = t.text
        url = t['href']
        urls.put_nowait([url, title])
    tasks_list = []
    for _ in range(10):
        task = gevent.spawn(getPictures)
        tasks_list.append(task)
    gevent.joinall(tasks_list)


import asyncio
import logging

from bleak import BleakClient, BleakScanner

DEVICE_NAME = "iBobber"
IBOBBER_ADDR = "34:14:B5:4B:B9:15"
ACCEL_SERVICE_UUID = "1791FFA0-3853-11E3-AA6E-0800200C9A66"
CUSTOM_SERVICE_UUID = "1791FF90-3853-11E3-AA6E-0800200C9A66"
BATT_SERVICE_UUID = "0000180F-0000-1000-8000-00805F9B34FB"
DEVICE_SERVICE_UUID = "0000180A-0000-1000-8000-00805F9B34FB"
OAD_SERVICE_UUID = "F000FFC0-0451-4000-B000-000000000000"

ALL_SERVICES = [
    ACCEL_SERVICE_UUID,
    CUSTOM_SERVICE_UUID,
    BATT_SERVICE_UUID,
    DEVICE_SERVICE_UUID,
    OAD_SERVICE_UUID,
]

log = logging.getLogger("iBobber")
log.setLevel(logging.DEBUG)

async def main():
    logging.basicConfig(
        level=logging.INFO,
        # level=logging.DEBUG,
        format="%(asctime)s %(levelname)-8s %(name)-15s %(message)s",
        # datefmt="%Y-%m-%d %H:%M:%S",
    )
    log.debug("meow")
    for device, advert in sorted(
        (await BleakScanner.discover(return_adv=True)).values(),
        key=lambda d: -d[1].rssi,
    ):
        if not (device.name == DEVICE_NAME or advert.local_name == DEVICE_NAME):
            continue
        log.debug("Got a bobber at %s", device.address)
        log.debug("services be like %s", advert.service_uuids)

        async with BleakClient(
            device.address,
            # services=ALL_SERVICES,
            timeout=9999,
        ) as client:
            log.info("Connected!")
            for service in client.services:
                log.debug("%s: %s", service.handle, service.description)
                for characteristic in service.characteristics:
                    log.debug(
                        "  %s: %s",
                        characteristic.handle,
                        characteristic.description,
                    )

asyncio.run(main())


import os
import sys

from typing import List

import yaml

# Permit imports from "buildscripts".
sys.path.append(os.path.normpath(os.path.join(os.path.abspath(__file__), '../../..')))

# pylint: disable=wrong-import-position
from buildscripts.idl import lib
from buildscripts.idl.idl import parser

def gen_all_feature_flags(idl_dirs: List[str] = None):
    """Generate a list of all feature flags."""
    default_idl_dirs = ["src", "buildscripts"]

    if not idl_dirs:
        idl_dirs = default_idl_dirs

    all_flags = []
    for idl_dir in idl_dirs:
        for idl_path in sorted(lib.list_idls(idl_dir)):
            if lib.is_third_party_idl(idl_path):
                continue
            # Most IDL files do not contain feature flags.
            # We can discard these quickly without expensive YAML parsing.
            with open(idl_path) as idl_file:
                if 'feature_flags' not in idl_file.read():
                    continue
            with open(idl_path) as idl_file:
                doc = parser.parse_file(idl_file, idl_path)
            for feature_flag in doc.spec.feature_flags:
                if feature_flag.default.literal != "true":
                    all_flags.append(feature_flag.name)

    with open("buildscripts/resmokeconfig/fully_disabled_feature_flags.yml") as fully_disabled_ffs:
        force_disabled_flags = yaml.safe_load(fully_disabled_ffs)

    return list(set(all_flags) - set(force_disabled_flags))

def gen_all_feature_flags_file(filename: str = "all_feature_flags.txt"):
    flags = gen_all_feature_flags()
    with open(filename, "w") as output_file:
        output_file.write("\n".join(flags))
        print("Generated: ", os.path.realpath(output_file.name))

def main():
    """Run the main function."""
    gen_all_feature_flags_file()

if __name__ == '__main__':
    main()


"""
H4xton fake typing chat exploit
"""
import requests,os,sys,time,random

class Discord():

    def TypeExploit(token,channelid):
        url = f"https://canary.discord.com/api/v9/channels/{channelid}/typing"
        headers = {
        'authority': 'canary.discord.com',
        'content-length': '0',
        'sec-ch-ua': '',
        'accept-language': 'da,en-US;q=0.9',
        'sec-ch-ua-mobile': '?1',
        'authorization': token,
        'user-agent': 'Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.164 Mobile Safari/537.36',
        'x-discord-locale': 'da',
        'accept': '*/*',
        'origin': 'https://canary.discord.com',
        'sec-fetch-site': 'same-origin',
        'sec-fetch-mode': 'cors',
        'sec-fetch-dest': 'empty'
        }
        while True:
            response = requests.request("POST", url, headers=headers, data=None)
            if response.status_code == 204:
                print("[+] Succesfuly send a Heartbeat")
            else:
                exit("[-] Failed to send a Heartbeat -> ".format(response.text))

if __name__ == "__main__":
    token = input("[+] Enter your token: ")
    channelid = input("[+] Channel ID: ")
    while True:
        Discord.TypeExploit(token,channelid)


import os
os.environ["XLA_FLAGS"] = "--xla_force_host_platform_device_count=4"

import sys
from typing import Literal
import pickle

sys.path.append(snakemake.config['workdir'])

import jax
from jax.config import config

config.update("jax_enable_x64", True)
print(f"Jax device count: {jax.local_device_count()}")

import torch

torch.set_default_dtype(torch.float64)

from arviz.data.inference_data import InferenceDataT

from src.utils.experiment_storage import ExperimentStorage, experiment_id_ctx
from src.napsu_mq.napsu_mq import NapsuMQModel, NapsuMQResult
from src.utils.string_utils import epsilon_float_to_str
from src.utils.timer import Timer
from src.utils.data_utils import transform_for_modeling
from src.utils.job_parameters import JobParameters
from src.utils.seed_utils import set_seed, get_seed
from src.utils.data_generator import create_dummy_dataset

"""
Create model with increasing number of variables in dataset and profiling enabled. Try to estimate the exponent for the runtime
using the number of canonical queries and the tree width.
"""

if __name__ == '__main__':

    parameter_combination_pickle_path = snakemake.input[0]
    parameter_combination_pickle_file = open(parameter_combination_pickle_path, "rb")
    parameter_combinations: JobParameters = pickle.load(parameter_combination_pickle_file)
    parameter_combination_pickle_file.close()

    epsilon = parameter_combinations.epsilon
    epsilon_str = epsilon_float_to_str(epsilon)
    experiment_id = parameter_combinations.experiment_id
    experiment_id_ctx.set(experiment_id)
    dataset_name = parameter_combinations.dataset
    query_list = parameter_combinations.query_list
    query_str = parameter_combinations.query_string
    laplace_approximation = parameter_combinations.laplace_approximation
    laplace_approximation_algorithm = parameter_combinations.laplace_approximation_algorithm
    algo = parameter_combinations.algo
    repeat_index = parameter_combinations.repeat_index

    seed = snakemake.config['seed']
    unique_seed = get_seed(seed, repeat_index)
    rng = set_seed(unique_seed)

    target_file = str(snakemake.output[0])

    storage_file_path = f"logs/napsu_linear_regression_test_storage_{experiment_id}.pickle"
    mode: Literal["append"] = "append"
    timer_file_path = "logs/napsu_linear_regression_test_timer.csv"

    storage = ExperimentStorage(file_path=storage_file_path, mode=mode)
    timer = Timer(file_path=timer_file_path, mode=mode)

    n_categories = int(dataset_name.split("x")[1])

    dataframe = create_dummy_dataset(n_columns=5, n_rows=10000, n_categories=n_categories)
    dataframe = transform_for_modeling(dataset_name, dataframe)

    n, d = dataframe.shape
    query = []
    delta = (n ** (-2))

    timer_meta = {
        "experiment_id": experiment_id,
        "dataset_name": dataset_name,
        "query": query,
        "epsilon": epsilon,
        "delta": delta,
        "MCMC_algo": algo,
        "laplace_approximation": laplace_approximation,
        "laplace_approximation_algorithm": laplace_approximation_algorithm,
        "repeat_index": repeat_index,
        "original_seed": unique_seed
    }

    pid = timer.start(f"Main run", **timer_meta)

    print(
        f"PARAMS: \n\tdataset name {dataset_name}\n\tcliques {query_str}\n\tMCMC algo {algo}\n\tepsilon {epsilon_str}\n\tdelta: {delta}\n\tLaplace approximation {laplace_approximation}")

    print("Initializing NapsuMQModel")
    model = NapsuMQModel()

    result: NapsuMQResult
    inf_data: InferenceDataT

    result, inf_data = model.fit(
        data=dataframe,
        dataset_name=dataset_name,
        rng=rng,
        epsilon=epsilon,
        delta=delta,
        column_feature_set=query,
        MCMC_algo=algo,
        use_laplace_approximation=laplace_approximation,
        return_inference_data=True,
        enable_profiling=False,
        laplace_approximation_algorithm=laplace_approximation_algorithm,
        laplace_approximation_forward_mode=True
    )

    timer.stop(pid)

    print("Writing model to file")
    result.store(target_file)

    inf_data.to_netcdf(f"logs/inf_data_linear_regression_{dataset_name}_{epsilon_str}e_{repeat_index}_repeat.nc")

    # Save storage and timer results every iteration
    storage.save_as_pickle(file_path=storage_file_path, experiment_id=experiment_id)
    timer.save(file_path=timer_file_path, mode=mode, index=False)


import json
import clearskies
from clearskies.handlers.exceptions import ClientError, InputError
from clearskies.handlers.base import Base
from .exceptions import ProducerError
class NoInput(clearskies.handlers.SchemaHelper, Base):
    _configuration_defaults = {
        'base_url': '',
        'can_rotate': True,
        'can_revoke': True,
        'create_callable': None,
        'revoke_callable': None,
        'rotate_callable': None,
        'payload_schema': None,
        'id_column_name': None,
        'create_endpoint': 'sync/create',
        'revoke_endpoint': 'sync/revoke',
        'rotate_endpoint': 'sync/rotate',
    }

    def __init__(self, di):
        super().__init__(di)

    def configure(self, configuration):
        # we don't need authentication but clearskies requires it, so provide one if it doesn't exist
        if 'authentication' not in configuration:
            configuration['authentication'] = clearskies.authentication.public()
        super().configure(configuration)

    def _finalize_configuration(self, configuration):
        # add in our base url and make sure the final result doesn't start or end with a slash
        base_url = configuration['base_url'].strip('/')
        for endpoint in ['create_endpoint', 'revoke_endpoint', 'rotate_endpoint']:
            configuration[endpoint] = (base_url + '/' + configuration[endpoint].strip('/')).lstrip('/')
        if configuration.get('payload_schema'):
            configuration['payload_schema'] = self._schema_to_columns(configuration['payload_schema'])
        return super()._finalize_configuration(configuration)

    def _check_configuration(self, configuration):
        super()._check_configuration(configuration)
        error_prefix = f"Configuration error for handler '{self.__class__.__name__}':"
        if not configuration.get('id_column_name'):
            raise ValueError(
                f"{error_prefix} you must provide 'id_column_name' - the name of a key from the response of the create callable that will be passed along to the revoke callable"
            )
        for action in ['revoke']:
            if not configuration.get(f'can_{action}'):
                continue
            if not configuration.get(f'{action}_callable'):
                raise ValueError(f"{error_prefix} you must provide '{action}_callable' or set 'can_{action}' to False")
            if not callable(configuration.get(f'{action}_callable')):
                raise ValueError(f"{error_prefix} '{action}_callable' must be a callable but was something else")
        if not configuration.get('create_callable'):
            raise ValueError(f"{error_prefix} you must provide 'create_callable'")
        if not callable(configuration.get('create_callable')):
            raise ValueError(f"{error_prefix} 'create_callable' must be a callable but was something else")
        if configuration.get('rotate_callable'):
            if 'can_rotate' in configuration and not configuration.get('can_rotate'):
                raise ValueError(
                    f"{error_prefix} 'rotate_callable' was provided, but can_rotate is set to False.  To avoid undefined behavior, this is not allowed."
                )
            if not callable(configuration['rotate_callable']):
                raise ValueError(
                    f"{error_prefix} 'rotate_callable' must be a callable (or None) but was something else"
                )
        if configuration.get('payload_schema') is not None:
            self._check_schema(configuration['payload_schema'], None, error_prefix)

    def handle(self, input_output):
        full_path = input_output.get_full_path().strip('/')
        if full_path == self.configuration('create_endpoint'):
            return self.create(input_output)
        elif full_path == self.configuration('revoke_endpoint'):
            if self.configuration('can_revoke'):
                return self.revoke(input_output)
            else:
                return self.dummy_revoke(input_output)
        elif full_path == self.configuration('rotate_endpoint') and self.configuration('can_rotate'):
            return self.rotate(input_output)
        return self.error(input_output, 'Page not found', 404)

    def _check_payload(self, payload):
        if not self.configuration('payload_schema'):
            return {}
        schema = self.configuration('payload_schema')
        return {
            **self._extra_column_errors(payload, schema),
            **self._find_input_errors(payload, schema),
        }

    def _get_payload(self, input_output):
        request_json = input_output.request_data(required=True)
        if 'payload' not in request_json:
            raise InputError("Missing 'payload' in JSON POST body")
        if not request_json['payload']:
            raise InputError("Provided 'payload' in JSON POST body was empty")
        if not isinstance(request_json['payload'], str):
            if isinstance(request_json['payload'], dict):
                raise InputError(
                    "'payload' in the JSON POST body was a JSON object, but it should be a serialized JSON string"
                )
            raise InputError("'payload' in JSON POST must be a string containing JSON")
        try:
            payload = json.loads(request_json['payload'])
        except json.JSONDecodeError:
            raise InputError("'payload' in JSON POST body was not a valid JSON string")
        return payload

    def _get_ids(self, input_output):
        request_json = input_output.request_data(required=True)
        if 'ids' not in request_json:
            raise InputError("Missing 'ids' in JSON POST body")
        return request_json['ids']

    def create(self, input_output):
        try:
            payload = self._get_payload(input_output)
        except InputError as e:
            return self.error(input_output, e.errors, 400)

        errors = self._check_payload(payload)
        if errors:
            return self.input_errors(input_output, errors)

        try:
            credentials = self._di.call_function(
                self.configuration('create_callable'),
                **payload,
                payload=payload,
                for_rotate=False,
            )
        except (InputError, ClientError, ProducerError) as e:
            return self.error(input_output, str(e), 400)

        # we need to return a meaningful id if we are going to revoke at the end
        if self.configuration('can_revoke'):
            id_column_name = self.configuration('id_column_name')
            if id_column_name not in credentials:
                raise ValueError(
                    f"Response from create callable did not include the required id column: '{id_column_name}'"
                )
            # akeyless will only accept strings as the id value - no integers/etc
            credential_id = str(credentials[id_column_name])
        else:
            credential_id = 'i_dont_need_an_id'

        return input_output.respond({
            'id': credential_id,
            'response': credentials,
        }, 200)

    def dummy_revoke(self, input_output):
        """
        Revoke, but don't revoke

        This is here because Akeyless always requires a revoke endpoint, but revokation is not always
        possible. So, if revoke is disabled, we still need to respond to the revoke endpoint.
        """
        try:
            payload = self._get_payload(input_output)
            ids = self._get_ids(input_output)
        except InputError as e:
            return self.error(input_output, e.errors, 400)

        errors = self._check_payload(payload)
        if errors:
            return self.input_errors(input_output, errors)

        return input_output.respond({
            'revoked': ids,
            'message': '',
        }, 200)

    def revoke(self, input_output):
        try:
            payload = self._get_payload(input_output)
            ids = self._get_ids(input_output)
        except InputError as e:
            return self.error(input_output, e.errors, 400)

        errors = self._check_payload(payload)
        if errors:
            return self.input_errors(input_output, errors)

        for id in ids:
            try:
                self._di.call_function(
                    self.configuration('revoke_callable'),
                    **payload,
                    payload=payload,
                    id_to_delete=id,
                )
            except (InputError, ClientError, ProducerError) as e:
                return self.error(input_output, str(e), 400)

        return input_output.respond({
            'revoked': ids,
            'message': '',
        }, 200)

    def rotate(self, input_output):
        try:
            payload = self._get_payload(input_output)
        except InputError as e:
            return self.error(input_output, e.errors, 400)

        errors = self._check_payload(payload)
        if errors:
            return self.input_errors(input_output, errors)

        # The user may have provided a rotate callable, in which case just use that.
        if self.configuration('rotate_callable'):
            new_payload = self._di.call_function(
                self.configuration('rotate_callable'),
                **payload,
                payload=payload,
            )
        # otherwise, perform a standard create+revoke
        else:
            try:
                new_payload = self._di.call_function(
                    self.configuration('create_callable'),
                    **payload,
                    payload=payload,
                    for_rotate=True,
                )
                if self.configuration('can_revoke'):
                    self._di.call_function(
                        self.configuration('revoke_callable'),
                        **new_payload,
                        payload=new_payload,
                        id_to_delete=payload.get(self.configuration('id_column_name')),
                    )
            except (InputError, ClientError, ProducerError) as e:
                return self.error(input_output, str(e), 400)

        return input_output.respond({
            'payload': json.dumps(new_payload),
        }, 200)

    def documentation(self):
        return []

    def documentation_security_schemes(self):
        return {}

    def documentation_models(self):
        return {}


from django import forms

from crispy_forms.layout import Submit

from core.forms.base_crispy_form import BaseCrispyForm
from core.models.round import Round

class WinnerForm(BaseCrispyForm, forms.ModelForm):
    SUBMIT_BUTTON_VALUE = "Declare Winner"
    SUBMIT_BUTTON_CSS_CLASSES = "btn-no-bg btn-outline-info"

    class Meta:
        model = Round
        fields = ["winner"]

    def __init__(
        self,
        *args,
        players=None,
        enable_reroll=True,
        moderator_id: int = None,
        **kwargs
    ):
        self.moderator_id = moderator_id
        super().__init__(*args, **kwargs)

        if self.instance.winner:
            self.helper.inputs.clear()
            self.helper.add_input(
                Submit(
                    name="action",
                    value="Start Next Round",
                    css_class="btn-no-bg btn-outline-primary",
                )
            )
            del self.fields["winner"]
        else:
            self.players = players

            player_choices = [{"pk": None, "player_name": "-----"}] + list(players)
            self.fields["winner"].choices = (
                (x["pk"], x["player_name"]) for x in player_choices
            )

            self.helper.add_input(
                Submit(
                    name="action",
                    value="Reveal Submitter",
                    css_class="btn-no-bg btn-outline-danger",
                )
            )
            if enable_reroll:
                self.helper.add_input(
                    Submit(
                        name="action",
                        value="Shuffle",
                        css_class="btn-no-bg btn-outline-primary",
                    )
                )

    def save(self, commit=True):
        self.instance.moderator_id = self.moderator_id

        super().save(self)


import codecs

from django.conf import settings
from django.views.generic.edit import CreateView
from django.contrib.auth.mixins import PermissionRequiredMixin
from django.urls import reverse
from django.http.response import HttpResponseRedirect
from django.contrib import messages

from achievement.models import Achievement

from ...models import (
    BulkCreationConsultant, Consultant
)
from ...forms import BulkCreationConsultantForm
from .helpers import read_name_email_coins_from_csv

class BulkCreationConsultantFormView(
        PermissionRequiredMixin,
        CreateView
):
    template_name = 'network/bulk_creation_form.html'
    permission_required = settings.CONSULTANT_FULL_PERMS_ADD_CONSULTANT
    model = BulkCreationConsultant
    form_class = BulkCreationConsultantForm
    raise_exception = True

    def get_success_message(self, *args, **kwargs):
        bulk_creation = kwargs.get('bulk_creation')
        return '{} invitations created successfully'.format(bulk_creation.consultants.success().count())

    def get_success_url(self, bulk_creation):
        return reverse('consultant:bulk-add-detail', kwargs={'pk': bulk_creation.pk})

    def create_bulk_creation(self, form):
        bulk_creation = form.save()
        bulk_creation.created_by = self.request.user
        bulk_creation.save()
        return bulk_creation

    def create_bulk_user(self, new_user, bulk_creation):
        return BulkCreationConsultant.objects.create(
            name=new_user.name,
            email=new_user.email,
            coins=new_user.coins,
            bulk_creation=bulk_creation,
        )

    def invite_consultant(self, new_user, custom_message):
        short_name = new_user.name.split(' ')[0]
        consultant = Consultant.objects.create_consultant(
            short_name=short_name,
            full_name=new_user.name,
            email=new_user.email,
            invite_user=self.request.user,
            registration_process=True,
            custom_text=custom_message,
            coins=new_user.coins,
        )
        return consultant

    def update_existing_user(self, new_user, bulk_creation_consultant):
        if not new_user.user.is_consultant:
            bulk_creation_consultant.set_error_email_used()
        else:
            consultant = new_user.user.consultant
            if not new_user.has_coins:
                bulk_creation_consultant.set_error_email_used()
            elif new_user.has_achievement_created():
                bulk_creation_consultant.set_error_achievement_created()
            else:
                Achievement.objects.create_reward_for_consultant(
                    consultant,
                    new_user.coins,
                )
            bulk_creation_consultant.set_consultant(consultant)

    def form_valid(self, form):
        bulk_creation = self.create_bulk_creation(form)

        filecontent = form.cleaned_data.get('file_csv')
        filecontent = codecs.iterdecode(filecontent, 'utf-8')
        users_list = read_name_email_coins_from_csv(filecontent)
        for new_user in users_list:
            bulk_creation_consultant = self.create_bulk_user(new_user, bulk_creation)
            if new_user.missing_information():
                bulk_creation_consultant.set_error_missing_information()
            elif new_user.exists:
                self.update_existing_user(new_user, bulk_creation_consultant)
            else:
                consultant = self.invite_consultant(
                    new_user, form.cleaned_data.get('custom_text'),
                )
                bulk_creation_consultant.set_consultant(consultant)
        success_message = self.get_success_message(**{'bulk_creation': bulk_creation})
        messages.success(self.request, success_message)
        return HttpResponseRedirect(self.get_success_url(bulk_creation))


import streamlit as st
import pytube as pt
import os
import subprocess
import re
from utils import logtime, load_ffmpeg
import whisper
from langchain.document_loaders import YoutubeLoader
from langchain.text_splitter import RecursiveCharacterTextSplitter

URL = 'URL'
TEXT = 'TEXT'
WHISPER = 'WHISPER'
PROCESSING = 'PROCESSING'
STATES = [URL, TEXT, WHISPER, PROCESSING]
AUDIO_FILE = "audio.mp3"
AUDIO_EXISTS = "AUDIO_EXISTS"
model = ''

st.title('Youtube Audio+Text')

def init_state():
    if URL not in st.session_state:
        st.session_state[URL] = ''

    if TEXT not in st.session_state:
        st.session_state[TEXT] = ''

    if WHISPER not in st.session_state:
        st.session_state[WHISPER] = ''

    if AUDIO_EXISTS not in st.session_state:
        st.session_state[AUDIO_EXISTS] = False

    if not st.session_state[URL]:
        clear_old_files()
        print("Clear old files")

def clear_old_files():
    for file in os.listdir():
        if file.endswith(".mp3") or file == 'transcript.txt':
            os.remove(file)
            print(f"Removed old files::{file}")

def extract_youtube_video_id(url):
  regex = r"v=([^&]+)"
  match = re.search(regex, url)
  if match:
    return match.group(1)
  else:
    return None

@logtime
def load_whisper():
    # if not model:
    model = whisper.load_model("small")
    print('Loaded Whisper Medium model')
    # else:
    #     print('Already downloaded Whisper model')
    print('Transcribing with Whisper model')
    result = model.transcribe("audio.mp3")
    st.session_state[WHISPER] = result["text"]
    write_file(result["text"], "whisper.txt")
AUDIO_FILE = "audio.mp3"

def load_audio():
    if os.path.exists(AUDIO_FILE):
        st.session_state[AUDIO_EXISTS] = True
        audio_file = open(AUDIO_FILE, 'rb')
        audio_bytes = audio_file.read()
        print(f"Audio file exists...{len(audio_bytes)}")
        st.audio(audio_bytes, format="audio/mp3")
    elif st.session_state[AUDIO_EXISTS]:
        st.session_state[AUDIO_EXISTS] = False

def display():
    container = st.container()
    text_container = st.container()
    # whisper_container = st.container()
    load_audio()

    #Download Button section
    col1, col2 = st.columns(2)
    with col1:
        if st.session_state[AUDIO_EXISTS]:
            st.download_button("Download Audio","file","audio.mp3","application/octet-stream")
    with col2:
        if os.path.exists("transcript.txt"):
            st.download_button("Download Transcript",st.session_state[TEXT],"transcript.txt","text/plain")

    with container:
        with st.form(key='input_form'):
            user_input = st.text_input("Youtube URL:", placeholder="http://www.youtube.com", key=URL)
            input_submit_button = st.form_submit_button(label='Send')

    if input_submit_button and user_input:

        st.write("You entered... " + st.session_state[URL])
        # transcribe()
        # download()
        # download_audio()
        load_whisper()

    with text_container:
       st.text_area(label="Youtube Transcript:",
                    height=200,
                    value=st.session_state[TEXT])
    # with whisper_container:
    #    st.text_area(label="Whisper Transcript:",
    #                 height=200,
    #                 value=st.session_state[WHISPER])

@logtime
def download_audio():
    if st.session_state[URL]:
        print("Downloading....")
        yt = pt.YouTube(st.session_state[URL])
        stream = yt.streams.filter(only_audio=True)[0]
        stream.download(filename="audio.mp3")
        print("Downloaded Audio file....")

def download():
  id = extract_youtube_video_id(st.session_state[URL])
  command = [f"yt-dlp --no-config -v --extract-audio --audio-format mp3 {st.session_state[URL]} -o audio.mp3"]
  print(command)
  out = subprocess.run(command, shell=True)
  print('Download with YT-DLP done!!')

@logtime
def transcribe():
    loader = YoutubeLoader.from_youtube_url(
        st.session_state[URL], add_video_info=True)
    splitter = RecursiveCharacterTextSplitter(chunk_size=2000,chunk_overlap=500)
    docs = loader.load_and_split(splitter)
    length = len(docs)
    index = int(length/3+1)
    print(f"Loaded {length} documents, Displaying {index}-th document")
    # st.session_state[TEXT] = docs[index].page_content
    st.session_state[TEXT] = write_chunks(docs,"transcript.txt")

@logtime
def write_chunks(docs, filename):
    full_doc = ''
    for doc in docs:
       full_doc = full_doc + doc.page_content + "\n"
    with open(filename, "w") as f:
        f.write(full_doc)
        return full_doc

def write_file(text, filename):
    with open(filename, "w") as f:
        f.write(text)
        # return full_doc

def main():
    # load_ffmpeg()
    init_state()
    display()

if __name__ == "__main__":
    main()