import json
from functools import wraps

from pgadmin.browser.server_groups.servers import databases
from flask import render_template, request, jsonify
from flask_babel import gettext
from pgadmin.browser.collection import CollectionNodeModule
from pgadmin.browser.utils import PGChildNodeView
from pgadmin.utils.ajax import make_json_response, \
    make_response as ajax_response, internal_server_error, gone
from pgadmin.utils.driver import get_driver
from pgadmin.browser.server_groups.servers.databases.extensions.utils \
    import get_extension_details
from config import PG_DEFAULT_DRIVER
from pgadmin.tools.schema_diff.node_registry import SchemaDiffRegistry
from pgadmin.tools.schema_diff.compare import SchemaDiffObjectCompare

class ExtensionModule(CollectionNodeModule):
    """
    class ExtensionModule():

        A collection Node which inherits CollectionNodeModule
        class and define methods to get child nodes, to load its own
        javascript file.
    """
    _NODE_TYPE = "extension"
    _COLLECTION_LABEL = gettext("Extensions")

    def __init__(self, *args, **kwargs):
        """
        Initialising the base class
        """
        super().__init__(*args, **kwargs)

    def get_nodes(self, gid, sid, did):
        """
        Generate the collection node
        """
        if self.has_nodes(sid, did,
                          base_template_path=ExtensionView.EXT_TEMPLATE_PATH):
            yield self.generate_browser_collection_node(did)

    @property
    def node_inode(self):
        """
        If a node have child return True otherwise False
        """
        return False

    @property
    def script_load(self):
        """
        Load the module script for extension, when any of the database nodes
        are initialized.
        """
        return databases.DatabaseModule.node_type

    @property
    def module_use_template_javascript(self):
        """
        Returns whether Jinja2 template is used for generating the javascript
        module.
        """
        return False

# Create blueprint of extension module
blueprint = ExtensionModule(__name__)

class ExtensionView(PGChildNodeView, SchemaDiffObjectCompare):
    """
    This is a class for extension nodes which inherits the
    properties and methods from NodeView class and define
    various methods to list, create, update and delete extension.

    Variables:
    ---------
    * node_type - tells which type of node it is
    * parent_ids - id with its type and name of parent nodes
    * ids - id with type and name of extension module being used.
    * operations - function routes mappings defined.
    """
    EXT_TEMPLATE_PATH = 'extensions/sql'
    node_type = blueprint.node_type

    parent_ids = [
        {'type': 'int', 'id': 'gid'},
        {'type': 'int', 'id': 'sid'},
        {'type': 'int', 'id': 'did'}
    ]
    ids = [
        {'type': 'int', 'id': 'eid'}
    ]

    operations = dict({
        'obj': [
            {'get': 'properties', 'delete': 'delete', 'put': 'update'},
            {'get': 'list', 'post': 'create', 'delete': 'delete'}
        ],
        'delete': [{'delete': 'delete'}, {'delete': 'delete'}],
        'nodes': [{'get': 'node'}, {'get': 'nodes'}],
        'sql': [{'get': 'sql'}],
        'msql': [{'get': 'msql'}, {'get': 'msql'}],
        'stats': [{'get': 'statistics'}],
        'dependency': [{'get': 'dependencies'}],
        'dependent': [{'get': 'dependents'}],
        'avails': [{}, {'get': 'avails'}],
        'schemas': [{}, {'get': 'schemas'}],
        'children': [{'get': 'children'}]
    })

    keys_to_ignore = ['oid', 'oid-2', 'owner']

    def check_precondition(f):
        """
        This function will behave as a decorator which will checks
        database connection before running view, it will also attaches
        manager,conn & template_path properties to self
        """

        @wraps(f)
        def wrap(*args, **kwargs):
            # Here args[0] will hold self & kwargs will hold gid,sid,did
            self = args[0]
            self.manager = get_driver(
                PG_DEFAULT_DRIVER
            ).connection_manager(kwargs['sid'])
            self.conn = self.manager.connection(did=kwargs['did'])
            self.template_path = self.EXT_TEMPLATE_PATH

            self.datistemplate = False
            if (
                self.manager.db_info is not None and
                kwargs['did'] in self.manager.db_info and
                'datistemplate' in self.manager.db_info[kwargs['did']]
            ):
                self.datistemplate = self.manager.db_info[
                    kwargs['did']]['datistemplate']

            return f(*args, **kwargs)

        return wrap

    @check_precondition
    def list(self, gid, sid, did):
        """
        Fetches all extensions properties and render into properties tab
        """
        SQL = render_template("/".join([self.template_path,
                                        self._PROPERTIES_SQL]),
                              conn=self.conn)
        status, res = self.conn.execute_dict(SQL)

        if not status:
            return internal_server_error(errormsg=res)
        return ajax_response(
            response=res['rows'],
            status=200
        )

    @check_precondition
    def nodes(self, gid, sid, did):
        """
        Lists all extensions under the Extensions Collection node
        """
        res = []
        SQL = render_template("/".join([self.template_path,
                                        self._PROPERTIES_SQL]),
                              conn=self.conn)
        status, rset = self.conn.execute_2darray(SQL)
        if not status:
            return internal_server_error(errormsg=rset)

        for row in rset['rows']:
            res.append(
                self.blueprint.generate_browser_node(
                    row['oid'],
                    did,
                    row['name'],
                    'icon-extension',
                    description=row['comment']
                ))

        return make_json_response(
            data=res,
            status=200
        )

    @check_precondition
    def node(self, gid, sid, did, eid):
        """
        This function will fetch the properties of extension
        """
        SQL = render_template("/".join([self.template_path,
                                        self._PROPERTIES_SQL]),
                              eid=eid, conn=self.conn)
        status, rset = self.conn.execute_2darray(SQL)
        if not status:
            return internal_server_error(errormsg=rset)

        for row in rset['rows']:
            return make_json_response(
                data=self.blueprint.generate_browser_node(
                    row['oid'],
                    did,
                    row['name'],
                    'icon-extension'
                ),
                status=200
            )

        return gone(gettext("Could not find the specified extension."))

    @check_precondition
    def properties(self, gid, sid, did, eid):
        """
        Fetch the properties of a single extension and render in properties tab
        """
        status, res = self._fetch_properties(did, eid)
        if not status:
            return res

        return ajax_response(
            response=res,
            status=200
        )

    def _fetch_properties(self, did, eid):
        """
        This function fetch the properties of the extension.
        :param did:
        :param eid:
        :return:
        """
        SQL = render_template("/".join(
            [self.template_path, self._PROPERTIES_SQL]), eid=eid,
            conn=self.conn)
        status, res = self.conn.execute_dict(SQL)
        if not status:
            return False, internal_server_error(errormsg=res)

        if len(res['rows']) == 0:
            return False, gone(
                gettext("Could not find the extension information.")
            )

        res['rows'][0]['is_sys_obj'] = (
            res['rows'][0]['oid'] <= self._DATABASE_LAST_SYSTEM_OID or
            self.datistemplate)

        return True, res['rows'][0]

    @check_precondition
    def create(self, gid, sid, did):
        """
        Create a new extension object
        """
        required_args = [
            'name'
        ]

        data = request.form if request.form else json.loads(
            request.data
        )

        for arg in required_args:
            if arg not in data:
                return make_json_response(
                    status=410,
                    success=0,
                    errormsg=gettext(
                        "Could not find the required parameter ({})."
                    ).format(arg)
                )

        status, res = self.conn.execute_dict(
            render_template(
                "/".join([self.template_path, self._CREATE_SQL]),
                data=data
            )
        )

        if not status:
            return internal_server_error(errormsg=res)

        status, res = get_extension_details(
            self.conn, data['name'],
            "/".join([self.template_path, self._PROPERTIES_SQL]))
        if not status:
            return internal_server_error(errormsg=res)

        return jsonify(
            node=self.blueprint.generate_browser_node(
                res['oid'],
                did,
                res['name'],
                'icon-extension'
            )
        )

    @check_precondition
    def update(self, gid, sid, did, eid):
        """
        This function will update an extension object
        """
        data = request.form if request.form else json.loads(
            request.data
        )

        try:
            SQL, name = self.getSQL(gid, sid, data, did, eid)
            # Most probably this is due to error
            if not isinstance(SQL, str):
                return SQL
            SQL = SQL.strip('\n').strip(' ')
            status, res = self.conn.execute_dict(SQL)
            if not status:
                return internal_server_error(errormsg=res)

            other_node_info = {}
            if 'comment' in data:
                other_node_info['description'] = data['comment']

            return jsonify(
                node=self.blueprint.generate_browser_node(
                    eid,
                    did,
                    name,
                    icon="icon-%s" % self.node_type,
                    **other_node_info
                )
            )
        except Exception as e:
            return internal_server_error(errormsg=str(e))

    @check_precondition
    def delete(self, gid, sid, did, eid=None, only_sql=False):
        """
        This function will drop/drop cascade a extension object
        """

        if eid is None:
            data = request.form if request.form else json.loads(
                request.data
            )
        else:
            data = {'ids': [eid]}

        cascade = self._check_cascade_operation()

        try:
            for eid in data['ids']:
                # check if extension with eid exists
                SQL = render_template("/".join(
                    [self.template_path, self._DELETE_SQL]), eid=eid)
                status, name = self.conn.execute_scalar(SQL)
                if not status:
                    return internal_server_error(errormsg=name)

                if name is None:
                    return make_json_response(
                        status=410,
                        success=0,
                        errormsg=gettext(
                            'Error: Object not found.'
                        ),
                        info=gettext(
                            'The specified extension could not be found.\n'
                        )
                    )

                # drop extension
                SQL = render_template("/".join(
                    [self.template_path, self._DELETE_SQL]
                ), name=name, cascade=cascade)

                # Used for schema diff tool
                if only_sql:
                    return SQL

                status, res = self.conn.execute_scalar(SQL)
                if not status:
                    return internal_server_error(errormsg=res)

            return make_json_response(
                success=1,
                info=gettext("Extension dropped")
            )
        except Exception as e:
            return internal_server_error(errormsg=str(e))

    @check_precondition
    def msql(self, gid, sid, did, eid=None):
        """
        This function returns modified SQL
        """
        data = {}
        for k, v in request.args.items():
            try:
                data[k] = json.loads(v)
            except ValueError:
                data[k] = v

        try:
            SQL, name = self.getSQL(gid, sid, data, did, eid)
            # Most probably this is due to error
            if not isinstance(SQL, str):
                return SQL
            SQL = SQL.strip('\n').strip(' ')
            if SQL == '':
                SQL = "--modified SQL"

            return make_json_response(
                data=SQL,
                status=200
            )
        except Exception as e:
            return internal_server_error(errormsg=str(e))

    def getSQL(self, gid, sid, data, did, eid=None):
        """
        This function will generate sql from model data
        """
        required_args = [
            'name'
        ]

        if eid is not None:
            SQL = render_template("/".join(
                [self.template_path, self._PROPERTIES_SQL]
            ), eid=eid, conn=self.conn)
            status, res = self.conn.execute_dict(SQL)
            if not status:
                return internal_server_error(errormsg=res)

            if len(res['rows']) == 0:
                return gone(
                    gettext("Could not find the extension information.")
                )

            old_data = res['rows'][0]
            for arg in required_args:
                if arg not in data:
                    data[arg] = old_data[arg]
            SQL = render_template("/".join(
                [self.template_path, self._UPDATE_SQL]
            ), data=data, o_data=old_data)
            return SQL, data['name'] if 'name' in data else old_data['name']
        else:
            SQL = render_template("/".join(
                [self.template_path, self._CREATE_SQL]
            ), data=data)
            return SQL, data['name']

    @check_precondition
    def avails(self, gid, sid, did):
        """
        This function with fetch all the available extensions
        """
        SQL = render_template("/".join([self.template_path, 'extensions.sql']))
        status, rset = self.conn.execute_dict(SQL)
        if not status:
            return internal_server_error(errormsg=rset)
        return make_json_response(
            data=rset['rows'],
            status=200
        )

    @check_precondition
    def schemas(self, gid, sid, did):
        """
        This function with fetch all the schemas
        """
        SQL = render_template("/".join([self.template_path, 'schemas.sql']))
        status, rset = self.conn.execute_dict(SQL)
        if not status:
            return internal_server_error(errormsg=rset)
        return make_json_response(
            data=rset['rows'],
            status=200
        )

    @check_precondition
    def sql(self, gid, sid, did, eid, json_resp=True):
        """
        This function will generate sql for the sql panel
        """
        SQL = render_template("/".join(
            [self.template_path, self._PROPERTIES_SQL]
        ), eid=eid, conn=self.conn)
        status, res = self.conn.execute_dict(SQL)
        if not status:
            return internal_server_error(errormsg=res)
        if len(res['rows']) == 0:
            return gone(
                gettext("Could not find the extension on the server.")
            )

        result = res['rows'][0]

        SQL = render_template("/".join(
            [self.template_path, self._CREATE_SQL]
        ),
            data=result,
            conn=self.conn,
            display_comments=True,
            add_not_exists_clause=True
        )

        if not json_resp:
            return SQL

        return ajax_response(response=SQL)

    @check_precondition
    def dependents(self, gid, sid, did, eid):
        """
        This function gets the dependents and returns an ajax response
        for the extension node.

        Args:
            gid: Server Group ID
            sid: Server ID
            did: Database ID
            eid: Extension ID
        """
        dependents_result = self.get_dependents(self.conn, eid)
        return ajax_response(
            response=dependents_result,
            status=200
        )

    @check_precondition
    def dependencies(self, gid, sid, did, eid):
        """
        This function gets the dependencies and returns an ajax response
        for the extension node.

        Args:
            gid: Server Group ID
            sid: Server ID
            did: Database ID
            lid: Extension ID
        """
        dependencies_result = self.get_dependencies(self.conn, eid)
        return ajax_response(
            response=dependencies_result,
            status=200
        )

    @check_precondition
    def fetch_objects_to_compare(self, sid, did):
        """
        This function will fetch the list of all the extensions for
        specified database id.

        :param sid: Server Id
        :param did: Database Id
        :return:
        """
        res = dict()

        sql = render_template("/".join([self.template_path,
                                        self._PROPERTIES_SQL]),
                              conn=self.conn)
        status, rset = self.conn.execute_2darray(sql)
        if not status:
            return internal_server_error(errormsg=rset)

        for row in rset['rows']:
            status, data = self._fetch_properties(did, row['oid'])
            if status:
                res[row['name']] = data

        return res

    def get_sql_from_diff(self, **kwargs):
        """
        This function is used to get the DDL/DML statements.
        :param kwargs
        :return:
        """
        gid = kwargs.get('gid')
        sid = kwargs.get('sid')
        did = kwargs.get('did')
        oid = kwargs.get('oid')
        data = kwargs.get('data', None)
        drop_sql = kwargs.get('drop_sql', False)

        if data:
            sql, name = self.getSQL(gid=gid, sid=sid, did=did, data=data,
                                    eid=oid)
        else:
            if drop_sql:
                sql = self.delete(gid=gid, sid=sid, did=did,
                                  eid=oid, only_sql=True)
            else:
                sql = self.sql(gid=gid, sid=sid, did=did, eid=oid,
                               json_resp=False)
        return sql

