import sys
import json
from pubcontrol import Item
from gripcontrol import GripPubControl, WebSocketEvent, \
    WebSocketMessageFormat, decode_websocket_events, encode_websocket_events

pub = GripPubControl({'control_uri': 'http://localhost:5561'})

opening = False
out_headers = []
out = []
for e in decode_websocket_events(sys.stdin.read()):
    if e.type == 'OPEN':
        if not opening:
            opening = True

            # enable GRIP
            out_headers.append(('Sec-WebSocket-Extensions', 'grip'))

            # ack the open
            out.append(e)

            # subscribe connection to channel
            cm = {'type': 'subscribe', 'channel': 'room'}
            out.append(WebSocketEvent('TEXT', 'c:%s' % json.dumps(cm)))
    elif e.type == 'CLOSE':
        out.append(e) # ack
        break
    elif e.type == 'TEXT':
        # broadcast to everyone
        pub.publish('room', Item(WebSocketMessageFormat(e.content)))

out_headers.append(('Content-Type', 'application/websocket-events'))

for header in out_headers:
    cm = {
        'type': 'setHeader',
        'payload': {
            'name': header[0],
            'value': header[1]
        }
    }
    sys.stderr.write('%s\n' % json.dumps(cm))

sys.stdout.write(encode_websocket_events(out))


import re
import shutil
import os
import shlex
import subprocess
import sys

debug = False
default_encoding = sys.getdefaultencoding()

def check_param(obj, obj_type):
    """Assert obj as type obj_type."""

    try:
        obj_type(obj)
    except Exception:
        raise AssertionError(
            "error: parameter ("
            + str(type(obj))
            + ": "
            + str(obj)
            + ") must be of "
            + str(obj_type)
            + "!"
        )

def replace(infile, outfile, tokens, values, formats=None, encoding=default_encoding):
    """
    Replace values in a file using delimiters.

    Parameters
    ----------
    infile : str
        Name of template file that will be parsed.
    outfile : str
        Name of file that will received the template parsed.
        If equal to *None* or to *infile*, the file will be replaced in-place.
    tokens : list of str
        Regexes that will be replaced.
        When dealing with overlapping tokens, longest tokens should go first.
    values : list
        Values (can be string, float, ...) that will replace
        the tokens. The list must have the same size as tokens.
    formats : list of str, optional
        A list of formats for printing the values in the parsed files at
        tokens locations
        see https://docs.python.org/2/library/string.html#formatspec
    encoding : str, optional
        File encoding
        see http://docs.python.org/2/library/codecs.html#codec-base-classes

    Raises
    ------
    AssertionError
        parameters badly set
    EOFError
        a token has not been found

    Examples
    --------
    >>> import openturns.coupling_tools as ct
    >>> # write a template file
    >>> with open('prgm.dat.in', 'w') as f:
    ...     count = f.write('E=@E_VAR F=-PF-')
    >>> # replace tokens from template
    >>> ct.replace('prgm.dat.in', 'prgm.dat',
    ...     tokens=["@E_VAR", '-PF-'], values=[1.4, '5'])
    >>> # display file
    >>> with open('prgm.dat', 'r') as f:
    ...     print(f.read())
    E=1.4 F=5
    """
    if len(tokens) != len(values):
        raise AssertionError("Error: tokens size is != values size!")
    check_param(tokens, list)
    check_param(values, list)
    if formats is not None:
        check_param(formats, list)
        if len(values) != len(formats):
            raise AssertionError("Error: values size is != formats size!")
    else:
        formats = ["{0}"] * len(values)

    inplace = False
    if (outfile is None) or (infile == outfile):
        inplace = True
        outfile = infile + ".temporary_outfile"

    infile_handle = open(infile, "rb")
    outfile_handle = open(outfile, "wb")

    regex_tokens = []
    found_tokens = []
    for token in tokens:
        regex_tokens.append(re.compile(token))
        found_tokens.append(False)

    for line in infile_handle:
        line = line.decode(encoding)
        i = 0
        for regex_token in regex_tokens:
            found = regex_token.search(line)
            while found is not None:
                found_tokens[i] = True
                line = line.replace(found.group(0), formats[i].format(values[i]))
                found = regex_token.search(line)
            i += 1

        outfile_handle.write(line.encode(encoding))

    infile_handle.close()
    outfile_handle.close()

    for token, found_token in zip(tokens, found_tokens):
        if not found_token:
            raise EOFError("Error: token '" + token + "' not found!")

    if inplace:
        os.remove(infile)
        shutil.move(outfile, infile)

class OTCalledProcessError(subprocess.CalledProcessError):
    def __str__(self):
        err_msg = (
            (":\n" + self.stderr[:200].decode()) if self.stderr is not None else ""
        )
        return super(OTCalledProcessError).__str__(self) + err_msg

def execute(
    cmd,
    cwd=None,
    shell=False,
    executable=None,
    hide_win=True,
    check=True,
    capture_output=False,
    timeout=None,
    env=None,
):
    """
    Launch an external process.

    Parameters
    ----------
    cmd : str
        Command line to execute, e.g.: "echo 42"
    cwd : str
        Current directory of the executed command.
    shell : bool, default=False
        If set to True, the command is started in a shell (bash).
    executable : str, default=False
        path to the shell. e.g. /bin/zsh.
    hide_win : str, default=True
        Hide cmd.exe popup on windows platform.
    check : bool, default=True
        If set to True: raise RuntimeError if return code of process != 0
    capture_output : bool, default=False
        Whether the output/error streams will be captured
    timeout : int
        Process timeout (Python >=3.3 only)
        On timeout and if psutil is available the children of the process
        are killed before the process itself
    env : dict, default=None
        Environment variables mapping for the new process

    Returns
    -------
    cp : subprocess.CompletedProcess
        Process state info

    Raises
    ------
    RuntimeError
        could not run

    Examples
    --------
    >>> import openturns.coupling_tools as ct
    >>> cp = ct.execute('echo 42', capture_output=True, shell=True)
    >>> cp.returncode
    0
    >>> int(cp.stdout)
    42
    """

    # split cmd if not in a shell before passing it to os.execvp()
    posix = os.name == "posix"
    process_args = cmd if shell else shlex.split(cmd, posix=posix)

    # override startupinfo to hide windows console
    startupinfo = None
    if hide_win and sys.platform.startswith("win"):
        startupinfo = subprocess.STARTUPINFO()
        startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW

    stdout = subprocess.PIPE if capture_output else None
    stderr = subprocess.PIPE if capture_output else None
    process = subprocess.Popen(
        process_args,
        shell=shell,
        cwd=cwd,
        executable=executable,
        stdout=stdout,
        stderr=stderr,
        startupinfo=startupinfo,
        env=env,
    )
    stdout_data = None
    stderr_data = None
    try:
        stdout_data, stderr_data = process.communicate(timeout=timeout)
    except subprocess.TimeoutExpired:
        # kill children is psutil is available
        try:
            import psutil
        except ImportError:
            pass
        else:
            parent = psutil.Process(process.pid)
            for child in parent.children(recursive=True):
                child.kill()
        process.kill()
        raise

    returncode = process.poll()

    # check return code
    if check and returncode:
        raise OTCalledProcessError(returncode, cmd, stdout_data, stderr_data)

    return subprocess.CompletedProcess(
        cmd, returncode, stdout=stdout_data, stderr=stderr_data
    )

def get_regex(filename, patterns, encoding=default_encoding):
    """
    Get values from a file using regex.

    Parameters
    ----------
    filename : str
        The name of the file to parse
    patterns : list of str
        Regex patterns that will permit one to get the values
        see https://docs.python.org/2/library/re.html for available patterns
        The value to be searched must be surrounded by parenthesis.
    encoding : str
        File encoding
        see http://docs.python.org/2/library/codecs.html#codec-base-classes

    Returns
    -------
    results : list of float
        Each value corresponds to each pattern.
        If nothing has been found, the corresponding value is set to None.

    Raises
    ------
    AssertionError
        parameters badly set
    EOFError
        no value found

    Examples
    --------
    >>> import openturns.coupling_tools as ct
    >>> # write an output file
    >>> with open('results.out', 'w') as f:
    ...     count = f.write('@E=-9.5E3')
    >>> # parse file with regex
    >>> ct.get_regex('results.out', patterns=[r"(\\R)"])
    [-9500.0]
    """

    if not isinstance(patterns, list) or len(patterns) == 0:
        raise AssertionError("error: patterns parameter badly set!")

    results = [None] * len(patterns)

    re_patterns = []
    for pattern in patterns:
        # OT-like shortcuts
        pattern = pattern.replace(
            r"\R", r"[+-]? *(?:\d+(?:\.\d*)?|\.\d+)(?:[eE][+-]?\d+)?"
        )
        pattern = pattern.replace(r"\I", r"[+-]? *\d+")

        re_patterns.append(re.compile(pattern))

    file_handle = open(filename, "rb")

    for line in file_handle:
        line = line.decode(encoding)
        i = 0
        for re_pattern in re_patterns:
            match = re_pattern.search(line)
            if match:
                results[i] = float(match.group(1))
            i += 1

    file_handle.close()

    for result, pattern in zip(results, patterns):
        if result is None:
            raise EOFError("error: no pattern (" + pattern + ") found!")

    return results

def get_real_from_line(line):
    """
    Try to get a real value from the beginning of a text line.

    Parameters
    ----------
    line : str
        Line to parse

    Returns
    -------
    value : float or int
        Found value. Raise an exception if nothing found.
    """

    # \S*: spaces are allowed at the beginning of the real
    real_regex = r"\s*[+-]? *(?:\d+(?:\.\d*)?|\.\d+)(?:[eE][+-]?\d+)?"
    re_real = re.compile(real_regex)

    # get the value
    match = re_real.match(line)
    if match:
        result = float(line[match.start(): match.end()])
    else:
        raise EOFError(
            "error: real not found at the beginning of this line: " "(" + line + ")!"
        )

    return result

def read_line(handle, seek=0, encoding=default_encoding):
    """
    Get next line.

    Parameters
    ----------
    seek : int
        if seek < 0: stop reading seek before the end of the file
    encoding : str
        file encoding, see http://docs.python.org/2/library/codecs.html#codec-base-classes
    """
    line = handle.readline().decode(encoding)

    if seek < 0 and line != "":
        if handle.tell() > -seek:
            if debug:
                sys.stderr.write("line before cut ->" + line + "<-\n")

            # cut the end of the line
            line = line[: len(line) - (handle.tell() + seek)]
            # after cut, if the line is empty, add a \n to behave like
            # readline() python function
            if line == "":
                line = "\n"
            # any further read will return ''
            handle.seek(0, os.SEEK_END)

            if debug:
                sys.stderr.write("line after cut ->" + line + "<-\n")

    if debug:
        sys.stderr.write("read_line: ->" + line + "<-\n")

    return line

