import os
import torch
from torch.utils import data
import torchaudio

from torch.utils.data import DataLoader, Dataset
from pathlib import Path

import matplotlib.pyplot as plt

def load_audio_files(path:str, label:str):
    dataset = []
    walker = sorted(str(p) for p in Path(path).glob(f'*.wav'))

    for i, file_path in enumerate(walker):
        path, filename = os.path.split(file_path)
        speaker, _ = os.path.splitext(filename)
        speaker_id, utterance_number = speaker.split("_nohash_")
        utterance_number = int(utterance_number)

        waveform, sample_rate = torchaudio.load(file_path)
        dataset.append([waveform, sample_rate, label, speaker_id, utterance_number])

    return dataset

trainset_speech_commands_yes = load_audio_files('./data/SpeechCommands/speech_commands_v0.02/yes', 'yes')
trainset_speech_commands_no = load_audio_files('./data/SpeechCommands/speech_commands_v0.02/no', 'no')

trainloader_yes = data.DataLoader(
    trainset_speech_commands_yes,
    batch_size=1,
    shuffle=True,
    num_workers=0
)

trainloader_no = data.DataLoader(
    trainset_speech_commands_no,
    batch_size=1,
    shuffle=True,
    num_workers=0
)

def show_waveform(waveform, sample_rate, label):
    """
    docstring
    """
    print("Waveform: {}\nSample rate: {}\nLabels: {}".format(waveform, sample_rate, label))
    new_sample_rate = sample_rate / 10
    print(new_sample_rate)

    channel = 0

    waveform_transformed = torchaudio.transforms.Resample(
        sample_rate, new_sample_rate)(waveform[channel,:].view(1,-1)
    )

yes_waveform = trainset_speech_commands_yes[0][0]
yes_sample_rate = trainset_speech_commands_yes[0][1]
no_waveform = trainset_speech_commands_no[0][0]
no_sample_rate = trainset_speech_commands_no[0][1]

show_waveform(yes_waveform, yes_sample_rate, "yes")

def show_spectrogram(waveform):
    spectrogram = torchaudio.transforms.Spectrogram()(waveform)
    #print(spectrogram)
    print("Shape of spectrogram: {}".format(spectrogram.size()))

    plt.figure()
    plt.imsave(f'test/spectrogram_img.png', spectrogram.log2()[0,:,:].numpy(), cmap='gray')

show_spectrogram(yes_waveform)

def show_merspectrogram(waveform, sample_rate):
    """
    docstring
    """
    mel_spectrogram = torchaudio.transforms.MelSpectrogram(
        sample_rate
    )(waveform)
    plt.figure()
    plt.imsave(f'test/spectrogram_img.png', mel_spectrogram.log2()[0,:,:].numpy(), cmap='gray')

def show_mfcc(waveform,sample_rate):
    mfcc_spectrogram = torchaudio.transforms.MFCC(sample_rate= sample_rate)(waveform)
    print("Shape of spectrogram: {}".format(mfcc_spectrogram.size()))

    plt.figure()
    fig1 = plt.gcf()
    plt.imshow(mfcc_spectrogram.log2()[0,:,:].numpy(), cmap='gray')

    plt.figure()
    plt.plot(mfcc_spectrogram.log2()[0,:,:].numpy())
    plt.draw()

show_mfcc(no_waveform, no_sample_rate)


import time
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import patches as mpl_patches
from matplotlib import lines as mpl_lines
from scipy.integrate import ode

# Physical constants
g = 9.8
damping_factor = 0.9 * 0
length_r1 = 2.0
length_r2 = 4.0
mass_bob1 = 10.0
mass_bob2 = 5.0
plotsize = 1.10 * (length_r1 + length_r2)

# initial state
theta1_initial = + 120 / 180 * np.pi
theta2_initial = + 180 / 180 * np.pi
theta1_dot_initial = 0
theta2_dot_initial = 0

def calc_xy(length, theta):
    x = length * np.sin(theta)
    y = - length * np.cos(theta)
    return x, y

_x1, _y1 = calc_xy(length_r1, theta1_initial)
bob1 = mpl_patches.Circle((_x1, _y1), 0.05 * plotsize, fc='g', alpha=1, zorder=2)
bob1.set_picker(0)
stick1 = mpl_lines.Line2D([0, _x1], [0, _y1], zorder=2)

_x2, _y2 = calc_xy(length_r2, theta2_initial)
_x2 += _x1
_y2 += _y1
bob2 = mpl_patches.Circle((_x2, _y2), 0.05 * plotsize, fc='r', alpha=1, zorder=2)
stick2 = mpl_lines.Line2D([_x1, _x2], [_y1, _y2], zorder=2)

def get_derivatives_double_pendulum(t, state):
    ''' definition of ordinary differential equation for a
        double pendulum
    '''
    theta1, theta1_dot, theta2, theta2_dot = state

    _num1 = -g * (2 * mass_bob1 + mass_bob2) * np.sin(theta1)
    _num2 = -mass_bob2 * g * np.sin(theta1-2*theta2)
    _num3 = -2*np.sin(theta1-theta2) * mass_bob2
    _num4 = theta2_dot * theta2_dot * length_r2 + \
            theta1_dot * theta1_dot * length_r1 * np.cos(theta1-theta2)
    _den = length_r1 * (2 * mass_bob1 + mass_bob2 - \
           mass_bob2 * np.cos(2*theta1-2*theta2))
    theta1_doubledot = (_num1 + _num2 + _num3 * _num4) / _den

    _num1 = 2 * np.sin(theta1-theta2)
    _num2 = (theta1_dot * theta1_dot * length_r1 * (mass_bob1 + mass_bob2))
    _num3 = g * (mass_bob1 + mass_bob2) * np.cos(theta1)
    _num4 = (theta2_dot * theta2_dot * length_r2 * \
            mass_bob2 * np.cos(theta1-theta2))
    _den = length_r2 * (2 * mass_bob1 + mass_bob2 - \
           mass_bob2 * np.cos(2*theta1-2*theta2))
    theta2_doubledot = (_num1 * (_num2 + _num3 + _num4)) / _den

    state_differentiated = np.zeros(4)
    state_differentiated[0] = theta1_dot
    state_differentiated[1] = theta1_doubledot
    state_differentiated[2] = theta2_dot
    state_differentiated[3] = theta2_doubledot

    return state_differentiated

dp_integrator = ode(get_derivatives_double_pendulum).set_integrator('vode')

def plot_double_pendulum(fig, trace_line, ax2, theta_line):
    # note a frame per second (fps) > 10 the actual time
    # may not be able to keep up with model time
    fps = 15
    seconds_per_frame = 1/fps

    def current_time():
        return time.time()

    def check_drift(_time, running_time):
        # check every 5 seconds
        if _time % 5 < seconds_per_frame:
            print(f'time (ms): {1000*_time:,.0f}, '
                  f'drift: {1000*(running_time - _time):,.0f}')

    t_initial = 0
    _time = t_initial

    state = np.array([theta1_initial, theta1_dot_initial,
                      theta2_initial, theta2_dot_initial])
    dp_integrator.set_initial_value(state, t_initial)

    _x2, _y2 = bob2.center
    x_traces = [_x2]
    y_traces = [_y2]

    time_window = 20
    time_base = - time_window

    actual_start_time = current_time()
    while dp_integrator.successful():

        theta1, _, theta2, _ = state

        _x1, _y1 = calc_xy(length_r1, theta1)
        bob1.center = (_x1, _y1)
        stick1.set_data([0, _x1], [0, _y1])

        _x2, _y2 = calc_xy(length_r2, theta2)
        _y2 += _y1
        _x2 += _x1
        bob2.center = (_x2, _y2)
        stick2.set_data([_x1, _x2], [_y1, _y2])

        x_traces.append(_x2)
        y_traces.append(_y2)
        trace_line.set_data(x_traces[:], y_traces[:])

        if _time % time_window < seconds_per_frame:
            time_base += time_window
            ax2.set_xlim(time_base, time_base+time_window)
            time_values = []
            theta_values = []

        time_values.append(_time)
        angle = -np.pi + (theta1 - np.pi) % (2 * np.pi)
        theta_values.append(np.degrees(angle))
        theta_line.set_data(time_values, theta_values)

        running_time = current_time() - actual_start_time
        check_drift(_time, running_time)

        while running_time < _time:
            running_time = current_time() - actual_start_time

        else:
            # blip the image
            fig.canvas.draw()
            fig.canvas.flush_events()

        state = dp_integrator.integrate(dp_integrator.t + seconds_per_frame)
        _time += seconds_per_frame

        if _time > 500:
            return

def main():
    '''  set plot and action
    '''
    fig, (ax1, ax2) = plt.subplots(2, 1, figsize=(5, 5))
    fig.canvas.set_window_title('Double Pendulum')
    ax1.set_title('s1: click on green bob to start ...')
    ax1.set_xlim(-plotsize, plotsize)
    ax1.set_ylim(-plotsize, plotsize)
    ax1.set_aspect(1)
    trace_line, = ax1.plot([0], [0], color='black', linewidth=0.2, zorder=1)

    theta_line, = ax2.plot([0], [0], color='black', linewidth=0.5, zorder=1)
    ax2.set_ylim(-200, 200)

    def on_pick(event):
        plot_double_pendulum(fig, trace_line, ax2, theta_line)

    ax1.add_patch(bob1)
    ax1.add_line(stick1)
    ax1.add_patch(bob2)
    ax1.add_line(stick2)

    bob1.figure.canvas.mpl_connect('pick_event', on_pick)

    plt.show()

if __name__ == "__main__":
    main()


#Data visualization in Python
#Markers, colors and rows type

import matplotlib.pyplot as plt

x = [1, 2, 3, 4, 5]
y = [2, 3, 7 , 1, 0]
z = [200, 5, 400, 3300, 10]

title = "Scatterplot"
axisx = "Axis X"
axisy = "Axis Y"

#Subtitles
plt.title(title)
plt.xlabel(axisx)
plt.ylabel(axisy)

plt.plot(x,y,color="k",linestyle="--")
plt.scatter(x, y, label = "My points", color = "#990000", marker=".", s=z)
plt.legend()

plt.show()


import sys
if sys.version_info < (3,):
    print('Python 2 is not supported by CVD-risk-scores.')
    sys.exit(-1)
if sys.platform == 'win32' and sys.maxsize.bit_length() == 31:
    print('32-bit Windows Python runtime is not supported. Please switch to 64-bit Python.')
    sys.exit(-1)

import pathlib
import traceback
import logging
import platform
from setuptools import setup, find_packages

# make sure python system version is at leaast 3.7.2
python_min_v = (3, 7, 2)
python_min_v_str = '.'.join(map(str, python_min_v))
if sys.version_info < python_min_v:
    logging.error("Python version required is at least {}. Currently you have {}.".format(python_min_v_str, platform.python_version()))

# determine semantic release version
DIR = pathlib.Path(__file__).parent
SOURCE_DIR = "src"
PROJECT_NAME = "cvd_risk_scores"