SchemaDiffRegistry(blueprint.node_type, ExtensionView, 'Database')
# Register and add ExtensionView as blueprint
ExtensionView.register_node_view(blueprint)


import albumentations as albu
from albumentations.pytorch import ToTensorV2

def blur_transforms(p=0.5, blur_limit=5):
    """
    Applies MotionBlur or GaussianBlur random with a probability p.

    Args:
        p (float, optional): probability. Defaults to 0.5.
        blur_limit (int, optional): Blur intensity limit. Defaults to 5.
    Returns:
        albumentation transforms: transforms.
    """
    return albu.OneOf(
        [
            albu.MotionBlur(always_apply=True),
            albu.GaussianBlur(always_apply=True),
        ],
        p=p,
    )

def color_transforms(p=0.5):
    """
    Applies RandomGamma or RandomBrightnessContrast random with a probability p.

    Args:
        p (float, optional): probability. Defaults to 0.5.
    Returns:
        albumentation transforms: transforms.
    """
    return albu.OneOf(
        [
            albu.RandomGamma(gamma_limit=(50, 150), always_apply=True),
            albu.RandomBrightnessContrast(
                brightness_limit=0.1, contrast_limit=0.2, always_apply=True
            ),
            albu.ChannelShuffle(always_apply=True),
            albu.ToGray(always_apply=True),
        ],
        p=p,
    )

def get_transfos(augment=True, resize=(256, 256), mean=0, std=1, strength=1):
    """
    Returns transformations for image augmentation.

    Args:
        augment (bool, optional): Whether to apply augmentations. Defaults to True.
        resize (tuple, optional): Resize dimensions (height, width) of the images. Defaults to (256, 256).
        mean (np array, optional): Mean for normalization. Defaults to 0.
        std (np array, optional): Standard deviation for normalization. Defaults to 1.
        strength (int, optional): Strength level for augmentations. Defaults to 1.

    Returns:
        albumentation transforms: Transforms for image augmentation.
    """
    resize_aug = [
        albu.Resize(resize[0], resize[1])
    ] if resize else []

    normalizer = albu.Compose(
        resize_aug
        + [
            ToTensorV2(),
        ],
        p=1,
    )

    if augment:
        if strength == 0:
            augs = [
                # albu.HorizontalFlip(p=0.5),
            ]
        elif strength == 1:
            augs = [
                albu.HorizontalFlip(p=0.5),
                albu.ShiftScaleRotate(
                    scale_limit=0.1,
                    shift_limit=0.0,
                    rotate_limit=20,
                    p=0.5,
                ),
                color_transforms(p=0.5),
            ]
        elif strength == 2:
            augs = [
                albu.HorizontalFlip(p=0.5),
                albu.ShiftScaleRotate(
                    scale_limit=0.2,
                    shift_limit=0.2,
                    rotate_limit=30,
                    p=0.75,
                ),
                color_transforms(p=0.5),
                blur_transforms(p=0.25),
            ]
        elif strength == 3:
            augs = [
                albu.HorizontalFlip(p=0.5),
                albu.ShiftScaleRotate(
                    scale_limit=0.2,
                    shift_limit=0.2,
                    rotate_limit=5,
                    p=1.,
                ),
                color_transforms(p=1.),
                blur_transforms(p=0.25),
            ]
    else:
        augs = []

    return albu.Compose(augs + [normalizer])


# 5. Longest Palindromic Substring
# Given a string s, find the longest palindromic substring in s.
# You may assume that the maximum length of s is 1000.
from typing import List

import pytest

def longest_palindrome_from_middle(s: str) -> str:
    # 1664 ms, faster than 47.77% of Python3 online submissions
    """
    For each index, compute:
        longest palindrome of odd length centered at that index, e.g. abCba
        longest palindrome of even length centered at that index, e.g. abCcba
    """
    longest = ""
    for index, c in enumerate(s):
        # odd length palindromes
        num_side = 0
        while True:
            if index - num_side - 1 < 0 or index + num_side + 1 >= len(s):
                break
            if s[index - num_side - 1] != s[index + num_side + 1]:
                break
            num_side += 1
        if num_side * 2 + 1 > len(longest):
            longest = s[index - num_side : index + num_side + 1]

        # even length palindrome, where c is the first of the 2 middle chars
        if index + 1 < len(s) and c == s[index + 1]:
            num_side = 0
            while True:
                if index - num_side - 1 < 0 or index + 2 + num_side >= len(s):
                    break
                if s[index - num_side - 1] != s[index + 2 + num_side]:
                    break
                num_side += 1
            if (num_side + 1) * 2 > len(longest):
                longest = s[index - num_side : index + num_side + 2]

    return longest

def longest_palindrome_dp(s: str) -> str:
    # 5088 ms, faster than 19.85% of Python3 online submissions
    size = len(s)
    if size <= 1:
        return s

    is_palindrome_array = [x[:] for x in [[False] * size] * size]
    longest = s[0]
    for length in range(1, size + 1):
        for start in range(size):
            i = start
            j = start + length - 1
            if j >= size:
                break

            if s[i] == s[j] and (i + 1 >= j or is_palindrome_array[i + 1][j - 1]):
                is_palindrome_array[i][j] = True
                if length > len(longest):
                    longest = s[i : j + 1]
    return longest

class Solution:
    def longestPalindrome(self, s: str) -> str:
        return longest_palindrome_from_middle(s)

@pytest.mark.parametrize(
    ("s", "expected"),
    [
        ("", [""]),
        ("a", ["a"]),
        ("aa", ["aa"]),
        ("ab", ["a", "b"]),
        ("aaa", ["aaa"]),
        ("aab", ["aa"]),
        ("aba", ["aba"]),
        ("bba", ["bb"]),
        ("abc", ["a", "b", "c"]),
        ("aaaa", ["aaaa"]),
        ("abba", ["abba"]),
        ("abcdeedcba", ["abcdeedcba"]),
        ("babad", ["bab", "aba"]),
        ("anana", ["anana"]),
        ("ananan", ["anana", "nanan"]),
        ("aabaaa", ["aabaa"]),
        ("ananana", ["ananana"]),
    ],
)
def test_longest_palindrome(s: str, expected: List[str]) -> None:
    assert longest_palindrome_from_middle(s) in expected
    assert longest_palindrome_dp(s) in expected