def get_line_col(
    filename, skip_line=0, skip_col=0, col_sep=None, seek=0, encoding=default_encoding
):
    """
    Get a value at specific line/columns coordinates.

    Parameters
    ----------
    filename : str
        The name of the file that will be parsed
    skip_line : int, default=0
        Number of lines skipped
        If skip_line < 0: count lines backward from the end of the file.
        Be careful: a last empty line is taken into account too!
        Default: 0: no line skipped
    skip_col : int, default=0
        Number of columns skipped from the beginning or end of the line.
        If skip_col < 0: count col backward from the end of the line.
        Default: 0: no column skipped
    col_sep : str
        Column separator
        Default: None: whitespace separator, see str.split
    seek : int, default=0
        if > 0, consider the file starts at pos seek.
        if < 0, consider the file ends at pos -seek (and NOT (end-(-seek))!).
        Default: 0: consider the whole file.
    encoding : str
        File encoding
        see http://docs.python.org/2/library/codecs.html#codec-base-classes

    Returns
    -------
    result : float
        Value to retrieve

    Examples
    --------
    >>> import openturns.coupling_tools as ct
    >>> with open('results.out', 'w') as f:
    ...     count = f.write('1.1 1.2 1.3 1.4')
    >>> ct.get_line_col(filename='results.out', skip_col=2)
    1.3
    """
    check_param(filename, str)
    check_param(skip_line, int)
    check_param(skip_col, int)
    check_param(seek, int)

    handle = open(filename, "rb")
    if seek > 0:
        handle.seek(seek)

    if debug:
        sys.stderr.write(
            "get_line_col(skip_line="
            + str(skip_line)
            + ", skip_col="
            + str(skip_col)
            + ", seek="
            + str(seek)
            + ")\n"
        )

    # skip line backward
    if skip_line < 0:
        # cache position of each beginning of line
        # last elt of the list is the last inserted
        lines_cache = []
        # determine number of elt to cache
        lines_cache_size = -skip_line

        # build lines cache
        previous_pos = handle.tell()
        line = read_line(handle, seek, encoding)
        while line != "":
            # append to cache
            lines_cache.append(previous_pos)
            if len(lines_cache) > lines_cache_size:
                # todo: list not optimized to del before the end
                del lines_cache[0]
            previous_pos = handle.tell()

            line = read_line(handle, seek, encoding)
            if debug:
                sys.stderr.write("lines_cache: " + str(lines_cache) + "\n")

        if len(lines_cache) < lines_cache_size:
            err_msg = (
                "error: the file has less than " + str(lines_cache_size) + " lines!"
            )
            handle.close()
            raise EOFError(err_msg)
        else:
            handle.seek(lines_cache[0])
            line_found = read_line(handle, seek, encoding)
            if debug:
                sys.stderr.write("line found: ->" + line_found + "<-\n")
    # skip line forward
    else:
        while skip_line >= 0:
            line = read_line(handle, seek, encoding)
            if skip_line > 0 and line == "":
                handle.close()
                raise EOFError('error: the file has less lines than "skip_line"!')
            skip_line -= 1
        line_found = line

    handle.close()

    # get the good col
    if skip_col != 0:
        try:
            line_found = line_found.split(col_sep)[skip_col]
        except Exception:
            raise EOFError("error: value not found on this line: (" + line_found + ")!")

    # get the value
    result = get_real_from_line(line_found)

    return result

def get_value(
    filename,
    token=None,
    skip_token=0,
    skip_line=0,
    skip_col=0,
    col_sep=None,
    encoding=default_encoding,
):
    """
    Get a value from a file using a delimiter and/or offsets.

    This function is optimized to be rather fast and takes low memory on human
    readable file.

    Parameters
    ----------
    filename : str
        The name of the file that will be parsed
    token : str
        A regex that will be searched.
        The value right after the token is returned.
        Default: None (no token searched)
    skip_token : int
        The number of tokens that will be skipped before getting
        the value. If set to != 0, the corresponding token parameter must
        not be equal to None.
        If skip_tokens < 0: count tokens backward from the end of the file.
        Default: 0: no token skipped
    skip_line : int
        Number of lines skipped from the token found.
        If corresponding token equal None, skip from the beginning of the file.
        If corresponding token != None, skip from the token.
        If skip_line < 0: count lines backward from the token or from the end
        of the file. Be careful: a last empty line is taken into account too.
        Default: 0: no line skipped
    skip_col : int
        Number of columns skipped from the token found.
        If corresponding token = None, skip words from the beginning
        of the line.
        If corresponding token != None, skip words from the token.
        If skip_col < 0: count col backward from the end of the line or from
        the token.
        Default: 0: no column skipped
    col_sep : str
        Column separator
        Default: None: whitespace separator, see str.split
    encoding : str
        File encoding
        see http://docs.python.org/2/library/codecs.html#codec-base-classes

    Returns
    -------
    value : float
        Value found

    Raises
    ------
    AssertionError
        parameters badly set
    EOFError
        no value found

    Examples
    --------
    using a single token

    >>> import openturns.coupling_tools as ct
    >>> with open('results.out', 'w') as f:
    ...     count = f.write('Y1=2.0, Y2=-6.6E56')
    >>> ct.get_value('results.out', token='Y1=')
    2.0

    using token and skip_tokens

    >>> with open('results.out', 'w') as f:
    ...     count = f.write('Y1=2.6 Y1=6.0 # temperature 2')
    >>> ct.get_value('results.out', token='Y1=', skip_token=1)
    6.0

    using column & line

    >>> with open('results.out', 'w') as f:
    ...     count = f.write('1.1 1.2 1.3 1.4')
    >>> ct.get_value(filename='results.out', skip_col=2)
    1.3
    """
    if debug:
        sys.stderr.write(
            "get_value(token="
            + str(token)
            + ", skip_token="
            + str(skip_token)
            + ", skip_line="
            + str(skip_line)
            + ", skip_col="
            + str(skip_col)
            + ")\n"
        )

    # check parameters
    check_param(filename, str)
    if token is not None:
        check_param(token, str)
    elif skip_token != 0:
        raise AssertionError(
            "error: skip_token parameter needs token " "parameter to be set!"
        )
    check_param(skip_token, int)
    check_param(skip_line, int)
    check_param(skip_col, int)

    result = None
    if not token:
        result = get_line_col(
            filename, skip_line, skip_col, col_sep=col_sep, encoding=encoding
        )
    else:
        handle = open(filename, "rb")

        re_token = re.compile(token)

        # store previous begin of line pos
        line_pos = handle.tell()

        # store token pos [elt 0: start pos of the token, elt 1: end of the
        # token]
        if skip_token >= 0:
            token_pos = None
        else:
            token_pos_cache = []

        line = handle.readline().decode(encoding)
        while line != "":
            for token_match in re_token.finditer(line):
                if skip_token > 0:
                    # token found but it's not the good one
                    skip_token -= 1
                elif skip_token == 0:
                    # token found
                    token_pos = [
                        line_pos + token_match.start(),
                        line_pos + token_match.end(),
                    ]
                    if debug:
                        sys.stderr.write("skip_token == 0, line: " + line + "\n")
                    break
                else:
                    # token wanted in revert order: we first cache them all
                    token_pos_cache.append(
                        [line_pos + token_match.start(), line_pos + token_match.end()]
                    )

            if skip_token >= 0 and token_pos is not None:
                # token found
                break

            line_pos = handle.tell()
            line = handle.readline().decode(encoding)

        # get the token from the cache
        if skip_token < 0 and len(token_pos_cache) >= -skip_token:
            token_pos = token_pos_cache[skip_token]

        if token_pos is None:
            handle.close()
            raise EOFError("error: no token (" + token + ") was found!")

        if skip_line == 0 and skip_col == 0:
            # get the real right after the token
            handle.seek(token_pos[1])
            line = handle.readline().decode(encoding)
            if debug:
                sys.stderr.write("first token, line_found: " + line + "\n")
            result = get_real_from_line(line)
            handle.close()  # fixme: no multiple close?
        else:
            # get the real by skipping from the token
            if skip_line < 0 or (skip_line == 0 and skip_col < 0):
                # search before the token
                seek_pos = -token_pos[0]
                skip_line -= 1  # skip line from the token
            else:
                # search after the token
                seek_pos = token_pos[1]
            handle.close()
            result = get_line_col(
                filename,
                skip_line,
                skip_col,
                col_sep=col_sep,
                seek=seek_pos,
                encoding=encoding,
            )

    return result

def get(
    filename,
    tokens=None,
    skip_tokens=None,
    skip_lines=None,
    skip_cols=None,
    encoding=default_encoding,
):
    """
    Get several values from a file using delimiters and/or offsets.

    This is equivalent to get_value
    Each following parameters must be a list. The first element of each list
    will be used to get the first value, the 2nd ...
    If the parameter is set to None, the list is set to the default value of
    get_value(), i.e skip_lines=None => [0, 0, ...].

    Parameters
    ----------
    filename : str
        The name of the file that will be parsed
    tokens : list of str
        see get_value
    skip_tokens : list of int
        see get_value
    skip_lines : list of int
        see get_value()
    skip_cols : list of int
        see get_value()
    encoding : str
        File encoding, see
        http://docs.python.org/2/library/codecs.html#codec-base-classes

    Returns
    -------
    result : list of float
        Values parsed

    Raises
    ------
    AssertionError
        parameters badly set
    EOFError
        no value found

    Examples
    --------
    using tokens

    >>> import openturns.coupling_tools as ct
    >>> with open('results.out', 'w') as f:
    ...     count = f.write('Y1=2.0, Y2=-6.6E2')
    >>> ct.get(filename='results.out', tokens=['Y1=', 'Y2='])
    [2.0, -660.0]
    """
    # test parameters and determine the number of value to return
    nb_values = None
    if tokens is not None:
        nb_values = len(tokens)

    err_msg = "error: skip_tokens parameter needs tokens parameter to be set!"
    if skip_tokens is not None:
        if nb_values is None:
            raise AssertionError(err_msg)
        elif nb_values != len(skip_tokens):
            raise AssertionError(err_msg)

    if skip_lines is not None:
        if nb_values is None:
            nb_values = len(skip_lines)
        elif nb_values != len(skip_lines):
            raise AssertionError(err_msg)

    if skip_cols is not None:
        if nb_values is None:
            nb_values = len(skip_cols)
        elif nb_values != len(skip_cols):
            raise AssertionError(err_msg)

    if nb_values is None:
        raise AssertionError("error: no parameters have been set!")

    # init properly every list
    if tokens is None:
        tokens = [None] * nb_values
    if skip_tokens is None:
        skip_tokens = [0] * nb_values
    if skip_lines is None:
        skip_lines = [0] * nb_values
    if skip_cols is None:
        skip_cols = [0] * nb_values

    results = [None] * nb_values

    # possible optimization: get values in one pass
    for n in range(nb_values):
        results[n] = get_value(
            filename,
            tokens[n],
            skip_tokens[n],
            skip_lines[n],
            skip_cols[n],
            encoding=encoding,
        )

    return results


