import re, time, sys, os
import editdistance
import pickle
import itertools

root = ""
path = os.path.join(root, "TRAIN_DATASET/")
from multiprocessing import Pool

NUM_PROCESSES = 1

def is_same_string(string_a, string_b, char_margin=3):

    if not string_a or len(string_a) == 0:
        raise ValueError('Invalid input string.')
    if not string_b or len(string_b) == 0:
        raise ValueError('Invalid input string.')

    d = editdistance.eval(string_a, string_b)

    if isinstance(char_margin, float):
        shortest_str_len = len(string_a) if len(string_a) < len(string_b) else len(string_b)
        return (False if float(d) / float(shortest_str_len) > char_margin else True), d
    else:
        return d < char_margin, d

def is_same_string_from_repo(website1_name, website2_name, string1, string2):

    vagalume_website_name = 'vagalume'

    if website1_name != vagalume_website_name or website2_name != vagalume_website_name:
        return False

    if string1 == string2 or string1 == string2 + " traducao" or string2 == string1 + " traducao":
        return True

    return False

def check_match(key1, key2):
# 'key[12]' is a string with the following:
# 'website_name|artist_name|lyrics_name'

    key1_split = key1.split('|')
    key2_split = key2.split('|')

    if not len(key1_split) == 3:
        print('Original key:{}\nSplit key:{}'.format(key1, key1_split))
        assert False
    assert len(key2_split) == 3

    try:
        # Checks whether artist name is the same
        is_same_artist_name, _ = is_same_string(key1_split[1], key2_split[1], 4)
        if not is_same_artist_name:
            return False

        is_same_lyrics_from_repo = is_same_string_from_repo(key1_split[0],
                                                                    key2_split[0],
                                                                    key1_split[2],
                                                                    key2_split[2])

        # Checks whether lyrics name is the same
        is_same_lyrics_name, _ = is_same_string(key1_split[2], key2_split[2], 4)
        if not is_same_lyrics_name and not is_same_lyrics_from_repo:
            return False

    except Exception:
        print("Error comparing '%s' and '%s'. Returning False for matching." % (key1, key2))
        return False

    return True

def generate_matches(lyrics_tuple_list):

    if not lyrics_tuple_list or len(lyrics_tuple_list) == 0:
        raise ValueError('Invalid lyrics dictionary')

    match_count = 0
    match_set = set()

    # combinations('ABCD', 2) --> AB AC AD BC BD CD

    for key1, key2 in itertools.combinations(lyrics_tuple_list, 2):
        if (key1, key2) not in match_set:
            is_match = check_match(key1, key2)
            if is_match:
                match_set.add((key1, key2))
                match_set.add((key2, key1))
                match_count += 1

    return match_count, match_set

def generate_ground_truth():

  pickle_ground_truth_output = "ground_truth_amostragem.p"
  tuples = []
  n_count = 0;

  for r,d,f in os.walk(path):
      for file in f:
        pathf = r.replace('\\','/')
        filename = pathf + '/' + file
        data = r.split("/")
        key = data[1] + "|" + data[2] + "|" + file
        print(key)
        #with open(filename, "rb") as fr:
        #  content = fr.read().decode("UTF-8")
        #t = (key,content)
        tuples.append(key)
        n_count = n_count + 1
      if n_count > 200000:
        break

  start = time.time()
  print('Split the data into {} chunks of {} elements.'.format(NUM_PROCESSES, len(tuples)))
  print("------------ generating pickle matches ---------------")
  count_true, matches = generate_matches(tuples)
  print('DONE!')

  #count_true = sum(r[0] for r in results)
  #matches = set.union(*(s[1] for s in results))

  print('Number of matches found = {}'.format(count_true))

  now = time.time() - start
  print("finished! minutes: ",now/60)
  #count_true, matches = generate_matches(tuples)

  with open(pickle_ground_truth_output, "wb") as file_out:
        pickle.dump((count_true, matches), file_out)
  print("------------------------------------------------------")

generate_ground_truth()


import os
import json

from . import data

def get_available_modules() -> dict:
    path = os.environ.get("DD_MODULES_PATH", "")
    if not path or not os.path.isdir(path):
        return {}

    modules = json.load(open(os.path.join(path, "modules.json")))
    for k in modules:
        fullpath = os.path.join(path, modules[k]["file"])
        assert os.path.exists(fullpath)
        modules[k]["file"] = fullpath

    return modules

def load_modules(for_guild=None) -> dict:
    path = os.path.join(data.get_data_dir(), "modules")
    if not os.path.isdir(path):
        return {}
    modules = {}

    if for_guild:
        files = [os.path.join(path, ("%x" % for_guild) + ".json")]
        if not os.path.exists(files[0]):
            return {}
    else:
        _, _, files = next(os.walk(path))

    for name in files:
        if name.endswith(".json"):
            p = os.path.join(path, name)
            with open(p) as f:
                try:
                    t = json.load(f)
                    modules[int(name[: -len(".json")], base=16)] = t
                except JSONDecodeError as e:
                    print(f"Error decoding {p}: {e}")
                    pass
    return modules

def save_modules(modules: dict, for_guild=None):
    path = os.path.join(data.get_data_dir(), "modules")
    if not os.path.isdir(path):
        os.makedirs(path, exist_ok=True)
    if for_guild and for_guild in modules:
        with open(os.path.join(path, ("%x" % for_guild) + ".json"), "w") as f:
            json.dump(modules[for_guild], f)
    else:
        for k, v in modules:
            with open(os.path.join(path, ("%x" % k) + ".json"), "w") as f:
                json.dump(v, f)


from __future__ import print_function, division
import numpy as np
import cv2
import glob
import pickle

# https://medium.com/@kennethjiang/calibrate-fisheye-lens-using-opencv-part-2-13990f1b157f
def undistort(img_path, camMat, dist, balance=0.0, dim2=None, dim3=None):
    img = cv2.imread(img_path)
    dim1 = img.shape[:2][::-1]  #dim1 is the dimension of input image to un-distort
    assert dim1[0]/dim1[1] == DIM[0]/DIM[1], "Image to undistort needs to have same aspect ratio as the ones used in calibration"
    if not dim2:
        dim2 = dim1
    if not dim3:
        dim3 = dim1
    scaled_K = K * dim1[0] / DIM[0]  # The values of K is to scale with image dimension.
    scaled_K[2][2] = 1.0  # Except that K[2][2] is always 1.0
    # This is how scaled_K, dim2 and balance are used to determine the final K used to un-distort image. OpenCV document failed to make this clear!
    new_K = cv2.fisheye.estimateNewCameraMatrixForUndistortRectify(scaled_K, D, dim2, np.eye(3), balance=balance)
    map1, map2 = cv2.fisheye.initUndistortRectifyMap(scaled_K, D, np.eye(3), new_K, dim3, cv2.CV_16SC2)
    undistorted_img = cv2.remap(img, map1, map2, interpolation=cv2.INTER_LINEAR, borderMode=cv2.BORDER_CONSTANT)
    cv2.imshow("undistorted", undistorted_img)
    cv2.waitKey(0)
    cv2.destroyAllWindows()