import requests
import os
import os.path
from pathlib import Path
import sys
import AESdecrypto
import traceback
import logger
import tkinter as tk
import _thread
import tkinter.messagebox as messagebox
import GUI
import subprocess

headers = {'user-agent':'User-Agent:Mozilla/5.0 (Windows NT 6.1; rv:2.0.1) Gecko/20100101 Firefox/4.0.1'}
global progressValue
progressValue = 0.00
global printer
isStop = False

#预处理参数
def preProcess(varType, url, p, func):
    global printer
    printer = p
    if varType=="-u":
        if url.find('?')!=-1:#判断链接是否带参数
            urlWOVar = url[0:url.rindex('?')]#去除参数
        else:
            urlWOVar = url
        if urlWOVar.startswith("http")==False or urlWOVar.endswith("m3u8")==False:#检验链接是否合法
            printer.print("The link is in the wrong format.","e")
        else:
            printer.print("Download:"+urlWOVar,"i")
            try:
                res = requests.get(url, timeout=5)#发起请求,10秒超时
            except:
                printer.print("#exception: #url:"+url+" #The request error.","e")
                printer.print(traceback.format_exc(),"e")
            else:
                if res.status_code==200:
                    if(res.text.find(".m3u8")!=-1):#主播放列表文件
                        m3u8PlaylistProcessor(url,urlWOVar,res.content,func)
                    elif(res.text.find(".ts")!=-1):#媒体播放列表
                        thread_m3u8MediaUrlProcessor(url,urlWOVar)
                    else:
                        printer.print("fail:Link error","e")
                else:
                    printer.print("fail:Request error","e")
    elif varType=="-f":
        content = open(url,"r").read()
        if(content.find(".m3u8")!=-1):#主播放列表文件
            printer.print("The m3u8 master playlist is not supported at this time.","w")
        elif(content.find(".ts")!=-1):#媒体播放列表文件
            thread_m3u8MediaFileProcessor(content)
        else:
            printer.print("fail:Link error","e")

# m3u8链接处理
def m3u8MediaUrlProcessor(url,urlWOVar):
    list1 = urlWOVar.split('/')
    filename = list1[-1]#获取m3u8文件名
    res = requests.get(url)#发起请求
    with open(filename,'wb') as f1:#打开文件，并写入内容
        f1.write(res.content)
    f1.close()
    m3u8MediaProcessor(url,urlWOVar,res.content.decode("utf-8"))
    return

# m3u8媒体文件处理
def m3u8MediaProcessor(url,urlWOVar,content):#链接处理
    #print("!!!"+url)
    #print("!!!"+urlWOVar)
    #print("!!!"+content[0:200])
    global progressValue
    list2 = content.split("\n")
    f2 = open('file.txt','wb')#file.txt用于辅助合并.ts文件
    isEncrypto = False
    if(content.find("#EXT-X-KEY")!=-1):#获取key
        for i in range(len(list2)):
            if(list2[i].find("#EXT-X-KEY")!=-1):
                key_dict = key_rvl(list2[i])
                isEncrypto=True
                break
    for i in range(len(list2)):#循环遍历
        if(isStop==True):
            exit()
        progressValue = round(i/len(list2),2)
        if list2[i].find('.ts')!=-1:#获取.ts文件的下载链接
            if list2[2].find('?')!=-1:#判断是否带参数
                url2=list2[i][0:url.rindex('?')]#去除参数
            else:
                url2=list2[i]
            if list2[i].startswith('http')==False:#判断是否带协议
                printer.print(urlWOVar[0:url.rindex('/')]+'/'+list2[i],"i")
                res1 = requests.get(urlWOVar[0:url.rindex('/')]+'/'+list2[i])#不带则加上下载链接前部
            else:
                printer.print(list2[i],"i")
                res1 = requests.get(list2[i])#带协议直接下载
            tsFilePath = Path("ts/")
            if tsFilePath.exists()==False:
                os.mkdir(tsFilePath)
            f3 = open("ts\\"+str(i)+".ts",'wb')#打开.ts文件
            if isEncrypto==False:#未加密
                f3.write(res1.content)#写入.ts文件
            else:#加密
                f3.write(AESdecrypto.aesDecrypt(key_dict["KEY"],res1.content,key_dict["IV"]))#先解密，再写入.ts文件
            f3.close()
            f2.write(("file '"+os.getcwd()+"\\ts\\"+str(i)+".ts"+"'\r\n").encode())#写入file.txt
    f2.close()
    progressValue = 1
    #合并TS文件
    mergeTs()

# m3u8媒体文件处理（以文件形式下载）
def m3u8MediaFileProcessor(content):
    global progressValue
    list2 = content.split("\n")
    f2 = open('file.txt','wb')#file.txt用于辅助合并.ts文件
    isEncrypto = False
    if(content.find("#EXT-X-KEY")!=-1):#获取key
        for i in range(len(list2)):
            if(list2[i].find("#EXT-X-KEY")!=-1):
                key_dict = key_rvl(list2[i])
                isEncrypto=True
                break
    for i in range(len(list2)):#循环遍历
        if(isStop==True):
            exit()
        progressValue = round(i/len(list2),2)
        if list2[i].find('.ts')!=-1:#获取.ts文件的下载链接
            if list2[2].find('?')!=-1:#判断是否带参数
                url2=list2[i][0:list2[i].rindex('?')]#去除参数
            else:
                url2=list2[i]
            if url2.startswith('http')==False:#判断是否带协议
                printer.print("File resolution error, please use the link to download.","i")
                return
            else:
                printer.print(url2,"i")
                res1 = requests.get(url2,headers=headers)
            tsFilePath = Path("ts/")
            if tsFilePath.exists()==False:
                os.mkdir(tsFilePath)
            f3 = open("ts\\"+str(i)+".ts",'wb')#打开.ts文件
            if isEncrypto==False:#未加密
                f3.write(res1.content)#写入.ts文件
            else:#加密
                f3.write(AESdecrypto.aesDecrypt(key_dict["KEY"],res1.content,key_dict["IV"]))#先解密，再写入.ts文件
            f3.close()
            f2.write(("file '"+os.getcwd()+'\\ts\\'+str(i)+".ts"+"'\r\n").encode())#写入file.txt
    f2.close()
    progressValue = 1
    #合并TS文件
    mergeTs()

#播放列表处理
def m3u8PlaylistProcessor(url,urlWOVar,content,func):
    protocol = urlWOVar.split("/")[0]
    host = urlWOVar.split("/")[2]
    cttList = content.decode("utf-8").split("\n")
    info = []
    downloadUrl = []
    for i in range(len(cttList)):
        if cttList[i].startswith("#EXT-X-STREAM-INF"):
            info.append(cttList[i])
        if cttList[i].endswith(".m3u8"):
            downloadUrl.append(cttList[i])
    printer.print("###m3u8PlaylistProcessor info:"+str(info),"i")
    printer.print("###m3u8PlaylistProcessor downloadUrl:"+str(downloadUrl),"i")

    func(info,protocol,host,url,downloadUrl)

#命令行获取用户输入
def cliInputAndDownload(info,protocol,host,url,downloadUrl):
    printer.print("Please select the video you want to download.","i")
    for i in range(len(info)):
        printer.print(str(i)+" "+info[i],"i")
    num = int(input("input:"))
    m3u8Playlist_download(protocol,host,url,downloadUrl,num)

#下载m3u8 master文件里的m3u8媒体文件
def m3u8Playlist_download(protocol,host,url,downloadUrl,num):
    printer.print('### m3u8Playlist_download: num='+str(num),"i")
    mediaUrl = downloadUrl[num]
    if mediaUrl.find('?')!=-1:#判断链接是否带参数
        mediaUrlWOVar = url[0:url.rindex('?')]#去除参数
    else:
        mediaUrlWOVar = mediaUrl
    if(mediaUrl.startswith("http")):
        printer.print("m3u8Media:"+mediaUrl,"i")
        m3u8MediaUrlProcessor(mediaUrl,mediaUrlWOVar)
    else:
        urlType1 = url[0:url.rindex("/")+1]+mediaUrl
        code = -1
        try:
            code = requests.get(urlType1,timeout=5).status_code#测试链接，解决不同的链接组合方式
        except Exception as e:
            #printer.print(str(e.args),"e")
            #printer.print("=====","e")
            #printer.print(traceback.format_exc(),"e")
            pass
        finally:
            if(code==200):
                printer.print("m3u8Media:"+url[0:url.rindex("/")+1]+mediaUrl+"\r\n"+url[0:url.rindex("/")+1]+mediaUrlWOVar,"i")
                m3u8MediaUrlProcessor(url[0:url.rindex("/")+1]+mediaUrl,url[0:url.rindex("/")+1]+mediaUrlWOVar)
            else:
                printer.print("m3u8Media:"+protocol+"//"+host+"/"+mediaUrl+"\r\n"+protocol+"//"+host+"/"+mediaUrlWOVar,"i")
                m3u8MediaUrlProcessor(protocol+"//"+host+"/"+mediaUrl,protocol+"//"+host+"/"+mediaUrlWOVar)

#key处理
def key_rvl(keyStr):
    list1 = keyStr.split(",")
    key_dict = {"METHOD":"","KEY":"","IV":""}
    for i in range(len(list1)):
        if(list1[i].find("METHOD")!=-1):
            key_dict["METHOD"] = list1[i][list1[i].index("METHOD")+7:len(list1[i])]
            continue
        if(list1[i].find("URI")!=-1):
            uri = list1[i][list1[i].index("URI")+5:len(list1[i])-1]
            printer.print(uri,"i")
            key_dict["KEY"] = requests.get(uri).content
            continue
        if(list1[i].find("IV")!=-1):
            key_dict["IV"] = list1[i][list1[i].index("IV")+3:len(list1[i])]
            continue
    if len(key_dict["IV"])==0:
        key_dict["IV"]=key_dict["KEY"]
    printer.print(str(key_dict),"i")
    return key_dict

#调用ffmpeg合并ts文件
def mergeTs():
    main = "ffmpeg.exe -f concat -safe 0 -i file.txt -c copy out.mp4 -y"#ffmpeg合并命令
    ctt = subprocess.getoutput(main)#调用ffmpeg合并.ts文件
    printer.print(str(ctt),"i")
    printer.print('###done!',"i")

#启动线程下载，避免阻塞GUI
def thread_download(protocol,host,url,downloadUrl,num,root):
    try:
        _thread.start_new_thread( m3u8Playlist_download, (protocol,host,url,downloadUrl,num) )
        root.destroy()
        #messagebox.showinfo('提示','已经开始下载！')
        GUI.progressBarGUI()
    except:
        printer.print("###Thread Error","e")
        printer.print(traceback.format_exc(),"e")