import sys
import time
import mdi
try:
    import numpy
    use_numpy = True
except ImportError:
    use_numpy = False
try:
    from mpi4py import MPI
    use_mpi4py = True
except ImportError:
    use_mpi4py = False

niterations = 10000
natoms = 10
get_coords = False

# initialize MPI
if use_mpi4py:
    mpi_world = MPI.COMM_WORLD
else:
    mpi_world = None

# get the Angstrom to Bohr conversion factor
conversion_factor = mdi.MDI_Conversion_Factor("angstrom","bohr")
print("Conversion Factor: " + str(conversion_factor))

# initialize the socket
mdi.MDI_Init(sys.argv[2],mpi_world)
if use_mpi4py:
    mpi_world = mdi.MDI_Get_Intra_Code_MPI_Comm()
    world_rank = mpi_world.Get_rank()
else:
    world_rank = 0

# connect to the production codes
ncodes = 1
if world_rank == 0:
    for icode in range(ncodes):
        comm = mdi.MDI_Accept_Communicator()

        # get the name of the code
        mdi.MDI_Send_Command("<NAME", comm)
        name = mdi.MDI_Recv(mdi.MDI_NAME_LENGTH, mdi.MDI_CHAR, comm)
        print('Received communicator: ' + str(name))

        if name.strip() == 'PONG' or name.strip() == "PONG_F90" or name.strip() == "PONG_Py":
            mm_comm = comm
        else:
            raise ValueError('Production code name not recognized')

if world_rank == 0:

    initial_time = time.clock();

    for iiter in range(niterations):

        # get the MM energy
        mdi.MDI_Send_Command("<NAME", comm)
        name = mdi.MDI_Recv(mdi.MDI_NAME_LENGTH, mdi.MDI_CHAR, comm)

        # get the MM coordinates
        if get_coords:
            mdi.MDI_Send_Command("<COORDS", comm)
            if use_numpy:
                coords = mdi.MDI_Recv(3*natoms, mdi.MDI_DOUBLE_NUMPY, comm)
            else:
                coords = mdi.MDI_Recv(3*natoms, mdi.MDI_DOUBLE, comm)

        if iiter % 1000 == 0:
            print("Iteration: " + str(iiter) + "   " + name )

    # close the production codes
    mdi.MDI_Send_Command("EXIT", mm_comm)

    wall_time = time.clock() - initial_time
    if get_coords:
        print( str(coords) )
    print("Ping-pong time: " + str(wall_time))
    print("   us: " + str(1000000.0*wall_time/(1.0*(2*niterations))))


import os
import disnake
from disnake.ext import tasks, commands
from disnake import ApplicationCommandInteraction

from helpers import checks
from helpers.save import get_save

import googleapiclient.discovery
import json
import datetime

if not os.path.isfile("config.json"):
    sys.exit("'config.json' not found! Please add it and try again.")
else:
    with open("config.json") as file:
        config = json.load(file)

api_service_name = "youtube"
api_version = "v3"

class YoutubeCom(commands.Cog, name=__name__):
    def __init__(self, bot):
        self.bot = bot
        self.check_task.start()

    def cog_unload(self):
        self.check_task.cancel()

    @commands.slash_command(
        name="youtubeadd",
        description="Add a youtube channel to the alert list",
        guild_ids=[797183317115142196, 715201231252881428],
    )
    @checks.not_blacklisted()
    @commands.has_permissions(manage_messages=True)
    async def youtubeadd(self, inter: ApplicationCommandInteraction, youtube_channel_id: str, discord_channel: disnake.abc.GuildChannel = None):
        save = get_save(inter.guild_id, __name__)
        if not save["USERS_ID"]:
            save["USERS_ID"] = []
        if not discord_channel:
            discord_channel = inter.channel
        save["USERS_ID"].append(youtube_channel_id)
        save["CHANNEL_ID"] = discord_channel.id
        save.save()
        await inter.response.send_message(f"added user {youtube_channel_id}")
        await self.check_channel(save, youtube_channel_id, discord_channel.id)
        #self.check_task.restart()

    @commands.slash_command(
        name="youtubeaddfilter",
        description="filter lines in the description to add to the notification",
        guild_ids=[797183317115142196, 715201231252881428],
    )
    @checks.not_blacklisted()
    @commands.has_permissions(manage_messages=True)
    async def youtubeaddfilter(self, inter: ApplicationCommandInteraction, line_filter: str):
        save = get_save(inter.guild_id, __name__)
        if not save["FILTERS"]:
            save["FILTERS"] = []
        save["FILTERS"].append(line_filter)
        save.save()
        await inter.response.send_message(f"added filter {line_filter}")

    @commands.slash_command(
        name="emojiadd",
        description="Add a youtube channel to the alert list",
        guild_ids=[797183317115142196, 715201231252881428],
    )
    @checks.not_blacklisted()
    @commands.has_permissions(manage_messages=True)
    async def emojiadd(self, inter: ApplicationCommandInteraction, emoji: str, channel: disnake.abc.GuildChannel):
        save = get_save(inter.guild_id, __name__)
        if not save["REACTIONS"]:
            save["REACTIONS"] = {}
        save["REACTIONS"][emoji] = channel.id
        save.save()
        await inter.response.send_message(f"added reaction {emoji}")

    @commands.slash_command(
        name="youtubecheck",
        description="Refresh youtube list",
        guild_ids=[797183317115142196, 715201231252881428],
    )
    @checks.not_blacklisted()
    @commands.has_permissions(manage_messages=True)
    async def youtubecheck(self, inter: ApplicationCommandInteraction):
        await inter.response.send_message(f"refreshing")
        save = get_save(inter.guild_id, __name__)
        if save:
            users = save['USERS_ID'] or []
            try:
                for user in users:
                    await self.check_channel(save, user_id=user, channel_id=save['CHANNEL_ID'])
                save["LAST_TIME"] = datetime.datetime.now(tz=datetime.timezone.utc).isoformat()
                save.save()
            except Exception as e:
                print(inter.guild_id, e)

    async def check_channel(self, save = None, user_id = 0, channel_id = 0, post_message=True):
        cached_runs = {}
        emojis = {}
        if save:
            cached_runs = save['CACHED_RUNS'] or {}
            emojis = save["REACTIONS"] or {}
        if not user_id:
            print("no user")
            return
        channel = self.bot.get_channel(channel_id)
        if not channel:
            print("no channel")
            return
        os.environ["OAUTHLIB_INSECURE_TRANSPORT"] = "1"
        youtube = googleapiclient.discovery.build(api_service_name, api_version, developerKey = config["youtube_api"])
        request = youtube.activities().list(
            part="snippet,contentDetails",
            channelId=user_id,
            maxResults=3
        )
        response = request.execute()
        for i in response["items"]:
            if i["snippet"]["type"] == "upload":
                id = i["contentDetails"]["upload"]["videoId"]
                if id not in cached_runs:
                    if not post_message:
                        cached_runs[id] = -1
                        continue
                    time = datetime.datetime.fromisoformat(i["snippet"]["publishedAt"])
                    if save["LAST_TIME"] and datetime.datetime.fromisoformat(save["LAST_TIME"]) > time:
                        continue
                    title = i["snippet"]["title"]
                    user = i["snippet"]["channelTitle"]
                    description = i["snippet"]["description"]
                    thumb = i["snippet"]["thumbnails"]["medium"]["url"]
                    link = f"http://youtube.com/watch?v={id}"

                    e = disnake.Embed(
                        title=f"**{title} ({user})**",
                        url=f"{link}",
                    )

                    if save["FILTERS"]:
                        lines = description.splitlines()
                        data = []
                        for l in lines:
                            for f in save["FILTERS"]:
                                if f in l:
                                    data.append(l)
                                    break
                        e.description = "\n".join(data)

                    e.set_thumbnail(url=thumb)
                    message = await channel.send(content=f"**{user}** *({time.time()})*", embed=e)
                    for e in emojis:
                        await message.add_reaction(e)
                    cached_runs[id] = message.id
        if save:
            save['CACHED_RUNS'] = cached_runs
            save.save()

    @tasks.loop(minutes=15.0)
    async def check_task(self) -> None:
        print("checking Youtube")
        for guild in self.bot.guilds:
            save = get_save(guild.id, __name__)
            users = save['USERS_ID'] or []
            try:
                for user in users:
                    await self.check_channel(save, user_id=user, channel_id=save['CHANNEL_ID'])
                save["LAST_TIME"] = datetime.datetime.now(tz=datetime.timezone.utc).isoformat()
                save.save()
            except Exception as e:
                print(guild, e)

    @check_task.before_loop
    async def before_printer(self):
        await self.bot.wait_until_ready()

    @commands.Cog.listener()
    async def on_raw_reaction_add(self, payload : disnake.RawReactionActionEvent) -> None:
        guild_id = payload.guild_id
        if payload.user_id == self.bot.user.id:
            return
        save = get_save(guild_id, __name__)
        if not save:
            return
        if save["CACHED_RUNS"] and save["REACTIONS"] and payload.message_id in save["CACHED_RUNS"].values() and payload.emoji.name in save["REACTIONS"]:
            try:
                src_channel = self.bot.get_channel(payload.channel_id)
                dst_channel = self.bot.get_channel(save["REACTIONS"][payload.emoji.name])
                message = await src_channel.fetch_message(payload.message_id)
                await dst_channel.send(content=message.content, embed=message.embeds[0])
            except Exception as e:
                print(e)

def setup(bot):
    s = YoutubeCom(bot)
    bot.add_cog(s)


# Code written to reproduce and repurpose what I already found online at:
    # https://stackoverflow.com/questions/42329766/python-nlp-british-english-vs-american-english
    # https://gist.github.com/rcortini/0d05417339bc74300ce3a971442a4d3c
    # https://github.com/hyperreality/American-British-English-Translator/blob/master/data/british_spellings.json

    # Credits to the creators of those SO answer and the dictionary!

######### OPTION 1: Use online version of the data #########

import requests

def americanize(string):
    url ="https://raw.githubusercontent.com/hyperreality/American-British-English-Translator/master/data/british_spellings.json"
    british_to_american_dict = requests.get(url).json()

    for british_spelling, american_spelling in british_to_american.items():
        string = string.replace(british_spelling, american_spelling)

    return string

def britishize(string):
    url ="https://raw.githubusercontent.com/hyperreality/American-British-English-Translator/master/data/american_spellings.json"
    american_to_british_dict = requests.get(url).json()

    for american_spelling, british_spelling in american_to_british.items():
        string = string.replace(american_spelling, british_spelling)

    return string

######### OPTION 2: Use local version of the data #########

# Read in the JSON from wherever it is locally
def replace_all(text, mydict):
    for gb, us in mydict.items():
        text = text.replace(us, gb)
    return text

# Example
gb_text = "I realise I can see the world in colour but I can't vocalise its splendour"
us_text = replace_all(gb_text, us2gb)