def main():
    long_description = (DIR / 'README.md').read_text()

    try:
        changelog = (DIR / 'CHANGELOG.md').read_text()
        __version__, *_ = re.findall(r"\[([0-9.]+)]", changelog)
    except (FileNotFoundError, ValueError) as ex:
        __version__ = '1.0.2'
        logging.error(ex)
        logging.error(traceback.print_exc())
        logging.warning(f'Unable to get semantic release version. Setting version to {__version__}.')

    setup(
        name=PROJECT_NAME,
        version=__version__,
        description='A Python package for computing cardiovascular disease risk using clinically validated models.',
        long_description=long_description,
        long_description_content_type='text/markdown',
        author='francesco-pisu',
        author_email='fra.pisu1@gmail.com',
        url='https://github.com/francescopisu/CVD-risk-scores',
        license='MIT',
        python_requires='>=3.7,<3.10',
        install_requires=[
            'numpy>=1.0.0,<2.0.0',
            'pandas>=1.0.0,<2.0.0',
            'typing-extensions>=3.0.0,<10.0.0',
            'pydantic>=1.0.0,<10.0.0'
        ],
        package_dir={f'{PROJECT_NAME}': f'{SOURCE_DIR}/{PROJECT_NAME}'},
        packages=find_packages(where=f'{SOURCE_DIR}', exclude=['tests']),
        # package_data={f'{PROJECT_NAME}': ['data/*.json']},
        include_package_data=True,
        classifiers=[
            'Development Status :: 2 - Pre-Alpha',
            'Intended Audience :: Science/Research',
            'Topic :: Scientific/Engineering :: Medical Science Apps.',
            'License :: OSI Approved :: MIT License',
            'Programming Language :: Python :: 3',
            'Programming Language :: Python :: 3.7',
            'Programming Language :: Python :: 3.8',
            'Programming Language :: Python :: 3.9',
        ]
    )

if __name__ == "__main__":
    main()


from flask import Flask, render_template, request, flash
import hashlib

app = Flask(__name__)
app.secret_key = "blockchain"

class Block:
    def __init__(self, name, data, previous_hash):
        self.name = name
        self.data = data
        self.previous_hash = previous_hash
        self.hash = self.create_hash()

    def create_hash(self):
        block_header = str(self.name) + str(self.data) + str(self.previous_hash)
        block_hash = hashlib.sha256(block_header.encode()).hexdigest()
        return block_hash

class Blockchain:
    def __init__(self):
        self.blockchain = [Block("Genesis Block", "This is the first block", "0")]

    def add_block(self, name, data):
        previous_hash = self.blockchain[-1].hash
        new_block = Block(name, data, previous_hash)
        self.blockchain.append(new_block)

bc = Blockchain()

@app.route("/")
def index():
    return render_template("index.html")

@app.route("/transfer_money", methods=["POST"])
def transfer_money():
    try:
        if request.method == "POST":
            name = request.form["name"]
            data = request.form["money"]
            bc.add_block(name, data)
            blocks = []
            for block in bc.blockchain:
                block_info = {
                    'Block Name':block.name,
                    'Block Data':block.data,
                    'hash':block.hash,
                    'Previous Hash':block.previous_hash
                }
                blocks.append(block_info)
            for i in blocks:
                print(i)
            flash("Money Sended Successfully!")
            return render_template("index.html")
        else:
            flash("Something Went Wrong!!")
            return render_template("index.html")
    except:
        flash("Some Error Occured!!")
        return render_template("index.html")

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


"""
COSMOS_IRAC_redshift_cuts.py
Author: Benjamin Floyd

Uses the COSMOS catalog to examine our IR-bright AGN selection cuts as a function of redshift to determine if we have
significant contamination in our sample.
"""

import matplotlib.pyplot as plt
import numpy as np
from astropy.table import Table
from matplotlib.lines import Line2D

# Read in th COSMOS catalog
cosmos = Table.read('Data/COSMOS2015_Laigle+_v1.1.fits.gz')

#%%
# Remove non-detections
cosmos = cosmos[(cosmos['SPLASH_1_MAG'] > -99) &
                (cosmos['SPLASH_2_MAG'] > -99) &
                (cosmos['SPLASH_3_MAG'] > -99) &
                (cosmos['SPLASH_4_MAG'] > -99)]
cosmos = cosmos[(cosmos['PHOTOZ'] >= 0) | (cosmos['ZQ'] >= 0)]

# Convert the IRAC magnitudes from AB to Vega (because I don't want to have to convert the selections to AB).
cosmos['SPLASH_1_MAG'] += -2.788
cosmos['SPLASH_2_MAG'] += -3.255
cosmos['SPLASH_3_MAG'] += -3.743
cosmos['SPLASH_4_MAG'] += -4.372

# Apply our 4.5 um magnitude cuts as we would in SDWFS/SPT
cosmos = cosmos[(10.45 < cosmos['SPLASH_2_MAG']) & (cosmos['SPLASH_2_MAG'] < 17.46)]

# Apply Stern Wedge selection
stern_agn = cosmos[(cosmos['SPLASH_3_MAG'] - cosmos['SPLASH_4_MAG'] > 0.6) &
                   (cosmos['SPLASH_1_MAG'] - cosmos['SPLASH_2_MAG'] >
                    0.2 * (cosmos['SPLASH_3_MAG'] - cosmos['SPLASH_4_MAG']) + 0.18) &
                   (cosmos['SPLASH_1_MAG'] - cosmos['SPLASH_2_MAG'] >
                    2.5 * (cosmos['SPLASH_3_MAG'] - cosmos['SPLASH_4_MAG']) - 3.5)]

# Separate the non-AGN galaxies from the AGN
stern_complement_ids = list(set(cosmos['NUMBER']) - set(stern_agn['NUMBER']))
non_agn = cosmos[np.in1d(cosmos['NUMBER'], stern_complement_ids)]

#%%
non_agn_0_2 = non_agn[non_agn['PHOTOZ'] <= 4]
stern_agn_0_2 = stern_agn[stern_agn['ZQ'] <= 4]

# [3.6] - [4.5] color
agn_color = stern_agn_0_2['SPLASH_1_MAG'] - stern_agn_0_2['SPLASH_2_MAG']
non_agn_color = non_agn_0_2['SPLASH_1_MAG'] - non_agn_0_2['SPLASH_2_MAG']

#%%
fig = plt.figure()
gs = fig.add_gridspec(nrows=1, ncols=2, width_ratios=(8, 3), wspace=0)

# Main plot
ax = fig.add_subplot(gs[0])
ax.hexbin(non_agn_0_2['PHOTOZ'], non_agn_color, gridsize=(2500, 100), bins='log', mincnt=1, cmap='Blues')
ax.hexbin(stern_agn_0_2['ZQ'], agn_color, gridsize=(50, 75), bins='log', mincnt=1, cmap='Reds', alpha=0.5)
ax.set(title='COSMOS', xlabel='Photometric Redshift', ylabel='[3.6] - [4.5] (Vega)', xlim=[0, 4], ylim=[0., 1.5])

ax.axhline(y=0.7, color='k', linewidth=2)
# ax.arrow(0.25, 0.7, 0, 0.2, color='k', width=0.004, head_width=6 * 0.004, length_includes_head=True)

handles, labels = ax.get_legend_handles_labels()
handles.extend([Line2D([0], [0], marker='h', color='w', markerfacecolor='lightcoral', markeredgecolor='firebrick',
                       markersize=10),
                Line2D([0], [0], marker='h', color='w', markerfacecolor='lightblue', markeredgecolor='steelblue',
                       markersize=10)
                ])
labels.extend(['Stern Wedge AGN', 'Non-Active Galaxies'])
ax.legend(handles, labels, frameon=False)

# Side histogram
ax_hist = fig.add_subplot(gs[1], sharey=ax)
ax_hist.tick_params(axis='y', labelleft=False)
min_z, max_z = ax.get_xlim()
binwidth = 0.05
bins_nonAGN = np.arange(np.min(non_agn_color), np.max(non_agn_color) + binwidth, binwidth)
bins_AGN = np.arange(np.min(agn_color), np.max(agn_color) + binwidth, binwidth)
ax_hist.hist(agn_color[(stern_agn_0_2['ZQ'] > min_z) & (stern_agn_0_2['ZQ'] < max_z)], bins=bins_AGN,
             orientation='horizontal', color='lightcoral', alpha=0.6)
ax_hist.hist(non_agn_color[(non_agn_0_2['PHOTOZ'] > min_z) & (non_agn_0_2['PHOTOZ'] < max_z)], bins=bins_nonAGN,
             orientation='horizontal', color='lightblue', alpha=0.6)
ax_hist.axhline(y=0.7, color='k', linewidth=2)
ax_hist.set(xlabel=r'$N$', xscale='log')

# fig.savefig('Data/Plots/COSMOS_IRAC_Redshift_z4.pdf')
plt.show()


"""
AUTHOR - VEDIKA YADAV
TASK - REAL-TIME FACE MASK DETECTION
"""
# importing required libraries
import cv2
import dlib
import imutils.face_utils as face

# Initialising Detector

detector = dlib.get_frontal_face_detector()
# Reading Video
capture = cv2.VideoCapture("./video.mp4")
while True:
    # Reading Video frame by frame
    ret, frame = capture.read()
    # Resizing Frame
    frame = cv2.resize(frame, (1024, 512))
    # Converting to GrayScale
    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
    rects = detector(gray)
    for rect in rects:
        # Detecting bounding box for faces without mask
        (bX, bY, bW, bH) = face.rect_to_bb(rect)
        # Drawing bounding box
        cv2.rectangle(frame, (bX, bY), (bX + bW, bY + bH), (0, 0, 255), 3)
        cv2.putText(frame, text="No Mask", org=(bX, bY), fontScale=1,
                    fontFace=cv2.FONT_HERSHEY_SIMPLEX, color=(0, 0, 255),
                    thickness=2)
    # Showing the detected frames of video
    cv2.imshow('Window', frame)
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break
capture.release()
# Closing all opencv windows
cv2.destroyAllWindows()


import keras
import pytest

from convnet3d.models import (
    reductionModel,
    submodels,
    convnet3dModel
)

class TestReductionModel(object):
    def test_TimeDistributed(self):
        with pytest.raises(TypeError) :
            roi_input = keras.layers.Input(shape=(10, 25, 60, 60, 1))
            fpr_model = reductionModel()
            outputs = keras.layers.TimeDistributed(fpr_model)(roi_input)
            model   = keras.models.Model(inputs=roi_input, outputs=outputs)
            model.summary()

    def test_inputs_outputs(self):
        fpr_model = reductionModel()
        assert isinstance(fpr_model.inputs, list)
        assert isinstance(fpr_model.outputs, list)

    def test_TimeDistributed_with_submodels(self):
        roi_input = keras.layers.Input(shape=(10, 25, 60, 60, 1))
        fpr_model = reductionModel()
        sub_models = submodels(fpr_model)
        sub_models_outputs = [keras.layers.TimeDistributed(subm)(roi_input) for subm in sub_models]
        model = keras.models.Model(inputs=roi_input, outputs=sub_models_outputs)
        model.summary()