def thread_m3u8MediaUrlProcessor(url,urlWOVar):
    try:
        _thread.start_new_thread(m3u8MediaUrlProcessor,(url,urlWOVar))
        GUI.progressBarGUI()
    except:
        printer.print("###Thread Error","e")
        printer.print(traceback.format_exc(),"e")

def thread_m3u8MediaFileProcessor(content):
    try:
        _thread.start_new_thread(m3u8MediaFileProcessor,(content,))
        GUI.progressBarGUI()
    except:
        printer.print("###Thread Error","e")
        printer.print(traceback.format_exc(),"e")


# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.db import migrations, models
from django.conf import settings

class Migration(migrations.Migration):

    dependencies = [
        ('projects', '0002_project_curr_phase'),
    ]

    operations = [
        migrations.AlterField(
            model_name='iterations_developers',
            name='developer',
            field=models.ForeignKey(to=settings.AUTH_USER_MODEL),
        ),
        migrations.AlterField(
            model_name='project',
            name='manager',
            field=models.ForeignKey(to=settings.AUTH_USER_MODEL),
        ),
    ]


import os
import numpy as np
import argparse

import tensorflow as tf
import keras

from keras.callbacks import EarlyStopping, ModelCheckpoint
from keras.layers import *
from keras import backend as K

parser = argparse.ArgumentParser(description='Train')
parser.add_argument('--gpu', type=str, default='0')
args = parser.parse_args()

os.environ["CUDA_VISIBLE_DEVICES"] = args.gpu

def video_base_model():

    _MASKING = 0
    _MAX_LEN = 6

    model = keras.models.Sequential()
    model.add(Masking(mask_value=_MASKING, input_shape=(_MAX_LEN, 4096)))
    model.add((LSTM(128, activation='relu')))
    model.add(Dense(64, activation='relu',name='BottleNeck'))
    model.add(Dense(7, name='emotion',activation='softmax'))
    model.summary()
    return model

def main():

    # Load data and label
    _ROOT_PATH = 'features/'

    x_train = np.load(_ROOT_PATH + "video_train.npy")
    x_val = np.load(_ROOT_PATH + "video_val.npy")
    y_train = np.load(_ROOT_PATH + "label_train.npy")
    y_val = np.load(_ROOT_PATH + "label_val.npy")

    # Convert labels to categorical one-hot encoding
    y_train = keras.utils.to_categorical(y_train, num_classes=7)
    y_val = keras.utils.to_categorical(y_val, num_classes=7)

    # Training Parameter setting
    gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=0.9)
    config = tf.ConfigProto(device_count={'GPU': 1, 'CPU': 30},
            gpu_options=gpu_options)
    sess = tf.Session(config=config)
    keras.backend.set_session(sess)

    # Model build
    model = video_base_model()

    model_path = 'model/' + 'video_model_' + 'acc_{val_acc:.4f}.h5'
    checkpoint = ModelCheckpoint(filepath=model_path, monitor='val_loss',
            verbose=1, save_best_only=True)
    early_stopping = EarlyStopping(monitor='val_acc', min_delta=0.0005,
            patience=30, verbose=1, mode='auto')

    # Train
    sgd = keras.optimizers.SGD(lr=0.01, decay=1e-6, momentum=0.9, nesterov=True)
    model.compile(loss='categorical_crossentropy', optimizer=sgd,
            metrics=['accuracy'])
    model.fit(x_train, y_train, batch_size=256, epochs=300,
            validation_data=(x_val,y_val), verbose=1,
            callbacks=[early_stopping, checkpoint])

    # Evaluation
    score = model.evaluate(x_val, y_val, batch_size=256)

    print(score)
    print('Test score:', score[0])
    print('Test accuracy:', score[1])

    K.clear_session()

if __name__ == '__main__':
    main()


from django.http import HttpResponseForbidden
from django.shortcuts import redirect, render
from django.contrib.auth.decorators import login_required
from django.conf import settings
from .models import API
from accounts.models import PAYMENT

import time
from datetime import date

from django.utils.encoding import force_bytes
from django.utils.http import urlsafe_base64_encode

# def paymentdouble(request):
#     final = True
#     paymentpurchased = PAYMENT.objects.filter(user=request.user, status="purchased")
#     paymentpaid = PAYMENT.objects.filter(user=request.user, status="paid")
#     if paymentpurchased.exists():
#         paypurobj = paymentpurchased.last()
#         if len(paymentpurchased) > 1:
#             for payments in paymentpurchased:
#                 paypurobj

def index(request):

    return render(request, "index.html")

@login_required(login_url='/signin/')
def pricing(request):
    paymentpurchased = PAYMENT.objects.filter(user=request.user, status="purchased")
    if paymentpurchased.exists():
        return render(request, "upgrade.html")
    context = {
        "stripe": settings.STRIPE_PUBLISHABLE_KEY,
    }
    return render(request, "pricing.html", context)

@login_required(login_url='/signin/')
def generateapi(request):
    api = API()
    payment = PAYMENT.objects.filter(user=request.user, status="paid")
    if payment.exists():
        tim = str(time.time()).split(".")[0]
        payment = payment.last()
        api.user = request.user
        api.key = urlsafe_base64_encode(force_bytes(tim))
        api.payment = payment
        api.save()
        payment.status = 'purchased'
        payment.save()
        return redirect("/setting/")
    return HttpResponseForbidden()

@login_required(login_url='/signin/')
def setting(request):
    payment = PAYMENT.objects.filter(user=request.user, status="purchased")
    if payment.exists():
        payment = payment.last()
        api = API.objects.filter(user=request.user, payment=payment).last()

        # Date Section

        olddate = api.created_at
        newdate = date.today()
        date1 = date(olddate.year, olddate.month, olddate.day)
        date2 = date(newdate.year, newdate.month, newdate.day)
        days_left = payment.sub_exp - (date2-date1).days
        context = {
            'api': api,
            "days_left": days_left if days_left > 0 else 0,
        }
        return render(request, "setting.html", context)
    return redirect('pricing')

def about(request):
    return render(request, "about.html")

def contact(request):
    context = {
        'black': True
    }
    return render(request, "contact.html", context)


import argparse
import lxml
import lxml.etree as ET
import sys
import codecs
import os

import html
import re

def lreplace(pattern, sub, string):
    """
    Replaces 'pattern' in 'string' with 'sub' if 'pattern' starts 'string'.
    """
    return re.sub('^%s' % pattern, sub, string)

def rreplace(pattern, sub, string):
    """
    Replaces 'pattern' in 'string' with 'sub' if 'pattern' ends 'string'.
    """
    return re.sub('%s$' % pattern, sub, string)

def FT2ST(segment):
    segmenttagsimple=segment
    segmenttagsimple=re.sub('(<[^>]+?/>)', "<t/>",segmenttagsimple)
    segmenttagsimple=re.sub('(</[^>]+?>)', "</t>",segmenttagsimple)
    segmenttagsimple=re.sub('(<[^/>]+?>)', "<t>",segmenttagsimple)
    return(segmenttagsimple)

def FT2NT(segment):
    segmentnotags=re.sub('(<[^>]+>)', " ",segment)
    segmentnotags=' '.join(segmentnotags.split()).strip()
    return(segmentnotags)

if __name__ == "__main__":
    parser = argparse.ArgumentParser(description='MTUOC program for converting all the TMX files in a given directory into a tab text.')
    parser.add_argument('-d','--dir', action="store", dest="inputdir", help='The input directory where the TMX files are located.',required=True)
    parser.add_argument('-o','--out', action="store", dest="outputfile", help='The output text file.',required=True)
    parser.add_argument('-s','--sl', nargs='+', action="store", dest="slcode", help='The codes for the source language.',required=False)
    parser.add_argument('-t','--tl', nargs='+', action="store", dest="tlcode", help='The codes for the target language.',required=False)
    parser.add_argument('--noTags', action='store_true', default=False, dest='noTags',help='Removes the internal tags.')
    parser.add_argument('--simpleTags', action='store_true', default=False, dest='simpleTags',help='Replaces tags with <t>, </t> or <t/>.')
    parser.add_argument('--noEntities', action='store_true', default=False, dest='noEntities',help='Replaces html/xml entities by corresponding characters.')

    args = parser.parse_args()
    direntrada=args.inputdir
    fsortida=args.outputfile
    sortida=codecs.open(fsortida,"w",encoding="utf-8")
    l1=args.slcode
    l2=args.tlcode

    parser = ET.XMLParser(recover=True)
    for root, dirs, files in os.walk(direntrada):
        for file in files:
            if file.endswith(".tmx"):
                try:
                    fentrada=os.path.join(root, file)
                    print(fentrada)

                    tree = ET.parse(fentrada, parser=parser)
                    rootT = tree.getroot()

                    for tu in rootT.iter('tu'):
                        sl_text=""
                        tl_text=""
                        for tuv in tu.iter('tuv'):
                            lang=tuv.attrib['{http://www.w3.org/XML/1998/namespace}lang']
                            for seg in tuv.iter('seg'):
                                try:
                                    text=ET.tostring(seg).decode("'utf-8").strip()
                                    text=lreplace("<seg>","",text)
                                    text=rreplace("</seg>","",text)
                                    if args.noEntities:
                                        text=html.unescape(text)
                                    if args.simpleTags:
                                        text=FT2ST(text)
                                    if args.noTags:
                                        text=FT2NT(text)
                                    if lang in l1: sl_text=text.replace("\n"," ")
                                    elif lang in l2: tl_text=text.replace("\n"," ")
                                except:
                                    sl_text=""
                                    tl_text=""

                        if not sl_text=="" and not tl_text=="":
                            try:
                                cadena=sl_text+"\t"+tl_text
                                sortida.write(cadena+"\n")
                            except:
                                pass
                except:
                    pass


# https://programmers.co.kr/learn/courses/30/lessons/67259
from collections import deque

def solution(board):
    dx = [0, 1, -1, 0]
    dy = [1, 0, 0, -1]

    n = len(board)
    visit = [[[10e9, 10e9, 10e9, 10e9] for _ in range(n)] for _ in range(n)]
    dq = deque()

    dq.append((0, 0, 0, 0))
    dq.append((0, 0, 0, 1))

    while (dq):
        nx, ny, c, state = dq.popleft()
        for i in range(4):
            temp = c + 100
            vx = nx + dx[i]
            vy = ny + dy[i]
            if -1 < vx < n and -1 < vy < n and board[vx][vy] == 0:
                if state != i:
                    temp += 500
                if visit[vx][vy][i] > temp:
                    visit[vx][vy][i] = temp
                    dq.append((vx, vy, temp, i))

    answer = min(visit[n - 1][n - 1])
    return answer


import numpy as np
import cv2
# import matplotlib