# -*- coding: utf-8 -*-
# +
import requests
import feast
import pandas as pd
from feast import Entity, Feature, FeatureView, FileSource, ValueType
import torch
import pymysql
from datetime import date
from sqlalchemy import create_engine
import numpy as np
pymysql.install_as_MySQLdb()
import MySQLdb
import math

import warnings
warnings.filterwarnings("ignore")

# -

def get_entities(path_,from_time_):
    parquet_ = pd.read_parquet(path_, engine='pyarrow')
    orders = parquet_[['ticket_id','event_timestamp']]

    ### 뽑을 rows의 entity == key 라고 볼 수 있음
    new_orders = orders[orders['event_timestamp']>= from_time_]
    new_orders.drop_duplicates(ignore_index=True, inplace=True)
    return new_orders

def get_df_from_online(feast_path_, entity_df_):
    fs_ = feast.FeatureStore(repo_path=feast_path_)

    online_ = fs_.get_online_features(
                entity_rows=[{"ticket_id": i} for i in entity_df_['ticket_id']],
                features=[
                    "dr_lauren_stat:time",
                    "dr_lauren_stat:weekday",
                    "dr_lauren_stat:weekend",
                    "dr_lauren_stat:instlo_1",
                    "dr_lauren_stat:instlo_2",
                    "dr_lauren_stat:inst_code",
                    "dr_lauren_stat:sysname_lo",
                    "dr_lauren_stat:sysname_eq",
                    "dr_lauren_stat:ntt_label",
            ],
    )

    df = pd.DataFrame.from_dict(online_.to_dict())

    return df[['time', 'weekday', 'weekend', 'instlo_1', 'instlo_2', 'inst_code', 'sysname_lo', 'sysname_eq','ntt_label']]

# ### request

# +
def get_from_bentoml(online_df_):
    response = requests.post("http://127.0.0.1:5000/predict", data=online_df_.iloc[:,:-1].to_json())
    infer_ = response.text
    predict_ = infer_[1:-1].split(', ')
    return predict_

def get_infer_df_(entity_df_, online_df_):
    infer_df = entity_df_.reset_index(drop=True)
    infer_df['label'] = online_df_['ntt_label']
    infer_df['predict'] = get_from_bentoml(online_df_)
    return infer_df

# +
### data create, insert, update
def get_commit_sql(conn_, sql_):
    with conn_.cursor() as cursor:
        cursor.execute(sql_)
        result = cursor.fetchall()
        conn.commit()

### data read
def get_data_sql(conn_, sql_):
    with conn_.cursor() as cursor:
        cursor.execute(sql_)
        result = cursor.fetchall()
        return result

# -

# ### create table

# +
# sql = '''
#     CREATE TABLE infer_df (
#         ticket_id int(11) NOT NULL AUTO_INCREMENT PRIMARY KEY,
#         event_timestamp varchar(255) NOT NULL,
#         label int(11) NOT NULL,
#         predict int(11) NOT NULL
#     ) ENGINE=InnoDB DEFAULT CHARSET=utf8
# '''
# get_commit_sql(conn, sql)
# -

def get_columns_(df_):
    columns_str = ', '.join([i for i in infer_df.columns])
    return columns_str

def get_values_(df_):
    value_ = []
    for row_ in df_.values:
        value_.append("("+", ".join(["'"+str(i)+"'" for i in row_]) + ")")

    values_str = ', '.join(value_)
    return values_str

def pred_into_db(conn_, infer_df_):
    columns_ = get_columns_(infer_df_)

    values_ = get_values_(infer_df_)

    sql = f"INSERT INTO infer_df ({columns_}) VALUES {values_} ;"
    get_commit_sql(conn_, sql)

def main():
    path = 'workspace/ML_Ops/feast_fea_/data/ppr_data_.parquet'
    from_time = '2021-07-01'

    entity_df = get_entities(path, from_time)

    feast_path = './'
    online_df = get_df_from_online(feast_path, entity_df)

    infer_df = get_infer_df_(entity_df, online_df)

    conn = pymysql.connect(host='localhost', port=3306, user='root', bind_address='127.0.0.1', passwd='', db='infer_db', charset='utf8')

    pred_into_db(conn,infer_df)

main()


import cv2
import numpy as np

image_obj = cv2.imread(r"C:\Users\User\Desktop\Feature Maps\ALLSTIMULI\ALLSTIMULI\i1252592787.jpeg")

gray = cv2.cvtColor(image_obj, cv2.COLOR_BGR2GRAY)

kernel = np.ones((4, 4), np.uint8)
dilation = cv2.dilate(gray, kernel, iterations=1)

blur = cv2.GaussianBlur(dilation, (5, 5), 0)

thresh = cv2.adaptiveThreshold(blur, 255, 1, 1, 11, 2)

# Now finding Contours         ###################
contours, _ = cv2.findContours(thresh, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)

coordinates = []

for cnt in contours:
    area = cv2.contourArea(cnt)
    if area > 1000:
        approx = cv2.approxPolyDP(
            cnt, 0.07 * cv2.arcLength(cnt, True), True)
        if len(approx) == 3:
            coordinates.append([cnt])
            cv2.drawContours(image_obj, [cnt], 0, (0, 0, 255), 3)

cv2.imshow("result.png", image_obj)
cv2.waitKey(0)


import numpy as np
import pandas as pd
import os
import math
import matplotlib.pyplot as plt
import csv
import functions

# Eastern / Central [0]
# Tap Mun [1]
# Tsuen Wan [2]
# Tung Chung [3]
# Yuen Long [4]
# Kwai Chung [5]
######################
# Kwun Tong [6]
# Macau [7]
# Sha Tin [8]
# Sham Shui Po [9]
filelist = ['Eastern', 'Tap Mun', 'Tsuen Wan', 'Tung Chung', 'Yuen Long', 'Kwai Chung', 'Kwun Tong', 'Macau', 'Sha Tin', 'ShamShuiPo']
subplot_matrix = []
o3_standard = 60  # ppbv

def Wind_Direction(station):
    if station == 1:  # Tap Mun
        return
    file = filelist[station]
    directory = "Regression Result/"
    N = []
    NE = []
    E = []
    SE = []
    S = []
    SW = []
    W = []
    NW = []
    df = pd.read_csv(directory + filelist[station] + '.csv')  # No headers
    df = df.sort_values(by='Wind Direction')
    df = np.array(df)
    timestamp = df[:, 0]
    if station != 0:  # Skip Central
        # May - October
        indices = []
        for time in range(len(timestamp)):
            month = int(timestamp[time].split('/')[1])
            if month < 5 or month > 10:
                indices.append(time)
        df = np.delete(df, indices, axis=0)  # Delete Rows if not May - October
    wd = df[:, 5]
    pm_short = df[:, -1]
    print("\n")
    print(filelist[station])
    print('min', min(wd), 'max', max(wd))
    x_axis = wd
    y = pm_short

    # Scatter Plot
    plt.scatter(x_axis, y)
    # plt.show()

    for count in range(len(x_axis)):
        adjust = 45/2
        if x_axis[count] < 45 - adjust:
            x_axis[count] = 'N'
            N.append(y[count])
        elif x_axis[count] < 90 - adjust:
            x_axis[count] = 'NE'
            NE.append(y[count])
        elif x_axis[count] < 135 - adjust:
            x_axis[count] = 'E'
            E.append(y[count])
        elif x_axis[count] < 180 - adjust:
            x_axis[count] = 'SE'
            SE.append(y[count])
        elif x_axis[count] < 225 - adjust:
            x_axis[count] = 'S'
            S.append(y[count])
        elif x_axis[count] < 270 - adjust:
            x_axis[count] = 'SW'
            SW.append(y[count])
        elif x_axis[count] < 315 - adjust:
            x_axis[count] = 'W'
            W.append(y[count])
        elif x_axis[count] < 360 - adjust:
            x_axis[count] = 'NW'
            NW.append(y[count])
        else:
            x_axis[count] = 'N'
            N.append(y[count])

    # Calculate Probability
    direction = [N, NE, E, SE, S, SW, W, NW]
    x_axis = ['N', 'NE', 'E', 'SE', 'S', 'SW', 'W', 'NW']
    y_axis = []
    for i in range(len(direction)):
        number = 0
        total = len(direction[i])
        for j in range(len(direction[i])):
            if math.exp(direction[i][j]) > 1:
                number += 1
        if total == 0:
            probability = 0
        else:
            probability = number/total
        y_axis.append(probability)
        print(x_axis[i], round(probability * 100, 4), '%')

    # Import Preprocessed WD Data
    df2 = pd.read_csv('preprocessed/' + filelist[station] + ' data.csv')
    df2 = np.array(df2)
    wd_raw = df2[:, -1]
    wd_time = df2[:, 0]
    o3_time = []
    o3_raw = []
    # Import Raw O3 Data
    if file == 'Eastern' or file == 'Kwai Chung' or file == 'Tung Chung' or file == 'Yuen Long':
        directory_o3 = "Supplement/O3-KZ/"
        with open(directory_o3 + file + ".csv") as infile:
            reader = csv.reader(infile)
            for rows, value in enumerate(reader):
                if rows > 3:
                    o3_time.append(value[0])
                    o3_raw.append(float(value[2]))
    else:
        directory_o3 = "Supplement/O3-KZ/O3-supplement/"
        with open(directory_o3 + file + "-1.csv") as infile:
            reader = csv.reader(infile)
            for rows, value in enumerate(reader):
                if rows > 3:
                    o3_time.append(value[0])
                    o3_raw.append(float(value[2]))
        with open(directory_o3 + file + "-2.csv") as infile:
            reader = csv.reader(infile)
            for rows, value in enumerate(reader):
                if rows > 3:
                    o3_time.append(value[0])
                    o3_raw.append(float(value[2]))
    # Macau: 2003-2015
    if station == 7:
        o3_time = o3_time[:-17543]
        o3_raw = o3_raw[:-17543]
    # Make Positive
    functions.makePositive(o3_raw)
    print("WD", min(wd_raw), max(wd_raw), "Ozone (O3)", min(o3_raw), max(o3_raw))

    # Initialize matrix full of zeros
    data_frame = [[0 for x in range(3)] for y in range(len(o3_time))]
    # First Column
    for row in range(len(data_frame)):
        data_frame[row][0] = o3_time[row]
    data_frame = np.array(data_frame)
    data_frame[:, 1] = o3_raw
    a = 0  # WD Timestamp's row
    b = 0  # O3 row
    column = wd_time  # [timestamp, value]
    # Match the timestamp between pm and wd
    while b < len(o3_time) and a < len(column):
        if column[a] != o3_time[b]:
            a -= 1
        else:
            data_frame[b][2] = wd_raw[a]  # Fill in the result matrix
        a += 1
        b += 1

    # Delete unnecessary rows in data frame
    delete_index = 0
    while float(data_frame[delete_index][2]) <= 0:
        delete_index += 1
    data_frame = data_frame[delete_index:]
    # May - October
    indices = []
    timestamp = data_frame[:, 0]
    for time in range(len(data_frame)):
        month = int(timestamp[time].split('/')[1])
        if month < 5 or month > 10:
            indices.append(time)
    data_frame = np.delete(data_frame, indices, axis=0)  # Delete Rows if not May - October

    # Set the directory
    directory = "ST Variation and WD/"
    if not os.path.exists(directory):
        os.makedirs(directory)

    # Write csv file of result matrix inside preprocessed folder
    with open(directory + str(filelist[station]) + ' WD data.csv', "w") as output:
        writer = csv.writer(output, lineterminator='\n')
        for val in data_frame:
            writer.writerow(val)

    N = []
    NE = []
    E = []
    SE = []
    S = []
    SW = []
    W = []
    NW = []
    x_axis = data_frame[:, 2]
    y = np.asfarray(data_frame[:, 1], float)
    for count in range(len(data_frame)):
        adjust = 45/2
        if float(x_axis[count]) < 45 - adjust:
            x_axis[count] = 'N'
            N.append(y[count])
        elif float(x_axis[count]) < 90 - adjust:
            x_axis[count] = 'NE'
            NE.append(y[count])
        elif float(x_axis[count]) < 135 - adjust:
            x_axis[count] = 'E'
            E.append(y[count])
        elif float(x_axis[count]) < 180 - adjust:
            x_axis[count] = 'SE'
            SE.append(y[count])
        elif float(x_axis[count]) < 225 - adjust:
            x_axis[count] = 'S'
            S.append(y[count])
        elif float(x_axis[count]) < 270 - adjust:
            x_axis[count] = 'SW'
            SW.append(y[count])
        elif float(x_axis[count]) < 315 - adjust:
            x_axis[count] = 'W'
            W.append(y[count])
        elif float(x_axis[count]) < 360 - adjust:
            x_axis[count] = 'NW'
            NW.append(y[count])
        else:
            x_axis[count] = 'N'
            N.append(y[count])

    # Calculate Probability
    direction = [N, NE, E, SE, S, SW, W, NW]
    x_axis = ['N', 'NE', 'E', 'SE', 'S', 'SW', 'W', 'NW']
    y_axis2 = []
    for i in range(len(direction)):
        number = 0
        total = len(direction[i])
        for j in range(len(direction[i])):
            if direction[i][j] > o3_standard:
                number += 1
        if total == 0:
            probability = 0
        else:
            probability = number / total
        y_axis2.append(probability)
        # print(x_axis[i], round(probability * 100, 4), '%')

    # Draw Graphs
    fig = plt.figure()  # Create a figure
    title = filelist[station]
    plt.plot(x_axis, y_axis, label='exp[O3 ST] > 1')
    plt.plot(x_axis, y_axis2, label='O3 > ' + str(o3_standard) + 'ppbv')
    plt.grid(True)
    subplot_matrix.append([x_axis, y_axis, y_axis2])
    plt.title(title)
    plt.xlabel("Wind Direction")
    plt.ylabel("Probability")
    plt.legend(loc='best')
    fig.savefig(directory + title + ".png")
    # plt.show()