def main():
    # termination criteria
    criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 30, 0.001)

    # prepare object points, like (0,0,0), (1,0,0), (2,0,0) ....,(6,5,0)
    objp = np.zeros((6*9,3), np.float32)
    objp[:,:2] = np.mgrid[0:9,0:6].T.reshape(-1,2)
    objp = objp * 0.0254

    # Arrays to store object points and image points from all the images.
    objpoints = [] # 3d point in real world space
    imgpointsL = [] # 2d points in image plane.
    imgpointsR = [] # 2d points in image plane.

    images = glob.glob('*.jpg')
    images.sort()
    width = 0
    height =0

    two_cams = False

    for fname in images:
        img = cv2.imread(fname)
        height, width = img.shape[0:2]
        if two_cams:
            imgL = img[:,0:width//2]
            imgR = img[:,width//2 : width]
            grayL = cv2.cvtColor(imgL,cv2.COLOR_BGR2GRAY)
            grayR = cv2.cvtColor(imgR,cv2.COLOR_BGR2GRAY)
            # Find the chess board corners
            retR, cornersR = cv2.findChessboardCorners(grayR, (9,6), None)
        else:
            imgL = img
            retR = True

        grayL = cv2.cvtColor(imgL,cv2.COLOR_BGR2GRAY)
        retL, cornersL = cv2.findChessboardCorners(grayL, (9,6), None)

        # If found, add object points, image points (after refining them)
        if retL == True and retR == True:
            print(fname)
            objpoints.append(objp)

            corners2L = cv2.cornerSubPix(grayL,cornersL,(11,11),(-1,-1),criteria)
            imgpointsL.append(corners2L)

            if two_cams:
                corners2R = cv2.cornerSubPix(grayR,cornersR,(11,11),(-1,-1),criteria)
                imgpointsR.append(corners2R)
                offset = np.ones((len(corners2R), 1, 2)) * width//2

            # Draw and display the corners
            imgL = cv2.drawChessboardCorners(imgL, (9,6), corners2L, retL)
            cv2.imshow('imgL',imgL)
            cv2.waitKey(5)

    calibration_flags = cv2.fisheye.CALIB_RECOMPUTE_EXTRINSIC+cv2.fisheye.CALIB_CHECK_COND+cv2.fisheye.CALIB_FIX_SKEW

    M1 = np.zeros((3, 3))
    d1 = np.zeros((4, 1))
    r1 = [np.zeros((1, 1, 3), dtype=np.float64) for i in range(len(objpoints))]
    t1 = [np.zeros((1, 1, 3), dtype=np.float64) for i in range(len(objpoints))]

    # rms, _, _, _, _ =  cv2.fisheye.calibrate(np.reshape(objpoints, (len(objpoints), 1, -1, 3)),
    #                                          np.reshape(imgpointsL, (len(objpoints), 1, -1, 2)),
    #                                          grayL.shape[::-1],
    #                                          M1,
    #                                          d1,
    #                                          r1,
    #                                          t1,
    #                                          calibration_flags,
    #                                          (cv2.TERM_CRITERIA_EPS+cv2.TERM_CRITERIA_MAX_ITER, 100, 1e-6))

    # print(rms)
    # print(M1)
    # print(d1)

    # m1 = np.reshape([898.8330441160133, 0, 640, 0, 898.5978305609192, 480, 0, 0, 1], (3,3))
    # d1 = np.array([-0.3984360149537951, 0.1302381183957002, 0.009177901416678803, 0.007710077375683005, 0])

    flags = 0
    # flags |= cv2.CALIB_FIX_INTRINSIC
    flags |= cv2.CALIB_FIX_PRINCIPAL_POINT
    # flags |= cv2.CALIB_USE_INTRINSIC_GUESS
    # flags |= cv2.CALIB_FIX_FOCAL_LENGTH
    # flags |= cv2.CALIB_FIX_ASPECT_RATIO
    flags |= cv2.CALIB_ZERO_TANGENT_DIST
    # flags |= cv2.CALIB_RATIONAL_MODEL
    # flags |= cv2.CALIB_SAME_FOCAL_LENGTH
    # flags |= cv2.CALIB_FIX_K3
    # flags |= cv2.CALIB_FIX_K4
    # flags |= cv2.CALIB_FIX_K5

    if two_cams:
        rms, M1, d1, r1, t1 = cv2.calibrateCamera(
                    objpoints, imgpointsL, (width//2, height), None, None, flags=flags)
        rms, M2, d2, r2, t2 = cv2.calibrateCamera(
                    objpoints, imgpointsR, (width//2, height), None, None, flags=flags)

    else:
        rms, M1, d1, r1, t1 = cv2.calibrateCamera(
                    objpoints, imgpointsL, (width, height), None, None, flags=flags)

    print(rms)
    print(M1)
    print(d1)

    # tot_error = 0
    # for i in xrange(len(objpoints)):
    #     imgpoints2, _ = cv2.projectPoints(objpoints[i], r1[i], t1[i], M1, d1)
    #     error = cv2.norm(imgpointsL[i],imgpoints2, cv2.NORM_L2)/len(imgpoints2)
    #     tot_error += error
    #     imgpoints2, _ = cv2.projectPoints(objpoints[i], r2[i], t2[i], M2, d2)
    #     error = cv2.norm(imgpointsR[i],imgpoints2, cv2.NORM_L2)/len(imgpoints2)
    #     tot_error += error

    # print("mean error: ", tot_error/len(objpoints)/2)

    if two_cams:
        stereo_flags = 0
        # stereo_flags |= cv2.CALIB_FIX_INTRINSIC
        stereo_flags |= cv2.CALIB_FIX_PRINCIPAL_POINT
        stereo_flags |= cv2.CALIB_USE_INTRINSIC_GUESS
        # stereo_flags |= cv2.CALIB_FIX_FOCAL_LENGTH
        # stereo_flags |= cv2.CALIB_FIX_ASPECT_RATIO
        stereo_flags |= cv2.CALIB_ZERO_TANGENT_DIST
        # stereo_flags |= cv2.CALIB_RATIONAL_MODEL
        stereo_flags |= cv2.CALIB_SAME_FOCAL_LENGTH
        # stereo_flags |= cv2.CALIB_FIX_K3
        # stereo_flags |= cv2.CALIB_FIX_K4
        # stereo_flags |= cv2.CALIB_FIX_K5

        stereocalib_criteria = (cv2.TERM_CRITERIA_MAX_ITER +
                                cv2.TERM_CRITERIA_EPS, 100, 1e-5)

        ret, M1, d1, M2, d2, R, T, E, F = cv2.stereoCalibrate(
            objpoints, imgpointsL,
            imgpointsR, M1, d1, M2,
            d2, (height,width//2),
            criteria=stereocalib_criteria, flags=stereo_flags)

        print(ret)
        print(M1)
        print(d1)

        newcameramtx, roi=cv2.getOptimalNewCameraMatrix(M1,d1,(width//2, height),.99)
        imgL = cv2.imread(images[0])[:,0:width//2].copy()

    else:
        newcameramtx, roi=cv2.getOptimalNewCameraMatrix(M1,d1,(width, height),.99)
        imgL = cv2.imread(images[0])[:,0:width].copy()
    # newcameramtx, roi = cv2.fisheye.estimateNewCameraMatrixForUndistortRectify(M1.astype(cv2.CV_32F), d1.astype(cv2.CV_32F), (width//2, height), np.eye(3), balance=0.5)
    # print("ROI\n", roi)

    print(imgL.shape)
    dst = cv2.undistort(imgL, M1, d1, None, M1)
    # crop the image
    # x,y,w,h = roi
    # dst = dst[y:y+h, x:x+w]

    # mapx,mapy = cv2.initUndistortRectifyMap(M1,d1,None,M1,(width//2, height),5)
    # dst = cv2.remap(img,mapx,mapy,cv2.INTER_LINEAR)
    cv2.imwrite('calibresult.png',dst)

    cv2.destroyAllWindows()

    if two_cams:
        camera_model = dict([('M1', M1), ('M2', M2), ('dist1', d1),
                            ('dist2', d2), ('rvecs1', r1),
                            ('rvecs2', r2), ('tvecs1', t1),
                            ('tvecs2', t2), ('R', R), ('T', T),
                            ('E', E), ('F', F)])
    else:
        camera_model = dict([('M1', M1), ('dist1', d1),
                            ('rvecs1', r1), ('tvecs1', t1)])

    with open('calib.pkl', 'wb') as f:
        pickle.dump(camera_model, f, pickle.HIGHEST_PROTOCOL)

    # print('Intrinsic_mtx_1\n', M1)
    # print('dist_1\n', d1)
    # print('Intrinsic_mtx_2\n', M2)
    # print('dist_2\n', d2)
    # print('R\n', R)
    # print('T\n', T)
    # print('E\n', E)
    # print('F\n', F)

    # for i in range(len(r1)):
    #     print("--- pose[", i+1, "] ---")
    #     ext1, _ = cv2.Rodrigues(r1[i])
    #     ext2, _ = cv2.Rodrigues(r2[i])
    #     print('Ext1', ext1)
    #     print('Ext2', ext2)

    # print('')

if __name__ == '__main__':
    main()
'''
import numpy as np
import cv2
import glob
import os
import argparse

# https://github.com/bvnayak/stereo_calibration/blob/master/camera_calibrate.py
class StereoCalibration(object):
    def __init__(self, filepath):
        # termination criteria
        self.criteria = (cv2.TERM_CRITERIA_EPS +
                         cv2.TERM_CRITERIA_MAX_ITER, 30, 0.001)
        self.criteria_cal = (cv2.TERM_CRITERIA_EPS +
                             cv2.TERM_CRITERIA_MAX_ITER, 100, 1e-5)

        # prepare object points, like (0,0,0), (1,0,0), (2,0,0) ....,(6,5,0)
        self.objp = np.zeros((9*6, 3), np.float32)
        self.objp[:, :2] = np.mgrid[0:9, 0:6].T.reshape(-1, 2)

        # Arrays to store object points and image points from all the images.
        self.objpoints = []  # 3d point in real world space
        self.imgpoints_l = []  # 2d points in image plane.
        self.imgpoints_r = []  # 2d points in image plane.

        self.cal_path = filepath
        self.read_images(self.cal_path)

    def read_images(self, cal_path):
        # images_right = glob.glob(cal_path + 'RIGHT/*.JPG')
        # images_left = glob.glob(cal_path + 'LEFT/*.JPG')
        # images_left.sort()
        # images_right.sort()

        # img_shape = (0,0)
        images = glob.glob(os.path.join(cal_path, "*.jpg"))
        img_shape = (0,0)

        print(images)

        for i, fname in enumerate(images):
            img = cv2.imread(images[i])
            width = img.shape[1]
            img_l = img[:,:width//2]
            img_r = img[:,width//2:]

            gray_l = cv2.cvtColor(img_l, cv2.COLOR_BGR2GRAY)
            gray_r = cv2.cvtColor(img_r, cv2.COLOR_BGR2GRAY)

            # Find the chess board corners
            ret_l, corners_l = cv2.findChessboardCorners(gray_l, (9, 6), None)
            ret_r, corners_r = cv2.findChessboardCorners(gray_r, (9, 6), None)

            # If found, add object points, image points (after refining them)
            self.objpoints.append(self.objp)

            if ret_l is True and ret_r is True:
                rt = cv2.cornerSubPix(gray_l, corners_l, (11, 11),
                                      (-1, -1), self.criteria)
                self.imgpoints_l.append(corners_l)

                # Draw and display the corners
                ret_l = cv2.drawChessboardCorners(img_l, (9, 6),
                                                  corners_l, ret_l)
                cv2.imshow('chessboard', img_l)
                cv2.waitKey(50)

                rt = cv2.cornerSubPix(gray_r, corners_r, (11, 11),
                                      (-1, -1), self.criteria)
                self.imgpoints_r.append(corners_r)

                # Draw and display the corners
                ret_r = cv2.drawChessboardCorners(img_r, (9, 6),
                                                  corners_r, ret_r)
                cv2.imshow('chessboard', img_r)
                cv2.waitKey(50)
            img_shape = gray_l.shape[::-1]

        rt, self.M1, self.d1, self.r1, self.t1 = cv2.calibrateCamera(
            self.objpoints, self.imgpoints_l, img_shape, None, None)
        rt, self.M2, self.d2, self.r2, self.t2 = cv2.calibrateCamera(
            self.objpoints, self.imgpoints_r, img_shape, None, None)

        self.camera_model = self.stereo_calibrate(img_shape)

    def stereo_calibrate(self, dims):
        flags = 0
        flags |= cv2.CALIB_FIX_INTRINSIC
        # flags |= cv2.CALIB_FIX_PRINCIPAL_POINT
        flags |= cv2.CALIB_USE_INTRINSIC_GUESS
        flags |= cv2.CALIB_FIX_FOCAL_LENGTH
        # flags |= cv2.CALIB_FIX_ASPECT_RATIO
        flags |= cv2.CALIB_ZERO_TANGENT_DIST
        # flags |= cv2.CALIB_RATIONAL_MODEL
        # flags |= cv2.CALIB_SAME_FOCAL_LENGTH
        # flags |= cv2.CALIB_FIX_K3
        # flags |= cv2.CALIB_FIX_K4
        # flags |= cv2.CALIB_FIX_K5

        stereocalib_criteria = (cv2.TERM_CRITERIA_MAX_ITER +
                                cv2.TERM_CRITERIA_EPS, 100, 1e-5)
        ret, M1, d1, M2, d2, R, T, E, F = cv2.stereoCalibrate(
            self.objpoints, self.imgpoints_l,
            self.imgpoints_r, self.M1, self.d1, self.M2,
            self.d2, dims,
            criteria=stereocalib_criteria, flags=flags)

        print('Intrinsic_mtx_1', M1)
        print('dist_1', d1)
        print('Intrinsic_mtx_2', M2)
        print('dist_2', d2)
        print('R', R)
        print('T', T)
        print('E', E)
        print('F', F)

        # for i in range(len(self.r1)):
        #     print("--- pose[", i+1, "] ---")
        #     self.ext1, _ = cv2.Rodrigues(self.r1[i])
        #     self.ext2, _ = cv2.Rodrigues(self.r2[i])
        #     print('Ext1', self.ext1)
        #     print('Ext2', self.ext2)

        print('')

        camera_model = dict([('M1', M1), ('M2', M2), ('dist1', d1),
                            ('dist2', d2), ('rvecs1', self.r1),
                            ('rvecs2', self.r2), ('R', R), ('T', T),
                            ('E', E), ('F', F)])

        cv2.destroyAllWindows()
        return camera_model

if __name__ == '__main__':
    parser = argparse.ArgumentParser()
    parser.add_argument('filepath', help='String Filepath')
    args = parser.parse_args()
    cal_data = StereoCalibration(args.filepath)
'''


# -*- coding: utf-8 -*-
import scrapy

from scrapy import Request
import re
from SpiderBlog.items import SpiderblogItem

class BlogSpider(scrapy.Spider):
    name = 'blog'
    #allowed_domains = ['cnblogs.com']
    start_urls = ['https://q.cnblogs.com/tag/list']

    def start_requests(self):

        for page in range(1,101):

            url='https://q.cnblogs.com/tag/list'

            yield Request(url=url,callback=self.parse_tag)

    def parse_tag(self, response):

        category_links=re.findall('<td.*?
<a href="(.*?)"',response.text,re.S)

        for category_link in category_links:

            for page in range(1,101):

                yield Request(url="https://q.cnblogs.com"+category_link+"?page=%s"%page,callback=self.parse_category_question_list)

    def parse_category_question_list(self,response):

        item=SpiderblogItem()

        question_titles=re.findall('
(.*?)<',response.text,re.S)

        question_links=re.findall('
.*?href="(.*?)"',response.text,re.S)

        question_pubAuthor=re.findall('news_contributor">(.*?)<',response.text,re.S)

        question_pubdate=re.findall('news_contributor">.*?<span title="(.*?)"',response.text,re.S)

        question_visitorsNum=re.findall('news_contributor.*?
(.*?)(.*?)
',response.text,re.S)

        question_summarys=re.findall('news_summary">(.*?)
',response.text,re.S)

        question_answeredNums=re.findall('diggnum .*?answered">(.*?)<',response.text,re.S)

        question_values=re.findall('
(.*?)
',response.text,re.S)

        for i in range(len(question_titles)):

            item['question_title']=question_titles[i]
            item['question_link']="https://q.cnblogs.com"+question_links[i]
            item['question_pubAuthor']=question_pubAuthor[i]
            item['question_pubdate']=question_pubdate[i]
            item['question_visitorsNum']=question_visitorsNum[i].strip().strip("浏览(").strip(")")
            item['question_tag']=str(re.findall('(.*?)<',question_tags[i].strip(),re.S)).strip("[").strip("]").replace("'","")
            item['question_summary']=question_summarys[i].strip()
            item['question_answeredNum']=question_answeredNums[i]

            data=re.findall('(.*?)<',question_values[i],re.S)
            if len(data)==0:
                item['question_value']="0"
            else:
                item['question_value']=data[0]

            yield item


from flask import Flask
from flask import render_template
from flask_wtf import FlaskForm
from wtforms import StringField, SubmitField

app = Flask(__name__)

app.config['SECRET_KEY'] = 'thesecretkey'

class FormInfo(FlaskForm):
    name = StringField('Enter your name here: ')
    submit = SubmitField('Submit')

@app.route('/')
def index():
    name = True
    form = FormInfo()

    if form.validate_on_submit():
        name = form.name.data
        form.name.data = ''

    return render_template('index2.html', name = name, form = form)

if __name__ == '__main__':
    app.run(debug = True)


import re
import subprocess
import os
from os.path import *
import contextlib

import sys
import pwd
import tarfile
from hashlib import md5
from io import BytesIO
import lzma

import ar
from hashstore import HashStore

class Error(Exception):
    pass

def _init_debinfo_cache():
    home_dir = pwd.getpwuid(os.getuid()).pw_dir
    debinfo_dir = os.environ.get("DEBINFO_DIR",
                                 join(home_dir, ".debinfo"))
    if not exists(debinfo_dir):
        os.makedirs(debinfo_dir)

    return HashStore(debinfo_dir)

_cache = _init_debinfo_cache()

def _extract_control(path):
    filenames = ar.list(path)
    filename = None

    for fn in filenames:
        if fn.startswith('control.tar'):
            filename = fn
            break

    if not filename:
        raise Error("no 'control.tar.[gz|bz2|xz]' in archive")

    _, compression_type = splitext(filename)
    compression_type = compression_type[1:]

    control_tar_archive = ar.extract(path, filename)

    if compression_type == 'xz':
        xz_str = lzma.decompress(control_tar_archive)
        xz_bytes = BytesIO(xz_str)
        tar = tarfile.open(mode="r:", fileobj=xz_bytes)
        reload(sys)
        sys.setdefaultencoding('utf8')
    else:
        fh = BytesIO(control_tar_archive)
        tar = tarfile.open(filename, mode="r:"+compression_type, fileobj=fh)

    reload(sys)
    sys.setdefaultencoding('utf8')

    try:
        return tar.extractfile("./control").read().encode('ascii', errors='replace')
    except KeyError:
        return tar.extractfile("control").read().encode('ascii', errors='replace')

def get_key(path):
    """calculate the debinfo key for a Debian binary package at <path>"""
    filename = None
    for fn in ar.list(path):
        if fn.startswith('control.tar'):
            filename = fn
            break
    return md5(ar.extract(path, filename)).hexdigest()

def get_control_by_key(key):
    """get control data from debinfo cache by <key> -> str"""
    return _cache.get(key)

def get_control_by_path(path, usecache=True):
    """get control data from a Debian binary package at <path> -> str (cached)

    If possible, the control file is retrieved from the debinfo cache.
    Otherwise the control file is extracted from the path and stored in the debinfo cache.
    """
    if not usecache:
        return _extract_control(path)

    key = get_key(path)
    control = _cache.get(key)
    if control is None:
        control = _extract_control(path).decode()
        _cache.set(key, control)

    return control

def parse_control(control):
    """parse control fields -> dict"""
    d = {}
    last_key = None
    for line in control.split("\n"):
        if not line or line[0] == " ":
            continue
        line = line.strip()
        if ':' in line:
            i = line.index(':')
            last_key = key = line[:i]
            val = line[i + 2:]
            d[key] = val
        else:
            key = last_key
            val = line.strip()
            d[key] = d[key].strip() + b' ' + val
    return d

def get_control_fields(path):
    """convenience function which extracts control fields from a Debian binary package -> dict"""
    control = get_control_by_path(path)
    return parse_control(control)


import os

from flask import Flask , request , redirect ,flash , url_for
from werkzeug.utils import secure_filename

app=Flask(__name__)

UPLOAD_FOLDER = "C:/Upload"
ALLOWED_EXTENSIONS = {'txt'}

app.config["UPLOAD_FOLDER"] = UPLOAD_FOLDER

def allowed_file(filename):
    return '.' in filename and \
           filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS

@app.route('/', methods=['GET', 'POST'])
def upload_file():
    if request.method == 'POST':

        if 'file' not in request.files:
            flash('No file part')
            return redirect(request.url)
        file = request.files['file']

        if file.filename == '':
            flash('No selected file')
            return redirect(request.url)
        if file and allowed_file(file.filename):
            filename = secure_filename(file.filename)
            file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
            return redirect(url_for('download_file', name=filename))
    return '''
    <!doctype html>
    <title>Upload new File</title>

Upload new File

    <form method=post enctype=multipart/form-data>
      <input type=file name=file>
      <input type=submit value=Upload>
    </form>
    '''

app.run(debug=True, host="localhost")


from abc import abstractmethod
from abc import ABC
import os
import json
import pandas as pd
from datetime import datetime, timedelta
import requests
import time
from influxdb import DataFrameClient
import numpy as np

class Data(ABC):

    def __init__(self):
        self.set_config()
        self.client     = None
        self.dataname   = self.__class__.__name__

        print(f'Created {self.dataname}()...')

    @abstractmethod
    def set_config(self):
        pass

    @abstractmethod
    def _retrieve_data_period(self):
        pass

    def create_history(self):
        print(f'Create complete {self.dataname} history...')

        if os.path.exists(self.sensor_file):
            print('   ...history already exists!')
            return

        start = datetime.strptime(self.history_start, '%Y-%m-%dT%H:%M:%S')
        end   = datetime.now() - timedelta(hours=5)

        # Get data
        data  = self._retrieve_data_period(start, end)

        # Write data to local file and DB
        client = self._get_connection_db()
        for sensor, data_sensor in data.items():
            self._write_local_file(data_sensor, sensor)
            self._write_data_db(client, data_sensor)

        self.write_sensor_file(data_sensor.sort_index().index[-1], list(data.keys()))
        print('   ...done')

    def update_latest_data(self):
        print(f'Update latest {self.dataname} data...')

        # Get period to update
        sensor_dict = self.read_sensor_file()
        buffer_time = timedelta(hours=self.hours_update_buffer)
        last_ts = datetime.fromtimestamp(sensor_dict['last_ts']) - buffer_time
        end     = datetime.now() + buffer_time

        # Get data
        print(last_ts, end)
        data  = self._retrieve_data_period(last_ts, end)

        # Write data to local file and DB
        client = self._get_connection_db()
        for sensor, data_sensor in data.items():
            self._update_local_file(data_sensor, sensor)
            self._write_data_db(client, data_sensor)

        self.write_sensor_file(data_sensor.sort_index().index[-1], list(data.keys()))

    def write_complete_history_to_db(self):
        print(f'Write complete {self.dataname} history to DB...')

        # Get sensors
        sensor_dict = self.read_sensor_file()

        client = self._get_connection_db()
        for sensor in sensor_dict['sensors'][::-1]:
            print(f'   {sensor}...')
            data_sensor = self._read_local_file(sensor)

            n_splits = int(np.ceil(data_sensor.shape[0] / 50000))
            print(f'   ...Import splitted into {n_splits} parts')
            data_sensor_splits = np.array_split(data_sensor, n_splits)
            for data_sensor_split in data_sensor_splits:
                self._write_data_db(client, data_sensor_split)
                time.sleep(1)

    def _get_connection_db(self):

        if self.client is not None:
            return self.client

        print('Connect to InfluxDB...')
        db_cfg          = self.influxdb_cfg
        dbname          = db_cfg['dbname']
        influx_conn_ok  = False
        while not influx_conn_ok:
            client = DataFrameClient(db_cfg['host'],
                                     db_cfg['port'],
                                     db_cfg['user'],
                                     db_cfg['password'],
                                     dbname)
            try:
                dbs = client.get_list_database()
                dbs = [x['name'] for x in dbs]
            except requests.exceptions.ConnectionError:
                print('   ...no InfluxDB connection yet. Waiting 5 seconds and retrying.')
                time.sleep(5)
            else:
                print('   ... ok!')
                influx_conn_ok = True

        self.client = client
        return self.client

    def _read_data_db(self, client):
        resp = client.query("select * from {}".format(self.influxdb_cfg['dbname']))
        print(resp)

    def _write_data_db(self, client, data):
        dbname = self.influxdb_cfg['dbname']
        n_entries = data.shape[0]
        self._create_table_if_needed(client, dbname)
        conn_ok  = False

        while not conn_ok:
            try:
                if dbname == 'uba':
                    data = data.astype(int)
                client.write_points(data,
                                    self.influxdb_cfg['dbname'],
                                    protocol=self.influxdb_cfg['protocol'])
            except requests.exceptions.ConnectionError:
                print('   ...no InfluxDB connection yet. Waiting 5 seconds and retrying.')
                time.sleep(5)
                client = self._get_connection_db()
            else:
                print('   ...ok!')
                conn_ok = True
        print('   ...imported {} entries into table {}'.format(n_entries, dbname))

    def _create_table_if_needed(self, client, tablename):
        dbs = client.get_list_database()
        dbs = [x['name'] for x in dbs]
        if tablename not in dbs:
            print("Create database: " + tablename)
            client.create_database(tablename)

    def _make_dir_avalable(self, dir):
        """
        Create directorie handed over, if not yet available
        """
        if not os.path.exists(dir):
            os.makedirs(dir)
            print('created following dir: {}'.format(dir))

    def write_sensor_file(self, last_ts, sensors):
        sensor_dict = {'sensors': sensors,
                       'last_ts': last_ts.timestamp(),
                       'last_ts_fmt': last_ts.strftime('%Y-%m-%dT%H:%M:%S')}
        with open(self.sensor_file, 'w') as fp:
            json.dump(sensor_dict, fp)

    def read_sensor_file(self):
        with open(self.sensor_file, 'r') as fp:
            sensor_dict = json.load(fp)
        return sensor_dict

    def _write_local_file(self, data, sensor):
        data.to_csv(self.local_file_data.format(sensor))

    def _read_local_file(self, sensor):
        data = pd.read_csv(self.local_file_data.format(sensor), index_col='Time', parse_dates=['Time'])
        return data

    def _update_local_file(self, data, sensor):
        data_file = self._read_local_file(sensor)

        len_before = data_file.shape[0]
        data = data.reset_index()
        data_file = data_file.reset_index()
        data_updated = data_file.merge(data, how='outer').set_index('Time').sort_index()

        self._write_local_file(data_updated, sensor)
        print('   ...added {} entries to dataset'.format(data_updated.shape[0] - len_before))

    # def _get_period_in_db(self, client):
    #     dbname = self.influxdb_cfg['dbname']
    #     res = client.query("select * from {}".format(dbname))[dbname].sort_index()
    #
    #     days = set([x.strftime('%Y-%m-%d') for x in res.index.tolist()])
    #
    #     return list(days)


import os
import cv2
import webbrowser

from PyQt5.QtCore import QRect
from PyQt5.QtGui import QPixmap, QImage
from PyQt5.QtWidgets import QMainWindow, QGridLayout, QWidget, QLabel, QPushButton, QAction, QFileDialog
from analyzer.VideoAnalyzerResultWriter import write_full_frames, write_html, write_replay_file
from windows.SelectNewClip import SelectNewClip
from util.Constants import Constants

def convert_cv_qt(cv_img):
    """Convert from an opencv image to QPixmap"""
    rgb_image = cv2.cvtColor(cv_img, cv2.COLOR_BGR2RGB)
    h, w, ch = rgb_image.shape
    bytes_per_line = ch * w
    convert_to_qt_format = QImage(rgb_image.data, w, h, bytes_per_line, QImage.Format_RGB888)
    return QPixmap.fromImage(convert_to_qt_format)

class ResultWindow(QMainWindow):

    def __init__(self, workfow_builder, workflow, workflow_full_frames, reference_img):
        super().__init__()

        self.workflow = workflow
        self.workflow_full_frames = workflow_full_frames
        self.workflow_builder = workfow_builder
        self.reference_img = reference_img

        self.setWindowTitle(Constants.Result.TITLE)
        self.setGeometry(10, 30, 600, 400)

        self.build_toolbar()
        self.build_content()
        self.as_html = workfow_builder.as_html()

    def build_content(self):
        grid = QGridLayout()
        wid = QWidget()
        wid.setLayout(grid)
        self.setCentralWidget(wid)

        row = 0
        for img_name, img in self.workflow:
            image_frame = QLabel()
            image_frame.setPixmap(convert_cv_qt(img))
            grid.addWidget(image_frame, row, 1)
            remove_frame_btn = QPushButton(Constants.Result.BTN_REMOVE)
            remove_frame_btn.clicked.connect(lambda state, x=img_name: self.remove_frame(x))
            grid.addWidget(remove_frame_btn, row, 2)
            select_new_clip_btn = QPushButton(Constants.Result.BTN_SELECT)

            select_new_clip_btn.clicked.connect(lambda state, x=row: self.select_new_for(x))
            grid.addWidget(select_new_clip_btn, row, 3)
            row += 1

    def remove_frame(self, img_name):
        self.workflow = [i for i in self.workflow if i[0] is not img_name]
        self.build_content()

    def select_new_for(self, row):
        self.row = row
        self.new_clip = SelectNewClip(self, self.reference_img, self.selected)
        self.new_clip.show()

    def selected(self, rect: QRect):
        x = rect.getRect()[0]
        y = rect.getRect()[1]
        width = rect.getRect()[2]
        height = rect.getRect()[3]
        img = self.reference_img[y:y + height, x:x + width, :]

        self.workflow[self.row] = (self.workflow[self.row][0], img)
        self.build_content()

    def build_toolbar(self):
        save_action = QAction(Constants.Result.TOOLBAR_SAVE, self)
        save_action.setShortcut(Constants.Result.SHORTCUT_SAVE)
        save_action.triggered.connect(self.save)

        open_browser_action = QAction(Constants.Result.TOOLBAR_OPEN_BROWSER, self)
        open_browser_action.setShortcut(Constants.Result.SHORTCUT_OPEN_BROWSER)
        open_browser_action.triggered.connect(self.open_in_browser)

        self.save_html = False
        toggleHTML = QAction(Constants.Result.TOOLBAR_OPEN_BROWSER, self)
        toggleHTML.setCheckable(True)
        toggleHTML.triggered.connect(self.toggle_html)

        self.save_full_frames = False
        toggleFullFrames = QAction(Constants.Result.TOOLBAR_EXPORT_FRAMES, self)
        toggleFullFrames.setCheckable(True)
        toggleFullFrames.triggered.connect(self.toggle_full_frames)

        self.toolbar = self.addToolBar(Constants.TXT_EMPTY)
        self.toolbar.addAction(save_action)
        self.toolbar.addAction(open_browser_action)
        self.toolbar.addAction(toggleHTML)
        self.toolbar.addAction(toggleFullFrames)

    def closeEvent(self, QCloseEvent):
        for img_name, _ in self.workflow:
            if os.path.exists(img_name):
                os.remove(img_name)

    def resizeEvent(self, event):
        QMainWindow.resizeEvent(self, event)

    def toggle_html(self):
        self.save_html = not self.save_html

    def toggle_full_frames(self):
        self.save_full_frames = not self.save_full_frames

    def save(self):
        target_dir = str(QFileDialog.getExistingDirectory(self, Constants.Result.TXT_SELECT_DIR))
        if target_dir:
            if self.save_html:
                write_html(target_dir, self.workflow_builder.as_html(), self.workflow)

            if self.save_full_frames:
                write_full_frames(target_dir, self.workflow_full_frames)

            write_replay_file(target_dir, self.workflow_builder.as_xml(), self.workflow)
            self.statusBar().showMessage(f"SAVED to {target_dir}")

    def open_in_browser(self):
        for img_name, img in self.workflow:
            cv2.imwrite(img_name, img)

        f = open('workflow.html', 'w')
        f.write(self.as_html)
        f.close()
        filename = 'file:///' + os.getcwd() + '/' + 'workflow.html'
        webbrowser.open_new_tab(filename)


__author__ = 'dmxjhg'
__date__ = '2019/8/13 19:50'

from rest_framework import serializers
from rest_framework.validators import UniqueTogetherValidator

from .models import UserFav, Questionnaire, Route
from .models import UserLeavingMessage

from article.serializers import ArticleSerializer

class UserFavSerializer(serializers.ModelSerializer):
    user = serializers.HiddenField(
        default=serializers.CurrentUserDefault()
    )  # 获取当前用户，drf文档中看用法，如果不这样，post的时候还需要传用户，这是不合理的

    class Meta:
        model = UserFav
        fields = ("user", "article", "id")  # id是为了删除操作
        validators = [
            UniqueTogetherValidator(
                queryset=UserFav.objects.all(),
                fields=('user', 'article'),
                message="已经收藏"
            )
        ]

class UserFavDetailSerializer(serializers.ModelSerializer):
    article = ArticleSerializer()

    class Meta:
        model = UserFav
        fields = ("article", "id")

class LeavingMessageSerializer(serializers.ModelSerializer):
    user = serializers.HiddenField(
        default=serializers.CurrentUserDefault()
    )
    add_time = serializers.DateTimeField(read_only=True, format='%Y-%m-%d %H:%M')

    class Meta:
        model = UserLeavingMessage
        fields = ("user", "message", "add_time")

class QuestionnaireSerializer(serializers.ModelSerializer):
    user = serializers.HiddenField(
        default=serializers.CurrentUserDefault()
    )
    add_time = serializers.DateTimeField(read_only=True, format='%Y-%m-%d %H:%M')

    class Meta:
        model = Questionnaire
        fields = ("user", "choose", "add_time", "id")

class RouteListSerializer(serializers.ModelSerializer):
    questionnaire = QuestionnaireSerializer()
    user = serializers.HiddenField(
        default=serializers.CurrentUserDefault()
    )
    add_time = serializers.DateTimeField(read_only=True, format='%Y-%m-%d %H:%M')
    route = serializers.CharField(read_only=True, )

    class Meta:
        model = Route
        fields = ("user", "questionnaire", "route", "add_time")


import unittest
import numpy as np
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression

class LinearRegressionTest(unittest.TestCase):
    def test_train_test_split(self):
        X, y = np.arange(10).reshape((5,2)), range(5)
        print(X, y)
        X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3)
        print(X_train, y_train)
        print(X_test, y_test)

        # Fit Training data
        regression = LinearRegression()
        regression.fit(X_train, y_train)

        # Predicting labels on the test set
        print(regression.predict(X_test))

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


import os
import sys
import shutil
import plistlib
import toml
import subprocess
import json
from urllib.request import urlopen, urlretrieve

class AppBundle:
    frameworks_url = 'https://api.github.com/repos/3-manifolds/frameworks/releases/latest'
    python_link = '../Frameworks/Python.framework/Versions/Current/bin/python3'

    def __init__(self):
        with open("info.toml") as info_toml:
            self.info = info = toml.load(info_toml)
        self.app_name = info['CFBundleDisplayName']
        self.bundle = self.app_name + '.app'
        self.icon_file = info['CFBundleIconFile']
        self.sdef_file = info['OSAScriptingDefinition']
        self.python_version = f'{sys.version_info.major}.{sys.version_info.minor}'

    def create_bundle_structure(self):
        """Construct a macOS Application bundle.

        The info.toml file provides configuration information.
        This assumes that the current working directory contains:
            A configuration file info.toml.
            A directory main_ex containing the main executable;
            An icon file named as specified in info.toml;
            An sdef file named as specified in info.toml;
            A python script named main.py which runs the app;
            A tarball (or a symlink to one) named Frameworks.tgz.
        """
        # Raises an exception if the bundle exists.
        os.mkdir(self.bundle)
        # Build the main executable (if necessary).
        os.chdir('main_ex')
        subprocess.run(['make'])
        os.chdir(os.path.pardir)
        # Download the frameworks (if necessary).
        tarball_base = f'Frameworks-{self.python_version}'
        tarball = tarball_base + '.tgz'
        if not os.path.exists(tarball):
            with urlopen(self.frameworks_url) as json_data:
                release_info = json.load(json_data)
            for asset in release_info['assets']:
                filename = asset['name']
                base, ext = os.path.splitext(filename)
                if base != tarball_base:
                    continue
                urlretrieve(asset['browser_download_url'], filename)
                if filename.endswith('.sha1'):
                    hash_file = filename
                elif filename.endswith('.tgz'):
                    tar_file = filename
            result = subprocess.run(['shasum', '-c', hash_file],
                        capture_output=True)
            if result.returncode:
                print('Framework download failed')
                sys.exit(1)
            os.unlink(hash_file)
        contents = os.path.join(self.bundle, 'Contents')
        macos = os.path.join(contents, 'MacOS')
        resources = os.path.join(contents, 'Resources')
        for subdir in (contents, macos, resources):
            os.makedirs(subdir, exist_ok=True)
        shutil.copy(self.icon_file, resources)
        shutil.copy(self.sdef_file, resources)
        main_ex_path = os.path.join(macos, self.app_name)
        shutil.copy('main_ex/AppMain', main_ex_path)
        plist_path = os.path.join(contents, 'Info.plist')
        with open(plist_path, "wb") as info_plist:
            plistlib.dump(self.info, info_plist)
        symlink_path = os.path.join(macos, 'Python')
        os.symlink(self.python_link, symlink_path)
        shutil.copy('main.py', resources)
        subprocess.run(['tar', 'xz', '-C', contents, '-f', tarball])

    def add_packages(self):
        if not os.path.exists('requirements.txt'):
            print('The requirements.txt file was not found.')
            sys.exit(1)
        bundle_name = self.app_name + '.app'
        if not os.path.exists(bundle_name):
            print(bundle_name, 'was not found')
            sys.exit(1)
        lib_dir = os.path.join(bundle_name, 'Contents', 'Frameworks',
            'Python.framework', 'Versions', 'Current', 'lib')
        package_dir = None
        for file in os.listdir(lib_dir):
            package_dir = os.path.join(lib_dir, file, 'site-packages')
            if os.path.exists(package_dir):
                break
        subprocess.check_call([sys.executable, '-m', 'pip', 'install', '-r',
            'requirements.txt', '--no-user', '--target', package_dir])


# -*- coding: utf-8 -*-
"""
verdi daemon cli utilities
"""
from aiida.common import aiidalogger

logger = aiidalogger.getChild('workflowmanager')

def is_daemon_user():
    """
    Return True if the user is the current daemon user, False otherwise.
    """
    from aiida.backends.utils import get_daemon_user
    from aiida.common.utils import get_configured_user_email

    daemon_user = get_daemon_user()
    this_user = get_configured_user_email()

    return daemon_user == this_user

def get_env_with_venv_bin():
    """get an environment to run the daemon in that respects the current virtualenvs"""
    import os
    import sys
    pybin = os.path.dirname(sys.executable)
    currenv = os.environ.copy()
    currenv['PATH'] = pybin + ':' + currenv['PATH']
    return currenv

def get_conffile_full_path():
    """Get the full path to the config file"""
    from os.path import expanduser, join
    from aiida.common import setup
    return expanduser(join(
        setup.AIIDA_CONFIG_FOLDER,
        setup.DAEMON_SUBDIR,
        setup.DAEMON_CONF_FILE))

def get_pid_full_path():
    """
    Return the full path of the supervisord.pid file.
    """
    from os.path import normpath, expanduser, join
    from aiida.common import setup

    return normpath(expanduser(
        join(setup.AIIDA_CONFIG_FOLDER,
             setup.DAEMON_SUBDIR, "supervisord.pid")))

def get_sock_full_path():
    """
    Return the full path of the supervisord.sock file.
    """
    from os.path import normpath, expanduser, join
    from aiida.common import setup

    return normpath(expanduser(
        join(setup.AIIDA_CONFIG_FOLDER,
             setup.DAEMON_SUBDIR, "supervisord.sock")))

def get_daemon_pid():
    """
    Return the daemon pid, as read from the supervisord.pid file.
    Return None if no pid is found (or the pid is not valid).
    """
    from os.path import isfile
    if isfile(get_pid_full_path()):
        try:
            return int(open(get_pid_full_path(), 'r').read().strip())
        except (ValueError, IOError):
            return None
    else:
        return None

def _clean_sock_files():
    """
    Tries to remove the supervisord.pid and .sock files from the .aiida/daemon
    subfolder. This is typically needed when the computer is restarted with
    the daemon still on.
    """
    import errno
    import os

    try:
        os.remove(get_sock_full_path())
    except OSError as e:
        # Ignore if errno = errno.ENOENT (2): no file found
        if e.errno != errno.ENOENT:  # No such file
            raise

    try:
        os.remove(get_pid_full_path())
    except OSError as e:
        # Ignore if errno = errno.ENOENT (2): no file found
        if e.errno != errno.ENOENT:  # No such file
            raise

def kill_daemon():
    """
    This is the actual call that kills the daemon.

    There are some print statements inside, but no sys.exit, so it is
    safe to be called from other parts of the code.
    """
    import click
    from signal import SIGTERM
    import errno
    import os

    pid = get_daemon_pid()
    if pid is None:
        click.echo("Daemon not running (cannot find the PID for it)")
        return

    click.echo("Shutting down AiiDA Daemon ({})...".format(pid))
    try:
        os.kill(pid, SIGTERM)
    except OSError as e:
        if e.errno == errno.ESRCH:  # No such process
            click.echo("The process {} was not found! Assuming it was already stopped.".format(pid))
            click.echo("Cleaning the .pid and .sock files...")
            _clean_sock_files()
        else:
            raise


import asyncio
import os
from math import floor
from typing import Sequence

import youtube_title_parse as ytp
import yt_dlp
from youtubesearchpython.__future__ import VideosSearch

from serenityapp.db import DBLink
import serenityapp.records as records
from serenityapp.config import (DOWNLOAD_PATH, DOWNLOADS_CODEC, UNKNOWN_ARTIST,
                                light_wait, path_exists)

class VoidLogger(object):

    def debug(msg):
        pass

    def warning(msg):
        pass

    def error(msg):
        pass

class AudioDL:

    audio_codec = 'mp3'
    current_title = ''
    __download_percent = 0
    __download_index = -1
    __downloading_titles = []
    __converting_titles = []
    __downloading = False
    __finished = True

    yt_dlp_options = {  # IMPORTANT. No OUTTMPL info.
        'extractaudio': True,
        'quiet': True,
        'format': 'bestaudio/best',
        'playlist': True,
        'logger': VoidLogger,
        'postprocessors': [{
            'key': 'FFmpegExtractAudio',
            'preferredcodec': audio_codec,
            'preferredquality': '192'
        }]
    }

    def set_codec(codec):
        AudioDL.audio_codec = codec
        AudioDL.yt_dlp_options['postprocessors']['preferredcodec'] = codec

    def __prog_hook(d: dict):
        if d['status'] == 'finished':
            AudioDL.__download_index -= 1
            if AudioDL.__downloading_titles:
                AudioDL.__downloading_titles.pop()
                light_wait()
                AudioDL.__download_percent = 0
            else:
                AudioDL.__download_percent = 100
        if d['status'] == 'downloading':
            p = d['_percent_str']
            p = floor(float(p[0:-1]))
            if AudioDL.__download_index >= len(AudioDL.__downloading_titles)-1:
                AudioDL.__download_percent = p

    def validate_title(title: str) -> str:
        if title is None:
            return None
        txt = title.replace("|", "¦")
        txt = txt.encode('ascii', 'ignore').decode()
        return "".join(i for i in txt if i not in r'\/:*?"<>')

    def scrape_title(url: str, force_title: str = ''):
        if force_title != '':
            return force_title
        else:
            return yt_dlp.YoutubeDL(
                {
                    'logger': VoidLogger,
                    'skip_download': True,
                    'quiet': True
                }
            ).extract_info(url, download=False).get('title', None)

    def get_online_data(valid_title: str):
        options = {
            'defaultArtist': UNKNOWN_ARTIST,
            'defaultTitle': valid_title
        }
        artist, track = ytp.get_artist_title(valid_title, options)
        return [artist, track, 0, valid_title]

    async def __search_youtube(query):
        try:
            videosSearch = VideosSearch(query, limit=1)
            videosResult = await videosSearch.next()
            return [videosResult['result'][0]['link'],
                    videosResult['result'][0]['title']]
        except Exception:
            return [None, None]

    def get_link_and_title(query):
        loop = asyncio.new_event_loop()
        return loop.run_until_complete(AudioDL.__search_youtube(query))

    def download(urls: list, data: Sequence):
        artist, track, _, title = data
        if title == '':
            return
        options = AudioDL.yt_dlp_options.copy()
        dl_path = DOWNLOAD_PATH + artist + os.sep + track
        options['outtmpl'] = dl_path + '.%(ext)s'
        downloader = yt_dlp.YoutubeDL(options)
        full_path = "{}.{}".format(dl_path, DOWNLOADS_CODEC)
        if not path_exists(full_path):
            os.makedirs(os.path.dirname(full_path), exist_ok=True)
            with downloader as dl:
                appended_c = False
                appended_d = False
                try:
                    dl.add_progress_hook(AudioDL.__prog_hook)
                    AudioDL.__download_index += 1
                    AudioDL.__downloading_titles.append(title)
                    appended_d = True
                    AudioDL.__downloading = False
                    AudioDL.__finished = False
                    AudioDL.__converting_titles.append(title)
                    dl.download(urls)
                    appended_c = True
                    if not AudioDL.__downloading_titles:
                        AudioDL.__downloading = False
                    dbLink = DBLink()
                    records.add_downloaded_song(full_path, data, dbLink)
                    light_wait()
                    if AudioDL.__converting_titles:
                        AudioDL.__converting_titles.pop()
                    AudioDL.__finished = (not AudioDL.__converting_titles
                                          and not AudioDL.__downloading_titles)
                except Exception:
                    if appended_c is True:
                        if AudioDL.__converting_titles:
                            AudioDL.__converting_titles.pop()
                    if appended_d is True:
                        if AudioDL.__downloading_titles:
                            AudioDL.__downloading_titles.pop()

    def download_percent():
        return AudioDL.__download_percent

    def is_downloading():
        return AudioDL.__downloading

    def download_started():
        return len(AudioDL.__downloading_titles) != 0

    def is_finished():
        return AudioDL.__finished

    def active_titles():
        return AudioDL.__converting_titles

    def data_on_disk(data):
        ''' Returns true if there is a file matching the data on disk. '''
        artist, track, _, _ = data
        dl_path = DOWNLOAD_PATH + os.sep + artist + os.sep + track
        full_path = "{}.{}".format(dl_path, DOWNLOADS_CODEC)
        return path_exists(full_path)


#!/usr/bin/env python3
"""Political Insults Game - Processor"""
import json
import logging
import re
import time
from typing import List
from datetime import datetime

import requests
from flask import Flask, request

from main import fight
from person import Person

app = Flask(__name__)

# pylint: disable=consider-using-f-string
# pylint: disable=logging-fstring-interpolation

# Opening page to input details.
# List to store received people.
people: List[dict] = []
# List to store fight
fight_log: List[str] = []

# Returns the results

# How to return webpages in delay - read about it.
@app.route('/fight')
def get_fight():
    """Return fight"""
    # Return results.
    printing_fight = json.dumps(fight_log)
    logging.debug(f"Cleaning up {printing_fight} from unneeded things")
    formatted_fight = re.sub(r"[\[\]]", "", printing_fight)
    return json.dumps(formatted_fight)

@app.route('/people', methods=['POST'])
def add_grids():
    """Create objects for fight"""
    # Add person to list.
    people.append(request.get_json())
    logging.debug(f"Received {people}")

    # Put the people into Person objects, calling make_person.
    p_1: Person = Person(gender=people[0]['Gender 1'], pronouns=people[0]['Pronouns 1'],
                         age=people[0]['Age 1'], skin_color=people[0]['Skin Color 1'],
                         political_ideology=people[0]['Political Ideology 1'])
    p_2: Person = Person(gender=people[0]['Gender 2'], pronouns=people[0]['Pronouns 2'],
                         age=people[0]['Age 2'], skin_color=people[0]['Skin Color 2'],
                         political_ideology=people[0]['Political Ideology 2'])
    logging.debug(f"{people} were put into {p_1} and {p_2}")
    # Getting current date and time
    now = datetime.now()
    dt_string = now.strftime("%d/%m/%Y %H:%M:%S")
    start = time.time()
    # Fight
    fight_string: str = \
        fight(one_who_is_making_statement=p_1, one_who_might_get_triggered=p_2, return_string='')
    end = time.time()
    total_time = end - start
    logging.debug(f"fight is {fight_string}")
    # Adding log to database
    total_time_string: str = str("%.2f" % total_time)
    requests.post("http://political_insults_database:3000/database",
                  json={"Time": total_time_string, "Date": dt_string,
                        "Log": fight_string}, timeout=10)
    logging.debug("Added to database")
    # Put fight log inside string and return that.
    fight_log.append(fight_string)
    logging.debug(f"adding {fight_string} to the log")
    return '', 204

if __name__ == "__main__":
    app.run(debug=True, port=8000, host='political_insults_processor')


from flask import Flask, request, jsonify
from exception import SignatureException
import file_handler as fh
import os
from dotenv import load_dotenv
import bugsnag
from bugsnag.flask import handle_exceptions

app = Flask(__name__)
app.secret_key = "secret key"
app.config['MAX_CONTENT_LENGTH'] = 16 * 1024 * 1024  # 16MB max image size

IMG_FILE = 'img'
IMG_FILE64 = 'img64'
IMG_FILE_BYTES = 'img_bytes'
IMG_RESPONSE_ENCODING = 'resp_enc'
IMG_SIZES = 'img_sizes'
ALLOWED_KEYS = (IMG_FILE, IMG_FILE64, IMG_FILE_BYTES)
ALLOWED_EXTENSIONS = ('png', 'jpg', 'jpeg')

load_dotenv()
STAGE = os.environ.get("STAGE", os.environ.get("FLASK_ENV", "development"))
if STAGE != "development" and "BUGSNAG_API_KEY" in os.environ and os.environ.get("BUGSNAG_API_KEY"):
    bugsnag.configure(
        api_key=os.environ.get("BUGSNAG_API_KEY"),
        project_root="",
        app_version="0.0.1",
        release_stage=STAGE,
    )
    handle_exceptions(app)

def allowed_file(filename):
    return '.' in filename and filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS

def get_value(k):
    if request.form is not None and k in request.form:
        return request.form[k]
    if request.json is not None and k in request.json:
        return request.json[k]
    return None

def is_file():
    return IMG_FILE in request.files

def is_file64():
    return get_value(IMG_FILE64) is not None

def is_file_bytes():
    return get_value(IMG_FILE_BYTES) is not None

def get_response_encoding():
    return get_value(IMG_RESPONSE_ENCODING) or fh.ENCODING_BASE64

def get_img_sizes():
    return get_value(IMG_SIZES) or {"img": (500, 50)}

def create_response(imgs):
    resp = jsonify(imgs)
    resp.status_code = 201
    return resp

def handle_file():
    file = request.files[IMG_FILE]
    if file.filename == '':
        resp = jsonify({'message': 'No file selected for uploading'})
        resp.status_code = 400
        return resp
    if file and allowed_file(file.filename):
        imgs = fh.get_from_file(request.files[IMG_FILE], get_response_encoding(), get_img_sizes())
        return create_response(imgs)
    else:
        resp = jsonify({'message': 'Allowed file types are {0}'.format(ALLOWED_EXTENSIONS)})
        resp.status_code = 400
        return resp

@app.route('/extract-signature', methods=['POST'])
def extract_signature():
    if is_file():
        return handle_file()

    elif is_file64():
        file64 = get_value(IMG_FILE64)
        imgs = fh.get_from_base64(file64, get_response_encoding(), get_img_sizes())
        return create_response(imgs)

    elif is_file_bytes():
        file_bytes = get_value(IMG_FILE_BYTES)
        imgs = fh.get_from_bytes(file_bytes, get_response_encoding(), get_img_sizes())
        return create_response(imgs)

    resp = jsonify({'message': 'No image in the request. Use {} in either form or json request'.format(ALLOWED_KEYS)})
    resp.status_code = 400
    return resp

@app.errorhandler(SignatureException)
def handle_image_exception(error):
    out = error.to_dict(get_response_encoding())
    response = jsonify(out)
    response.status_code = error.status_code
    return response

@app.errorhandler(Exception)
def handle_exception(e):
    bugsnag.notify(e)
    resp = jsonify({"errors": [{"error_code": "unexpected_error", "message": "An error has occurred while processing the image. Please try again in a few moments."}]})
    resp.status_code = 500
    return resp

if __name__ == "__main__":
    app.run(host="0.0.0.0", port="7000")


import json
import boto3
from decimal import Decimal

def lambda_handler(event, context):
    candidateId = event['query']['candidateId']
    candidateResult = getTableData('CandidateTestResults', {'candidateId':candidateId})
    candidateData = getTableData('CandidateRegistrationDetails', {'id': candidateId})
    candidateResult['name'] = candidateData['name']
    candidateResult['email'] = candidateData['email']
    return {
        'statusCode': 200,
        'headers': {
            'Access-Control-Allow-*': '*'

        },
        'body': json.dumps(candidateResult, cls=DecimalEncoder)
    }

def getDynamoDbResource():
    return boto3.resource('dynamodb')

def getItem(table_name, item):
    dynamodb = getDynamoDbResource()
    table = dynamodb.Table(table_name)
    return table.get_item(Key=item)

def getTableData(tableName, item):
    dynamodbObj = getItem(tableName, item)
    return dynamodbObj['Item']

class DecimalEncoder(json.JSONEncoder):
    def default(self, obj):
        if isinstance(obj, Decimal):
            return str(obj)
        return json.JSONEncoder.default(self, obj)


#!/usr/bin/env python

import mirheo as mir
import numpy as np
import sys

from parameters import (PipeFlowParams,
                        load_parameters)

def get_pv_num_particles(pv):
    return len(pv.getCoordinates())

def run_capillary_flow(p: 'PipeFlowParams',
                       *,
                       no_visc: bool,
                       ranks: tuple=(1,1,1),
                       restart_directory: str=None,
                       checkpoint_directory: str=None):
    """
    Arguments:
        p: the simulation parameters (see parameters.py).
        no_visc: if True, disable the viscosity of membrane.
        ranks: number of ranks per dimension.
        restart_directory: if set, specify from which directory to restart.
        checkpoint_directory: if set, specify to which directory to dump checkpoints.
    """

    rc   = p.rc
    L    = p.L
    R    = p.R
    Vmax = p.Vmax
    RA   = p.RA

    dt_solv = min([dpd.get_max_dt() for dpd in [p.dpd_ii, p.dpd_oo, p.dpd_rbco, p.dpd_rbci]])

    if no_visc:
        p.rbc_params.set_viscosity(0)

    tend = 5000 * R / Vmax

    domain = (L, 2*R + 4*rc, 2*R + 4*rc)

    if checkpoint_directory is not None:
        u = mir.Mirheo(ranks, domain, debug_level=3, log_filename='log', checkpoint_every=10000, checkpoint_folder=checkpoint_directory)
    else:
        u = mir.Mirheo(ranks, domain, debug_level=3, log_filename='log')

    vv = mir.Integrators.VelocityVerlet('vv')
    u.registerIntegrator(vv)

    # Membranes

    mesh_rbc = mir.ParticleVectors.MembraneMesh(p.mesh_ini.vertices.tolist(),
                                                p.mesh_ref.vertices.tolist(),
                                                p.mesh_ini.faces.tolist())
    pv_rbc = mir.ParticleVectors.MembraneVector('rbc', mass=p.m, mesh=mesh_rbc)
    u.registerParticleVector(pv_rbc, mir.InitialConditions.Restart('generate_ic'))

    # Solvent

    pv_outer = mir.ParticleVectors.ParticleVector('outer', mass=p.m)
    ic_outer = mir.InitialConditions.Uniform(number_density=p.nd)
    u.registerParticleVector(pv=pv_outer, ic=ic_outer)

    rbc_checker = mir.BelongingCheckers.Mesh('rbc_checker')
    u.registerObjectBelongingChecker(rbc_checker, pv_rbc)
    pv_inner = u.applyObjectBelongingChecker(rbc_checker, pv_outer, inside='inner', correct_every=10000)

    rbc_bouncer = mir.Bouncers.Mesh("rbc_bouncer", "bounce_maxwell", kBT=p.kBT)
    u.registerBouncer(rbc_bouncer)

    # Interactions

    dpd_oo = mir.Interactions.Pairwise('dpd_oo', rc=rc, kind="DPD", **p.dpd_oo.to_interactions())
    dpd_ii = mir.Interactions.Pairwise('dpd_ii', rc=rc, kind="DPD", **p.dpd_ii.to_interactions())
    dpd_io = mir.Interactions.Pairwise('dpd_io', rc=rc, kind="DPD", **p.dpd_io.to_interactions())

    u.registerInteraction(dpd_oo)
    u.registerInteraction(dpd_ii)
    u.registerInteraction(dpd_io)

    dpd_rbco = mir.Interactions.Pairwise('dpd_rbco', rc=rc, kind="DPD", **p.dpd_rbco.to_interactions())
    dpd_rbci = mir.Interactions.Pairwise('dpd_rbci', rc=rc, kind="DPD", **p.dpd_rbci.to_interactions())

    # Walls

    wall = mir.Walls.Cylinder('pipe', center=(domain[1]/2, domain[2]/2),
                              radius=R, axis='x', inside=True)
    u.registerWall(wall)
    frozen = u.makeFrozenWallParticles("frozen", walls=[wall], interactions=[dpd_oo], integrator=vv, number_density=p.nd, nsteps=int(1.0/dt_solv), dt=dt_solv)
    u.setWall(wall, pv_outer)

    # Set interactions between pvs

    u.registerInteraction(dpd_rbco)
    u.registerInteraction(dpd_rbci)

    u.setInteraction(dpd_oo, pv_outer, pv_outer)
    u.setInteraction(dpd_ii, pv_inner, pv_inner)
    u.setInteraction(dpd_io, pv_inner, pv_outer)

    u.setInteraction(dpd_rbco, pv_rbc, pv_outer)
    u.setInteraction(dpd_rbci, pv_rbc, pv_inner)

    u.setInteraction(dpd_oo, pv_outer, frozen)
    u.setInteraction(dpd_io, pv_inner, frozen)
    u.setInteraction(dpd_rbco, pv_rbc, frozen)

    # set bouncers
    u.setBouncer(rbc_bouncer, pv_rbc, pv_inner)
    u.setBouncer(rbc_bouncer, pv_rbc, pv_outer)

    # Integrators

    u.setIntegrator(vv, pv_outer)
    u.setIntegrator(vv, pv_inner)

    dt_rbc_el = p.rbc_params.get_max_dt_elastic(mass=p.m)
    substeps = 1 + int(dt_solv / dt_rbc_el)
    dt = dt_solv

    if no_visc:
        rbc_int = mir.Interactions.MembraneForces('int_rbc', **p.rbc_params.to_interactions(), stress_free=True)
        u.registerInteraction(rbc_int)

        if substeps == 1:
            u.setIntegrator(vv, pv_rbc)
            u.setInteraction(rbc_int, pv_rbc, pv_rbc)
        else:
            vv_rbc = mir.Integrators.SubStep("vv_rbc", substeps, fastForces=[rbc_int])
            u.registerIntegrator(vv_rbc)
            u.setIntegrator(vv_rbc, pv_rbc)

    else: # we use Shardlow integration
        dt_rbc_visc = p.rbc_params.get_max_dt_visc(mass=p.m)

        rbc_int = mir.Interactions.MembraneForces('int_rbc', **p.rbc_params.to_interactions_zero_visc(), stress_free=True)
        u.registerInteraction(rbc_int)

        vv_rbc = mir.Integrators.SubStepShardlowSweep("vv_rbc", substeps=10, fastForces=rbc_int, nsweeps=20, **p.rbc_params.to_viscous())
        u.registerIntegrator(vv_rbc)
        u.setIntegrator(vv_rbc, pv_rbc)

    # Plugins

    t_dump_every = 0.2 * R / Vmax
    dump_every = int(t_dump_every/dt)
    stats_every = dump_every

    h = 0.1 * rc
    u.registerPlugins(mir.Plugins.createWallRepulsion("wall_force", pv_rbc, wall, C=p.max_contact_force/h, h=h, max_force=p.max_contact_force))

    # estimate the number density of particles, accounting for pv_rbcs
    rho = p.nd

    f = (p.pressure_gradient / p.nd, 0, 0)
    u.registerPlugins(mir.Plugins.createAddForce("body_force_solvent", pv_outer, f))
    u.registerPlugins(mir.Plugins.createAddForce("body_force_hemoglobine", pv_inner, f))

    u.registerPlugins(mir.Plugins.createDumpMesh("mesh_dump", pv_rbc, dump_every, 'ply/'))
    u.registerPlugins(mir.Plugins.createStats("stats", every=stats_every, filename='stats.csv', pvs=[pv_inner, pv_outer, pv_rbc]))
    u.registerPlugins(mir.Plugins.createDumpObjectStats("rbc_stats", ov=pv_rbc, dump_every=stats_every, path="obj_stats"))

    if u.isMasterTask():
        print(f"tend = {tend}")
        print(f"dt = {dt}")
        print(f"substeps = {substeps}")
        sys.stdout.flush()

    u.dumpWalls2XDMF([wall], h=(1,1,1), filename='h5/wall')

    if restart_directory is not None:
        u.restart(restart_directory)

    u.run(int(tend/dt), dt)

def main(argv):
    import argparse
    parser = argparse.ArgumentParser(description='Run RBCs flowing in a capillary pipe.')
    parser.add_argument('--params', type=str, default="parameters.pkl", help="Parameters file.")
    parser.add_argument('--ranks', type=int, nargs=3, default=(1, 1, 1), help="Ranks in each dimension.")
    parser.add_argument('--no-visc', action='store_true', default=False, help="Set zero membrane viscosity.")
    parser.add_argument('--restart-dir', type=str, default=None, help="The restart directory name (no restart if not set)")
    parser.add_argument('--checkpoint-dir', type=str, default=None, help="The checkpoint directory name (no checkpoint if not set)")
    args = parser.parse_args(argv)

    p = load_parameters(args.params)

    run_capillary_flow(p,
                       no_visc = args.no_visc,
                       ranks=tuple(args.ranks),
                       restart_directory=args.restart_dir,
                       checkpoint_directory=args.checkpoint_dir)

if __name__ == '__main__':
    main(sys.argv[1:])


import datetime
import webbrowser

import speech_recognition as sr
import pyttsx3
import wikipedia
import os
import random
import smtplib

engine=pyttsx3.init('sapi5')
voices=engine.getProperty('voices')
engine.setProperty('voice', voices[0].id)

def speak(audio):
    engine.say(audio)
    engine.runAndWait()

""" Wish you according to the time """

def wishme():
    hour=int(datetime.datetime.now().hour)
    if(hour>=1 and hour<12):
        speak("Good Morning Sir!")

    elif(hour>=12 and hour<18):
        speak("Good Afternoon Sir!")

    else:
        speak("Good Evening Sir!")

    speak("I'm your jarvis . How may I help you !")

"""Take the command from the user and return it as string """

def takeCommand():

    r=sr.Recognizer()
    with sr.Microphone() as source:
        print("Listening .....")
        r.pause_threshold = 1
        r.energy_threshold = 200
        audio = r.listen(source)

    try:
        print("Recognizing .... ")
        query = r.recognize_google(audio, language='en-in')
        print(f"User said : {query}/n")

    except Exception as e:
        print("Please say that again ... ")
        return "None"
    return query
def sendMail(to,content):
    server=smtplib.SMTP('smtp.gmail.com',587)
    server.ehlo()
    server.starttls()
    server.login("youremail","yourmail password")
    server.sendmail("your mail",to,content)
    server.close()

if __name__ == '__main__':
    wishme()

    while True:
        query = takeCommand().lower()
        if 'wikipedia' in query:
            speak("Searching wikipedia....")
            query=query.replace("wikipedia","")
            results=wikipedia.summary(query,sentence=2)
            speak("According to wikipedia")
            print(results)
            speak(results)

        elif 'open youtube' in query:
            webbrowser.open("youtube.com")

        elif 'open google' in query:
            webbrowser.open("google.com")

        elif 'open stackflow' in query:
            webbrowser.open("stackflow.com")

        elif 'play song' in query:
            music_dir='G:\\2022 Songs'
            songs=os.listdir(music_dir)
            print(songs)
            randomno=random.randint(0,len(songs))
            os.startfile(os.path.join(music_dir,songs[randomno]))

        elif 'time' in query:
            strTime=datetime.datetime.now().strftime("%H:%M:%S")
            speak(f"Sir ,the time is {strTime}")

        elif 'open code' in query:
            codepath="C:\\Users\\sahil\\AppData\\Local\\Programs\\Microsoft VS Code\\Code.exe"
            os.startfile(codepath)

        elif 'send mail' in query:
            try:
                speak("What should I send ?")
                content=takeCommand()
                to="sahilbeny24@gmail.com"
                sendEmail(to,content)
                speak("Email sent scuccessfully.")
            except Exception as e:
                speak("Sorry my friend I was unable to send mail.")

        else:
            print("Sorry I can't Help with that.")

        if "quit" in query or "stop" in query or "exit" in query:
            speak("Thanks for using Jarvis . See you again")
            print("MADE BY --- SAHIL BENIWAL ")
            print(".....Thanks a lot .....")
            exit()


from tkinter import *
import bs4
import requests
import threading
import time
import datetime
import plyer
from PIL import Image,ImageTk

def get_Data(url):
	r = requests.get(url)
	return r

def get_corona_detail_india():
	url = "https://www.mohfw.gov.in/"
	data = get_Data(url)
	bs = bs4.BeautifulSoup(data.text,'html.parser')
	info = bs.findAll(True,{"class":["bg-blue","bg-green","bg-red","bg-orange"]})
	covid_info = ""
	for mydata in info:
		count = mydata.find("strong").get_text()
		text = mydata.find("span").get_text()
		covid_info += text + " : " + count + "\n"
	return covid_info

def get_corona_detail_mah():
	url = "https://www.mohfw.gov.in/"
	data = get_Data(url)
	bs = bs4.BeautifulSoup(data.text,'html.parser')
	mahinfo = ""
	for tr in bs.findAll('tr'):
		mahinfo += tr.get_text()
	item_list = mahinfo.split("\n\n")
	state = "Maharashtra"
	for item in item_list[1:36]:
		state_data = item.split('\n')
		if state_data[1] == state:
			stext = f"{state_data[1]}\nActive Cases : {state_data[2]}\nCured/Discharged : {state_data[3]}\nDeaths : {state_data[4]}\nConfirmed Cases : {state_data[5]}"
	return stext

def notify_me():
	while True:
		plyer.notification.notify(
			title = "COVID-19 cases of Maharashtra",
			message = get_corona_detail_mah(),
			timeout = 10,
			app_icon = 'corona.ico'
		)
		time.sleep(30)

#Create GUI
root = Tk()
root.iconbitmap("corona.ico")
c1 = PhotoImage(file="back2.png")
root.geometry("600x500+300+200")
root.title("COVID-19 INDIA TRACKER ")

panel = Label(root,image=c1)
panel.grid(row=0,column=0)
display = Label(root,text=get_corona_detail_india(),font=("poppins",20,"italic"))
display.grid(row=0,column=0,sticky=N)
display1 = Label(root,text=get_corona_detail_mah(),font=("poppins",20,"italic"),background='gray99')
display1.grid(row=0,column=0)

#Create new thread
th = threading.Thread(target=notify_me)
th.setDaemon(True)
th.start()

root.mainloop()


import numpy as np
import cv2
from matplotlib import pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
from matplotlib import patches,  lines
import os

def TwoD2ThreeD(P, coord, z):
    # transform 2D points in the image to 3D points in the space
	# input:    P: (3, 4) projection matrix
	#           coord: 2D coordinate matrix (non-homo) of size (2, N)
	#           z: array of z in 3D space of size (N,)
	# output:   3D coordinate matrix (non-homo) of size (3, N)
	temp_P = np.copy(P[:, 0:3]).astype(float)
	TwoD_homo = convertToHomo(coord)
	res = np.zeros((3, coord.shape[1]))
	res[2, :] = z

	for i in range(coord.shape[1]):
		temp_P[:, 2] = z[i]*P[:, 2] + P[:, 3]
		temp = np.dot(np.linalg.inv(temp_P), TwoD_homo[:, i])
		res[0, i] = temp[0]/temp[2]
		res[1, i] = temp[1]/temp[2]

	return res

def ThreeD2TwoD(P, coord):
    # transform 3D points in the space to 2D points in the image
    # input:    P: (3, 4) projection matrix
    #           coord: 3D coordinate matrix (non-homo) of size (3, N)
    # output:   2D coordinate matrix (non-homo) of size (2, N)
    ThreeD_homo = convertToHomo(coord)
    TwoD_homo = P.dot(ThreeD_homo)
    return convertFromHomo(TwoD_homo)

def convertToHomo(coord):
    res = np.ones((coord.shape[0]+1, coord.shape[1]))
    res[:coord.shape[0], :] = coord
    return res

def convertFromHomo(coord):
    res = coord[:-1, :]
    for i in range(res.shape[0]):
        res[i, :] = res[i, :]/coord[-1,:]
    return res

def load_gt_disp_kitti(path):
    gt_disparities = []
    disp = cv2.imread(path, -1)
    disp = disp.astype(np.float32) / 256
    gt_disparities.append(disp)
    return gt_disparities

def convert_disps_to_depths_kitti(disparities_list, width_to_focal, original_shape = (375, 1242), mode = 'pred', filename = '../images/data/left_img/000056_10.png', res_dir = '../images/res/'):
    depths_list = []
    height, width = original_shape
    for i in range(len(disparities_list)):
        disp = disparities_list[i]
        disp = width * cv2.resize(disp, (width, height), interpolation=cv2.INTER_LINEAR)

        if(mode == 'pred'):
            depth = width_to_focal[width] * 0.54 / disp
        else:
            # print(gt_disp)
            mask = disp > 0
            # 0.54, 0.5707 width_to_focal[width]
            depth =  width_to_focal[width] *  0.54 / (disp + (1.0 - mask))

        output_name = (filename.split('/')[-1]).split('.')[0]
        plt.imsave(os.path.join(res_dir+output_name+'_depth_cal.png'), depth, cmap='plasma')
        depths_list.append(depth)
    return depths_list

def plot_mask(image, masks):
    # only plot 2D region of the object detected by Mask-RCNN
    height, width = image.shape[:2]
    trial_img = np.zeros((height, width, 3))
    _, ax = plt.subplots(1)
    ax.set_ylim(height + 10, -10)
    ax.set_xlim(-10, width + 10)
    ax.axis('off')
    # ax.set_title(title)
    alpha = 0.5
    N = masks.shape[2]
    for i in range(N):
        mask = masks[:, :, i]
        mask_pos = np.where((mask==1))
        # print("0 (y) range {}~{}, 1 (x) range: {}~{}".format(np.min(mask_pos[0]), np.max(mask_pos[0]),
        #                                 np.min(mask_pos[1]), np.max(mask_pos[1])))
        for c in range(3):
            trial_img[:, :, c] = np.where(mask == 1,
                                    trial_img[:, :, c] *
                                    (1 - alpha) + alpha * 1 * 255,
                                    trial_img[:, :, c])

    ax.imshow(trial_img.astype(np.uint8))
    plt.show()

def get_pcd_masked(masks, depth_data, P, is_show = False, threshold = 500, filename = '../images/data/left_img/000056_10.png', res_dir = '../images/res/'):
    # only plot 3D points from the object detected by Mask-RCNN
    # input:    mask: a list of output masks from MaskRCNN with size of (H,W,N)
    #           depth_data: depth information for the same image
    #           P: (3, 4) projection matrix
    # output:   pcd_3D_list: list of array of object position in 3D space with size of (3, N)
    fig = plt.figure()
    ax = fig.add_subplot(111, projection='3d')
    pcd_3D_list = []
    for idx in range(masks.shape[2]):
        mask = masks[:,:,idx]
        mask_pos = np.where((mask==1)*(depth_data < 200.0))  #
        # print("mask size = {}".format(mask_pos[0].shape))
        # print("filered size 1: {}".format(mask_pos[0].shape[0]))
        if(mask_pos[0].shape[0] < threshold):
            continue

        depth_masked = depth_data[mask_pos]

        d_median = np.median(depth_masked)
        inlier_idx = np.where((depth_masked - d_median > -1)*(depth_masked - d_median < 6))
        # print("filered size 2: {}".format(inlier_idx[0].shape[0]))
        if(inlier_idx[0].shape[0] < threshold):
            continue
        depth_masked = depth_masked[inlier_idx]

        pcd_2D = np.array([mask_pos[1][inlier_idx], mask_pos[0][inlier_idx]])

        pcd_3D = TwoD2ThreeD(P, pcd_2D, depth_masked)
        pcd_3D_list.append(pcd_3D)
        if(is_show):
            ax.scatter(pcd_3D[0, :], pcd_3D[1, :], pcd_3D[2, :], c='r', marker='.')

            vertice3D, edge = boundingbox(pcd_3D)
            for i in range(edge.shape[0]):
                #line_plot = np.array([],[])
                ax.plot([vertice3D[0, edge[i, 0]], vertice3D[0, edge[i, 1]]],
                    [vertice3D[1, edge[i, 0]], vertice3D[1, edge[i, 1]]],
                    [vertice3D[2, edge[i, 0]], vertice3D[2, edge[i, 1]]], 'b')

    print("{} mask, {} saved".format(masks.shape[2], len(pcd_3D_list)))

    if(is_show):
        Xmax, Xmin, Ymax, Ymin, Zmax, Zmin = 10, -10, 10, 0, 60, 0
        ax.plot([0, 0],[0, 0], [Zmax, 0])
        ax.axes.set_xlim3d(left=-20, right=20)
        ax.axes.set_ylim3d(bottom=-2, top=6)
        ax.axes.set_zlim3d(bottom=0, top=60)

        ax.view_init(elev = -40, azim = -90)
        ax.set_xlabel('x [m]')
        ax.set_ylabel('y [m]')
        ax.set_zlabel('z [m]')
        plt.grid()
        output_name = (filename.split('/')[-1]).split('.')[0]
        fig.savefig(res_dir+output_name+'_pcd.png')
        # plt.show()
    return pcd_3D_list

def boundingbox(pcd_3D, z_offset = 0):
    # generate 3D bounding box
    xyz_max = np.max(pcd_3D, axis=1)
    xyz_min = np.min(pcd_3D, axis=1)
    x1, x2 = xyz_min[0],xyz_max[0]
    y1, y2 = xyz_min[1],xyz_max[1]
    z1, z2 = xyz_min[2],xyz_max[2] + z_offset
    vertice3D = np.array([[x1, x1, x1, x1, x2, x2, x2, x2],
                        [y1, y1, y2, y2, y1, y1, y2, y2],
                        [z1, z2, z1, z2, z1, z2, z1, z2]])
    edge = np.array([[0, 1],[0, 2],[2, 3],[1, 3],[0, 4],[1, 5],
                        [2, 6],[3, 7], [5, 7],[4, 5],[6, 7],[4, 6] ])
    return vertice3D, edge

def get_boundingbox(pcd_3D_list, image, P, res_dir, img_name, z_offset = 0):
    # plot the bounding box back to 2D orginal image
    # input: pcd_3D_list a list of 3D coordinate of detected objects, with size of (3, N)
    #       image: origin 2D image
    #       P: transformation matrix of size (3, 4)
    # output: None
    fig, ax1 = plt.subplots(ncols=1)
    ax1.imshow(image)
    for idx in range(len(pcd_3D_list)):
        pcd_3D = pcd_3D_list[idx]
        vertice3D, edge = boundingbox(pcd_3D, z_offset)
        vertice2D = ThreeD2TwoD(P, vertice3D)
        for i in range(edge.shape[0]):
            ax1.plot([vertice2D[0, edge[i, 0]], vertice2D[0, edge[i, 1]]],
                [vertice2D[1, edge[i, 0]], vertice2D[1, edge[i, 1]]], 'r')

    # plt.show()
    ax1.axis('off')
    plt.tight_layout()
    plt.savefig(res_dir+img_name+'_box.png', bbox_inches='tight', pad_inches=0, dpi=199)    #

####################### NOT USED BELOW #######################

def generate_xy(d_dim):
    # generate all 2D coordinate in an image of size d_dim
    # output: (2, d_dim[0]*d_dim[1]) array
	Y_size, X_size = d_dim
	coord_y = np.repeat(np.array(range(Y_size)), X_size).astype(float)	# column direction
	coord_x = np.repeat(np.array(range(X_size)), Y_size).astype(float)
	coord_x = (coord_x.reshape(-1, Y_size)).transpose().reshape(-1)		# row direction
	coord_x = np.expand_dims(coord_x, axis=0)
	coord_y = np.expand_dims(coord_y, axis=0)
	return np.concatenate((coord_x, coord_y), axis=0)

def plot_pcd(depth_data, P):
    # plot all 3D points from depth image
    ref_pcd_2D = generate_xy(depth_data[0].shape)
    ref_pcd_3D = TwoD2ThreeD(P, ref_pcd_2D, depth_data[0].reshape(-1))
    fig = plt.figure()
    ax = fig.add_subplot(111, projection='3d')
    ax.scatter(ref_pcd_3D[0, :], ref_pcd_3D[1, :], ref_pcd_3D[2, :], c='r', marker='.')
    plt.show()

def test_transformation():
    a = np.array([[1, 1, 1], [2.15, 2.15, 2.15], [1, 4, 2], [1, 1, 0.5]])
    a = a.T
    f = 2
    Pin = np.array([[f, 0 ,2],[0, f , 2],[0, 0, 1]]).astype(float)
    Pex = np.array([[1, 0 ,2, 0],[0, 1 , 2, 0],[0, 0, 1, 1]]).astype(float)
    P = Pin.dot(Pex)
    P = np.array([[0, -1, 0, 0],[1, 0, 0, 0],[0, 0, 1, 0]]).astype(float)
    print("original 3D: ")
    print(a)
    test_res = ThreeD2TwoD(P, a)
    print("converted 2D:")
    print(test_res)
    test_a = TwoD2ThreeD(P, test_res, a[2, :])
    print("calculated 3D:")
    print(test_a)


#####################################
# tabcsvly                          #
# 31/11/2022                        #
# Daniel Anthony Baudyš (ton1czech) #
#####################################

# import modules
from bs4 import BeautifulSoup
from urllib.request import urlopen
import pandas as pd
import re

# get user url
url = input('type your URL [http://...]: ')
# get user table
num = int(input('what table [1-99] (from above): '))
# get user filename
filename = input('choose file name [a-z]: ')

def main():
    # init scraper
    soup = BeautifulSoup(urlopen(url).read().decode('utf-8'), 'html.parser')

    # fint html table
    table = str(soup.find_all('table')[num-1])

    # regex
    table = re.sub("\[.*\]", '', table)

    # use pandas to red html table
    df = pd.read_html("".join(table))[0]

    # show the result
    print(df)
    # save data into .csv file
    df.to_csv(f'{filename}.csv', index=False)

if __name__ == "__main__":
    main()