img = cv2.imread('data/gradient.png', 0)
img2 = cv2.imread('data/sudoku.png', 0)
_, th1 = cv2.threshold(img2, 127, 255, cv2.THRESH_BINARY)
th2 = cv2.adaptiveThreshold(img2, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY, 11, 10)
th3 = cv2.adaptiveThreshold(img2, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, 11, 10)

cv2.imshow('res', img2)
cv2.imshow('th2', th2)
cv2.imshow('th3', th3)

cv2.waitKey(0)
cv2.destroyAllWindows()


# -*- coding: utf-8 -*-
# Define here the models for your spider middleware
#
# See documentation in:
# https://doc.scrapy.org/en/latest/topics/spider-middleware.html
'''
Created on 05-Jun-2018

@author: srinivasan
'''

import logging
from scrapy import signals
from scrapy.http.response.html import HtmlResponse
from scrapy.utils.response import response_status_message
from selenium.webdriver.support.wait import WebDriverWait
import time
import tldextract

from scrapy.downloadermiddlewares.retry import RetryMiddleware
from scrapy.exceptions import IgnoreRequest, NotConfigured

from Data_scuff.extensions.exception.seleniumexception import SeleniumExtensionsException
from Data_scuff.extensions.request import SeleniumRequest
from Data_scuff.selenium.driver import Driver

logger = logging.getLogger(__name__)

class SeleniumMiddleware:

    def __init__(self, driver_name, driver_type):
        try:
            from shutil import which
            self.driver = Driver(driver_type).getDriver(executable_path=which(driver_name),
                                                        run_headless=True, load_images=False)
        except Exception as e:
            raise SeleniumExtensionsException("problem to get selenium driver-{}", e)

    @classmethod
    def from_crawler(cls, crawler):
        driver_name = crawler.settings.get('SELENIUM_DRIVER_NAME')
        driver_type = crawler.settings.get('SELENIUM_DRIVER_TYPE')
        if not driver_name :
            raise NotConfigured('SELENIUM_DRIVER_NAME and SELENIUM_DRIVER_TYPE  must be set')
        middleware = cls(driver_name=driver_name, driver_type=driver_type)
        crawler.signals.connect(middleware.spider_closed, signals.spider_closed)
        return middleware

    def process_request(self, request, spider):
        if not isinstance(request, SeleniumRequest):
            return request
        self.driver.get(request.url)

        for cookie_name, cookie_value in request.cookies.items():
            self.driver.add_cookie(
                {
                    'name': cookie_name,
                    'value': cookie_value
                }
            )
        if request.wait_until:
            WebDriverWait(self.driver, request.wait_time).until(
                request.wait_until
            )
        body = str.encode(self.driver.page_source)
        request.meta.update({'driver': self.driver})
        return HtmlResponse(
            self.driver.current_url,
            body=body,
            encoding='utf-8',
            request=request)

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

class BlockDomainMiddleWare:

    def __init__(self, settings):
        self.settings = settings

    def process_request(self, request, spider):
        if self.settings.get('BLOCK_DOMAINS', []) or (hasattr(spider, 'blocked_domains') and spider.blocked_domains):
            blockedDomains = self.settings.getlist('BLOCK_DOMAINS', []) + spider.blocked_domains
            domain_obj = tldextract.extract(request.url)
            domain = domain_obj.registered_domain
            logger.info("Current domain {}".format(domain))
            if(domain in blockedDomains):
                logger.info("Blocked domain: {} (url: {})".format(domain, request.url))
                raise IgnoreRequest("URL blocked: {}".format(request.url))

    @classmethod
    def from_crawler(cls, crawler):
        return cls(crawler.settings)

"""
Don't allow the scrapy the already scrap data based on the url
"""

class DropDuplicateUrlMiddleWare:

    stats_name = 'dropDuplicateUrl'

    def __init__(self, stats):
        self.stats = stats
        self.urls_seen = set()

    def process_request(self, request, spider):
        if request.url not in getattr(spider, 'start_urls', []) + getattr(spider, 'dont_dropUrls', []):
            import hashlib
            value = hashlib.md5(request.url.encode('utf-8')).hexdigest()
            if value in self.urls_seen:
                self.stats.inc_value('{}/Error'.format(self.stats_name), spider=spider)
                logger.info("Ignore this {} URL it's already scraped".format(request.url))
                raise IgnoreRequest("Ignore this {} URL it's already scraped".format(request.url))
            else:
                self.urls_seen.add(value)

    @classmethod
    def from_crawler(cls, crawler):
        return cls(crawler.stats)

"""
Delay the Request until page download completed
"""

class DelayedRequestMiddleware:

    def __init__(self, settings):
        self.settings = settings

    @classmethod
    def from_crawler(cls, crawler):
        return cls(crawler.settings)

    def process_request(self, request, spider):
        if self.settings.getbool('DEALY_REQ_ENABLED', False) or \
            request.meta.get('delay_request', None):
            self.crawler.engine.pause()
            time.sleep(self.settings.getint("DEALY_SECONDS", default=30))
            self.crawler.engine.unpause()

class TooManyRequestsRetryMiddleware(RetryMiddleware):

    def __init__(self, crawler):
        super(TooManyRequestsRetryMiddleware, self).__init__(crawler.settings)
        self.crawler = crawler

    @classmethod
    def from_crawler(cls, crawler):
        return cls(crawler)

    def process_response(self, request, response, spider):
        if request.meta.get('dont_retry', False):
            return response
        elif response.status in [429, 500, 405, 403, 409]:
            self.crawler.engine.pause()
            time.sleep(60)
            self.crawler.engine.unpause()
            reason = response_status_message(response.status)
            return self._retry(request, reason, spider) or response
        elif response.status in [501]:
            self.crawler.engine.pause()
            time.sleep(300)
            self.crawler.engine.unpause()
            reason = response_status_message(response.status)
            return self._retry(request, reason, spider) or response

        elif response.status in self.retry_http_codes:
            reason = response_status_message(response.status)
            return self._retry(request, reason, spider) or response
        return response


from flask import flash, request
from mysqlconnection import connectToMySQL

class Survey():
    def __init__(self,data):
        self.id = data['id']
        self.name = data['name']
        self.location = data['location']
        self.language = data['language']
        self.comment = data['comment']
        self.created_at = data['created_at']
        self.updated_at = data['updated_at']

    @classmethod
    def create_survey(cls, data):
        query = "INSERT INTO students (name, location, language, comment) VALUES (%(name)s, %(location)s, %(language)s, %(comment)s);"
        new_survey = connectToMySQL('dojo_survey_schema').query_db(query, data)
        return new_survey

    @staticmethod
    def validate_survey(survey):
        is_valid = True
        if len(survey['name']) < 3:
            flash("Name must be at least 3 characters.")
            is_valid = False
        if len(survey['location']) < 3:
            flash("Location must be at least 3 characters.")
            is_valid = False
        if len(survey['language']) < 3:
            flash("Language must be at least 3 characters.")
            is_valid = False
        if len(survey['comment']) < 1:
            flash("A comment is required.")
            is_valid = False
        return is_valid


from django.shortcuts import render, get_object_or_404, redirect
from django.http import JsonResponse
from django.core import serializers
from django.views.generic import ListView, DetailView, CreateView
from django.db.models import Sum, Count, Avg, Q, Count, Case, When, F, Func
from django.db.models.functions import TruncMonth, Extract
from django.contrib.auth.mixins import LoginRequiredMixin, UserPassesTestMixin
from django.contrib.auth.models import User
from django.views.generic import (
    ListView,
    DetailView,
    CreateView,
    UpdateView,
    DeleteView,
    FormView,
)
from django.urls import reverse_lazy

# Import users views
from users import views as user_views

# Models created
from .models import BillPaid, Carrier, Bill, Product

# Forms created
from .forms import NewBillForm, NewCarrierForm, NewProductForm, UpdateBillForm

# Class to have output rounded, used for averages
class Round(Func):
    function = "ROUND"
    template = "%(function)s(%(expressions)s, 2)"

"""
# Class to have month-year
class MonthYear(Func):
    function1 = "MONTH"
    function2 = "YEAR"
    template = "%(function1)s(%(expressions)s) - %(function2)s(%(expressions)s)"
"""

# Homepage view.
def homepage(request):
    # Shows any unpaid bills:
    unpaid = (
        BillPaid.objects.all()
        .filter(paidBool=0)
        .values("paidID", "billID", "totalPaid", "notes")
    )

    # Shows summed amount for total due
    unpaid_total = (
        BillPaid.objects.all().filter(paidBool=0).aggregate(grandTotal=Sum("totalPaid"))
    )
    # unpaid_total = 10
    # Show previous 5 bills
    paid = BillPaid.objects.all().select_related("billID").order_by("-paidDate")[:5]

    # Group by, average each carrier type
    averages = (
        Bill.objects.select_related("carrierID")
        .values("carrierID")
        .annotate(avgCarrierAmount=Round(Avg(F("charge") + F("anc_fees") + F("taxes"))))
        .order_by("carrierID")
        .values("carrierID", "avgCarrierAmount")
    )

    carriers = (
        Carrier.objects.values("carrierId", "carrierName")
        .annotate(carrierID=F("carrierId"))
        .values("carrierID", "carrierName")
    )

    averages_dict = list(averages)
    carriers_dict = list(carriers)

    # Creates a list of dictionaries which combines the Averages and Carriers queries
    combined = []
    for i in range(len(averages_dict)):
        combined.append(averages_dict[i] | carriers_dict[i])
        print(combined)

    # Total average amount
    averages_total = (
        Bill.objects.values("carrierID")
        .annotate(avgCarrierAmount=Round(Avg(F("charge") + F("anc_fees") + F("taxes"))))
        .aggregate(Sum("avgCarrierAmount"))
    )

    context = {
        "unpaid": unpaid,
        "paid": paid,
        "unpaid_total": unpaid_total,
        "averages": averages,
        "averages_total": averages_total,
        "combinedAvg": combined,
    }
    return render(request, "bills/home.html", context=context)