for file in range(len(filelist)):
    Wind_Direction(file)

# subplot
fig = plt.figure(figsize=(13, 10))
for file in range(len(filelist)):
    titles = filelist[file]
    if file < 1:
        print("\n")
        print(titles)
        data = subplot_matrix[file]
        ax = plt.subplot(3, 3, file+1)
    elif file != 1:
        print(titles)
        data = subplot_matrix[file-1]
        ax = plt.subplot(3, 3, file)
    if file != 1:
        ax.plot(data[0], data[1], label='exp[O3 ST] > 1')
        ax.plot(data[0], data[2], label='O3 > ' + str(o3_standard) + 'ug/m3')
        # ax.xaxis.set_major_locator(plt.MaxNLocator(5))  # Set Maximum number of x-axis values to show
        plt.title(titles, fontsize=16)
        plt.xlabel("Wind Direction")
        plt.ylabel("Probability")
        ax.legend(loc='best')
        ax.grid(True)

plt.tight_layout()
directory = "ST Variation and WD/"
fig.savefig(directory + 'A Combined Subplot.png')


import random
import numpy as np
import torch
from torch import Tensor
import torch.nn as nn
from torch.nn import Parameter
import torch.nn.functional as F
from tqdm import tqdm

class Node2Vec:
    def __init__(self, G, dim=128):
        self.G = G
        self.dim = dim
        self.device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
        self.Embeddings = torch.zeros(self.G.n, self.dim).to(self.device)
        self.Embeddings.requires_grad = False
        nn.init.normal_(self.Embeddings, mean=0.0, std=1.0/(self.dim**0.5))
        self.UnigramTable = list()
        print("InitUnigramTable...")
        self.initUnigramTable()
        self.ridx = 0

    def initUnigramTable(self, size=10000000):
        count = [len(self.G.A[i])+1 for i in range(self.G.n)]
        count = np.array(count) ** 0.75
        total_pow = np.sum(count)
        i = 0
        prefix_sum = count[i] / total_pow
        for idx in tqdm(range(size)):
            self.UnigramTable.append(i)
            if idx > prefix_sum * size:
                i += 1
                if i >= self.G.n:
                    i = self.G.n - 1
                prefix_sum += count[i] / total_pow
        print('shuffling...')
        random.shuffle(self.UnigramTable)

    def training(self, max_iter=100, negative=100, rewalk=True, weight_decay=1e-4):
        unisize = len(self.UnigramTable)
        lr = 0.01
        iter_gradient_norm = 0
        for iter in tqdm(range(max_iter)):
            iter_length = 0
            if rewalk:
                print('Random walking...')
                self.G.clean_walk()
                for i in tqdm(range(self.G.n)):
                    self.G.perform_walk(i, 2, 0.5, 20)
                iter_length = len(self.G.Walks)
            else:
                iter_length = self.G.n
            random.shuffle(self.G.Walks)

            for walk in tqdm(self.G.Walks[:iter_length]):
                u = walk[0]
                embedu = self.Embeddings[u]

                idx_p = torch.tensor(walk[1:], dtype=torch.int64).to(self.device)
                embedvs_p = torch.index_select(self.Embeddings, 0, idx_p)
                p_p = 1.0 - torch.sigmoid(torch.matmul(embedvs_p, embedu))

                # negative sample
                neighbors = list()
                count = 0
                while count < negative:
                    v = self.UnigramTable[self.ridx]
                    if not v in self.G.A_set[u]:
                        neighbors.append(v)
                        count += 1
                    self.ridx += 1
                    if self.ridx >= len(self.UnigramTable):
                        self.ridx = 0

                idx_n = torch.tensor(neighbors, dtype=torch.int64).to(self.device)
                embedvs_n = torch.index_select(self.Embeddings, 0, idx_n)
                p_n = 1.0 - torch.sigmoid(-torch.matmul(embedvs_n, embedu))

                # gradient descent
                dEu = lr * (torch.matmul(p_p, embedvs_p) - torch.matmul(p_n, embedvs_n)) - weight_decay * embedu
                self.Embeddings[u] += dEu
                self.Embeddings.index_add_(0, idx_p, lr * (p_p.view(-1, 1) * (embedu - dEu)) - weight_decay * embedvs_p)
                self.Embeddings.index_add_(0, idx_n, -lr * (p_n.view(-1, 1) * (embedu - dEu)) - weight_decay * embedvs_n)

                if (iter + 1) % 10 == 0:
                    iter_gradient_norm += (torch.norm(dEu) / lr).item()

            if (iter + 1) % 25 == 0:
                lr *= 0.5
            if (iter + 1) % 10 == 0:
                print('iter : {} ; average_gradient_norm : {}'.format(iter+1,
                    iter_gradient_norm / iter_length))
                iter_gradient_norm = 0

    def save_embeddings(self, pth):
        torch.save(self.Embeddings, pth)


from os import name
import sys
import xml.etree.ElementTree as ET
from xml.dom import minidom

import warnings
warnings.filterwarnings("ignore", category=DeprecationWarning)

class DFA:
    Q            = [[0, "q0"], [1, "q1"], [2, "q2"], [3, "q3"], [4, "q4"]]                           ## list of states as ID and name
    sigma        = [0, 1]                        ## list of alphabet
    delta        = [                            ## transition function list of sets in format start id, input code, end id
                    [0,0,0],
                    [0,1,1],
                    [1,0,1],
                    [1,1,2],
                    [2,0,2],
                    [2,1,3],
                    [3,0,3],
                    [3,1,4],
                    [4,0,4],
                    [4,1,4]

                    ]
    q0           = 0                            ## start state as ID
    F            = [3]                          ## accept states
    currentState = q0                           ## this starts on q0 and will be changed as the dfa is calculated

    def genXML(self):

        root = ET.Element("structure")
        automaton = ET.Element("automaton")
        root.append(automaton)
        for thing in self.Q:
            state = ET.Element("state")
            state.set("id", str(thing[0]))
            state.set("name", thing[1])
            automaton.append(state)
            if thing[0] == self.q0:
                initialTagger = ET.Element("initial")
                state.append(initialTagger)
            if thing[0] in self.F:
                finalTagger = ET.Element("final")
                state.append(finalTagger)
        for transSet in self.delta:
            transition = ET.Element("transition")
            fromTag = ET.Element("from")
            toTag = ET.Element("to")
            readTag = ET.Element("read")
            fromTag.text = str(transSet[0])
            toTag.text = str(transSet[2])
            readTag.text = str(transSet[1])
            transition.append(fromTag)
            transition.append(toTag)
            transition.append(readTag)
            automaton.append(transition)

        tree = ET.ElementTree(root)
        with open ("genned.xml", "wb") as files :
            tree.write(files)
        xmlstr = ET.tostring(element=automaton, encoding='unicode', method='xml')
        print(xmlstr)

myDFA = DFA()

myDFA.genXML()


import pandas as pd
from bokeh.models import BasicTicker, ColorBar, LinearColorMapper
from bokeh.palettes import Blues
from bokeh.plotting import figure
from bokeh.transform import transform
from sklearn import datasets
from sklearn.metrics import confusion_matrix

from dara.core import DataVariable, DerivedVariable, Variable, py_component
from dara.core.definitions import ComponentInstance
from dara.components import Stack, Bokeh, Grid, Text, Select, Heading

from dara_interactivity.custom_components import information_button, code_modal
from dara_interactivity.definitions import (
    TASK_WITH_PROGRESS_BAR_CODE, DERIVED_VARIABLE_WITH_TASK_CODE, HYPERPARAMETERS_CODE
)
from dara_interactivity.tasks import grid_search