class TestConvnet3dModel(object):
    def test_simple(self):
        model = convnet3dModel()  # noqa: F841


# Importamos las bibliotecas

from multiprocessing.connection import wait
import os
import sys
import random
import pygame

from .config import *
from .platform import Platform
from .player import Player
from .wall import Wall
from .coin import Coin

# Defenimos una clase para el videojuego
class Game:

    # Definimos un metodo de inicializacion del videojuego
    def __init__(self):
        # Pintamos nuestra ventana
        pygame.init()
        # Generamos una ventana
        self.surface = pygame.display.set_mode((WIDTH,HEIGHT))
        # Titulo de la ventana
        pygame.display.set_caption(TITLE)
        # Conocer si la aplicacion se esta ejecutando o no
        self.running = True
        #
        # self.playing = True
        # Actualizar los frames por segundo
        self.clock = pygame.time.Clock()
        # Colocamos una funete a nuestro score (Alcance)
        self.font = pygame.font.match_font(FONT)
        # Ruta absoluta de un archivo
        self.dir = os.path.dirname(__file__)
        # Ruta de la carpeta de sonidos
        self.dir_sounds = os.path.join(self.dir, 'sources/sounds')
        # Ruta de la carpeta de imagenes
        self.dir_images = os.path.join(self.dir, 'sources/sprites')

    # Definimos un metodo para iniciar el juego
    def start(self):
        # Damos un menu de opciones
        self.menu()
        # Ejecutar el metodo new
        self.new()

    # Definimos un metodo para crear un nuevo juego (Nueva partida)
    def new(self):
        # El scor comienza en 0
        self.score = 0
        # Nivel 0 del juego
        self.level = 0
        #
        self.playing = True
        self.background =  pygame.image.load(os.path.join(self.dir_images, 'background.png'))
        #
        self.generate_elements()
        # Ejecutar el metodo run
        self.run()

    # Crea los elementos deljuego
    def generate_elements(self):
        self.platform = Platform()
        self.player = Player(100, self.platform.rect.top - 200, self.dir_images)

        # Agregamos las paredes
        # self.wall = Wall(500, self.platform.rect.top)

        # Agrupar los diferentes sprites a utilizar en el video juego
        self.sprites = pygame.sprite.Group()
        self.walls = pygame.sprite.Group()
        self.coins = pygame.sprite.Group()

        # Agregamos la plataforma a la lista
        self.sprites.add(self.platform)
        self.sprites.add(self.player)
        # self.sprites.add(self.wall)

        self.generate_walls()
        # self.generate_coins()

    # Genera nuevos obstaculos
    def generate_walls(self):
        last_position = WIDTH + 400
        # Si no hay obstaculos entonces los generamos
        if not len(self.walls) > 0:
            for w in range(0, MAX_WALLS):
                # Posiciones aleatorias
                left = random.randrange( last_position + 800, last_position + 1600)
                # Generamos objetos de tipo wall
                wall = Wall(left, self.platform.rect.top, self.dir_images)
                last_position = wall.rect.right

                # Agrgar a las listas
                self.sprites.add(wall)
                self.walls.add(wall)

            # Cuando se genera un obstaculo se incrementa en 1 el nivel
            self.level += 1
            # Generamos mas monedas
            self.generate_coins()

    #
    def generate_coins(self):
        last_position = WIDTH + 100

        for c in range(0, MAX_COINS):
            pos_x = random.randrange(last_position + 180, last_position + 300)

            # Colocamos todas las monedas a un altura de 100 pixeles
            coin = Coin(pos_x, 120, self.dir_images)

            last_position = coin.rect.right

            self.sprites.add(coin)
            self.coins.add(coin)

    # Definimos un metodo para correr el juego
    def run(self):
        # Iniciamos el ciclo del juego
        while self.running:
            self.clock.tick(FPS)
            self.events()
            self.update()
            self.draw()

    # Definimos un metodo para escuchar eventos
    def events(self):
        # Optenemos los objetos de eventos realizados
        for event in pygame.event.get():
            # Si cerramos la ventana detenemos la ejecucion del codigo
            if event.type == pygame.QUIT:
                self.running = False
                pygame.quit
                sys.exit()
        key = pygame.key.get_pressed()

        if key[pygame.K_SPACE]:
            self.player.jump()

        # Reiniciar el juego
        if key[pygame.K_r] and not self.playing:
            self.new()

    # Definimos un metodo para colocar los elementos en el juego
    def draw(self):
        # Colocar color a la superficie
        # self.surface.fill(BLACK)
        self.surface.blit(self.background, (0,0))
        # Pintamos el score en pantalla
        self.draw_text()
        # Dibujar todos los sprites que la lista almacene
        self.sprites.draw(self.surface)
        # Actualizamos la ventana
        pygame.display.flip()

    # Definimos un metodo para actualizar la pantalla
    def update(self):
        if not self.playing:
            return

        # Si hubo alguna colicion
        wall = self.player.collide_with(self.walls)
        if wall:
            if self.player.collide_bottom(wall):
                self.player.skid(wall)
            else:
                self.stop()

        # obsevamos los eventos de colicion
        coin = self.player.collide_with(self.coins)
        # Si existe una colicion
        if coin:
            self.score += 1
            # La moneda se destruye una vez se a colicionado con ella
            coin.kill()
            sound = pygame.mixer.Sound(os.path.join(self.dir_sounds, 'coin.wav'))
            sound.play()

        # Actualizamos los sprites
        self.sprites.update()

        # validamos los desplazamientos con la plataforma
        self.player.validate_platform(self.platform)

        # Actualizamos lo elementos visibles y quitamos el resto
        self.update_elements(self.walls)
        self.update_elements(self.coins)

        # Generar obstaculos cuando no halla elementos en la lista walls
        self.generate_walls()

        # Damos un retardo a todas la animaciones
        pygame.time.delay(21)

    # Eliminamos Todos los elementos que no son visibles en pantalla para liberar memoria
    def update_elements(self, elements):
        for element in elements:
            # Si el elemento no es visible lo quitamos
            if not element. rect. right > 0:
                # Elimina el objeto
                element.kill()

    # Definimos un metodo para detener el video juego
    def stop(self):
        sound = pygame.mixer.Sound(os.path.join(self.dir_sounds, 'lose.wav'))
        sound.play()
        # Detenemos al jugador
        self.player.stop()
        # Detenemos los objetos
        self.stop_elements(self.walls)

        self.playing = False

    # Detener todos los elementos
    def stop_elements(self, elements):
        for element in elements:
            element.stop()

    # Formato para el score
    def score_format(self):
        return 'Score : {}'.format(self.score)

    # Formato del nivel
    def level_format(self):
        return 'Level : {}'.format(self.level)

    # Pinta el score
    def draw_text(self):
        self.display_text(self.score_format(), 36, BLACK, WIDTH // 2, 30)
        self.display_text(self.level_format(), 36, BLACK, 80, TEXT_POSY)

        # Si el jugador ya no esta jugan 'Perdio'
        if not self.playing:
            self.display_text('Perdiste', 60, BLACK, WIDTH // 2, HEIGHT // 2)
            self.display_text('Preciona r para volver a comenzar', 30, BLACK, WIDTH // 2, 70)

    # Texto emn pantalla
    def display_text(self, text, size, color, pos_x, pos_y):
        # Obtenemos la fuente
        font = pygame.font.Font(self.font, size)
        # obtenemos el texto
        text = font.render(text, True, color)
        # Obtenemos el rectangulo
        rect = text.get_rect()
        # Posicion del texto
        rect.midtop = (pos_x, pos_y)
        # Pintamos el texto en pantalla
        self.surface.blit(text, rect)

    # Menu del juego
    def menu(self):
        self.surface.fill(GREEN_LIGHT)
        self.display_text('Presiona una tecla para comenzar', 36, BLACK, WIDTH // 2, 10)
        # Actualizamos la pantalla
        pygame.display.flip()
        # Esperamos un evento
        self.wait()

    # Metodo a la espera de eventos
    def wait(self):
        wait = True

        while wait:
            self.clock.tick(FPS)

            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    wait = False
                    self.running = False
                    pygame.quit()
                    sys.exit()

                if event.type == pygame.KEYUP:
                    wait = False


from urllib.request import Request, urlopen
from bs4 import BeautifulSoup
from nltk.corpus import words
import json
import os
from collections import defaultdict

def get_kannada_word(search_word):
	vals = []
	dict_page = 'https://www.shabdkosh.com/search-dictionary?e='+search_word+'&lc=kn&sl=en&tl=kn'

	req = Request(dict_page, headers={'User-Agent': 'Mozilla/5.0'})
	page = urlopen(req)

	soup = BeautifulSoup(page, 'html.parser')
	res_list = soup.find_all('ol', {'class': 'eirol'})

	for ol in res_list:
		li = ol.find_all('li')
		for a in li:
			for x in a.find_all('a'):
				t = x.text
				if len(t.split(' ')) <= 1:
					vals.append(t)
	return vals

def return_english(sentence):
	for i in range(len(sentence)):
		if sentence[i] in words.words():
			return i, sentence[i]

	return -1, None

def load_json(path, file):
	os.chdir(path)
	b = defaultdict(lambda: defaultdict(lambda: 0.2))
	if os.path.isfile(file):
		f = open(file, "r", encoding="utf-8")

		json_data = json.loads(f.read())

		b = defaultdict(lambda: defaultdict(lambda: 10 ** -20))

		for t in json_data:
			string = t[:]
			t1, t2 = string.split(":", 1)
			if t1 == "None":
				t1 = None
			if t2 == "None":
				t2 = None
			b[(t1, t2)] = json_data[string]

	return b

def load_json_bi(path, file):
	os.chdir(path)
	b = defaultdict(lambda: defaultdict(lambda: 0.2))
	if os.path.isfile(file):
		f = open(file, "r", encoding="utf-8")

		json_data = json.loads(f.read())

		b = defaultdict(lambda: defaultdict(lambda: 10 ** -20))

		for t in json_data:
			if t == "null" or t == "None":
				b[None] = json_data[t]
			else:
				b[t] = json_data[t]

	return b

def calculate_prob(t_a, m):
	t = [None, None]
	prob = 1
	for ww in t_a:
		prob_t = m[tuple(t[-2:])].get(ww, 10 ** -20)
		prob *= prob_t
		t.append(ww)
	return prob

def calculate_prob_bi(t_a, m):
	t = None
	prob = 1
	for ww in t_a:
		prob_t = m[t].get(ww, 10 ** -20)
		prob *= prob_t
		t = ww
	return prob

def get_sentence(sentence, model, n=3):
	sentence_array = sentence.split(" ")

	idx, word = return_english(sentence_array)

	if idx != -1:
		wws = get_kannada_word(word)

		if len(wws) == 0:
			return []

		p = - 100
		chosen_word = ""
		t_array = sentence_array[:]
		for w in wws:
			t_array[idx] = w
			if n == 3:
				t = calculate_prob(t_array, model)
			else:
				t = calculate_prob_bi(t_array, model)
			if t > p:
				p = t
				chosen_word = w

		t_array[idx] = chosen_word
		return t_array

if __name__ == "__main__":
	sw = "net"
	print(get_kannada_word(sw))
	mo = load_json("E:/NITK/6th Sem/Computer Graphics/kannada-rocks", "data.json")

	ss = "ಆಗ ನಮ್ಮೂರಿನ crow ನೆಂಟರು ಬೇರೆ ಬಂದಿದ್ದಾರೆ ಎಂದು ಆಗುತ್ತದೆ"
	updated = get_sentence(ss, mo)


import sys
import random
import numpy as np
import os
import subprocess
import uuid
import shutil
import time
import pickle
from ws2vtk import main as ws2vtk
from llres_maybe import scale_params
import matplotlib as mpl
mpl.use('Agg')
import matplotlib.pyplot as plt

## Dennis Conway

#
# with open('wae.pickle') as f:
#     b = pickle.load(f)

blob = [1, 0, 0, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5]
# background, intensity, strength, attenuation, xpos, ypos, xrad, yrad, theta, zpos, zrad, xrot, yrot
def pad_model(b, no_blobs):
    no_blobs = len(b[1:])/9
    b = list(b)
    # b[6] = 0
    inserts = []
    for i in range(no_blobs):
        for j in [7, 10, 11]:
            inserts.append(i*9+1+j)
    for i in inserts[::-1]:
        b.insert(i, 0)
    return b

def get_model(b, no_blobs):
    b = pad_model(b, no_blobs)
    c=' '.join(map(str,b))
    print(c)

    model_template = './template'
    target_model = './targ'

    callString='./saveModel '+ model_template + ' ' + c + ' > ' + target_model
    os.system(callString)
    time.sleep(2)
    # mtNS, N, E, D = ws2vtk(target_model, './oneblob.resp')

    # return mtNS, N, E, D

def test_yr_blobs(fwd, blobs):
    rms = [runw3(fwd, scale_params(blobs[0]), 1, off_diag=False)]
    for idx, b in enumerate(blobs[1:]):
        if (blobs[idx] != b).any():
            rms.append(runw3(fwd, scale_params(b), 1, off_diag=False))
        else:
            rms.append(rms[-1])
    return rms

def runw3(inv, b, no_blobs, off_diag=True):
    get_model(b, no_blobs)
    p1 = subprocess.Popen(['./wsinv3dmt'])
    p1.wait()
    #rmses = [runw3(hey, pad_model(scale_params(i[0]), 1), 1) for i in traj[:200]]
    time.sleep(12)
    # return
    #storing response and model files with unique identifier
    response='./fwd_resp.02.new'
    inv.w3 = inv.pop_data(response)
    rms = np.sqrt(np.average((inv.w3 - inv.data)**2/inv.data_errors**2))
    return rms


import pygame as pyg
import neat
import sys
import math
import os
import random

def init():
    global SCREEN_HEIGHT, SCREEN_WIDTH, SCREEN, FONT, Bird, Pipe
    pyg.init()
    SCREEN_HEIGHT = 600
    SCREEN_WIDTH = 800
    SCREEN = pyg.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
    pyg.display.set_caption('FlappyBird')

    FONT = pyg.font.Font('freesansbold.ttf', 20)

    class Bird:
        X_POS = 80
        Y_POS = 310

        def __init__(self):
            self.x = self.X_POS+25/2
            self.y = self.Y_POS+25/2
            self.gravity = 0.03*3*2*5
            self.velocity = 0
            self.lift = -2
            self.rect = pyg.Rect(self.X_POS, self.Y_POS, 25, 25)

        def update(self):
            self.velocity += self.gravity
            self.y += self.velocity
            if self.y >= 600-25/2:
                self.y = 600-25/2
                self.velocity = 0
            if self.y <= 0:
                self.y = 0
                self.velocity = 0
            self.rect = pyg.Rect(self.x-25/2, self.y-25/2, 25, 25)
            # pyg.draw.rect(SCREEN, (255, 0, 0), self.rect, 2)

        def draw(self):
            pyg.draw.circle(SCREEN, (0, 0, 0), (self.x, self.y), 25/2)

        def jump(self):
            self.velocity += self.lift

    class Pipe:
        def __init__(self):
            self.height = SCREEN_HEIGHT
            self.width = SCREEN_WIDTH
            self.x = self.width
            self.top = random.random() * (self.height/2)
            self.bottom = self.top+200
            self.rect1 = pyg.Rect(self.x, 0, 50, self.top)
            self.rect2 = pyg.Rect(self.x, self.bottom, 50, self.height - self.bottom)

        def show(self):
            pyg.draw.rect(SCREEN, (0, 0, 0), self.rect1)
            pyg.draw.rect(SCREEN, (0, 0, 0), self.rect2)

        def update(self):
            self.x -= gameSpeed
            self.rect1 = pyg.Rect(self.x, 0, 50, self.top)
            self.rect2 = pyg.Rect(self.x, self.bottom, 50,  self.height - self.bottom)
            if self.rect1.x < -self.rect1.width:
                pipes.pop()

def remove(index):
    birds.pop(index)

def distance(pos_a, pos_b):
    dx = pos_a[0]-pos_b[0]
    dy = pos_a[1]-pos_b[1]
    return math.sqrt(dx**2+dy**2)

highScore = 0

def evalGenomes():
    global birds, pipes, gameSpeed, points, highScore
    init()
    clock = pyg.time.Clock()
    points = 0
    birds = [Bird()]
    pipes = []
    gameSpeed = 5

    def score():
        global points, gameSpeed
        points += 1
        if points % 250 == 0:
            gameSpeed += 1
        text = FONT.render(f'Points:  {str(points)}', True, (0, 0, 0))
        SCREEN.blit(text, (650, 50))
        text = FONT.render(f'HighScore:  {str(highScore)}', True, (0, 0, 0))
        SCREEN.blit(text, (450, 50))

    def statistics():
        global birds, gameSpeed
        text_3 = FONT.render(f'Game Speed:  {str(gameSpeed)}', True, (0, 0, 0))

        SCREEN.blit(text_3, (50, 510))

    global run
    run = True
    while run:
        for event in pyg.event.get():
            if event.type == pyg.QUIT:
                run = False

        SCREEN.fill((255, 255, 255))

        for bird in birds:
            bird.update()
            bird.draw()

        if len(birds) == 0:
            break

        if len(pipes) == 0:
            pipes.append(Pipe())

        for pipe in pipes:
            pipe.show()
            pipe.update()
            for i, bird in enumerate(birds):
                if bird.rect.colliderect(pipe.rect1) or bird.rect.colliderect(pipe.rect2):
                    remove(i)

        for i, bird in enumerate(birds):
            keys = pyg.key.get_pressed()
            if keys[pyg.K_SPACE]:
                bird.jump()

        statistics()
        score()
        highScore = max(highScore, points)

        clock.tick(30)
        pyg.display.update()

if __name__ == '__main__':
    evalGenomes()


from midiutil import MIDIFile
from modules.model.SBSong import SBSong
from modules.model.SBCommonTypes import SBChordHarmonic

# from mingus.core.progressions import to_chords, determine
# import mingus.core.notes as notes

class SBCtrlSongToMIDI:
    def __init__(self, song, midi_filename):
        self.song = song
        self.midi_filename = midi_filename
        track = 0
        time = 0  # In beats
        tempo = self.song.getTempo()  # In BPM

        self.midi_file = MIDIFile(1)
        self.midi_file.addTempo(track, time, tempo)
        # simple piano chord structure interval
        self.chord_major_int = [0,4,7]
        self.chord_minor_int = [0,3,7]
        self.chord_dim_int = [0,3,6]
        self.chord_aug_int =[0,4,8]
        self.midi_notes = {"C": 60, "Db": 61, "D": 62, "Eb": 63, "E": 64, "F": 65, "Gb": 66, "G": 67, "Ab": 68, "A": 69, "Bb": 70, "B": 71}

    def process(self):
        track = 0
        channel = 0
        time = 0  # In beats
        duration = self.song.getBeatsPerBar()  # In beats
        volume = 100  # 0-127, as per the MIDI standard

        for sp in self.song.getParts():
            for r in range(sp.getNrRepeats()):
                for c in sp.getChords():
                    print("writing note: " + c.getNoteStr())
                    #self.midi_file.addNote(track,channel, self.midi_notes[c.getNoteStr()], time, duration, volume)
                    for b in range(c.getLengthInBars()):
                        if (c.getHarmonic() == SBChordHarmonic.MAJOR):
                            for n in self.chord_major_int:
                                note = self.midi_notes[c.getNoteStr()] + n + c.getPitch()*12
                                self.midi_file.addNote(track, channel, note, time, duration, volume)
                        elif (c.getHarmonic() == SBChordHarmonic.MINOR):
                            for n in self.chord_minor_int:
                                note = self.midi_notes[c.getNoteStr()] + n + c.getPitch()*12
                                self.midi_file.addNote(track, channel, note, time, duration, volume)
                        elif (c.getHarmonic() == SBChordHarmonic.DIMINISHED):
                            for n in self.chord_dim_int:
                                note = self.midi_notes[c.getNoteStr()] + n + c.getPitch()*12
                                self.midi_file.addNote(track, channel, note, time, duration, volume)
                        elif (c.getHarmonic() == SBChordHarmonic.AUGMENTED):
                            for n in self.chord_aug_int:
                                note = self.midi_notes[c.getNoteStr()] + n + c.getPitch()*12
                                self.midi_file.addNote(track, channel, note, time, duration, volume)

                        time = time + self.song.getBeatsPerBar()

    def writeMIDIFile(self):
        self.process()
        with open(self.midi_filename, "wb") as output_file:
            self.midi_file.writeFile(output_file)


#!/usr/bin/env python
# coding: utf-8

# En primer lugar, cargué el archivo "movies.json":

# In[18]:

import json
with open('movies.json',encoding='utf8') as movies_file:
    movies = json.load(movies_file)

# Misión 1: "Modelación de entidades".
# Se definen las clases "Pelicula", "Actor" y "Genero":

# In[30]:

class Pelicula:

    def __init__(self,titulo,ano,reparto,genero):
        self.titulo = titulo
        self.ano = ano
        self.reparto = reparto
        self.genero = genero

class Actor:

    def __init__(self,elenco,ano_aparicion):
        self.elenco = elenco
        self.ano_aparicion = ano_aparicion
        self.lista_actuaciones = []
        self.par_actor_ano = ()

    def add_tupla(self):
        self.lista_actuaciones.append(self.par_actor_ano)

class Genero:

    def __init__(self,lista_generos):
        self.lista_generos = []

    def lista_de_generos(self):
        for gen in Pelicula.genero:
            self.lista_generos.append(gen)

# Misión 2: "Carga de datos". Se extraen los datos y se cargan en los objetos correspondientes:

# In[46]:

peliculas = []
for movie in movies:
    a = Pelicula(movie["title"],movie["year"],movie["cast"],movie["genres"])
    peliculas.append(a)

actores = []
for movie in movies:
    b = Actor(movie["cast"],movie["year"])
    actores.append(b)

generos = []
for movie in movies:
    c = Genero(movie["genres"])
    generos.append(c)

# Misión 3: "Consultas sobre los datos". Se intenta responder las consultas pedidas:

# Consulta 1:

# In[54]:

### metodo para encontrar los 5 generos mas populares

generos = []
contador_rep = 0
for j in movies:
    generos += j['genres']

#print(generos)

aux = []
bla = {}
for i in generos:
    if i not in aux:
        a = generos.count(i)
        bla[i] = a
#print(bla)
## bla es el diccionario que contiene los generos como llaves y la cantidad de peliculas que pertenecen a ese genero como valores

bla2 = {}
j = 1
b = sorted(bla.values())
mayor = max(bla.values())

for rep in bla:
    if bla[rep] == mayor and j <= 5:
        bla2[rep] = mayor
        j += 1
    elif bla[rep] == mayor-1 and j <= 5:
        bla2[rep] = mayor-1
        j += 1
    elif bla[rep] == mayor-2 and j <=5:
        bla2[rep] = mayor-2
        j += 1

#print(bla2)
### bla2 es un diccionario que contiene los 5 generos mas populares como llaves y su cantidad de repeticiones como valores

# Consulta 2:

# In[56]:

#posible metodo para obtener los tres anos que mas peliculas se hacen

anos = []
contador_rep = 0
for j in movies:
    anos.append(j['year'])

aux = []
bla = {}
for i in anos:
    if i not in aux:
        a = anos.count(i)
        bla[i] = a

#print(bla)
#bla es el diccionario que contiene los anos como llaves y la cantidad de peliculas realizadas por ano como valores

mayor = max(bla.values())

bla2 = {}
i = 1
b = sorted(bla.values())

for rep in bla:
    if bla[rep] == b[len(b)-1] and i <= 3:
        bla2[rep] = b[len(b)-1]
        i += 1
for rep in bla:
    if bla[rep] == b[len(b)-2] and i <= 3:
        bla2[rep] = b[len(b)-2]
        i += 1
for rep in bla:
    if bla[rep] == b[len(b)-3] and i <= 3:
        bla2[rep] = b[len(b)-3]
        i += 1

#print(bla2)
#bla2 = diccionario que contiene los tres años con mas ocurrencias

# El resto de las consultas preferí no incluirlas en el código, pues tenían algunos errores importantes.

# In[ ]:

# In[ ]:


import requests
from WebScraper import ScrapeHelper
from bs4 import BeautifulSoup
from Classes.JobPostModule import JobPost

def get_job_list(search_term, search_page, search_city='', search_state=''):
    url = get_url(search_term, search_page, search_city, search_state)
    return get_job_posts(url, search_term)

def get_url(search_term, search_page_number=0, search_city='', search_state=''):
    """Returns URL that queries Indeed for the given search term at the given location.
    search_page_number counts from 0! """

    base_url = 'https://www.indeed.com/jobs?q='

    search_term = search_term.replace(' ', '+')

    search_city = search_city.replace(' ', '+')

    if len(search_state) > 0:
        search_state = '%2C+' + search_state

    if search_page_number > 0:
        return base_url + search_term + '&l=' + search_city + search_state + '&start=' + str(search_page_number * 10)
    else:
        return base_url + search_term + '&l=' + search_city + search_state

def get_job_posts(target_url, search_term):
    """Given a url, returns a list of objects of instance JobPost"""

    # custom parameters
    unknown_string = 'UNKNOWN'
    unwanted_company_string = 'Seen by Indeed'

    # variables
    url_list = []
    seen_by_indeed_count = 0

    page = requests.get(target_url)

    # Create a BeautifulSoup object
    big_soup = BeautifulSoup(page.text, 'html.parser')

    # Let's clean this up
    for script in big_soup(["script", "style"]):  # remove all javascript and stylesheet code
        script.decompose()

    for div in big_soup.find_all("div", {'class': 'jobsearch-SerpJobCard-footer'}):
        div.decompose()

    for table in big_soup.find_all("table", {'class': 'jobCardShelfContainer'}):
        table.decompose()

    for div2 in big_soup.find_all("div", {'class': 'tab-container'}):
        div2.decompose()

    parent_search_section = big_soup.find_all('div', class_='jobsearch-SerpJobCard unifiedRow row result')

    if parent_search_section is None:
        ScrapeHelper.print_error_string(big_soup.prettify())
    else:
        for x02 in parent_search_section:
            # print(x02.prettify())
            try:
                x1 = x02.find('span', class_='company')
                if not x1 is None:
                    company = x1.text.strip('\n')
                else:
                    company = unknown_string

                if not company == unwanted_company_string:
                    x0 = x02.find('a', class_='jobtitle turnstileLink')
                    links = 'https://www.indeed.com' + x0.get('href')
                    names = x0.get_text().strip('\n')

                    x4 = x02.find('div', class_='recJobLoc')
                    if not x4 is None:
                        location = x4.get('data-rc-loc')
                    else:
                        location = unknown_string

                    url_list.append(
                        JobPost(job_title=names, url=links, company=company, location=location, search_term=search_term,
                                source=ScrapeHelper.INDEED))
                    # print(names + ' -- ' + location + ' -- ' + company + ' -- ' + links)
                else:
                    seen_by_indeed_count += 1
            except:
                ScrapeHelper.print_error_string(x02.prettify())

    # print('Seen By Indeed: ' + str(seen_by_indeed_count))
    return url_list

def get_document(y: JobPost):
    try:
        complete_job_profile(y)
        new_jd = {
            "JobTitle": y.job_title,
            "CompanyID": 0,
            "company": y.company,
            "location": y.location,
            "rating": y.company_rating,
            "salary": y.salary,
            "commitment": y.commitment_level,
            "url": y.url,
            "search_terms": y.search_terms,
            "source": y.source,
            "description": y.description,
            "dates_found": y.dates_found,
            "post_date": y.post_date
        }
        return new_jd
    except Exception as e:
        ScrapeHelper.print_error_string('Complete Job Post Error ' + y.source + ' ' + y.url + ' : ' + str(e))
        return None

def complete_job_profile(job_post: JobPost):
    page = requests.get(job_post.url)

    # Create a BeautifulSoup object
    big_soup = BeautifulSoup(page.text, 'html.parser')

    # Let's clean this mess up
    for script in big_soup(["script", "style"]):  # remove all javascript and stylesheet code
        script.decompose()

    # Ok ok I think we're ready...
    primary_description = big_soup.find("div", class_='jobsearch-jobDescriptionText')
    if not primary_description is None:
        job_post.description = primary_description.get_text().replace('\n', ' ')

    rating_section = big_soup.find("meta", itemprop='ratingValue')
    if not rating_section is None:
        job_post.company_rating = rating_section.get('content')

    salary_section = big_soup.find("span", class_='icl-u-xs-mr--xs')
    if not salary_section is None:
        job_post.salary = salary_section.text

    time_section = big_soup.find("span", class_='jobsearch-JobMetadataHeader-item icl-u-xs-mt--xs')
    if not time_section is None:
        job_post.commitment_level = time_section.text

    return


from typing import List, Dict

from pydantic import ValidationError

from func.client import client, retry
from func.data import all_avatars, all_avatars_map, all_avatars_name, dump_avatars
from models.avatar import YattaAvatar
from models.wiki import Content, Children
from res_func.url import avatar_yatta_url

def fix_avatar_eidolons(values: Dict) -> Dict:
    if values.get("eidolons") is None:
        values["eidolons"] = []
    else:
        eidolons = []
        for eidolon in values["eidolons"].values():
            eidolons.append(eidolon)
        values["eidolons"] = eidolons
    return values

@retry
async def get_single_avatar(url: str) -> None:
    req = await client.get(url)
    try:
        avatar = YattaAvatar(**fix_avatar_eidolons(req.json()["data"]))
    except Exception as e:
        print(f"{url} 获取角色数据失败")
        raise e
    all_avatars.append(avatar)
    all_avatars_map[avatar.id] = avatar
    all_avatars_name[avatar.name] = avatar

@retry
async def get_all_avatar() -> List[str]:
    req = await client.get(avatar_yatta_url)
    return list(req.json()["data"]["items"].keys())

async def fix_avatar_icon(content: Content):
    avatar = all_avatars_name.get(content.title.replace("·", "•"))
    if not avatar:
        return
    avatar.icon = content.icon

async def fetch_avatars(child: Children):
    print("获取角色数据")
    avatars = await get_all_avatar()
    for avatar_id in avatars:
        try:
            await get_single_avatar(f"{avatar_yatta_url}/{avatar_id}")
        except ValidationError:
            print(f"{avatar_yatta_url}/{avatar_id} 获取角色数据失败，角色格式异常")
    print("修复角色图标")
    for content in child.list:
        await fix_avatar_icon(content)
    for avatar in all_avatars:
        if not avatar.icon.startswith("http"):
            avatar.icon = ""
    print("获取角色数据完成")
    await dump_avatars()


import re
import bs4
import real_estate.real_estate_property as rep

class PageScraper(object):
    MIN_PRICE = 10000
    UNDER_CONTRACT_REGEX = '.*(under contract|under offer)(?i)'

    DIGITS_ONLY = re.compile(r'\A(\d+)\Z')
    APPROX_LAND_AREA = re.compile(r'\A(\d+) m² \(approx\)')

    def html_to_soup(html):
        return bs4.BeautifulSoup(html, "html.parser")

    def no_results_check(soup, page_num):
        no_results = PageScraper.check_for_no_results(soup)
        if no_results:
            print("Found the 'no results' page, final page is number %i" %
                  (page_num - 1))
        return no_results

    def check_for_no_results(soup):
        ds_contents = PageScraper.get_ds_contents(soup)
        search_results = ds_contents.find(
            'form', {'id': 'searchResultsForm'}, recursive=False
        )

        err = RuntimeError('HTML not understood.')

        if search_results is not None:
            no_results = search_results.find(
                'div', {'id': 'noresults'}, recursive=False
            )
            if no_results is not None:
                return True
            else:
                raise(err)
        else:
            results = PageScraper.get_results(ds_contents)
            if results is not None:
                return False
            else:
                raise(err)

    def find_articles(soup):
        ds_contents = PageScraper.get_ds_contents(soup)
        results = PageScraper.get_results(ds_contents)

        articles = results.find(
            'div', {'id': 'searchResultsTbl'}
        ).find_all(
            'article', recursive=False
        )

        PageScraper.num_articles_check(results, len(articles))
        return articles

    def get_ds_contents(soup):
        return soup.find(
            'body', {'id': 'searchResults'}
        ).find(
            'div', {'id': 'DSContents'}
        )

    def get_results(ds_contents):
        return ds_contents.find('div', {'id': 'results'}, recursive=False)

    def num_articles_check(results, articles_len):
        results_info_text = results.find(
            'div', {'id': 'resultsInfo'}
        ).find(
            'p'
        ).get_text()

        results_on_page_search = re.search('(\d+) - (\d+)', results_info_text)
        num_articles_lower = int(results_on_page_search.group(1))
        num_articles_upper = int(results_on_page_search.group(2))
        num_articles_on_page = num_articles_upper - num_articles_lower + 1

        if num_articles_on_page != articles_len:
            raise(RuntimeError(
                'Inconsistent number of articles on page: ' +
                '%i v. %i' % (num_articles_on_page, articles_len)
            ))

    def create_properties(articles):
        properties = []
        for article in articles:
            data_content_type = article['data-content-type']

            if data_content_type == 'residential':
                p = PageScraper.scrape_residential_property(article)
                properties.append(p)
            elif data_content_type == 'new apartment project':
                child_properties = PageScraper.scrape_new_apartment_project(
                    article)
                for p in child_properties:
                    properties.append(p)
            elif data_content_type == 'residential land':
                p = PageScraper.scrape_residential_land(article)
                properties.append(p)
            elif data_content_type == 'house land package':
                p = PageScraper.scrape_house_land_package(article)
                properties.append(p)
            elif data_content_type == 'rural':
                p = PageScraper.scrape_rural_property(article)
                properties.append(p)
            else:
                p = rep.DataContentTypeNotSupported(data_content_type)
                properties.append(p)
        return properties

    def populate_state_and_postcode(properties, state, postcode):
        for p in properties:
            p.state_and_postcode = rep.StateAndPostcode(state, postcode)
        return properties

    def scrape_residential_property(article):
        listing_info = PageScraper.get_listing_info(article)

        sale_type = PageScraper.residential_sale_type(listing_info, False)
        vcard_name_soup = PageScraper.find_vcard_name_soup(listing_info)
        property_type = PageScraper.extract_property_type(vcard_name_soup)
        features = PageScraper.extract_property_features(listing_info)
        details = PageScraper.create_property_details(
            property_type, features)
        address_text = PageScraper.get_address_text(article)
        residential_property = rep.Property(sale_type, details, address_text)
        return residential_property

    def scrape_residential_land(article):
        listing_info = PageScraper.get_listing_info(article)

        sale_type = PageScraper.residential_sale_type(listing_info, False)
        features = PageScraper.maybe_extract_property_features(listing_info)
        vcard_name_soup = PageScraper.find_vcard_name_soup(listing_info)
        property_type = PageScraper.extract_property_type(vcard_name_soup)
        details = PageScraper.create_property_details(property_type, features)
        address_text = PageScraper.get_address_text(article)
        residential_land = rep.Property(sale_type, details, address_text)
        return residential_land

    def scrape_new_apartment_project(article):
        project_wrapper = article.find(
            'div', {'class': 'resultBodyWrapper projectWrapper rui-clearfix'}
        )

        children = article.find(
            'div', {'class': 'project-child-listings'}
        ).find_all(
            'a', recursive=False
        )

        child_properties = []
        for child in children:
            sale_type = PageScraper.new_project_sale_type(child)

            features_soup = child.find(
                'div', {'class': 'child'}
            ).find(
                'div', {'class': 'features'}
            )

            property_type = PageScraper.extract_property_type(child)
            features = PageScraper.extract_property_features(
                features_soup)
            details = PageScraper.create_property_details(
                property_type, features)
            address_text = PageScraper.get_address_text(child)

            child_properties.append(
                rep.Property(sale_type, details, address_text)
            )

        return child_properties

    def scrape_house_land_package(article):
        listing_info = PageScraper.get_listing_info(article)

        sale_type = PageScraper.residential_sale_type(listing_info, True)
        vcard_name_soup = PageScraper.find_vcard_name_soup(listing_info)
        property_type = PageScraper.extract_property_type(vcard_name_soup)
        features = PageScraper.extract_property_features(listing_info)
        details = PageScraper.create_property_details(
            property_type, features)
        address_text = PageScraper.get_address_text(article)
        residential_property = rep.Property(sale_type, details, address_text)
        return residential_property

    def scrape_rural_property(article):
        listing_info = PageScraper.get_listing_info(article)

        sale_type = PageScraper.residential_sale_type(listing_info, False)
        vcard_name_soup = PageScraper.find_vcard_name_soup(listing_info)

        property_type_text = PageScraper.get_property_type_text(
            vcard_name_soup)
        if property_type_text in (
            'other', 'mixed+farming', 'cropping', 'horticulture', 'dairy',
            'livestock', 'farmlet', 'viticulture'
        ):
            property_type = rep.Rural()
        elif property_type_text in ('lifestyle',):
            property_type = rep.PropertyTypeNotSupported(
                property_type_text, vcard_name_soup
            )
        else:
            raise ValueError(
                "Rural property type text was not 'other', it was: %s" %
                property_type_text
            )

        features = PageScraper.maybe_extract_property_features(listing_info)
        details = PageScraper.create_property_details(
            property_type, features)
        address_text = PageScraper.get_address_text(article)

        residential_property = rep.Property(sale_type, details, address_text)
        return residential_property

    def get_listing_info(article):
        return article.find(
            'div', {'class': 'listingInfo rui-clearfix'}
        )

    def extract_property_type(soup_with_href):
        property_type_text = PageScraper.get_property_type_text(soup_with_href)

        if property_type_text == 'house':
            return rep.House()
        elif (property_type_text == 'townhouse' or
                property_type_text == 'villa' or
                property_type_text == 'terrace'):
            return rep.TownHouse()
        elif (property_type_text == 'unit' or
                property_type_text == 'apartment' or
                property_type_text == 'flat'):
            return rep.Unit()
        elif property_type_text == 'serviced+apartment':
            return rep.ServicedApartment()
        elif property_type_text == 'studio':
            return rep.Studio()
        elif property_type_text == 'residential+land':
            return rep.Land()
        elif property_type_text == 'duplex+semi+detached':
            return rep.Duplex()
        elif property_type_text == 'retirement+living':
            return rep.RetirementLiving()
        elif property_type_text == 'unitblock':
            return rep.UnitBlock()
        elif property_type_text == 'acreage+semi+rural':
            return rep.SemiRural()
        elif property_type_text == 'other':
            return rep.NotSpecified()
        else:
            return rep.PropertyTypeNotSupported(
                property_type_text, soup_with_href)

    def get_property_type_text(soup):
        return re.search(
            '^\/property-(\w+\+?\w*\+?\w*\+?\w*)', soup['href']
        ).group(1)

    def find_vcard_name_soup(listing_info):
        return listing_info.find(
            'div', {'class': 'vcard'}
        ).find(
            'a', {'class': 'name'}
        )

    def maybe_extract_property_features(soup):
        property_features = PageScraper.get_property_features_soup(soup)
        if property_features is None:
            return {}
        else:
            return PageScraper.parse_property_features(property_features)

    def get_property_features_soup(soup):
        return soup.find(
            'dl', {'class': 'rui-property-features rui-clearfix'}
        )

    def extract_property_features(soup):
        property_features = PageScraper.get_property_features_soup(soup)
        return PageScraper.parse_property_features(property_features)

    def parse_property_features(features_soup):
        names = PageScraper.find_and_get_all_text(features_soup, ['dt'])
        names = [x.lower() for x in names]
        values = [
            PageScraper.feature_value_to_int(x, names, features_soup) for x in
            PageScraper.find_and_get_all_text(features_soup, ['dd'])
        ]
        features = dict(zip(names, values))
        return features

    def feature_value_to_int(x, names, soup):
        if PageScraper.DIGITS_ONLY.match(x) is not None:
            return int(x)
        elif PageScraper.APPROX_LAND_AREA.match(x) is not None:
                return int(PageScraper.APPROX_LAND_AREA.match(x).group(1))
        else:
            raise ValueError(
                ('Feature value string not parsable, value: %s' % x) +
                ('For names:' + ', '.join([str(a) for a in names])) +
                ('Full feature soup:\n' + str(soup))
            )

    def create_property_details(property_type, features):
        property_details = rep.Details(
            property_type=property_type,
            bedrooms=features.get('bedrooms', None),
            bathrooms=features.get('bathrooms', None),
            garage_spaces=features.get('car spaces', None),
            land_area=None,
            floor_area=None
        )
        return property_details

    def find_and_get_all_text(soup, inputs):
        return [x.get_text() for x in soup.find_all(*inputs)]

    def residential_sale_type(listing_info, off_plan):
        property_stats = PageScraper.get_property_stats(listing_info)
        under_contract = PageScraper.check_if_under_contract(property_stats)
        sale_type_text = PageScraper.find_sale_type_text(
            property_stats)
        sale_type = PageScraper.deduce_sale_type(
            sale_type_text, under_contract, off_plan)
        return sale_type

    def search_for_price_text(property_stats):
        return property_stats.find(
            'p', {'class': 'priceText'}, recursive=False
        )

    def get_property_stats(listing_info):
        return listing_info.find(
            'div', {'class': 'propertyStats'}
        )

    def find_sale_type_text(property_stats):
        price_search = PageScraper.search_for_price_text(property_stats)
        contact_agent_search = property_stats.find(
            'p', {'class': 'contactAgent'}, recursive=False
        )
        type_search = property_stats.find(
            'p', {'class': 'type'}, recursive=False
        )

        if price_search is not None and price_search.has_attr("title"):
            return price_search['title']
        elif price_search is not None:
            return price_search.get_text()
        elif type_search is not None:
            return type_search.get_text()
        elif contact_agent_search is not None:
            return contact_agent_search.get_text()
        else:
            return 'UnableToFindSaleTypeText'

    def new_project_sale_type(child):
        price_text = child.find(
            'div', {'class': 'child'}
        ).find(
            'div', {'class': 'priceAndPropertyTypeContainer'}
        ).find(
            'span', {'class': 'price rui-truncate'}
        ).get_text()

        sale_type = PageScraper.deduce_sale_type(price_text, False, False)
        return sale_type

    def deduce_sale_type(sale_text, under_contract, off_plan):
        prices = PageScraper.extract_prices(sale_text, PageScraper.MIN_PRICE)

        if off_plan:
            return rep.OffPlan(prices, under_contract=under_contract)
        elif re.match('^(?i)auction', sale_text) is not None:
            return rep.Auction(under_contract=under_contract)
        elif re.match('^(?i)tender', sale_text) is not None:
            return rep.Tender(under_contract=under_contract)
        elif PageScraper.check_for_sale_by_negotiation(sale_text):
            return rep.Negotiation(under_contract=under_contract)
        elif prices is not None:
            return rep.PrivateTreaty(prices, under_contract=under_contract)
        elif re.match('^(?i)contact agent', sale_text) is not None:
            return rep.ContactAgent(under_contract)
        elif sale_text == 'UnableToFindSaleTypeText':
            return rep.UnableToFindSaleTypeText()
        else:
            return rep.SaleTypeParseFailed()

    def check_for_sale_by_negotiation(sale_text):
        if (re.match('(?i)(price )?by negotiation', sale_text) is not None or
                re.match('(?i)by negotiaton', sale_text) is not None or
                re.match('(?i)by negotation', sale_text) is not None):
            # Found typos when scraping from the website
            return True
        else:
            return False

    def extract_prices(price_text, min_price):
        dollar_price_regex = '(\$\d+(,\d+)*)'
        dollarless_price_regex = '(\d+(,\d+)*)'

        if re.search(dollar_price_regex, price_text) is not None:
            return PageScraper.text_to_prices(dollar_price_regex, price_text)
        elif re.search(dollarless_price_regex, price_text) is not None:
            prices = PageScraper.text_to_prices(
                dollarless_price_regex, price_text)
            prices = [x for x in prices if x >= min_price]
            if len(prices) != 0:
                return prices
            else:
                return None
        else:
            return None

    def check_for_missed_prices(price_text):
        digits_regex = '(\d+,?\d*)'
        if re.search(digits_regex, price_text) is not None:
            digit_strings = re.findall(digits_regex, price_text)
            numbers = [int(x.replace(',', ''))for x in digit_strings]
            if any([x > 999 for x in numbers]):
                print(
                    'Note: Possible price will be missed: %s' % price_text
                )

    def text_to_prices(regex, price_text):
        price_strings = re.findall(regex, price_text)
        prices = [
            int(x.replace('$', '').replace(',', ''))
            for x, _ in price_strings
        ]
        return prices

    def check_if_under_contract(property_stats):
        return PageScraper.check_if_under_special(
            property_stats, PageScraper.UNDER_CONTRACT_REGEX
        )

    def check_if_under_special(property_stats, regex):
        searches = PageScraper.under_special_searches(property_stats)
        tests = [PageScraper.maybe_check_for_match(x, regex) for x in searches]
        return any(tests)

    def under_special_searches(property_stats):
        type_search = property_stats.find('p', {'class': 'type'})
        price_search = PageScraper.search_for_price_text(property_stats)
        return [type_search, price_search]

    def maybe_check_for_match(search, regex):
        if search is not None:
            text = search.get_text()
            return PageScraper.check_for_match(text, regex)
        else:
            return False

    def check_for_match(text, regex):
        if re.match(regex, text) is not None:
            return True
        else:
            return False

    def find_and_parse_address(article):
        # address_text = PageScraper.find_address_text(article)
        # address = PageScraper.parse_address(address_text)
        # return address
        raise RuntimeError('This method has been deprecated.')

    def get_address_text(article):
        return rep.AddressText(PageScraper.find_address_text(article))

    def find_address_text(article):
        photoviewer_search = article.find(
            'div', {'class': 'photoviewer'}
        )

        property_image_search = article.find(
            'div', {'class': 'propertyImage'}
        )

        if photoviewer_search is not None:
            return photoviewer_search.find('a').find(
                'img', recursive=False
            )['alt']
        elif property_image_search is not None:
            return property_image_search.find('img', recursive=False)['alt']
        else:
            raise RuntimeError('Could not find address text.')


from collections import OrderedDict
import logging, itertools

import numpy as np

# from enthought.tvtk.api import tvtk

class Electrodes(OrderedDict):
    """
    A set of named electrodes, each consisting of multiple faces
    (planar), each face consisting of multiple signed loops:
    {name: [[(sign, coordinates), ...more loops], ...more faces]}
    Faces are counterclockwise, negative loops are holes.
    """
    @classmethod
    def from_trap(cls, trap, scale=1.):
        """load ed electrodes in 'trap' format (inventor export)"""
        electrodes = cls()
        state = None
        for line in trap:
            line = line.rstrip()
            if not line.strip() or line.startswith("#"):
                pass
            elif line.startswith("WP"):
                pass # ignored
            elif line.startswith("S,"):
                state = "face"
                name = line.split(", ")[1]
                if name.startswith("TRAPELECTRODE_"):
                    name = name[len("TRAPELECTRODE_"):]
                if not name in electrodes:
                    electrodes[name] = []
            elif line.startswith("OUTERLOOP"):
                state = "loop"
                coords = []
                face.append((1, coords))
            elif line.startswith("INNERLOOP"):
                state = "loop"
                coords = []
                face.append((-1, coords))
            elif line.startswith("ENDOFFACE"):
                state = "face"
            else:
                #elif line.startswith(" ") or line.startswith("-") or \
                #    line.startswith("0."):
                try:
                    vector = map(float, line.split())
                    if len(vector) != 3:
                        raise ValueError("wrong length")
                except ValueError as e:
                    logging.warn("failed to parse line in trap %s: %s",
                            trap, line)
                if state == "face":
                    face = []
                    #face.normal = vector
                    electrodes[name].append(face)
                else:
                    if coords and np.allclose(coords[-1], vector):
                        logging.warn("two subsequent points are close %s, %s",
                                coords[-1], vector)
                    else:
                        coords.append(vector)

        # cleanup
        for name, faces in electrodes.iteritems():
            for face in faces[:]:
                for loop in face[:]:
                    face.remove(loop)
                    sign, coords = loop
                    coords = np.array(coords)/scale
                    if coords.shape[0] < 3:
                        logging.warn("not a loop: %s %s, removing", coords, name)
                    else:
                        face.append((sign, coords))
                if not face:
                    logging.warn("empty face: %s %s, removing", face, name)
                    faces.remove(face)

        return electrodes

    @classmethod
    def from_polygons(cls, poly):
        """load loops from 2d polygons (shapely package)"""
        obj = cls()
        for name, mp in poly:
            try:
                mp = list(mp)
            except TypeError:
                mp = [mp]
            face = []
            for pi in mp:
                ext = pi.exterior
                if not ext.is_ccw:
                    ext.coords = list(ext.coords[::-1])
                co = np.array(ext.coords[:-1])
                loop = np.zeros((co.shape[0], 3))
                loop[:, :2] = co
                face.append((1, loop))
                for ii in pi.interiors:
                    if not ii.is_ccw:
                        ii.coords = list(ii.coords[::-1])
                    co = np.array(ii.coords[:-1])
                    loop = np.zeros((co.shape[0], 3))
                    loop[:, :2] = co
                    face.append((-1, loop))
            obj[name] = [face]
        return obj

    @classmethod
    def from_system(cls, sys):
        """load loops from a planar 2d gapless electrode system
        (electrode package)"""
        # FIXME: should add ground plane where there are not electrodes
        obj = cls()
        for ele in sys:
            face = []
            for sign, coords in zip(ele.orientations(), ele.paths):
                loop = np.zeros((coords.shape[0], 3))
                loop[:, :2] = coords
                face.append((sign, loop))
            obj[ele.name] = [face]
        return obj

    def to_vtk(self, filename):
        raise NotImplemented

    def cleanup(self, tol=1e-9):
        """remove close adjacent points"""
        for name, faces in self.iteritems():
            for face in faces:
                for i, loop in enumerate(face[:]):
                    sign, coords = loop
                    coords_next = np.roll(coords, 1, axis=0)
                    l = np.square(coords-coords_next).sum(axis=1)
                    coords = coords[l > tol**2]
                    face[i] = (sign, coords)

    def extrude(self, thickness, names=None):
        """
        extrude the (xy planar) electrode surfaces by thickness into
        into the -z direction. adds one face per pair of loop points.
        """
        if names is None:
            names = self.keys()
        dz = np.array([0., 0, thickness])
        for name in names:
            for sign, coords in itertools.chain(*self[name]):
                for ai, bi in zip(coords, np.roll(coords, 1, axis=0)):
                    coords = np.array([ai, bi, bi+dz, ai+dz])
                    if sign < 0:
                        coords = coords[::-1]
                    self[name].append([(1, coords)])

if __name__ == "__main__":
    import sys
    ele = Electrodes.from_trap(open(sys.argv[1]))
    #print ele


import os
from pprint import pprint
import arrow
import yaml
from flask import Flask, request, jsonify
from SELAN_PLOTS.CLIENT_APPS.multi_well_swabbing.html_build_multi_well_swabbing import multi_well_swabbing_html
from SELAN_PLOTS.CLIENT_APPS.sandbox.templates.webix_html_builder import webix_builder
from SELAN_PLOTS.CLIENT_APPS.single_well_multi_plots.html_build_single_well_multi_plots import \
    single_well_multi_plot_html

app = Flask(__name__)

from SELAN_PLOTS.make_plots import swabbing_data_plots

swabbing_data_pack = swabbing_data_plots()

@app.route('/')
def hello_world():
    return "HELLO THIS IS JUST A CHECK"

@app.route('/webix_try')
def webix_try():
    my_tag = webix_builder()
    return my_tag.get_html()

@app.route('/_get_min_max_range', methods=['POST'])
def get_min_max():
    try:
        print ('******************* REQUEST RECEIVED')
        property = request.form.get('prop_name')
        well_name = request.form.get('well_name')
        result = swabbing_data_pack.get_min_max(well_name, property)
        print (result)
        return jsonify(result)
    except Exception as e:
        print("_get_min_maxERROR", e)

@app.route('/_get_filtered_plot', methods=['POST'])
def get_filtered_plot():
    try:
        well_name = request.form.get('well_name')
        plot_name = request.form.get('plot_name')
        property = request.form.get('prop_name')
        dtype = request.form.get('dtype')
        if 'date' in dtype:
            min = arrow.get(request.form.get('min')).datetime
            max = arrow.get(request.form.get('max')).datetime
            print("ARROW ", min, max)
            print("string date", request.form.get('min'), request.form.get('max'))
        else:
            min = request.form.get('min')
            max = request.form.get('max')

        filters = []
        filters.append(dict(property=property, min=min, max=max))

        plot_data = swabbing_data_pack.make_plots(well_name, plot_name, filters, 'json')

        return plot_data
    except Exception as e:
        print("_get_filtered_plot  ERROR", e)

@app.route('/_get_well_data', methods=['POST'])
def get_well_data():
    try:
        well_name = request.form.get('well_name')
        prop_name = request.form.get('prop_name')
        print('getting data for ', well_name, prop_name)
        data_series = swabbing_data_pack.get_prop(well_name, prop_name)

        print(data_series.dtype)

        return jsonify(dict(series=data_series.tolist(), data_type=str(data_series.dtype)))

    except Exception as e:
        print("GET DATA ERROR  ", e)
        raise e

@app.route('/swabbing/<well_name>')
def swabbing_well_plot(well_name):
    print("IN FLASK APP", 'SWABBING', well_name)
    return swabbing_data_pack.make_plots(well_name, 'SWABBING', 'div')

@app.route('/production/<well_name>')
def production_well_plot(well_name):
    print("IN FLASK APP", 'PRODUCTION', well_name)
    return swabbing_data_pack.make_plots(well_name, 'PRODUCTION', 'div')

@app.route('/well_plots')
def well_plots():
    my_tag = single_well_multi_plot_html()
    return my_tag.get_html()

@app.route('/_get_well_plots', methods=['POST'])
def get_well_plots():
    meta_list = request.form.getlist('send_request')
    plot_list = []
    for item in meta_list:
        plot_list.append(yaml.load(item))
    print(plot_list[0], plot_list[1])
    return jsonify(get_plots(plot_list))

@app.route('/_get_well_plot', methods=['POST'])
def get_well_plot():
    well_name = request.form.get('well_name')
    plot_name = request.form.get('plot_name')
    tab_id = request.form.get('tab_id')

    print(well_name, plot_name, tab_id)

    return jsonify({'tab_content': make_plots(well_name, plot_name),
                    'well_name': well_name,
                    'plot_name': plot_name,
                    'tab_id': tab_id
                    })

@app.route('/multi_well_swabbing')
def multi_well_swabbing():
    my_tag = multi_well_swabbing_html()
    return my_tag.get_html()

@app.route('/_get_well_list', methods=['POST'])
def get_well_list():
    try:
        print("HELLO THIS IS A REQUEST FOR WELL LIST FROM FILE")
        well_list = get_well_list_from_file()
        return jsonify(result=well_list)

    except Exception as e:
        return (e)

if __name__ == '__main__':
    package_path = os.getcwd()
    package_MISC = os.path.join(package_path, 'MISC')
    pprint(package_path)
    app.run(debug=True)


# coding=utf-8
__author__ = 'Gartand'
__version__ = '1.2'

import clr
clr.AddReferenceByPartialName("Fougerite")
import Fougerite

blue = "[color #0099FF]"
red = "[color #FF0000]"
pink = "[color #CC66FF]"
teal = "[color #00FFFF]"
green = "[color #009900]"

class Report:

    def On_PluginInit(self):
        self.IniFile()

    def argsToText(self, args):
        text = str.join(" ", args)
        return text

    def isMod(self, id):
        if DataStore.ContainsKey("Moderators", id):
            return True
        return False

    def IniFile(self):
        if not Plugin.IniExists("ReportedPlayers"):
            ini = Plugin.CreateIni("ReportedPlayers")
            ini.Save()
        return Plugin.GetIni("ReportedPlayers")

    def GetQuoted(self, array, Reporter):
        text = str.join(" ", array)
        if not '"' in text:
            Reporter.MessageFrom('Report', 'Usage: /report "PlayerName" "message"')
            Reporter.MessageFrom('Report', 'Quote signs (") are required.')
            return False
        groups = text.split('"')
        n = len(groups)
        list = []
        for x in xrange(0, n):
            if x % 2 != 0:
                list.append(str(groups[x]))
        return list

    """
        CheckV method based on Spock's method.
        Upgraded by DreTaX
        Can Handle Single argument and Array args.
        V4.1
    """

    def GetPlayerName(self, namee):
        try:
            name = namee.lower()
            for pl in Server.Players:
                if pl.Name.lower() == name:
                    return pl
            return None
        except:
            return None

    def CheckV(self, Player, args):
        systemname = "Report"
        count = 0
        if hasattr(args, '__len__') and (not isinstance(args, str)):
            p = self.GetPlayerName(str.join(" ", args))
            if p is not None:
                return p
            for pl in Server.Players:
                for namePart in args:
                    if namePart.lower() in pl.Name.lower():
                        p = pl
                        count += 1
                        continue
        else:
            nargs = str(args).lower()
            p = self.GetPlayerName(nargs)
            if p is not None:
                return p
            for pl in Server.Players:
                if nargs in pl.Name.lower():
                    p = pl
                    count += 1
                    continue
        if count == 0:
            Player.MessageFrom(systemname, "Couldn't find [color#00FF00]" + str.join(" ", args) + "[/color]!")
            return None
        elif count == 1 and p is not None:
            return p
        else:
            Player.MessageFrom(systemname, "Found [color#FF0000]" + str(count) + "[/color] player with similar name. [color#FF0000] Use more correct name!")
            return None

    def SaveReportToIni(self, Reporter, ReportedName, Text):
        ini = self.IniFile()
        enum = ini.EnumSection("Reports")
        length = len(enum)
        if length == 0:
            lastreportnumber = 0
        else:
            lastreportnumber = int(enum[length - 1])
        ini.AddSetting("Reports", str(lastreportnumber + 1), "Reporter: " + Reporter.Name + " | Reported:  " + ReportedName + "  | Reason: " + Text)
        ini.Save()

    def On_Command(self, Reporter, cmd, args):
        ini = self.IniFile()
        enum = ini.EnumSection("Reports")
        length = len(enum)
        if cmd == "report":
            if len(args) == 0:
                Reporter.MessageFrom("Report", green + "Get420Reported++ " + __version__ + " Made by " + __author__)
                Reporter.MessageFrom("Report", '/report "Name" "Reason" - Quote signs (") are required.')
                if Reporter.Admin or self.isMod(Reporter.SteamID):
                    Reporter.MessageFrom("Report", "/report list - List Reports")
                    Reporter.MessageFrom("Report", "/report view - View report.")
                    Reporter.MessageFrom("Report", "/report delete - Mark and Delete report.")
                    Reporter.MessageFrom("Report", "/report deleteall - Delete all")
            else:
                if args[0] == "help":
                    Reporter.MessageFrom("Report", green + "Get420Reported++ " + __version__ + " Made by " + __author__)
                    Reporter.MessageFrom("Report", '/report "Name" "Reason" - Quote signs (") are required.')
                    if Reporter.Admin or self.isMod(Reporter.SteamID):
                        Reporter.MessageFrom("Report", "/report list - List Reports")
                        Reporter.MessageFrom("Report", "/report view - View report.")
                        Reporter.MessageFrom("Report", "/report delete - Mark and Delete report.")
                        Reporter.MessageFrom("Report", "/report deleteall - Delete all")
                elif args[0] == "list":
                    if not Reporter.Admin and not self.isMod(Reporter.SteamID):
                        return
                    Reporter.MessageFrom("Report", green + "There are (" + teal + str(length) + green + ") reports atm.")
                    for key in enum:
                        Reporter.MessageFrom("Report", "Ticket: " + key)
                elif args[0] == "view":
                    if not Reporter.Admin and not self.isMod(Reporter.SteamID):
                        return
                    # cmd args[0] args[1]
                    # /report view id
                    if len(args) == 1:
                        Reporter.MessageFrom("Report", red + "Usage: /report view id")
                        return
                    id = args[1]
                    if not id.isdigit():
                        Reporter.MessageFrom("Report", red + "ID must be a number.")
                        return
                    if not ini.GetSetting("Reports", id) and ini.GetSetting("Reports", id) is None:
                        Reporter.MessageFrom("Report", red + "This report doesn't exist!")
                        return
                    Reason = ini.GetSetting("Reports", id)
                    Reporter.MessageFrom("Report", red + "You are viewing Ticket ID: " + id)
                    Reporter.MessageFrom("Report", green + "Case: " + teal + Reason)
                elif args[0] == "delete":
                    if not Reporter.Admin and not self.isMod(Reporter.SteamID):
                        return
                    if len(args) == 1:
                        Reporter.MessageFrom("Report", red + "Usage: /report delete id")
                        return
                    id = args[1]
                    if not id.isdigit():
                        Reporter.MessageFrom("Report", red + "ID must be a number.")
                        return
                    ini.DeleteSetting("Reports", id)
                    Reporter.MessageFrom("Report", red + "Case:" + id + " deleted.")
                    ini.Save()
                elif args[0] == "deleteall":
                    if not Reporter.Admin and not self.isMod(Reporter.SteamID):
                        return
                    for key in enum:
                        ini.DeleteSetting("Reports", key)
                    Reporter.MessageFrom("Report", red + "All cases deleted.")
                    ini.Save()
                else:
                    if len(args) <= 1:
                        Reporter.MessageFrom('Report', 'Usage: /report "PlayerName" "message"')
                        Reporter.MessageFrom('Report', 'Quote signs (") are required.')
                        return
                    array = self.GetQuoted(args, Reporter)
                    if not array:
                        return
                    Reported = self.CheckV(Reporter, array[0])
                    if Reported is None:
                        return
                    self.HandleReport(Reporter, Reported, array[1])

    def HandleReport(self, Reporter, Reported, Message):
        if len(Message) > 47:
            Reporter.MessageFrom("Report", red + "Too long reason. Write It shorter.")
            return
        #Keresett Jatekos
        for player in Server.Players:
            if player.Admin or self.isMod(player.SteamID):
                player.MessageFrom("Report", red + "New report submitted!")
                player.MessageFrom("Report", red + "Check it with the /report view command.")
        Plugin.Log("ReportLogs", "Reporter: " + Reporter.Name + " | " + Reporter.SteamID + " | Reported: " + Reported.Name +
            " | " + Reported.SteamID + " |  Reason: " + Message)
        self.SaveReportToIni(Reporter, Reported.Name, Message)
        Reporter.MessageFrom("Report", red + "Report submitted.")


#!/usr/bin/env python
#
import os
import time
import requests
import json
import logging
import uuid
from datadog import initialize, api

# [start config]
# GCP image file url
GCP_IMAGE = 'https://storage.googleapis.com/xxx-testing-monitor/sample-invoice.png'

# S3 image file url
S3_IMAGE = 'https://s3-eu-west-1.amazonaws.com/xxx-public/xxx-testing-monitor/sample-invoice.png'

# Configure this environment variable via Dockerfile ENV
#
# Google API Key
API_KEY = os.environ['API_KEY']

# Datadog authentication api
options = {
    'api_key': 'xxx',
    'app_key': 'xxx'
}

initialize(**options)
#
logging.basicConfig(format='%(asctime)s %(levelname)s: %(message)s', level=logging.INFO,
                    datefmt='%Y-%m-%d %I:%M:%S')
# [end config]

def _generate_payload_data(image_uri):
    """Generate the request payload data
    Format copied from https://cloud.google.com/vision/docs/ocr"""
    sending_request = {
        "requests": [
            {
                "image": {
                    "source": {
                        "imageUri": image_uri
                    }
                },
                "features": [
                    {
                        "type": "DOCUMENT_TEXT_DETECTION",
                        "maxResults": 1000
                    }
                ]
            }
        ]
    }
    return sending_request

def detect_document_uri(image_uri=GCP_IMAGE, image_source='gcp_storage', env='staging'):
    """Input image url location ,image source location,
       post response status code to datadog"""

    # make a UUID based on the host ID and current time
    uid = uuid.uuid1()

    # cache brake
    cache_brake = '?uuid={}'.format(uid)

    sending_request = _generate_payload_data("{}{}".format(image_uri, cache_brake))

    # start the timer
    start_timer = time.time()

    # submit the post
    response = requests.post(
        url='https://vision.googleapis.com/v1/images:annotate?key={}'.format(API_KEY),
        data=json.dumps(sending_request),
        headers={'Content-Type': 'application/json', 'Cache-Control': 'no-cache'}
    )

    status_code = str(response.status_code)

    # stop the timer second to millisecond
    latency = (time.time() - start_timer) * 1000

    metric = "probe.google.vision.gauge.latency"
    # Post data to datadog
    api.Metric.send([{
        'metric': metric,
        'type': 'gauge',
        'host': 'k8s',
        'points': [[time.time(), float(latency)]],
        'tags': {'service': 'google-vision', 'env': env,
                            'status-code': status_code,
                            'location': 'kuberntes',
                            'image_location': image_source
                 }

    }])
    logging.info("METRIC:{} STATUS_CODE:{} LATENCY:{}".format(metric, status_code, float(latency)))
    return float(latency)

if __name__ == '__main__':
        print(detect_document_uri())
        print(detect_document_uri(S3_IMAGE, image_source='s3_storage'))