# Average bill view
def AverageBillPaidView(request):
    # Get the running average billed amount for all bills
    runningAvg = (
        Bill.objects.values("carrierID")
        .annotate(avgCarrierAmount=Round(Avg(F("charge") + F("anc_fees") + F("taxes"))))
        .aggregate(totalavgCarrierAmount=Sum("avgCarrierAmount"))
    )

    # We want to get the average cost for each carrier/utility, by year and month
    monthlyAvg = (
        Bill.objects.select_related("related bill", "related carrier")
        .prefetch_related("related bill", "related carrier")
        .annotate(month=TruncMonth("dueDate"))
        .values("month", "carrierID_id")
        .annotate(
            avgCarrierAmount=Round(
                Avg(F("charge") + F("credit") + F("anc_fees") + F("charge"))
            )
        )
        .values("month", "avgCarrierAmount", "carrierID_id")
        .order_by("month", "carrierID_id")
    )

    carriers = (
        Carrier.objects.values("carrierId", "carrierName")
        .annotate(carrierID=F("carrierId"))
        .values("carrierID", "carrierName")
    )

    monthlyAvg_dict = list(monthlyAvg)
    carriers_dict = list(carriers)

    # Creates a list of dictionaries which combines the Averages and Carriers queries
    monthlyAvg = []
    for i in range(len(monthlyAvg_dict)):
        monthlyAvg.append(monthlyAvg_dict[i] | carriers_dict[i % len(carriers_dict)])

    # Combines the two items we want to show in view
    context = {"runningAvg": runningAvg, "monthlyAvg": monthlyAvg}

    return render(
        request=request, template_name="bills/bills-avg.html", context=context
    )

# Monthly Breakdown view
# Create a view that summarizes the bills table
def MonthlyBreakdownListView(request):
    # Will return a grouped breakdown for each month
    context = (
        BillPaid.objects.annotate(month=TruncMonth("paidDate"))
        .values("month")
        .annotate(sums=Sum("totalPaid"))
        .values("month", "sums")
        .order_by("month")
    )

    return render(
        request=request, template_name="bills/bills-mb.html", context={"mb": context}
    )

""" Adding New Bills, Carriers, Products, etc. """

class BillCreateView(LoginRequiredMixin, CreateView):
    form_class = NewBillForm
    template_name = "bills/bill-form.html"

    login_url = "/login/"
    redirect_field_name = "redirect_to"

    success_url = reverse_lazy("bills/home.html")

    def form_valid(self, form):
        return super().form_valid(form)

class CarrierCreateView(LoginRequiredMixin, CreateView):
    form_class = NewCarrierForm
    template_name = "bills/carrier-form.html"

    login_url = "/login/"
    redirect_field_name = "redirect_to"

    success_url = reverse_lazy("bills/home.html")

    def form_valid(self, form):
        return super().form_valid(form)

class ProductCreateView(LoginRequiredMixin, CreateView):
    form_class = NewProductForm
    template_name = "bills/product-form.html"

    login_url = "/login/"
    redirect_field_name = "redirect_to"

    success_url = reverse_lazy("bills/home.html")

    def form_valid(self, form):
        return super().form_valid(form)

""" Listing all available bills in order """

class BillListView(ListView):
    model = BillPaid
    # format: <app>/<model>_<viewtype>.html
    template_name = "bills/BillPaid_listview.html"
    context_object_name = "bills"
    ordering = ["-paidDate"]

""" Paying unpaid bills """

class paidBillDetailView(LoginRequiredMixin, DetailView):
    model = BillPaid
    fields = "__all__"

class paidBillUpdateView(LoginRequiredMixin, UpdateView):
    form_class = UpdateBillForm
    template_name = "bills/bill-form.html"
    login_url = "/login/"
    model = BillPaid
    success_url = reverse_lazy("bills/home.html")

    login_url = "/login/"
    redirect_field_name = "redirect_to"

    def form_valid(self, form):
        return super().form_valid(form)


import argparse
import os
import pickle
import subprocess
import sys
import tempfile
from pathlib import Path

import cog
import librosa
import matplotlib.pyplot as plt
import numpy as np
import soundfile as sf
import torch
import torch.utils.data as utils
from tqdm import tqdm

from models.MMUB import MIMO_UNet_Beamforming
#from models.FaSNet import FaSNet_origin
from models.SELDNet import Seldnet_augmented
from utility_functions import (gen_submission_list_task2, load_model,
                               spectrum_fast)

class Predictor(cog.Predictor):
    def setup(self):
        """Load the model"""
        use_cuda = False
        gpu_id = 0
        if use_cuda:
            self.device = "cuda:" + str(gpu_id)
        else:
            self.device = "cpu"
        task1_pretrained_path = "pretrained/baseline_task1_checkpoint"
        task2_pretrained_path = "pretrained/baseline_task2_checkpoint"
        self.model_task1 = MIMO_UNet_Beamforming(
            fft_size=512,
            hop_size=128,
            input_channel=4)

        self.model_task2 = Seldnet_augmented(
            time_dim=2400,
            freq_dim=256,
            input_channels=4,
            output_classes=14,
            pool_size=[[8, 2], [8, 2], [2, 2], [1, 1]],
            pool_time=True,
            rnn_size=256,
            n_rnn=3,
            fc_size=1024,
            dropout_perc=0.3,
            cnn_filters=[64, 128, 256, 512],
            class_overlaps=3,
            verbose=False,
        )
        self.model_task1 = self.model_task1.to(self.device)
        self.model_task2 = self.model_task2.to(self.device)

        print("loading models")
        _ = load_model(self.model_task1, None, task1_pretrained_path, use_cuda)
        _ = load_model(self.model_task2, None, task2_pretrained_path, use_cuda)
        self.model_task1.eval()
        self.model_task2.eval()

    @cog.input(
        "input",
        type=Path,
        help="Input 1st order Ambisonics sound path"
        )
    @cog.input(
        "task", type=int, help="Task to evaluate", default=1, options=[1, 2]
    )
    @cog.input(
        "output_type",
        type=str,
        help="Can be 'data' or 'plot'",
        default="plot",
        options=["data", "plot"],
    )
    def predict(self, input, task, output_type):
        """Compute prediction"""
        # preprocessing
        sr_task1 = 16000
        sr_task2 = 32000
        input = str(input)

        if task == 1:
            x_in, sr = librosa.load(input, sr_task1, mono=False)
            x = torch.tensor(x_in).float().unsqueeze(0)
            with torch.no_grad():
                y = enhance_sound(x, self.model_task1, self.device, 76672, 0.5)
                y = np.squeeze(y)
                # write output
                if output_type == "data":
                    output_path_wav = Path(tempfile.mkdtemp()) / "output.wav"
                    output_path_mp3 = Path(tempfile.mkdtemp()) / "output.mp3"
                    sf.write(output_path_wav, y, sr_task1)
                    subprocess.check_output(
                        [
                            "ffmpeg",
                            "-i",
                            str(output_path_wav),
                            "-ab",
                            "320k",
                            str(output_path_mp3),
                        ],
                    )
                    np.save("test_input/prova_in.npx", x_in)
                    np.save("test_input/prova_out.npx", y)

                    return output_path_mp3

                elif output_type == "plot":
                    output_path_png = Path(tempfile.mkdtemp()) / "output.png"
                    plot_task1(output_path_png, x_in, y)

                    return output_path_png

        elif task == 2:
            x, sr = librosa.load(input, sr_task2, mono=False)
            x = spectrum_fast(
                x, nperseg=512, noverlap=112, window="hamming", output_phase=False
            )
            x = torch.tensor(x).float().unsqueeze(0)
            with torch.no_grad():
                sed, doa = self.model_task2(x)
            sed = sed.cpu().numpy().squeeze()
            doa = doa.cpu().numpy().squeeze()

            # write output
            if output_type == "data":
                seld = gen_submission_list_task2(
                    sed, doa, max_overlaps=3, max_loc_value=1
                )

                return seld

            elif output_type == "plot":
                output_path_png = Path(tempfile.mkdtemp()) / "output.png"
                plot_task2(output_path_png, sed, doa)

                return output_path_png

def enhance_sound(predictors, model, device, length, overlap):
    """
    Compute enhanced waveform using a trained model,
    applying a sliding crossfading window
    """

    def pad(x, d):
        # zeropad to desired length
        pad = torch.zeros((x.shape[0], x.shape[1], d))
        pad[:, :, : x.shape[-1]] = x
        return pad

    def xfade(x1, x2, fade_samps, exp=1.0):
        # simple linear/exponential crossfade and concatenation
        out = []
        fadein = np.arange(fade_samps) / fade_samps
        fadeout = np.arange(fade_samps, 0, -1) / fade_samps
        fade_in = fadein * exp
        fade_out = fadeout * exp
        x1[:, :, -fade_samps:] = x1[:, :, -fade_samps:] * fadeout
        x2[:, :, :fade_samps] = x2[:, :, :fade_samps] * fadein
        left = x1[:, :, :-fade_samps]
        center = x1[:, :, -fade_samps:] + x2[:, :, :fade_samps]
        end = x2[:, :, fade_samps:]
        return np.concatenate((left, center, end), axis=-1)

    overlap_len = int(length * overlap)  # in samples
    total_len = predictors.shape[-1]
    starts = np.arange(0, total_len, overlap_len)  # points to cut
    # iterate the sliding frames
    for i in range(len(starts)):
        start = starts[i]
        end = starts[i] + length
        if end < total_len:
            cut_x = predictors[:, :, start:end]
        else:
            # zeropad the last frame
            end = total_len
            cut_x = pad(predictors[:, :, start:end], length)

        # compute model's output
        cut_x = cut_x.to(device)
        predicted_x = model(cut_x, torch.tensor([0.0]))
        predicted_x = predicted_x.cpu().numpy()

        # reconstruct sound crossfading segments
        if i == 0:
            recon = predicted_x
        else:
            recon = xfade(recon, predicted_x, overlap_len)

    # undo final pad
    recon = recon[:, :, :total_len]

    return recon

def plot_task1(output_path, x, y):
    x = np.mean(x, axis=0)

    plt.figure(1)
    plt.suptitle("TASK 1 BASELINE MODEL", fontweight="bold")
    plt.subplot(211)
    plt.title("Noisy input (mono sum)")
    plt.specgram(x, NFFT=512, Fs=16000, mode="psd", scale="dB")
    plt.ylabel("Frequency (Hz)")
    plt.subplot(212)
    plt.title("Enhanced output")
    plt.specgram(y, NFFT=512, Fs=16000, mode="psd", scale="dB")
    plt.ylabel("Frequency (Hz)")
    plt.xlabel("Time (seconds)")
    plt.ylabel("Frequency")

    plt.tight_layout()
    plt.savefig(output_path, format="png", dpi=300)

def plot_task2(output_path, sed, doa):
    n = sed.shape[1]
    x = doa[:, :n]
    y = doa[:, n : n * 2]
    z = doa[:, n * 2 :]
    positions = np.arange(0, sed.shape[0] + 1, 50)
    labels = np.array(positions / 10, dtype="int32")

    plt.figure(1)
    plt.suptitle("TASK 2 BASELINE MODEL", fontweight="bold")

    plt.subplot(221)
    plt.title("Sound activations")
    plt.pcolormesh(sed.T)
    plt.ylabel("Sound class")
    plt.xticks(positions, labels)
    plt.subplot(222)
    plt.title("X axis")
    plt.pcolormesh(x.T)
    plt.xticks(positions, labels)
    plt.subplot(223)
    plt.title("Y axis")
    plt.pcolormesh(y.T)
    plt.ylabel("Sound class")
    plt.xlabel("Time")
    plt.xticks(positions, labels)
    plt.subplot(224)
    plt.title("Z axis")
    plt.pcolormesh(z.T)
    plt.xticks(positions, labels)
    plt.xlabel("Time")

    plt.tight_layout()
    plt.savefig(output_path, format="png", dpi=300)