@py_component(track_progress=True)
def confusion_matrix_plot(results: tuple, target_names) -> ComponentInstance:
    """
    Constructs a confusion matrix plot from the results of a Support Vector Machine prediction

    :param results: tuple of arrays representing the true and predicted values
    :param target_names: the names representing the different classes of the classification dataset
    :return ComponentInstance
    """
    truth, predictions = results
    df = pd.DataFrame(confusion_matrix(truth, predictions), index=target_names, columns=target_names)
    df.index.name = 'Actual'
    df.columns.name = 'Prediction'
    df = df.stack().rename('value').reset_index()

    mapper = LinearColorMapper(
        palette=Blues[9], low=df.value.min(), high=df.value.max()
    )
    p = figure(title='Confusion Matrix', sizing_mode='stretch_both',
               toolbar_location=None, x_axis_label='Predicted', y_axis_label='Actual',
               x_axis_location='above', x_range=target_names, y_range=target_names[::-1])
    p.rect(x='Actual', y='Prediction', width=1, height=1, source=df, line_color=None,
           fill_color=transform('value', mapper))

    color_bar = ColorBar(color_mapper=mapper, location=(0, 0), label_standoff=10,
                         ticker=BasicTicker(desired_num_ticks=3))
    p.add_layout(color_bar, 'right')

    return Bokeh(p, height='99%')

def expensive_calculations_page() -> ComponentInstance:
    """
    Constructs the layout of the page.

    The page displays a some instructional text, along with modals displaying code snippets.

    :return: ComponentInstance
    """
    # Load data from sklearn
    iris = datasets.load_iris()
    target_names = iris.target_names
    data = pd.DataFrame(iris.data, columns=iris.feature_names)
    data['species'] = iris.target

    # Define hyperparameter Variables
    kernel_var = Variable(['linear', 'poly', 'rbf', 'sigmoid'])
    c_var = Variable([0.001, 0.005, 0.01, 0.05, 0.1, 0.5, 1, 5])
    gamma_var = Variable([5, 1, 0.1, 0.05, 0.01, 0.005, 0.001, 0.0005, 0.0001])
    results = DerivedVariable(
        grid_search,
        variables=[DataVariable(data), kernel_var, c_var, gamma_var],
        run_as_task=True
    )

    # Define Variables that show/hide the code modals
    show_hyperparameters_instantiation = Variable(False)
    show_dv_task_instantiation = Variable(False)
    show_progress_bar_implementation = Variable(False)

    return Stack(
        Heading('Heavy CPU Bound Calculations'),
        Text('Click on the ? buttons to show the underlying code in which the information is referring to.', italic=True),
        Grid(
            Grid.Row(
                Grid.Column(
                    Text('CPU bound calculations are calculations that require intensive processing. \
                          Typically this will include machine learning calculations or expensive data manipulations. \
                          Due to Python\'s Global Interpreter Lock, it helps to offload heavy calculations to \
                          seperate processes. Offloading computation to another process can get complicated \
                          fairly quickly, so Dara provides an easy way define certain functions as tasks \
                          that can be offloaded to another process outside the main one running your web app.'),
                    span=11
                ),
            ),
            padding='10px'
        ),
        Grid(
            Grid.Row(
                Grid.Column(
                    Stack(
                        Stack(
                            Text('Kernel:', width='7%'),
                            Select(
                                items=['linear', 'poly', 'rbf', 'sigmoid'],
                                value=kernel_var,
                                multiselect=True
                            ),
                            direction='horizontal'
                        ),
                        Stack(
                            Text('C:', width='7%'),
                            Select(
                                items=[0.001, 0.005, 0.01, 0.05, 0.1, 0.5, 1, 5],
                                value=c_var,
                                multiselect=True
                            ),
                            direction='horizontal'
                        ),
                        Stack(
                            Text('Gamma:', width='7%'),
                            Select(
                                items=[5, 1, 0.1, 0.05, 0.01, 0.005, 0.001, 0.0005, 0.0001],
                                value=gamma_var,
                                multiselect=True
                            ),
                            direction='horizontal'
                        )
                    ),
                    span=11
                ),
                Grid.Column(
                    Stack(
                        information_button(show_hyperparameters_instantiation),
                        justify='center',
                        align='center'
                    ),
                    span=1
                )
            ),
            Grid.Row(
                Grid.Column(
                    Stack(confusion_matrix_plot(results, target_names)),
                    span=6
                ),
                Grid.Column(
                    Grid(
                        Grid.Row(
                            Grid.Column(
                                Stack(
                                    Text('In this example, the user can choose some hyperparameters for doing a grid search on a \
                                          Support Vector Machine. It will use these hyperparameters to choose the best model \
                                          and display a confusion matrix. Depending on how many choices there are it could be \
                                          helpful to offload this calculation to a separate process. \
                                          To use this feature you must put the definition of the resolver of the \
                                          DerivedVariable into a separate file. This example will define the resolver in \
                                          tasks.py.'),
                                    justify='center'
                                ),
                            ),
                            Grid.Column(
                                Stack(
                                    information_button(show_dv_task_instantiation),
                                    justify='center',
                                    align='center'
                                ),
                                span=2
                            )
                        ),
                        Grid.Row(
                            Grid.Column(
                                Stack(
                                    Text('It is often beneficial to provide the end-user with live progress updates \
                                          on a long-running task. Dara provides a simple API to send progress updates \
                                          from inside a task and display them in the form of a progress bar while a \
                                          task is running, rather than displaying a plain loading spinner which is the default. \
                                          This involves wrapping your task function in a track_progress decorator while setting \
                                          track_progress to True in the py_component that is waiting for the task results.'),
                                    justify='center'
                                )
                            ),
                            Grid.Column(
                                Stack(
                                    information_button(show_progress_bar_implementation),
                                    justify='center',
                                    align='center'
                                ),
                                span=2
                            )
                        )
                    ),
                    span=6
                ),
            ),
            padding='10px'
        ),
        code_modal(HYPERPARAMETERS_CODE, show_hyperparameters_instantiation),
        code_modal(DERIVED_VARIABLE_WITH_TASK_CODE, show_dv_task_instantiation),
        code_modal(TASK_WITH_PROGRESS_BAR_CODE, show_progress_bar_implementation),
    )


from collections import namedtuple
import numpy as np
import math

Move = namedtuple("Move", ["dest"])
Wait = namedtuple("Wait", [])

STATE_GOING_UP = "up"
STATE_GOING_DOWN = "down"
STATE_EMPTY = "both"

ControllerStatus = namedtuple("ControllerStatus",
                              ["wait_up", "wait_down",
                               "locations", "members",
                               "status"])

Person = namedtuple("Person",
                    ["dest_floor", "spawn_floor", "spawn_tick"])

def make_person_from_jsval(v):
    return Person(
        dest_floor=int(v['df']),
        spawn_floor=int(v['sf']),
        spawn_tick=int(v['st']))

class World:
    def __init__(self, conf):
        self.nfloors = conf['num_floors']
        self.nlifts = conf['num_lifts']
        self.ticks_per_day = conf['tick_per_day']
        self.max_people_per_lift = conf['max_people_per_lift']
        self.lift_inv_speed = conf['lift_inv_speed']

        self.lift_stop_duration = conf['lift_stop_duration']

        self.spawn_lambdas = np.zeros((self.ticks_per_day, self.nfloors))
        self.destination_probs = {}
        for dest_conf in conf['destinations']:
            key = dest_conf['type']
            val = np.array(dest_conf['matrix'])
            val = val / np.array([val.sum(axis=1)]).T # normalize
            self.destination_probs[key] = val

        self.destination_types = [None for _ in range(self.ticks_per_day)]
        for spawn_conf in conf['people_incoming']:
            beg, end = spawn_conf['range']
            lambdas = np.array(spawn_conf['incoming_lambda'])
            self.spawn_lambdas[beg:end, :] = lambdas
            for t in range(beg, end):
                self.destination_types[t] = spawn_conf['destination_type']

    def spawn_people(self, tick):
        people = np.random.poisson(self.spawn_lambdas[tick])

        ret = [[] for _ in range(self.nfloors)]
        probmat = self.destination_probs[self.destination_types[tick]]
        for i, (n, ps) in enumerate(zip(people, probmat)):
            if n == 0:
                continue

            for _ in range(n):
                d = np.random.choice(range(self.nfloors), p=ps)
                ret[i].append(Person(dest_floor=d, spawn_floor=i, spawn_tick=tick))
        return ret

def next_int(fr):
    if fr.denominator == 1:
        return fr.numerator + 1
    else:
        return math.ceil(fr)

def prev_int(fr):
    if fr.denominator == 1:
        return fr.numerator - 1
    else:
        return math.floor(fr)


import tensorflow as tf
import keras

def create_model(input_globalvars,input_cpf,input_npf,input_sv,nlabels):

    cpf = input_cpf
    for nfilters in [32,32,8]:
        cpf = keras.layers.Conv1D(nfilters,1,padding='same',activation='selu')(cpf)
        cpf = keras.layers.Dropout(0.1)(cpf)
    cpf = keras.layers.Flatten()(cpf)

    npf = input_npf
    for nfilters in [16,16,4]:
        npf = keras.layers.Conv1D(nfilters,1,padding='same',activation='selu')(npf)
        npf = keras.layers.Dropout(0.1)(npf)
    npf = keras.layers.Flatten()(npf)

    sv = input_sv
    for nfilters in [32,32,8]:
        sv = keras.layers.Conv1D(nfilters,1,padding='same',activation='selu')(sv)
        sv = keras.layers.Dropout(0.1)(sv)
    sv = keras.layers.Flatten()(sv)

    globalvars = input_globalvars
    features = keras.layers.Concatenate(axis=1)([cpf,npf,sv,globalvars])
    for nnodes in [200,100,100]:
        features = keras.layers.Dense(nnodes,activation='selu')(features)
        features = keras.layers.Dropout(0.1)(features)
    predicted_labels = keras.layers.Dense(nlabels)(features)

    model = keras.models.Model(
        inputs=[
            input_globalvars,
            input_cpf,
            input_npf,
            input_sv
        ],
        outputs=[predicted_labels]
    )

    return model


import json
import os

from typing import Dict, Any, Union, Optional
from typing_extensions import Literal
from functools import lru_cache

from .. import load_shared_data, get_shared_data_root

from .pipette_definition import (
    PipetteConfigurations,
    PipetteLiquidPropertiesDefinition,
)
from .model_constants import MOUNT_CONFIG_LOOKUP_TABLE, _MAP_KEY_TO_V2
from .types import (
    PipetteChannelType,
    PipetteModelType,
    PipetteGenerationType,
    PipetteVersionType,
    PipetteModelMajorVersion,
    PipetteModelMinorVersion,
    LiquidClasses,
)

LoadedConfiguration = Dict[str, Union[str, Dict[str, Any]]]

def _get_configuration_dictionary(
    config_type: Literal["general", "geometry", "liquid"],
    channels: PipetteChannelType,
    model: PipetteModelType,
    version: PipetteVersionType,
    liquid_class: Optional[LiquidClasses] = None,
) -> LoadedConfiguration:
    if liquid_class:
        config_path = (
            get_shared_data_root()
            / "pipette"
            / "definitions"
            / "2"
            / config_type
            / channels.name.lower()
            / model.value
            / liquid_class.name
            / f"{version.major}_{version.minor}.json"
        )
    else:
        config_path = (
            get_shared_data_root()
            / "pipette"
            / "definitions"
            / "2"
            / config_type
            / channels.name.lower()
            / model.value
            / f"{version.major}_{version.minor}.json"
        )
    return json.loads(load_shared_data(config_path))

@lru_cache(maxsize=None)
def _geometry(
    channels: PipetteChannelType,
    model: PipetteModelType,
    version: PipetteVersionType,
) -> LoadedConfiguration:
    return _get_configuration_dictionary("geometry", channels, model, version)

@lru_cache(maxsize=None)
def _liquid(
    channels: PipetteChannelType,
    model: PipetteModelType,
    version: PipetteVersionType,
) -> Dict[str, LoadedConfiguration]:
    liquid_dict = {}
    for liquid_class in LiquidClasses:
        try:
            liquid_dict[liquid_class.name] = _get_configuration_dictionary(
                "liquid", channels, model, version, liquid_class
            )
        except FileNotFoundError:
            continue

    return liquid_dict

@lru_cache(maxsize=None)
def _physical(
    channels: PipetteChannelType,
    model: PipetteModelType,
    version: PipetteVersionType,
) -> LoadedConfiguration:
    return _get_configuration_dictionary("general", channels, model, version)

@lru_cache(maxsize=None)
def load_serial_lookup_table() -> Dict[str, str]:
    """Load a serial abbreviation lookup table mapped to model name."""
    config_path = get_shared_data_root() / "pipette" / "definitions" / "2" / "general"
    _lookup_table = {}
    _channel_shorthand = {
        "eight_channel": "M",
        "single_channel": "S",
        "ninety_six_channel": "H",
    }
    _channel_model_str = {
        "single_channel": "single",
        "ninety_six_channel": "96",
        "eight_channel": "multi",
    }
    _model_shorthand = {"p1000": "p1k", "p300": "p3h"}
    for channel_dir in os.listdir(config_path):
        for model_dir in os.listdir(config_path / channel_dir):
            for version_file in os.listdir(config_path / channel_dir / model_dir):
                version_list = version_file.split(".json")[0].split("_")
                built_model = f"{model_dir}_{_channel_model_str[channel_dir]}_v{version_list[0]}.{version_list[1]}"

                model_shorthand = _model_shorthand.get(model_dir, model_dir)

                if (
                    model_dir == "p300"
                    and int(version_list[0]) == 1
                    and int(version_list[1]) == 0
                ):
                    # Well apparently, we decided to switch the shorthand of the p300 depending
                    # on whether it's a "V1" model or not...so...here is the lovely workaround.
                    model_shorthand = model_dir
                serial_shorthand = f"{model_shorthand.upper()}{_channel_shorthand[channel_dir]}V{version_list[0]}{version_list[1]}"
                _lookup_table[serial_shorthand] = built_model
    return _lookup_table

def load_liquid_model(
    model: PipetteModelType,
    channels: PipetteChannelType,
    version: PipetteVersionType,
) -> Dict[str, PipetteLiquidPropertiesDefinition]:
    liquid_dict = _liquid(channels, model, version)
    return {
        k: PipetteLiquidPropertiesDefinition.parse_obj(v)
        for k, v in liquid_dict.items()
    }

def _change_to_camel_case(c: str) -> str:
    # Tiny helper function to convert to camelCase.
    config_name = c.split("_")
    if len(config_name) == 1:
        return config_name[0]
    return f"{config_name[0]}" + "".join(s.capitalize() for s in config_name[1::])

def update_pipette_configuration(  # noqa: C901
    base_configurations: PipetteConfigurations,
    v1_configuration_changes: Dict[str, Any],
    liquid_class: Optional[LiquidClasses] = None,
) -> PipetteConfigurations:
    """Helper function to update 'V1' format configurations (left over from PipetteDict).

    #TODO (lc 7-14-2023) Remove once the pipette config dict is eliminated.
    Given an input of v1 mutable configs, look up the equivalent keyed
    value of that configuration."""
    quirks_list = []
    dict_of_base_model = base_configurations.dict(by_alias=True)

    for c, v in v1_configuration_changes.items():
        lookup_key = _change_to_camel_case(c)
        if c == "quirks" and isinstance(v, dict):
            quirks_list.extend([b.name for b in v.values() if b.value])
        elif liquid_class:
            if lookup_key == "tipLength":
                new_names = _MAP_KEY_TO_V2[lookup_key]
                top_name = new_names["top_level_name"]
                nested_name = new_names["nested_name"]
                # This is only a concern for OT-2 configs and I think we can
                # be less smart about handling multiple tip types by updating
                # all tips.
                for k in dict_of_base_model["liquid_properties"][liquid_class][
                    new_names["top_level_name"]
                ].keys():
                    dict_of_base_model["liquid_properties"][liquid_class][top_name][k][
                        nested_name
                    ] = v
            else:
                dict_of_base_model["liquid_properties"][liquid_class].pop(lookup_key)
                dict_of_base_model["liquid_properties"][liquid_class][lookup_key] = v
        else:
            try:
                dict_of_base_model.pop(lookup_key)
                dict_of_base_model[lookup_key] = v
            except KeyError:
                # The name is not the same format as previous so
                # we need to look it up from the V2 key map
                new_names = _MAP_KEY_TO_V2[lookup_key]
                top_name = new_names["top_level_name"]
                nested_name = new_names["nested_name"]
                if new_names.get("liquid_class"):
                    # isinstances are needed for type checking.
                    liquid_class = LiquidClasses[new_names["liquid_class"]]
                    dict_of_base_model[top_name][liquid_class][nested_name] = v
                else:
                    # isinstances are needed for type checking.
                    dict_of_base_model[top_name][nested_name] = v
    dict_of_base_model["quirks"] = list(
        set(dict_of_base_model["quirks"]) - set(quirks_list)
    )

    # re-serialization is not great for this nested enum so we need
    # to perform this workaround.
    if not liquid_class:
        liquid_class = LiquidClasses.default
    dict_of_base_model["liquid_properties"][liquid_class]["supportedTips"] = {
        k.name: v
        for k, v in dict_of_base_model["liquid_properties"][liquid_class][
            "supportedTips"
        ].items()
    }
    dict_of_base_model["liquid_properties"] = {
        k.name: v for k, v in dict_of_base_model["liquid_properties"].items()
    }
    dict_of_base_model["plungerPositionsConfigurations"] = {
        k.name: v
        for k, v in dict_of_base_model["plungerPositionsConfigurations"].items()
    }
    return PipetteConfigurations.parse_obj(dict_of_base_model)

def load_definition(
    model: PipetteModelType,
    channels: PipetteChannelType,
    version: PipetteVersionType,
) -> PipetteConfigurations:
    if (
        version.major not in PipetteModelMajorVersion
        or version.minor not in PipetteModelMinorVersion
    ):
        raise KeyError("Pipette version not found.")

    geometry_dict = _geometry(channels, model, version)
    physical_dict = _physical(channels, model, version)
    liquid_dict = _liquid(channels, model, version)

    generation = PipetteGenerationType(physical_dict["displayCategory"])
    mount_configs = MOUNT_CONFIG_LOOKUP_TABLE[generation][channels]

    return PipetteConfigurations.parse_obj(
        {
            **geometry_dict,
            **physical_dict,
            "liquid_properties": liquid_dict,
            "version": version,
            "mount_configurations": mount_configs,
        }
    )


import os
import traceback
import copy
import time
import sys
import subprocess
from logging import handlers
from multiprocessing import Pool, Process, Manager
import shutil
import logging
import datetime

root_path = os.path.dirname(os.path.abspath(__name__))
LOG_DIR = os.path.join(root_path, 'log')
log_name = 'unpack_apk.log' + datetime.datetime.now().strftime("%Y%m%d")
logger = logging.getLogger(__name__)
formatter = logging.Formatter('[%(asctime)s] - %(name)s - %(levelname)s - %(lineno)d - %(message)s')
filehandler = handlers.RotatingFileHandler(os.path.join(LOG_DIR, log_name))
filehandler.setFormatter(formatter)
logger.addHandler(filehandler)
logger.setLevel(logging.INFO)

failed_apk_path = os.path.join(root_path, 'failed_apk.txt')
success_result_path = os.path.join(root_path, 'result')
failed_result_path = os.path.join(root_path, 'result_failed')