import requests
import polars as pl
from io import StringIO
import os

def get_download_link(ticker):
    period_1 = "1"
    period_2 = "9999999999"

    link = "https://query1.finance.yahoo.com/v7/finance/download/" + ticker + "?period1=" + period_1 + "&period2=" + period_2 + "&interval=1d&events=history&includeAdjustedClose=true"

    return link

def get_data(link):
    try:
        response = requests.get(link, headers={'User-agent': 'Mozilla/5.0'})
        response.raise_for_status()

        return response.text
    except requests.exceptions.RequestException as e:
        print(e.response.text)

def clean_data(data: str) -> pl.DataFrame:
    df = pl.read_csv(StringIO(data))
    # df = df.drop(["Open","High","Low","Close"])

    # df = df.rename({"Adj Close": "Price"})

    return df

def collect_price_historical(ticker):
    link = get_download_link(ticker)
    data = get_data(link)
    df = clean_data(data)

    return df

def save_price_historical(df, ticker, fs, s3_prefix):
    filepath = os.path.join(s3_prefix, "prices", ticker + "_prices.csv")
    with fs.open(filepath, 'wb') as outfile:
        df.write_csv(outfile)

def collect_index_historical(ticker):
    link = get_download_link(ticker)
    data = get_data(link)
    df = clean_data(data)

    df = df.select(pl.col(["Date", "Adj Close"]))
    df = df.rename({"Adj Close": ticker})

    return df

def save_index_historical(df, ticker, fs, s3_prefix):
    filepath = os.path.join(s3_prefix, "indexes", ticker + ".csv")
    with fs.open(filepath, 'wb') as outfile:
        df.write_csv(outfile)

def main():
    df = collect_price_historical("F")
    # collect_price_historical("AAPL")

if __name__ == "__main__":
    main()


# -*- coding: utf-8 -*-
"""
Created on Sat Oct 14 17:57:35 2017

@author: ruonan.ding
"""

import numpy as np
from collections import defaultdict
import itertools
import sys

import gym
env = gym.make("Taxi-v2")
observation = env.reset()

def elipson_Greedy_policy(Q, epsilon, nA):
    def policy_fn(observation):
        A = np.ones(nA, dtype=float) * epsilon / nA
        best_action = np.argmax(Q[observation])
        A[best_action] += (1.0 - epsilon)
        return A
    return policy_fn

def q_learning(env, num_episodes, discount_factor=1.0, alpha=0.5, epsilon=0.1):
    """
    Q-Learning algorithm: Off-policy TD control. Finds the optimal greedy policy
    while following an epsilon-greedy policy

    Args:
        env: OpenAI environment.
        num_episodes: Number of episodes to run for.
        discount_factor: Lambda time discount factor.
        alpha: TD learning rate.
        epsilon: Chance the sample a random action. Float betwen 0 and 1.

    Returns:
        A tuple (Q, episode_lengths).
        Q is the optimal action-value function, a dictionary mapping state -> action values.
        stats is an EpisodeStats object with two numpy arrays for episode_lengths and episode_rewards.
    """

    # The final action-value function.
    # A nested dictionary that maps state -> (action -> action-value).
    Q = defaultdict(lambda: np.zeros(env.action_space.n))

    # The policy we're following
    policy = elipson_Greedy_policy(Q, epsilon, env.action_space.n)

    for i_episode in range(num_episodes):
        # Print out which episode we're on, useful for debugging.
        if (i_episode + 1) % 100 == 0:
            print("\rEpisode {}/{}.".format(i_episode + 1, num_episodes), end="")
            sys.stdout.flush()

        # Reset the environment and pick the first action
        state = env.reset()

        # One step in the environment
        # total_reward = 0.0
        for t in itertools.count():

            # Take a step
            action_probs = policy(state)
            action = np.random.choice(np.arange(len(action_probs)), p=action_probs)
            next_state, reward, done, _ = env.step(action)

            # TD Update
            best_next_action = np.argmax(Q[next_state])
            td_target = reward + discount_factor * Q[next_state][best_next_action]
            td_delta = td_target - Q[state][action]
            Q[state][action] += alpha * td_delta

            if done:
                td_target = reward
                td_delta = td_target - Q[state][action]
                Q[state][action] += alpha * td_delta
                break

            state = next_state

    return Q

Q = q_learning(env, 15000, discount_factor= 0.9, alpha=1, epsilon=0.5)

print(Q[83][5])
print(Q[377][1])
print(Q[253][0])
print(Q[398][3])
print(Q[462][4])

Q[339][3]


from typing import List

import numpy as np

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

class VAE(nn.Module):
    def __init__(
        self,
        in_channels: int = 3,
        input_size: int = 128,
        hidden_dims: List[int] = [32, 32, 64, 128],
        latent_dim: int = 512,
        **kwargs,
    ) -> None:
        super(VAE, self).__init__()

        self.in_channels = in_channels
        self.input_size = input_size
        self.hidden_dims = hidden_dims
        self.latent_dim = latent_dim

        current_size = self.input_size
        # Build Encoder
        module_list = nn.ModuleList()
        for h_dim in hidden_dims:
            module_list.append(
                nn.Sequential(
                    nn.Conv2d(
                        in_channels=in_channels,
                        out_channels=h_dim,
                        kernel_size=3,
                        stride=2,
                        padding=1,
                    ),
                    nn.BatchNorm2d(h_dim),
                    nn.LeakyReLU(),
                )
            )
            current_size = current_size // 2
            in_channels = h_dim
        self.encoder = nn.Sequential(*module_list)

        self.intermediate_size = current_size

        self.fc_mu = nn.Linear((current_size**2) * self.hidden_dims[-1], self.latent_dim)
        self.fc_var = nn.Linear((current_size**2) * self.hidden_dims[-1], self.latent_dim)

        hidden_dims.reverse()

        # Build Decoder
        self.decoder_input = nn.Linear(self.latent_dim, (current_size**2) * self.hidden_dims[-1])
        module_list = nn.ModuleList()
        for i in range(len(hidden_dims) - 1):
            module_list.append(
                nn.Sequential(
                    nn.ConvTranspose2d(
                        in_channels=hidden_dims[i],
                        out_channels=hidden_dims[i + 1],
                        kernel_size=3,
                        stride=2,
                        padding=1,
                        output_padding=1,
                    ),
                    nn.BatchNorm2d(hidden_dims[i + 1]),
                    nn.LeakyReLU(),
                )
            )

        module_list.append(
            nn.Sequential(
                nn.ConvTranspose2d(
                    in_channels=hidden_dims[-1],
                    out_channels=hidden_dims[-1],
                    kernel_size=3,
                    stride=2,
                    padding=1,
                    output_padding=1,
                ),
                nn.BatchNorm2d(hidden_dims[-1]),
                nn.LeakyReLU(),
                nn.Conv2d(in_channels=hidden_dims[-1], out_channels=self.in_channels, kernel_size=3, padding="same"),
                nn.Tanh(),
            )
        )
        self.decoder = nn.Sequential(*module_list)

    def encode(self, input: torch.Tensor) -> List[torch.Tensor]:
        """Encodes the input by passing through the encoder network
        and returns the latent codes.

        :param input: _description_
        :type input: torch.Tensor
        :raises NotImplementedError: _description_
        :return: _description_
        :rtype: List[torch.Tensor]
        """

        x = self.encoder(input)
        x = torch.flatten(x, start_dim=1)

        # Split the result into mu and var components
        # of the latent Gaussian distribution
        mu = self.fc_mu(x)
        log_var = self.fc_var(x)

        return [mu, log_var]

    def decode(self, input: torch.Tensor) -> torch.Tensor:
        """_summary_

        :param input: _description_
        :type input: torch.Tensor
        :return: _description_
        :rtype: torch.Tensor
        """
        x = self.decoder_input(input)

        # Reshaping back to 2D with the channels and the intermediate size it had after encoder
        x = x.view(-1, self.hidden_dims[-1], self.intermediate_size, self.intermediate_size)

        # Reconstructing image
        x = self.decoder(x)

        return x

    def reparameterize(self, mu: torch.Tensor, logvar: torch.Tensor) -> torch.Tensor:
        """
        Reparameterization trick to sample from N(mu, var) from
        N(0,1).

        :param mu: _description_
        :type mu: torch.Tensor
        :param logvar: _description_
        :type logvar: torch.Tensor
        :return: _description_
        :rtype: torch.Tensor
        """

        std = torch.exp(0.5 * logvar)
        eps = torch.randn_like(std)

        return eps * std + mu

    def forward(self, input: torch.Tensor) -> torch.Tensor:
        """_summary_

        :param input: _description_
        :type input: torch.Tensor
        :return: _description_
        :rtype: torch.Tensor
        """

        mu, logvar = self.encode(input)
        z = self.reparameterize(mu, logvar)

        return [self.decode(z), mu, logvar]

    def loss_function(
        self,
        input: torch.Tensor,
        outputs: List[torch.Tensor],
        kld_weight: float,
        **kwargs,
    ) -> dict:

        recons = outputs[0]
        mu = outputs[1]
        logvar = outputs[2]

        recons_loss = F.mse_loss(recons, input)

        kld_loss = torch.mean(-0.5 * torch.sum(1 + logvar - mu**2 - logvar.exp(), dim=1), dim=0)

        loss = recons_loss + kld_weight * kld_loss

        return loss, {"loss": loss.detach(), "Reconstruction_Loss": recons_loss.detach(), "KLD": -kld_loss.detach()}

    def sample(self, num_samples: int, current_device: int) -> torch.Tensor:

        z = torch.randn(num_samples, self.latent_dim)

        z = z.to(current_device)

        return self.decode(z)


#!/usr/bin/env python3
# -*- coding:utf-8 -*-
import pika

connection = pika.BlockingConnection(pika.ConnectionParameters(host='localhost'))

channel = connection.channel()

channel.exchange_declare(exchange='test_ch', exchange_type='direct', durable=True)

channel.queue_declare(queue='test_queue', durable=True)

channel.queue_bind(queue='test_queue', exchange='test_ch', routing_key='confirmation')

# 定义一个消息确认函数，消费者成功处理完消息后会给队列发送一个确认信息，然后该消息会被删除
def ack_info_handler(ch, method, properties, body):
    """ack_info_handler """
    print('ack_info_handler() called!')
    # 发送确认ack
    ch.basic_ack(delivery_tag=method.delivery_tag)
    if bytes.decode(body) == 'quit':
        ch.basic_cancel(consumer_tag='hello_confirmation')
        ch.stop_consuming()
    else:
        print(bytes.decode(body))