class unpack2:
    def __init__(self, serialno, apk_filepath):
        self.__inited = False
        file_dir = os.path.abspath(__file__)
        self.__path = os.path.dirname(file_dir)
        self.__apk_filepath = os.path.abspath(apk_filepath)
        pullname = os.path.split(self.__apk_filepath)[1].rsplit(".", 1)[0]
        self.__pullname = pullname  # 包名
        self.__result_failed = os.path.join(self.__path, 'result_failed')
        pull_dst = os.path.join(self.__path, "result")
        self.__pull_dst = os.path.join(pull_dst, pullname)
        if not os.path.exists(self.__pull_dst):
            os.makedirs(self.__pull_dst)
        self.__build_dex = os.path.join(self.__path, "build_dex")
        self.__adb_path = os.path.join(self.__path, "adb")
        self.__adb_command = self.__adb_path + " -s " + serialno + " "
        self.__packagename, self.__activity_name = self.__get_package_and_activity(apk_filepath)
        self.__packagepath = "/data/data/" + self.__packagename
        self.__unpackfilepath = self.__packagepath + "/dexInfos"
        # self.__logfile = os.path.join(self.__path, "log.txt")
        print(serialno, apk_filepath, self.__packagename)
        self.__inited = True

    def __del__(self):
        if not self.__inited:
            return
        self.__force_stop()
        self.__uninstall_apk()

    def __run_command(self, command):
        print(command)
        return subprocess.Popen(command, shell = True, stdout = subprocess.PIPE, stdin = subprocess.PIPE, stderr = subprocess.PIPE)

    def __get_package_and_activity(self, apk_filepath):
        aapt = os.path.join(self.__path, "aapt")
        command = aapt + " d badging " + apk_filepath + " | awk -F \" |\'|:\" \'{if($1 == \"package\")print $4; if($1 == \"launchable-activity\")print $4;}\' "
        process = self.__run_command(command)
        lines = process.stdout.readlines()
        try:
            package_name = lines[0].strip().decode()
            activity_name = lines[1].strip().decode()
        except Exception as e:
            process.terminate()
            # unzip and zip
            self.__deal_with_apk(apk_filepath)
            aapt = os.path.join(self.__path, "aapt")
            command = aapt + " d badging " + apk_filepath + " | awk -F \" |\'|:\" \'{if($1 == \"package\")print $4; if($1 == \"launchable-activity\")print $4;}\' "
            process = self.__run_command(command)
            lines = process.stdout.readlines()
            try:
                package_name = lines[0].strip().decode()
                activity_name = lines[1].strip().decode()
            except Exception as e:
                process.terminate()
                # move to failed result
                self.__move_to_failed_result()
                raise
        return package_name, activity_name

    def __move_to_failed_result(self):
        if os.path.exists(self.__pull_dst):
            command = 'mv {} {}'.format(self.__pull_dst, self.__result_failed)
            process = self.__run_command(command)
            process.communicate()
            process.terminate()
        else:
            os.makedirs(os.path.join(self.__result_failed, self.__pullname))

    def __deal_with_apk(self, origin_file):
        cwd = os.getcwd()
        _set = os.path.split(origin_file)
        apk_table = _set[0]
        apk_name = _set[1]
        deal_apk_name = apk_name.split('.apk')[0]
        os.chdir(apk_table)
        command = 'unzip {} -d {}'.format(apk_name, deal_apk_name)  # com.anzhui.apk , com.anzhui
        process = self.__run_command(command)
        process.communicate()
        process.terminate()
        os.remove(origin_file)

        os.chdir(os.path.join(apk_table, deal_apk_name))
        command = 'zip -q -r {} *'.format(apk_name)
        process = self.__run_command(command)
        process.communicate()
        process.terminate()

        command = 'mv {} {}'.format(apk_name, apk_table)
        process = self.__run_command(command)
        process.communicate()
        process.terminate()
        shutil.rmtree(os.path.join(apk_table, deal_apk_name))
        os.chdir(cwd)

    def __launch_start(self):
        command = self.__adb_command + " shell am start -n " + self.__packagename + "/" + self.__activity_name
        process = self.__run_command(command)
        process.communicate()
        process.terminate()

    def __install_apk(self):
        command = self.__adb_command + " install -r " + self.__apk_filepath
        process = self.__run_command(command)
        process.communicate()
        process.terminate()

    def __uninstall_apk(self):
        command = self.__adb_command + " uninstall " + self.__packagename
        process = self.__run_command(command)
        process.communicate()
        process.terminate()

    def __force_stop(self):
        command = self.__adb_command + " shell am force-stop " + self.__packagename
        process = self.__run_command(command)
        process.communicate()
        process.terminate()

    def __check_end_file(self):
        command = self.__adb_command + " shell ls " + self.__unpackfilepath + "/end"
        process = self.__run_command(command)
        if(process.stdout.read().find(b"No such file or directory") == -1):
            process.terminate()
            return True
        process.terminate()
        return False

    def __check_process(self):
        command = self.__adb_command + " shell \"ps | grep " + self.__packagename + "\""
        process = self.__run_command(command)
        if(process.stdout.read() == b''):
            process.terminate()
            return False
        process.terminate()
        return True

    def __get_classes_size(self):
        command = self.__adb_command + " shell ls -l " + self.__unpackfilepath + "/classes | awk \'{print $4;}\'"
        process = self.__run_command(command)
        readres = process.stdout.read().strip()
        process.terminate()
        if(readres == b'file'):
            return -1
        return int(readres)

    def __restart_app(self):
        self.__force_stop()
        self.__launch_start()

    def __pull_files(self):
        time.sleep(3)
        command = self.__adb_command + " pull " + self.__unpackfilepath + "/ " + self.__pull_dst
        process = self.__run_command(command)
        process.communicate()
        process.terminate()
        time.sleep(3)

    def __rebuild_dex(self):
        cwd = os.getcwd()
        os.chdir(self.__pull_dst)
        lines = []
        try:
            with open("dex", "rb") as dex:
                lines = dex.readlines()
        except Exception as e:
            logger.info(e)
            raise
        with open("dex", "wb") as dex:
            for line in lines[1:]:
                dex.write(line)
        command = self.__build_dex
        process = self.__run_command(command)
        process.communicate()
        process.terminate()
        os.chdir(cwd)

    def __check_device(self):
        command = self.__adb_command + " shell id " # adb  -s  serialno shell id
        process = self.__run_command(command)
        if(len(process.stderr.read()) > 0):
            process.terminate()
            return False
        process.terminate()
        return True

    def __check_result_dex(self):
        result_dex_path = os.path.join(self.__pull_dst, 'result.dex')
        print(result_dex_path)
        if os.path.exists(result_dex_path):
            flag = False
            with open(str(result_dex_path), 'rb+') as ff:
                first_line = ff.readline().strip()
                if first_line:
                    first_line = bytes.decode(first_line)
                    if not first_line.startswith('dex'):
                        print(first_line)
                        flag = True
                    else:
                        print('startswith dex')
                        pass
            if flag:
                self.__first_line_switch(result_dex_path,)

    def __first_line_switch(self, dst_file, s=1):
        with open(dst_file, 'rb+') as ff:
            ff.seek(40, 0)
            remain = ff.read()
        # 默认替换第一行内容
        with open(dst_file, 'wb+') as f:
            f.write(remain)
        print('switch success!')

    def start(self):
        if not self.__check_device():
            return -2
        self.__uninstall_apk()
        self.__install_apk()
        self.__launch_start()
        time.sleep(10)
        lastsize = 0
        restarttimes = 0
        while True:
            if(self.__check_end_file() == True):
                break
            if(self.__check_process() == False):
                if(self.__get_classes_size() == -1):
                    return -1
                self.__restart_app()
            current_size = self.__get_classes_size()
            if(lastsize < current_size):
                lastsize = current_size
            elif(lastsize >= current_size):
                self.__restart_app()
                restarttimes = restarttimes + 1
            if(restarttimes > 30):
                return -1
            time.sleep(3)
        self.__pull_files()
        self.__rebuild_dex()
        self.__check_result_dex() # 检查生成的result.dex文件
        #todo dey2dex 已解决
        return 0

serialnos = []
def unpack_process(serialno, apkpath):
    try:
        un = unpack2(serialno, apkpath)
        # retval = un.start()
    except Exception as e:
        logger.info(traceback.format_exc())
        retval = -1
    else:
        try:
            retval = un.start()
        except Exception as e:
            logger.info(traceback.format_exc())
            retval = -1
    if(retval == -1):
        apk_name = os.path.split(apkpath)[1]
        pack_name = apk_name.split('.apk')[0]
        if os.path.exists(os.path.join(success_result_path, pack_name)):
            # move to failed result
            pack_path = os.path.join(success_result_path, pack_name)
            dst_path = failed_result_path
            move_to_failed_result(pack_path, dst_path)

        logger.info("unpack failed with " + apkpath)
        with open(failed_apk_path, 'a')as f:
            f.write(apk_name + '\n')
    else:
        logger.info('success unpack with ' + apkpath)
    return serialno,

def __run_command(command):
    logger.info(command)
    # print(command)
    return subprocess.Popen(command, shell = True, stdout = subprocess.PIPE, stdin = subprocess.PIPE, stderr = subprocess.PIPE)

def move_to_failed_result(origin_path, dst_path):
    command = 'mv {} {}'.format(origin_path, dst_path)
    process = __run_command(command)
    process.communicate()
    process.terminate()

def reuse(args):
    global serialnos

    serialnos.remove(args[0])

def adb_devices():
    senos = []
    adb_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), "adb")
    process = subprocess.Popen(adb_path + " devices", shell = True, stdin = subprocess.PIPE, stdout = subprocess.PIPE, stderr = subprocess.PIPE)
    lines = process.stdout.readlines()
    for line in lines:
        arr = line.split()
        if len(arr) == 2 and arr[1] == b'device':
            senos.append(arr[0].decode())
    return senos

def unpack_apks(apklist):
    global serialnos
    pool = Pool(10)
    for apkpath in apklist:
        apkpath = apkpath.strip()
        while(True):
            senos = adb_devices()
            flag = False
            for seno in senos:
                if seno not in serialnos:
                    flag = True
                    break
            if flag:
                break
        serialnos.append(seno)
        pool.apply_async(unpack_process, args = (seno, apkpath), callback = reuse)
    pool.close()
    pool.join()

def main():
    with open("list", "r") as fff:
        lines = fff.readlines()
    unpack_apks(lines)

if __name__ == '__main__':
    main()


import requests
from fastapi import APIRouter
news = APIRouter()
import requests
import os
from bson import json_util
from datetime import datetime
api_key = os.environ.get("API_key")

import uuid
from pydantic import BaseModel, Field
from pymongo import MongoClient
CONNECTION_STRING = 'mongodb://localhost:27017'
client = MongoClient(CONNECTION_STRING)
db = client['news_artikel']
collection = db['news']

class News(BaseModel):
  id: str = Field(default_factory=uuid.uuid4, alias="_id")
  description: str
  title:str
  url:str
  urlToImage:str
  author:str
  created_at:datetime = Field(default_factory=datetime.utcnow)

def create_news():
    url = ('https://newsapi.org/v2/everything?'
           'q=Apple&'
           'from=2023-06-04&'
           'sortBy=popularity&'
           f'apiKey={api_key}')

    response = requests.get(url)
    data = response.json()

    articl = []
    count=0
    for article in data["articles"]:
        description = article["description"]
        title = article["title"]
        urlToImage = article["urlToImage"]
        url = article["url"]
        author = article["author"]
        created_at = datetime.utcnow()
        articl.append({
            "description": description,
            "title": title,
            "urlToImage": urlToImage,
            "url": url,
            "author": author,
            "created_at": created_at
        })
        values =[[
            description,
            title,
            urlToImage,
            url,
            author,
            created_at,
        ]]
        result =[]
        collection.insert_many([{
            "description": value[0],
            "titel": value[1],
            "urlToImage": value[2],
            "url":value[3],
            "autor":value[4],
            "created_at":value[5]
        }for value in values])
        result.append({description,title,urlToImage,url,author, created_at})
        count+=1
        if count>=2:
           break

    return(result)

@news.get("/news")
def get_news():
    articles = create_news()
    result = collection.find({}, {"_id": 0}).sort("created_at", -1).limit(2)
    result_dicts = [doc for doc in result]
    return result_dicts


#DANILO COURA MOREIRA

import time as time
import numpy as np
import matplotlib.pyplot as plt

def normal_equation(points):
	points_x_with_1 = np.append(np.ones((len(points),1)),points[:,0:1],axis=1)
	#w = (H'H)-1 H'y
	return np.dot(np.dot(np.linalg.inv(np.dot(np.transpose(points_x_with_1),points_x_with_1)),np.transpose(points_x_with_1)),points[:,1])

def run():
	tic = time.time()
	points = np.genfromtxt("income.csv", delimiter=",")
	#y = w0 + w1x (slope formula)
	[w0, w1] = normal_equation(points)
	tac = time.time()
	print('Tempo de processamento versão Equação Normal: {0:0.2f} segundos'.format(tac-tic))
	axis_x = range(0,25)
	plt.figure(1)
	plt.xlabel('Escolaridade (anos)')
	plt.ylabel('Salário (mil/ano)')
	plt.title('Regressão Linear')
	plt.plot(points[:,0], points[:,1], 'ro')
	plt.plot(axis_x, w0+w1*axis_x)
	plt.text(5, 60, 'Y = {0:0.3f} + {1:0.3f}x'.format(w0,w1), fontsize=12)
	plt.show()

if __name__ == '__main__':
	run()