# 关闭自动确认消费ACK
channel.basic_consume(queue='test_queue', on_message_callback=ack_info_handler, auto_ack=False, consumer_tag='hello_confirmation')

print('ready to consume msg...')
channel.start_consuming()


from sqlalchemy import Column, Integer, String, Date, ForeignKey

from config import Base

class Event(Base):
    __tablename__ = 'event'

    id = Column(Integer, primary_key=True)
    name = Column(String(80), nullable=False)
    date = Column(Date, nullable=False)
    attendees = Column(Integer, nullable=False, default=0)

    created_by = Column(Integer, ForeignKey('user.id'), nullable=False)
    type = Column(Integer, ForeignKey('event_type.id'), nullable=False)


from flask import Blueprint, render_template, request, redirect, url_for
from app.database import get_db
from app.auth import login_required, has_role
from app.auth.roles import *
from .forms import CreateBathForm, EditBathForm

edit_bath_bp = Blueprint('edit_bath', __name__)

@edit_bath_bp.route('/add_bath', methods=['GET', 'POST'])
@has_role(ADMIN_ROLE)
@login_required
def add_bath():
	form = CreateBathForm(request.form)
	if request.method == 'POST' and form.validate():
		db = get_db()
		address = form.address.data
		per_hour_rent = form.per_hour_rent.data
		min_order_time_hours = form.min_order_time_hours.data
		accessories = form.accessories.data
		working_hours_from = form.working_hours_from.data
		working_hours_to = form.working_hours_to.data
		human_capacity = form.human_capacity.data
		db.add_bath(address, per_hour_rent, min_order_time_hours, accessories,
					working_hours_from, working_hours_to, human_capacity)
		return redirect(url_for('admin.edit_bath.edit_bathes'))
	return render_template('edit_bathes/add_bath.html', form=form)

@edit_bath_bp.route('/edit_bathes', methods=['GET', 'POST'])
@has_role(ADMIN_ROLE)
@login_required
def edit_bathes():
	db = get_db()
	bathes = db.get_bathes()
	return render_template('edit_bathes/edit_bathes.html', bathes=bathes)

@edit_bath_bp.route('/edit_bath/<int:bath_id>', methods=['GET', 'POST'])
@has_role(ADMIN_ROLE)
@login_required
def edit_bath(bath_id: str):
	db = get_db()
	form = EditBathForm(request.form)
	if request.method == 'POST' and form.validate():
		address = form.address.data
		per_hour_rent = form.per_hour_rent.data
		min_order_time_hours = form.min_order_time_hours.data
		accessories = form.accessories.data
		working_hours_from = form.working_hours_from.data
		working_hours_to = form.working_hours_to.data
		human_capacity = form.human_capacity.data
		status = form.status.data
		db.update_bath_info(bath_id, address, per_hour_rent, min_order_time_hours, accessories, working_hours_from,
							working_hours_to, human_capacity, status)

		return redirect(url_for('admin.edit_bath.edit_bathes'))

	bath = db.execute_query_fetchone(f"SELECT * from bath WHERE bath_id = '{bath_id}'")
	data = {k.replace('bath_', ''): v for k, v in bath.items()}
	form.process(data=data)
	form.per_hour_rent.render_kw = {'placeholder': data['per_hour_rent']}
	return render_template('edit_bathes/edit_bath.html', form=form, bath=bath)


import requests
from lxml import etree

def get_index():
    header = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) '
                      'Chrome/47.0.2526.108 Safari/537.36 2345Explorer/8.8.0.16453 '
    }
    url = 'http://sh.ziroom.com/z/nl/z1.html'
    response = requests.get(url, headers=header)
    return response.text

def parse_index(html):
    url_list = []
    name_list = []
    data = etree.HTML(html)
    items = data.xpath('//*[@id="selection"]/div/div/dl[2]/dd/ul/li[position()>1]')
    for item in items:
        href = item.xpath('./span/a/@href')
        name = item.xpath('./span/a/text()')
        # print(href[0].replace('//', ''))
        url_list.append(href[0].replace('//', 'http://'))
        name_list.append(name[0])
    # print(url_list)
    return url_list, name_list

def districts():
    html = get_index()
    list = parse_index(html)
    return list

if __name__ == '__main__':
    districts()


from rest_framework.permissions import BasePermission
from rest_framework.request import Request

from main.models import APIKeyManager

class HasAPIKey(BasePermission):
    message = 'Invalid API Key'

    def has_permission(self, request: Request, view) -> bool:
        if 'HTTP_X_API_KEY' in request.META:
            key = request.META['HTTP_X_API_KEY']
            if APIKeyManager().validate_key(key):
                return True
        return False


import logging

from django.core.management.base import BaseCommand

from framework.celery_tasks import app as celery_app
from osf.models import Registration
from osf.utils.workflows import RegistrationModerationStates

logger = logging.getLogger(__name__)

@celery_app.task(name='management.commands.correct_registration_moderation_states')
def correct_registration_moderation_states(page_size=None):
    '''Backfill the moderation_state field of all registrations based on its current Sanction.'''

    # Any registration with a non-INITIAL state is already subject to the new
    # sanction state machine flows and should have the correct moderation_state
    # Unapproved RegistrationApprovals and Embargoes should have an INITIAL state,
    # and are excluded.
    default_value = RegistrationModerationStates.INITIAL.db_name
    out_of_date_registrations = Registration.objects.filter(
        deleted__isnull=True, moderation_state=default_value
    ).exclude(
        registration_approval__state='unapproved'
    ).exclude(embargo__state='unapproved')

    if page_size:
        out_of_date_registrations = out_of_date_registrations[:page_size]

    corrected_registration_count = 0
    for registration in out_of_date_registrations:
        registration.update_moderation_state()
        if registration.moderation_state != default_value:
            logger.info(
                f'Corrected moderation_state value for Registration with ID {registration._id}. '
                f'New value is {registration.moderation_state}'
            )
            corrected_registration_count += 1

    logger.info(f'Corrected the moderation_state for {corrected_registration_count} Registrations')
    return corrected_registration_count

class Command(BaseCommand):

    def add_arguments(self, parser):
        parser.add_argument(
            '--page_size',
            type=int,
            default=0,
            help='How many rows to process at a time. 0 indicates "all"',
        )

    def handle(self, *args, **options):
        page_size = options.get('page_size', 0)
        correct_registration_moderation_states(page_size)


import re

import nltk
from cleantext import clean
from nltk.tokenize import sent_tokenize

nltk.download("punkt")

def convert_text_into_sentences(text):
    assert isinstance(text, str)

    # Normalize a text.
    text = clean(
        text,
        fix_unicode=True,
        to_ascii=True,
        lower=True,
        no_line_breaks=False,
        no_urls=True,
        no_emails=True,
        no_phone_numbers=True,
        no_numbers=False,
        no_digits=False,
        no_currency_symbols=False,
        no_punct=False,
    )

    # Remove tags.
    text = re.sub("<[^<]+?>", "", text)
    # Remove invalid charactors.
    text = re.sub("[#%'\(\)\*\+\-\\\/:;<=>@^_`|~\[\]]+", "", text)
    # Convert a text into sentences.
    sentences = sent_tokenize(text)

    return sentences


import textwrap
import sys
from functools import partial
from datetime import datetime

LINEFEED = [chr(10)]
CR = [chr(13)]
MAX_SIZE = 48
ESC = chr(27)
GS = chr(29)
FULL_CUT = [ESC, "m"]
CENTER = [ESC, chr(97), chr(1)]
LEFT = [ESC, chr(97), chr(0)]
RIGHT = [ESC, chr(97), chr(2)]
FEED = [ESC + "J", chr(255)]
THICK_HR = "".join([chr(205) * MAX_SIZE])
THIN_HR = "".join([chr(196) * MAX_SIZE])
REGULAR = [GS, chr(77), chr(0)]
XS = [GS, chr(77), chr(1)]
XSS = [GS, chr(77), chr(2)]
DOUBLE = [GS, chr(33), chr(17)]
NORMAL = [GS, chr(33), chr(0)]
RESET = [ESC, chr(33), chr(0)]
ALL_RIGHT = [ESC, "D", chr(20), chr(15)]

def write_command(_printer, commands):
    for c in commands:
        _printer.write(c)
    return

def write_normal(_printer, command):
    _printer.write(command)
    return

with open("/dev/usb/lp0", "w") as printer:
    _w = partial(write_command, printer)
    w = partial(write_normal, printer)

    """for i in range(30, 255):

        printer.write(str(i))
        w(CENTER)
        printer.write(chr(32))

        printer.write(chr(i))
        printer.write(LINEFEED)
        printer.flush()
    printer.write(LINEFEED)

    # printer.write(ESC + str(74) + "150")
    # printer.write(LINEFEED)

    printer.flush()"""

    bla = sys.stdin.read()
    counter, text = bla.splitlines()

    now = datetime.now()

    _w(RESET)
    w(THICK_HR)
    _w(LINEFEED)
    _w(LEFT)
    _w(DOUBLE)
    w("EVERYTHING")
    _w(LINEFEED)
    w("DEVOLVES INTO")
    _w(LINEFEED)
    _w(CENTER)
    w("SILLY")
    _w(LINEFEED)
    _w(RIGHT)
    w("WORDS")
    _w(LINEFEED)
    _w(NORMAL)
    _w(LEFT)
    w(THIN_HR)
    _w(LINEFEED)
    w("Fabian Frei")  # 12
    w("".join([" "] * (48 - 14)))
    w(counter)  # 3 counter
    _w(LEFT)
    _w(LINEFEED)
    w("Linz, {}.{}.{}".format(now.day, now.month, now.year))
    _w(LINEFEED)
    _w(LINEFEED)
    _w(LINEFEED)
    _w(CENTER)
    w("you discovered")
    _w(LINEFEED)
    _w(LINEFEED)
    _w(DOUBLE)
    w(textwrap.fill(text, 20))
    _w(LINEFEED)
    _w(LINEFEED)
    _w(RESET)
    w("Thank you!")
    _w(LINEFEED)
    _w(THIN_HR)
    _w(LINEFEED)
    _w(LEFT)
    w(textwrap.fill("Go to tofu.wtf/buzzwords to see ALL the words", MAX_SIZE))
    _w(LINEFEED)
    _w(LINEFEED)
    _w(LINEFEED)
    _w(RESET)
    _w(CENTER)
    w("".join([chr(222), chr(220), chr(216), chr(220), chr(221)]))
    _w(LINEFEED)
    _w(LINEFEED)
    _w(FEED)
    _w(FULL_CUT)
    _w(RESET)
    printer.flush()