#!/usr/bin/env python2
import os, datetime
from time import sleep

import finalip_lib as l
import requests
from lxml.html import fromstring

DIR = os.path.join(os.environ['READER_ROOT'], '..', 'finalips')
try:
    os.mkdir(DIR)
except OSError:
    pass

MONTHS = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December']

def save(meta_session, p_t03, p_t04, next_url = None):
    'Make the request, save the html of the response, and pass the meta-session along.'
    session = meta_session[0]
    if next_url == None:
        # First page
        response = l.apex_submit(meta_session, p_t03, p_t04)
    else:
        # Next page
        response = session.get(next_url)

    # Check whether there's anything on the page
    if 'Search Criteria Returned No Results.' in response.text.encode('utf-8'):
        return

    # Parse the HTML.
    html = fromstring(response.text.encode('utf-8'))

    # Save
    filedir = os.path.join(DIR, p_t03, p_t04)
    option_texts = html.xpath('//select[@name="X01"]/option[@selected]/text()')
    if len(option_texts) > 0:
        filename = option_texts[0]
    else:
        # May 2012, rows 301 to 330 has trouble.
        # import pdb
        # pdb.set_trace()
        return

    try:
        os.makedirs(filedir)
    except OSError:
        pass
    h = open(os.path.join(filedir, filename), 'w')
    h.write(response.text.encode('utf-8'))
    h.close()

    print('Downloaded %s %s, page %s' % (MONTHS[int(p_t04) - 1], p_t03, filename))

    # Pass state
    meta_session = session, response, html
    return meta_session

def _month_done(p_t03, p_t04):
    monthdir = os.path.join(DIR, p_t03, p_t04)
    if not os.path.isdir(monthdir):
        return False

    filenames = os.listdir(monthdir)
    if len(filenames) > 0:
        total_rows = filenames[0].split(' ')[-1]
        for filename in filenames:
            if '%s of %s' % (total_rows, total_rows) in filename:
                return True
    return False

def _get_first_meta_session():
    session = requests.session()
    response = session.get('http://geo.usace.army.mil/egis/f?p=340:2:1433809126328401::NO:RP::')
    html = fromstring(response.text.encode('utf-8'))
    return (session, response, html)
meta_session = _get_first_meta_session()

for p_t03 in l.p_t03s(meta_session[2]):
    for p_t04 in l.p_t04s(meta_session[2]):
        # Skip this month if it is already done
        params = (MONTHS[int(p_t04) - 1], p_t03)

        # May 2012, rows 301 to 330 has trouble.
        if params == ('May', '2012'):
            continue

        if _month_done(p_t03, p_t04):
            print('Skipping %s %s' % params)
            continue
        else:
            print('Working on %s %s' % params)

        _meta_session = save(meta_session, p_t03, p_t04)
        if _meta_session == None:
            print('No results for %s %s' % params)
            continue
        else:
            meta_session = _meta_session

        while True:
            sleep(1)
            html = meta_session[2]
            nexts = html.xpath('//a[text()="Next >"]/@href')
            if len(nexts) == 0:
                print('End of %s %s' % params)
                break
            else:
                meta_session = save(meta_session, p_t03, p_t04, u'http://geo.usace.army.mil/egis/' + nexts[0])


from django.db.models import Avg, Count, F
from django.contrib.auth.decorators import login_required
from django.contrib import messages
from django.contrib.postgres.search import (
    SearchVector, SearchQuery, SearchRank)
from django.shortcuts import render, get_object_or_404, redirect
from django.conf import settings

from .models import Region, Trail, Comment, Favorite
from .forms import CommentForm, SearchForm
from .utils import get_page

def index(request):
    """
    Renders index page.
    Gets all regions list (navigation) and top trails list
    from context processor and inclusion tag.
    """
    return render(request, 'trails/index.html')

def region_detail(request, slug_region):
    """
    Renders page with detailed information about region.
    Gets all regions list (navigation) and top region trails
    from context processor and inclusion tag.
    """
    region = get_object_or_404(
        Region.objects.prefetch_related('photos'),
        slug=slug_region
    )
    context = {
        'region': region,
    }
    return render(request, 'trails/region_details.html', context=context)

def trails_list(request, slug_region=None):
    """
    Renders page with trails list for selected region (if slug_region)
    or total list (if slug_region is None).
    Gets all regions list (navigation) from context processor.
    """
    if slug_region:
        region = get_object_or_404(Region, slug=slug_region)
        trails = region.trails.filter(is_published=True)
    else:
        region = None
        trails = Trail.objects.filter(is_published=True)
    trails = (trails
              .select_related('region')
              .annotate(avg_rank=Avg('comments__ranking'))
              .annotate(count_favorite=Count('favorite'))
              .order_by(F('avg_rank').desc(nulls_last=True), '-count_favorite')
              )
    favorites = []
    if request.user.is_authenticated:
        favorites = (Favorite.objects
                     .filter(user=request.user)
                     .values_list('trail', flat=True))
    page_obj = get_page(
        request, trails, settings.TRAILS_NUMBER_ALL_TRAILS_PAGE)
    context = {
        'page_obj': page_obj,
        'region': region,
        'favorites': favorites,
    }
    return render(request, 'trails/trails_list.html', context)

def trail_detail(request, slug_trail):
    """
    Renders page with detailed information about trail.
    Gets all regions list (navigation) and top region trails
    from context processor and inclusion tag.
    """
    trail = get_object_or_404(
        Trail.objects.select_related('region').prefetch_related('photos'),
        slug=slug_trail, is_published=True
    )
    favorite = (Favorite.objects
                .filter(user=request.user, trail=trail).exists()
                if request.user.is_authenticated
                else False)
    form = CommentForm()
    comments = trail.comments.filter(is_active=True).select_related('author')
    agregate_comments = comments.aggregate(Avg('ranking'), Count('id'))
    context = {
        'trail': trail,
        'favorite': favorite,
        'average_rating': agregate_comments['ranking__avg'],
        'count': agregate_comments['id__count'],
        'comments': comments[:settings.COMMENTS_NUMBER_TRAIL_PAGE],
        'form': form,
    }
    return render(request, 'trails/trail_details.html', context=context)

@login_required
def add_comment(request, slug_trail):
    """
    Creates new comment if user is authenticated or redirects to login.
    """
    trail = get_object_or_404(Trail, slug=slug_trail, is_published=True)
    form = CommentForm(request.POST or None)
    if form.is_valid():
        comment = form.save(commit=False)
        comment.author = request.user
        comment.trail = trail
        comment.save()
        messages.success(request, 'Комментарий успешно сохранен.')
    else:
        messages.error(request, 'Возникла ошибка.  Комментарий не сохранен.')
    return redirect('trails:trail_detail', slug_trail=slug_trail)

@login_required
def edit_comment(request, slug_trail, pk):
    comment = get_object_or_404(Comment, pk=pk, is_active=True)
    if request.user != comment.author:
        return redirect('trails:trail_detail', slug_trail)
    form = CommentForm(request.POST or None, instance=comment)
    if form.is_valid():
        form.save()
        messages.success(request, 'Изменения успешно сохранены.')
        return redirect('trails:trail_detail', comment.trail.slug)
    return render(request, 'trails/comment_edit.html', {'form': form})

@login_required
def delete_comment(request, slug_trail, pk):
    comment = get_object_or_404(Comment, pk=pk, is_active=True)
    if request.user != comment.author:
        return redirect('trails:trail_detail', slug_trail)
    form = CommentForm(instance=comment)
    if request.method == 'POST':
        comment.delete()
        messages.success(request, 'Комментарий успешно удалён.')
        return redirect('trails:trail_detail', slug_trail)
    return render(request, 'trails/comment_delete.html', {'form': form})

def comments_list(request, slug_trail):
    """
    Renders comments to trail paginated list.
    Gets all regions list (navigation) and top region trails
    from context processor and inclusion tag.
    """
    trail = get_object_or_404(
        Trail.objects.select_related('region'),
        slug=slug_trail, is_published=True
    )
    comments = trail.comments.filter(is_active=True).select_related('author')
    page_obj = get_page(
        request, comments, settings.COMMENTS_NUMBER_COMMENTS_PAGE)
    context = {
        'trail': trail,
        'page_obj': page_obj,
    }
    return render(request, 'trails/comments_list.html', context=context)

def trails_search(request):
    """Postgres full text search in trails text data."""
    form = SearchForm()
    query = None
    results = []
    if 'query' in request.GET:
        form = SearchForm(request.GET)
        if form.is_valid():
            query = form.cleaned_data['query']
            search_vector = (
                SearchVector(
                    'name', weight='A', config='russian')
                + SearchVector(
                    'short_description', weight='B', config='russian')
                + SearchVector(
                    'full_description', weight='B', config='russian')
                + SearchVector(
                    'start_point_description', weight='C', config='russian')
                + SearchVector(
                    'aqua', weight='C', config='russian')
            )
            search_query = SearchQuery(query, config='russian')

            results = (Trail.objects.filter(is_published=True)
                       .annotate(search=search_vector,
                                 rank=SearchRank(search_vector, search_query))
                       .filter(search=search_query)
                       .filter(rank__gte=0.3)
                       .order_by('-rank'))
    context = {
        'form': form,
        'query': query,
        'results': results,
    }
    return render(request, 'trails/search.html', context=context)

@login_required
def favorite_list(request):
    """Current user's favorite trails list."""
    trails = (Trail.objects
              .filter(favorite__user=request.user, is_published=True)
              .select_related('region')
              .annotate(avg_rank=Avg('comments__ranking'))
              .order_by(F('avg_rank').desc(nulls_last=True))
              )
    favorites = trails.values_list('id', flat=True)
    page_obj = get_page(
        request, trails, settings.TRAILS_NUMBER_ALL_TRAILS_PAGE)
    context = {
        'page_obj': page_obj,
        'favorites': favorites
    }
    return render(request, 'trails/favorite.html', context)

@login_required
def add_favorite(request, slug_trail):
    """Add trail to current user's favorites."""
    trail = get_object_or_404(Trail, slug=slug_trail, is_published=True)
    Favorite.objects.get_or_create(user=request.user, trail=trail)
    return redirect('trails:favorite_list')

@login_required
def delete_favorite(request, slug_trail):
    """Delete trail from current user's favorites."""
    trail = get_object_or_404(Trail, slug=slug_trail, is_published=True)
    Favorite.objects.filter(user=request.user, trail=trail).delete()
    redirect_path = request.META.get('HTTP_REFERER') or 'trails:favorite_list'
    return redirect(redirect_path)


from modulus.models.multiplicative_filter_net import MultiplicativeFilterNetArch
import torch
import numpy as np
from modulus.key import Key

def make_dict(nr_layers):
    _dict = dict()
    names = [("weight", "weights"), ("bias", "biases"), ("weight_g", "alphas")]
    tri_names = ("frequency", "phase")
    for tri_name in tri_names:
        _dict["first_filter." + tri_name] = "fourier_filter_first_" + tri_name + ":0"
    for i in range(nr_layers):
        for pt_name, tf_name in names:
            _dict["fc_layers." + str(i) + ".linear." + pt_name] = (
                "fc_" + str(i) + "/" + tf_name + ":0"
            )
        for tri_name in tri_names:
            _dict["filters." + str(i) + "." + tri_name] = (
                "fourier_filter_layer" + str(i) + "_" + tri_name + ":0"
            )
    for pt_name, tf_name in names[:2]:
        _dict["final_layer.linear." + pt_name] = "fc_final/" + tf_name + ":0"
    return _dict

def test_multiplicative_filter():
    filename = "./test_models/data/test_multiplicative_filter.npz"
    test_data = np.load(filename, allow_pickle=True)
    data_in = test_data["data_in"]
    Wbs = test_data["Wbs"][()]
    params = test_data["params"][()]
    # create graph
    arch = MultiplicativeFilterNetArch(
        input_keys=[Key("x"), Key("y")],
        output_keys=[Key("u")],
        layer_size=params["layer_size"],
        nr_layers=params["nr_layers"],
    )
    name_dict = make_dict(params["nr_layers"])
    for _name, _tensor in arch.named_parameters():
        if _tensor.requires_grad:
            if "filter" in _name:
                _tensor.data = torch.from_numpy(Wbs[name_dict[_name]])
            else:
                _tensor.data = torch.from_numpy(Wbs[name_dict[_name]].T)

    data_out2 = arch(
        {"x": torch.from_numpy(data_in[:, 0:1]), "y": torch.from_numpy(data_in[:, 1:2])}
    )
    data_out2 = data_out2["u"].detach().numpy()
    # load outputs
    data_out1 = test_data["data_out"]
    # verify
    assert np.allclose(data_out1, data_out2, atol=1e-4), "Test failed!"
    print("Success!")

if __name__ == "__main__":
    test_multiplicative_filter()


import demistomock as demisto  # noqa: F401
from CommonServerPython import *  # noqa: F401

''' IMPORTS '''
import traceback
import requests

'''CONSTANTS'''
BitSight_date_time_format = '%Y-%m-%d'

# Disable insecure warnings
requests.packages.urllib3.disable_warnings()

class Client(BaseClient):
    """
    Client will implement the service API, should not contain Cortex XSOAR logic.
    Should do requests and return data
    """

    def get_companies_guid(self):
        uri = 'v1/companies'
        return self._http_request(
            method='GET',
            url_suffix=uri
        )

    def get_company_detail(self, guid):
        uri = f'v1/companies/{encode_string_results(guid)}'
        return self._http_request(
            method='GET',
            url_suffix=uri
        )

    def get_company_findings(self, guid, first_seen, last_seen, severity_gte, grade_gt, asset_category, risk_vector):
        uri = f'v1/companies/{encode_string_results(guid)}/findings'

        params = {
            'first_seen_gte': first_seen,
            'last_seen_lte': last_seen,
            'unsampled': 'true',
            'expand': 'attributed_companies'
        }

        if severity_gte:
            params['severity_gte'] = severity_gte

        if grade_gt:
            params['details.grade'] = grade_gt

        if asset_category:
            params['assets.category'] = asset_category

        if risk_vector:
            params['risk_vector'] = risk_vector

        return self._http_request(
            method='GET',
            url_suffix=uri,
            params=params
        )

'''HELPER FUNCTIONS'''

def get_time_elapsed(fetch_time, last_run, first_fetch):
    today = datetime.today()
    now = datetime.now()
    if 'time' in last_run:
        # Get Last run and parse to date format. Bitsight report will be pulled from last run date to Yesterday's date
        last_run_time = last_run['time']
        last_run = datetime.strptime(last_run_time, '%Y-%m-%dT%H:%M:%SZ')
        last_run_time = last_run.strftime(BitSight_date_time_format)
        time_elapsed_in_minutes = (now - last_run).total_seconds() / 60
    else:
        # If last run time is not set, data will be pulled using fetch_time
        # i.e. last 10min if fetch events is set to 10min
        last_run_time = (today - timedelta(days=first_fetch)).strftime(
            BitSight_date_time_format)
        time_elapsed_in_minutes = fetch_time

    return time_elapsed_in_minutes, last_run_time

'''COMMAND FUNCTIONS'''

def fetch_incidents(client, last_run, params):
    events = []
    minuets_in_day = 1440

    try:
        # If there is no fetch time configured, it will be set to 0 and no events will be pulled
        first_fetch = int(params.get('first_fetch', 1))
        fetch_time = params.get('fetch_time', '00:01')
        current_time = datetime.now().strftime('%H:%M')
        time_elapsed_in_minutes, last_run_date = get_time_elapsed(minuets_in_day, last_run, first_fetch)

        if (time_elapsed_in_minutes >= minuets_in_day) and (current_time >= fetch_time):
            report_entries = []
            findings_res = get_company_findings_command(client, params, last_run_date, True)
            report_entries.extend(findings_res.get('results', []))

            for entry in report_entries:
                # Set the Raw JSON to the event. Mapping will be done at the classification and mapping
                event = {
                    "name": "BitSight Finding - " + entry.get('temporary_id'),
                    'occurred': entry.get('first_seen') + 'T00:00:00Z',
                    "rawJSON": json.dumps(entry)}
                events.append(event)
            last_run_time = datetime.now().strftime('%Y-%m-%dT%H:%M:%SZ')

            last_run = {'time': last_run_time}
    except Exception as e:
        demisto.error('Failed to fetch events.')
        raise e

    return last_run, events

def test_module(client):
    """
    Returning 'ok' indicates that the integration works like it is supposed to. Connection to the service is successful.
    Anything else will fail the test.
    """
    res = client.get_companies_guid()

    available_guids = {c["guid"] for c in res["companies"]}
    requested_guid = demisto.params().get("guid", None)

    if requested_guid is None:
        raise Exception("Must provide a GUID ")

    if requested_guid in available_guids:
        return 'ok', None, None
    else:
        raise Exception(f"Failed to execute test_module "
                        f"Response: {res}")

def get_companies_guid_command(client):
    generic_iam_context_data_list = []
    res_json = client.get_companies_guid()

    generic_iam_context = {
        'companyName': 'my_company',
        'shortName': 'my_company',
        'guid': res_json.get('my_company', {}).get('guid')
    }
    generic_iam_context_data_list.append(generic_iam_context)
    companies_list = res_json.get('companies', [])
    for company in companies_list:
        generic_iam_context = {
            'companyName': company.get('name'),
            'shortName': company.get('shortname'),
            'guid': company.get('guid')
        }
        generic_iam_context_data_list.append(generic_iam_context)

    readable_output = tableToMarkdown(name='Get Companies GUID:',
                                      t=generic_iam_context_data_list,
                                      headers=["companyName", "shortName", "guid"],
                                      removeNull=True
                                      )

    return readable_output, generic_iam_context_data_list, res_json

def get_company_details_command(client, args):
    guid = args.get('guid')
    res_json = client.get_company_detail(guid)

    generic_iam_context = {
        'guid': res_json.get('guid'),
        'customId': res_json.get('custom_id'),
        'name': res_json.get('name'),
        'description': res_json.get('description'),
        'ipv4Count': res_json.get('ipv4_count'),
        'peopleCount': res_json.get('people_count'),
        'shortName': res_json.get('shortname'),
        'industry': res_json.get('industry'),
        'industrySlug': res_json.get('industry_slug'),
        'subIndustry': res_json.get('sub_industry'),
        'subIndustrySlug': res_json.get('sub_industry_slug'),
        'homePage': res_json.get('homepage'),
        'primaryDomain': res_json.get('primary_domain'),
        'type': res_json.get('type'),
        'displayURL': res_json.get('display_url'),
        'ratingDetails': res_json.get('rating_details'),
        'ratings': res_json.get('ratings'),
        'searchCount': res_json.get('search_count'),
        'subscriptionType': res_json.get('subscription_type'),
        'sparkline': res_json.get('sparkline'),
        'subscriptionTypeKey': res_json.get('subscription_type_key'),
        'subscriptionEndDate': res_json.get('subscription_end_date'),
        'bulkEmailSenderStatus': res_json.get('bulk_email_sender_status'),
        'serviceProvider': res_json.get('service_provider'),
        'customerMonitoringCount': res_json.get('customer_monitoring_count'),
        'availableUpgradeTypes': res_json.get('available_upgrade_types'),
        'hasCompanyTree': res_json.get('has_company_tree'),
        'hasPreferredContact': res_json.get('has_preferred_contact'),
        'isBundle': res_json.get('is_bundle'),
        'ratingIndustryMedian': res_json.get('rating_industry_median'),
        'primaryCompany': res_json.get('primary_company'),
        'permissions': res_json.get('permissions'),
        'isPrimary': res_json.get('is_primary'),
        'securityGrade': res_json.get('security_grade'),
        'inSpmPortfolio': res_json.get('in_spm_portfolio'),
        'isMycompMysubsBundle': res_json.get('is_mycomp_mysubs_bundle'),
        'companyFeatures': res_json.get('company_features')
    }
    company_info = {
        'guid': res_json.get('guid'),
        'customId': res_json.get('custom_id'),
        'name': res_json.get('name'),
        'description': res_json.get('description'),
        'ipv4Count': res_json.get('ipv4_count'),
        'peopleCount': res_json.get('people_count'),
        'shortName': res_json.get('shortname'),
        'industry': res_json.get('industry'),
        'industrySlug': res_json.get('industry_slug'),
        'subIndustry': res_json.get('sub_industry'),
        'subIndustrySlug': res_json.get('sub_industry_slug'),
        'homePage': res_json.get('homepage'),
        'primaryDomain': res_json.get('primary_domain'),
        'type': res_json.get('type'),
        'displayURL': res_json.get('display_url')
    }
    ratings = []
    for rating in res_json.get('ratings', []):
        rating_dict = {
            'rating': rating.get('rating'),
            'rating_date': rating.get('rating_date'),
            'range': rating.get('range')
        }
        ratings.append(rating_dict)

    rating_details = []
    for rating_detail_key in res_json.get('rating_details', {}):
        rating_detail = res_json.get('rating_details', {}).get(rating_detail_key, {})
        rating_detail_dict = {
            'name': rating_detail.get('name'),
            'rating': rating_detail.get('rating'),
            'percentile': rating_detail.get('percentile'),
            'display_url': rating_detail.get('display_url')
        }
        rating_details.append(rating_detail_dict)

    readable = {
        'Company Info': company_info,
        'Ratings': ratings,
        'Rating Details': rating_details
    }

    readable_output = tableToMarkdown(name='Get Company Details:',
                                      t=readable,
                                      headers=["Company Info", "Ratings", "Rating Details"],
                                      removeNull=True
                                      )
    return readable_output, generic_iam_context, res_json

def get_company_findings_command(client, args, first_seen=None, fetch_incidents=False):
    if fetch_incidents:
        guid = args.get('guid', None)
        if not guid:
            res = client.get_companies_guid()
            if res.status_code == 200:
                res_json = res.json()
                guid = res_json.get('my_company', {}).get('guid')
            else:
                raise Exception('Unable to fetch GUID')
        severity = args.get('findings_min_severity', None)
        if severity:
            severity = severity.lower()
        grade = args.get('findings_grade', None)
        if type(grade) is list:
            grade = ','.join(grade)
        asset_category = args.get('findings_asset_category', None)
        if asset_category:
            asset_category = asset_category.lower()
        risk_vector_list = args.get('risk_vector')
        if not isinstance(risk_vector_list, list):
            risk_vector_list = risk_vector_list.split(',')
        if 'All' in risk_vector_list:
            risk_vector_list = []
        first_seen = first_seen
        last_seen = (datetime.today() - timedelta(days=1)).strftime(BitSight_date_time_format)
    else:
        guid = args.get('guid')
        severity = args.get('severity', None)
        grade = args.get('grade', None)
        asset_category = args.get('asset_category', None)
        if severity:
            severity = severity.lower()
        if grade:
            grade = grade.lower()
        if asset_category:
            asset_category = asset_category.lower()
        risk_vector_list = args.get('risk_vector_label', None)
        if risk_vector_list:
            risk_vector_list = risk_vector_list.split(',')
        else:
            risk_vector_list = []
        first_seen = args.get('first_seen')
        last_seen = args.get('last_seen')

    severity_mapping = {
        'minor': 1,
        'moderate': 2,
        'material': 3,
        'severe': 4
    }

    asset_category_mapping = {
        'low': 'low,medium,high,critical',
        'medium': 'medium,high,critical',
        'high': 'high,critical',
        'critical': 'critical'
    }

    risk_vector_mapping = {
        'web application headers': 'application_security',
        'botnet infections': 'botnet_infections',
        'breaches': 'data_breaches',
        'desktop software': 'desktop_software',
        'dkim': 'dkim',
        'dnssec': 'dnssec',
        'file sharing': 'file_sharing',
        'insecure systems': 'insecure_systems',
        'malware servers': 'malware_servers',
        'mobile app publications': 'mobile_app_publications',
        'mobile application security': 'mobile_application_security',
        'mobile software': 'mobile_software',
        'open ports': 'open_ports',
        'patching cadence': 'patching_cadence',
        'potentially exploited': 'potentially_exploited',
        'server software': 'server_software',
        'spam propagation': 'spam_propagation',
        'spf': 'SPF',
        'ssl certificates': 'ssl_certificates',
        'ssl configurations': 'ssl_configurations',
        'unsolicited communications': 'unsolicited_comm'
    }

    risk_vector = ''
    for vector in risk_vector_list:
        risk_vector += risk_vector_mapping[vector.lower()] + ','

    risk_vector = risk_vector[:-1]

    severity_gte = None
    if severity:
        severity_gte = severity_mapping[severity]

    asset_category_eq = None
    if asset_category:
        asset_category_eq = asset_category_mapping[asset_category]

    res_json = client.get_company_findings(guid, first_seen, last_seen, severity_gte, grade, asset_category_eq,
                                           risk_vector)

    if not fetch_incidents:
        generic_iam_context_data_list = []
        readable_list = []
        results = res_json.get('results')
        if results:
            for result in results:
                generic_iam_context = {
                    'temporaryId': result.get('temporary_id'),
                    'affectsRating': result.get('affects_rating'),
                    'assets': result.get('assets'),
                    'details': result.get('details'),
                    'evidenceKey': result.get('evidence_key'),
                    'firstSeen': result.get('first_seen'),
                    'lastSeen': result.get('last_seen'),
                    'relatedFindings': result.get('related_findings'),
                    'riskCategory': result.get('risk_category'),
                    'riskVector': result.get('risk_vector'),
                    'riskVectorLabel': result.get('risk_vector_label'),
                    'rolledupObservationId': result.get('rolledup_observation_id'),
                    'severity': result.get('severity'),
                    'severityCategory': result.get('severity_category'),
                    'tags': result.get('tags'),
                    'duration': result.get('duration'),
                    'comments': result.get('comments'),
                    'remainingDecay': result.get('remaining_decay')
                }

                generic_iam_context_data_list.append(generic_iam_context)
                readable = {
                    'Evidence Key': result.get('evidence_key'),
                    'Risk Vector Label': result.get('risk_vector_label'),
                    'First Seen': result.get('first_seen'),
                    'Last Seen': result.get('last_seen'),
                    'ID': result.get('temporary_id'),
                    'Risk Category': result.get('risk_category'),
                    'Severity': result.get('severity_category'),
                }
                readable_list.append(readable)
        else:
            generic_iam_context_data_list.append({})
            readable_list.append({})

        readable_output = tableToMarkdown(name='Get Company findings:',
                                          t=readable_list,
                                          headers=["Evidence Key", "Risk Vector Label", "First Seen", "Last Seen",
                                                   "ID", "Risk Category", "Severity"],
                                          removeNull=True
                                          )
        return readable_output, generic_iam_context_data_list, res_json
    else:
        return res_json

def main():
    command = demisto.command()
    params = demisto.params()
    """
        PARSE AND VALIDATE INTEGRATION PARAMS
    """
    base_url = params.get('url')
    verify_certificate = not params.get('insecure', False)
    proxy = params.get('proxy', False)
    api_key = params.get('apikey', {})

    demisto.info(f'Command being called is {command}')

    client = Client(
        base_url=base_url,
        verify=verify_certificate,
        proxy=proxy,
        ok_codes=[200],
        auth=requests.auth.HTTPBasicAuth(api_key, '')
    )

    try:
        '''EXECUTION CODE'''
        if command == 'bitsight-get-company-details':
            readable_output, context, res_json = get_company_details_command(client, demisto.args())
            results = CommandResults(
                readable_output=readable_output,
                outputs_prefix='BitSight.Company',
                outputs=context,
                outputs_key_field='guid',
                raw_response=res_json
            )
            return_results(results)
        elif command == 'bitsight-get-company-findings':
            readable_output, context, res_json = get_company_findings_command(client, demisto.args())
            results = CommandResults(
                readable_output=readable_output,
                outputs_prefix='BitSight.Finding',
                outputs=context,
                outputs_key_field='guid',
                raw_response=res_json
            )
            return_results(results)
        elif command == 'test-module':
            human_readable, outputs, raw_response = test_module(client)
            return_outputs(readable_output=human_readable, outputs=outputs, raw_response=raw_response)
        elif command == 'bitsight-get-companies-guid':
            readable_output, context, res_json = get_companies_guid_command(client)
            results = CommandResults(
                readable_output=readable_output,
                outputs_prefix='BitSight.GUID',
                outputs=context,
                outputs_key_field='temporary_id',
                raw_response=res_json
            )
            return_results(results)
        elif command == 'fetch-incidents':
            last_run = demisto.getLastRun()

            last_run_curr, events = fetch_incidents(client, last_run, params)

            if last_run != last_run_curr:
                demisto.setLastRun({'time': last_run_curr['time']})
                demisto.incidents(events)
            else:
                demisto.incidents([])

    # Log exceptions
    except Exception:
        return_error(f'Failed to execute {demisto.command()} command. Traceback: {traceback.format_exc()}')

if __name__ in ['__main__', 'builtin', 'builtins']:
    main()


from flask import Flask, request, abort,jsonify

app = Flask(__name__)

_dataStore = {}

@app.route('/data/<username>',methods=['GET'])
def GetPos(username):
    if not username is None:
        if _dataStore.get(username) == None:
            return abort(404)
        else:
            if len(_dataStore[username]) > 0:
                return _dataStore[username].pop(0)
            else:
                return abort(404)
    else:
        return abort(404)

@app.route('/data/<username>',methods=['POST'])
def POSTPos(username):
    if not username is None:
        if _dataStore.get(username) == None:
            _dataStore[username] = []
        _dataStore[username].append(request.data)
        return '1'
    else:
        return abort(404)
# @app.route('/data/<username>')
# def Postid(username):
#     return username

if __name__ == '__main__':
    app.run(host='0.0.0.0')


import argparse
import logging
import os
import traceback
from textwrap import dedent
from time import sleep

import requests
import telegram
from dotenv import load_dotenv

def get_devman_reviews(token, timestamp_to_request):
    url = 'https://dvmn.org/api/long_polling/'
    headers = {
        'Authorization': f'Token {token}'
        }
    params = {
        'timestamp': timestamp_to_request
        }
    response = requests.get(url, headers=headers, params=params)
    response.raise_for_status()
    return response.json()

def get_delay_argument():
    parser = argparse.ArgumentParser(
        description='Получайте сообщение о проверке задания в Телеграм.')
    parser.add_argument('-d', '--delay', default=60,
                        help="""
                        Время паузы перед отправкой нового запроса
                        в случае отсутствия соединения с сетью в секундах.
                        """
                        )
    args = parser.parse_args()
    return args.delay

class TelegramLogsHandler(logging.Handler):

    def __init__(self, tg_bot, chat_id):
        super().__init__()
        self.chat_id = chat_id
        self.tg_bot = tg_bot

    def emit(self, record):
        log_entry = self.format(record)
        self.tg_bot.send_message(chat_id=self.chat_id, text=log_entry)

if __name__ == '__main__':
    delay = get_delay_argument()
    logging.basicConfig(
        filename='logs.log',
        level=logging.ERROR,
        format='%(asctime)s - %(levelname)s - %(message)s')
    load_dotenv()
    devman_api_token = os.environ['DEVMAN_API_TOKEN']
    telegram_bot_token = os.environ['TELEGRAM_BOT_TOKEN']
    telegram_chat_id = os.environ['TELEGRAM_CHAT_ID']
    bot = telegram.Bot(token=telegram_bot_token)
    logger = logging.getLogger('TelegramLogger')
    logger.setLevel(logging.INFO)
    logger.addHandler(TelegramLogsHandler(bot, telegram_chat_id))
    last_attempt_timestamp = None
    logger.info('Бот успешно запущен!')
    while True:
        try:
            review = get_devman_reviews(
                devman_api_token, last_attempt_timestamp)
            if review['status'] == 'timeout':
                last_attempt_timestamp = review['timestamp_to_request']
            else:
                review_details = review['new_attempts'][0]
                lesson_title = review_details['lesson_title']
                lesson_url = review_details['lesson_url']
                review_is_negative = review_details['is_negative']
                message_template = f'У Вас проверили работу: '
                if not review_is_negative:
                    message = f"""\
                            {message_template} "{lesson_title}".
                            Преподавателю всё понравилось, можно приступать
                            к следующему уроку!
                            """
                else:
                    message = f"""\
                            {message_template} "{lesson_title}".
                            К сожалению, в работе нашлись ошибки, подробности
                            по ссылке: {lesson_url}
                            """
                bot.send_message(chat_id=telegram_chat_id,
                                 text=dedent(message))
        except requests.exceptions.ReadTimeout as et_error:
            logging.exception(et_error)
        except requests.exceptions.ConnectionError as c_error:
            logging.exception(c_error)
            sleep(delay)
        except Exception as error:
            logging.exception(error)
            logger.error('Бот упал с ошибкой:')
            logger.error(f'{error}\n{traceback.format_exc()}')


import json
import logging

from django.http import HttpResponse, HttpResponseBadRequest
from django.utils.decorators import method_decorator
from django.utils.encoding import force_text
from django.views.decorators.csrf import csrf_exempt
from django.views.generic import View

from closeio import utils

from . import signals

logger = logging.getLogger(__name__)

class CloseIOWebHook(View):
    @method_decorator(csrf_exempt)
    def dispatch(self, *args, **kwargs):
        return super(CloseIOWebHook, self).dispatch(*args, **kwargs)

    def post(self, request, *args, **kwargs):
        try:
            query = json.loads(force_text(request.body))
        except (ValueError, TypeError):
            logger.exception("CloseIO webhook request could not be parsed.")
            return HttpResponseBadRequest()

        try:
            event = query['event']
            model = query['model']
            data = utils.parse(query['data'])
        except KeyError:
            logger.exception("CloseIO webhook request could not be dispatched.")
            return HttpResponseBadRequest()

        data_to_send = dict(
            instance=data
        )

        if event == 'create':
            signals.closeio_create.send(
                sender=self.__class__,
                model=model,
                **data_to_send
            )

        elif event == 'update':
            signals.closeio_update.send(
                sender=self.__class__,
                model=model,
                **data_to_send
            )

        elif event == 'delete':
            data_to_send = dict(
                instance_id=data.get('id', '')
            )
            signals.closeio_delete.send(
                sender=self.__class__,
                model=model,
                **data_to_send
            )

        elif event == 'merge':
            data_to_send = dict(
                source_id=data.get('source_id', ''),
                destination_id=data.get('destination_id', ''),
            )
            signals.closeio_merge.send(
                sender=self.__class__,
                model=model,
                **data_to_send
            )

        expl_signal_name = '%s_%s' % (model, event)

        if hasattr(signals, expl_signal_name):
            signal = getattr(signals, expl_signal_name)
            signal.send(
                sender=self.__class__,
                **data_to_send
            )

        signals.closeio_event.send(
            sender=self.__class__,
            model=model,
            event=event,
            instance=data,
        )

        return HttpResponse()


import matplotlib.pyplot as plt
import numpy as np

# 样本数据
data = np.random.randn(1000)
print(data)

# 绘制直方图
plt.hist(data, edgecolor='black')  # 使用5个柱形，黑色边界

# 添加标签和标题
plt.xlabel('Value')
plt.ylabel('Frequency')
plt.title('Histogram')

# 显示图形
plt.show()


from flask import Flask,request,redirect,render_template,url_for,jsonify
import nltk
from nltk.stem.lancaster import LancasterStemmer
stemmer = LancasterStemmer()
from sklearn.preprocessing import LabelEncoder
import numpy as np
from collections import Counter
import tflearn
import tensorflow
import random
import json
import pickle

with open("intents.json") as file:
    intents = json.load(file)

app=Flask(__name__)

data = pickle.load( open( "training_data", "rb" ) )
words = data['words']
classes = data['classes']
train_x = data['train_x']
train_y = data['train_y']

tensorflow.reset_default_graph()

net = tflearn.input_data(shape=[None, len(train_x[0])])
net = tflearn.fully_connected(net, 8)
net = tflearn.fully_connected(net, 8)
net = tflearn.fully_connected(net, len(train_y[0]), activation="softmax")
net = tflearn.regression(net)

model = tflearn.DNN(net)
model.load("model.tflearn")

def clean_up_sentence(sentence):
    # tokenize the pattern
    sentence_words = nltk.word_tokenize(sentence)
    # stem each word
    sentence_words = [stemmer.stem(word.lower()) for word in sentence_words]
    return sentence_words

# return bag of words array: 0 or 1 for each word in the bag that exists in the sentence
def bow(sentence, words, show_details=False):
    # tokenize the pattern
    sentence_words = clean_up_sentence(sentence)
    # bag of words
    bag = [0]*len(words)
    for s in sentence_words:
        for i,w in enumerate(words):
            if w == s:
                bag[i] = 1
                if show_details:
                    print ("found in bag: %s" % w)

    return(np.array(bag))

context = {}

ERROR_THRESHOLD = 0.25
def classify(sentence):
    # generate probabilities from the model
    results = model.predict([bow(sentence, words)])[0]
    # filter out predictions below a threshold
    results = [[i,r] for i,r in enumerate(results) if r>ERROR_THRESHOLD]
    # sort by strength of probability
    results.sort(key=lambda x: x[1], reverse=True)
    return_list = []
    for r in results:
        return_list.append((classes[r[0]], r[1]))
    # return tuple of intent and probability
    return return_list

def response(sentence, userID='123', show_details=False):
    results = classify(sentence)
    # if we have a classification then find the matching intent tag
    if results:
        # loop as long as there are matches to process
        while results:
            for i in intents['intents']:
                # find a tag matching the first result
                if i['tag'] == results[0][0]:
                    # set context for this intent if necessary
                    if 'context_set' in i:
                        if show_details: print ('context:', i['context_set'])
                        context[userID] = i['context_set']

                    # check if this intent is contextual and applies to this user's conversation
                    if not 'context_filter' in i or \
                        (userID in context and 'context_filter' in i and i['context_filter'] == context[userID]):
                        if show_details: print ('tag:', i['tag'])
                        # a random response from the intent
                        return random.choice(i['responses'])

            results.pop(0)

# hi=chat(starts)
@app.route('/',methods=['GET'])
def hello():

    return jsonify({"message":hi})

conversations="check"
save=""
lists=[]
report=[]
resultvalue=[0,0,0,0,0,0]
resultlist=[[0,6,14,21,29,31,36],
            [1,10,17,20,25,32,38],
            [2,7,16,22,26,30,40],
            [3,11,12,19,27,33,39],
            [4,9,15,18,28,35,41],
            [5,8,14,23,24,34,37]]
field=["Realistic","Investigative","Artistic","Social","Enterprising","Conventional"]
@app.route('/',methods=['POST'])
def newhello():
    global save
    global lists
    global resultlist
    hi=request.json['message']
    print(f"hi  {hi}")
    print(f"saved  {save}")
    conversations=hi.lower()
    check=response(hi.lower())
    print(f"check  {check}")
    checks=save
    print(f"checks  {checks}")
    now =response(conversations)
    if hi.lower() == "start":
        lists=[]
        checks=""
        # check=response(hi.lower())
        # lists.append(hi.lower())
        conversations=hi.lower()+" "+checks[14:].lower()
        save=response(conversations)
        now =response(conversations)
    if checks[-9:].lower() == "yes or no":
        print("start")
        lists.append(hi.lower())
        print(lists)
        conversations=hi.lower()+" "+checks[14:].lower()
        save=response(conversations)
        now =response(conversations)
    if hi.lower() == "result":
        now = len(lists)

        label_encoder = LabelEncoder()

        # Encode labels in column 'species'.
        Y= label_encoder.fit_transform(lists)
        print(Y)
        for i in range(len(resultlist)):
            for j in range(len(resultlist[i])):
                # print(resultlist[i][j])
                resultvalue[i] += Y[resultlist[i][j]]
        print(resultvalue)
        for x in zip(field,resultvalue):
            report.append(x)
        print(report)
        now= str(report)
        k = Counter(report)

# Finding 3 highest values
        high = k.most_common(3)
        print(high)
        # for test in len(lists):
        #     if "yes" in (lists[0],lists[6],lists[13],lists[21],lists[29],lists[31],lists[36]):
        #         print(f" number ")
        #     else:
        #         pass

    print(f"length {len(lists)}")
    print(f" conversations  {conversations} ")
    return jsonify({"message":now})

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


from collections import OrderedDict
import os
from src.exam.exams import *
from src.student.students import *

def rank_students(filename, students, school, exam, coeff):
	ranking = students.get_ranking(opj(school, "ORAL/0", exam))

	table = Table()

	for id, rank in ranking:
		student = students.get_student(id)
		report = student.get(opj("0", school))
		if report == None:
			continue

		line = OrderedDict()
		line["Rang (F)"] = rank
		line["Rang (E)"] = report.get(opj("ECRIT/0", exam, "Rang (classe)")).split("/")[0].strip()
		line["Rang (N)"] = report.get(opj("ORAL/0", exam, "Rang"))
		line["Nom"] = id
		line["Total"] = report.get(opj("ORAL/0", exam, "Tot."))
		line["Total / " + str(coeff)] = line["Total"] / coeff
		table.set(line, -1)

	with open(filename, "w") as file:
		file.write(table.to_txt())

def list_students(folder):
	html = '<meta charset="utf-8">\n\n
'

	for id in old(folder):
		html += "
" + id + "
"

		for fpower in old(opj(folder, id)):
			power = fpower.replace(" ", "/")
			html += "
" + power + " :
"

			schools_exts = OrderedDict()

			for basename in olf(opj(folder, id, fpower)):
				school, ext = os.path.splitext(basename)
				if not(school in schools_exts.keys()):
					schools_exts[school] = []
				schools_exts[school].append(ext)

			for school, exts in schools_exts.items():
				html += "
" + school + " : "
				html += ", ".join([
					'' + ext[1:] + ""
					for ext in exts
				])
				html += "
"

			html += "
"
		html += "

\n"
	html += "
"

	with open(opj(folder, "index.html"), "w") as file:
		file.write(html)

year = 2016
exams_folder = "data/exams"
students_folder = "data/students"

Exams().load(year).save(exams_folder)

for mode in ["offline", "online"]:
	input_folder = opj(students_folder, "raw")
	output_folder = opj(students_folder, mode)

	students = Students()\
	.load(input_folder, year, mode == "online")\
	.compute_rankings(["Tot. ecrit", "Tot.", "Note"])\
	.add_ranks("Rang (classe)")

	if mode == "online":
		students.anonymize_general_ranks("Rang")

	students.save(output_folder)

	rank_students(
		opj(output_folder, "Classement - Mines Ponts.txt"),
		students,
		"Mines Ponts",
		"Mines Ponts MP",
		12
	)
	rank_students(
		opj(output_folder, "Classement - Centrale.txt"),
		students,
		"Centrale",
		"Centrale Paris filière MP",
		19
	)

	list_students(output_folder)


from json import load, dump

from data.config import ADMIN_ID

class UserNotFoundError(Exception):
    pass

async def get_users() -> dict:
    with open('data/users.json', 'r', encoding='utf-8') as f: return load(f)

async def get_admins(skip_cadmin: bool = False) -> dict:
    admins = {}
    with open('data/users.json', 'r', encoding='utf-8') as f:
        data = load(f)
        for user_id in data.keys():
            if data[user_id]["admin"]:
                admins[user_id] = data[user_id]
        if skip_cadmin: admins.pop(str(ADMIN_ID))
        return admins

async def add_user(user_id: str, username: str, admin: bool = False):
    with open('data/users.json',  'r+', encoding='utf-8') as f:
        data = load(f)
        data[user_id] = {"username": username, "admin": admin}
        f.seek(0)
        dump(data, f, indent=4, ensure_ascii=False)
        f.truncate()

async def delete_admin(user_id: str):
    with open('data/users.json', 'r+', encoding='utf-8') as f:
        data = load(f)
        data[user_id]["admin"] = False
        f.seek(0)
        dump(data, f, indent=4, ensure_ascii=False)
        f.truncate()

async def give_admin(user_id: str):
    try:
        with open('data/users.json', 'r+', encoding='utf-8') as f:
            data = load(f)
            data[user_id]["admin"] = True
            f.seek(0)
            dump(data, f, indent=4, ensure_ascii=False)
            f.truncate()
            return True

    except KeyError:
        return False


import argparse
import json
from urllib.request import urlopen

def get_parser():
    """Get parser for command line arguments."""
    parser = argparse.ArgumentParser(description="URL Downloader")
    parser.add_argument("-q", "--query", dest="query", help="Query/Filter", default='-')
    parser.add_argument("-k", "--key", dest="key", help="Google API Key")
    return parser

if __name__ == '__main__':

    parser = get_parser()
    args = parser.parse_args()

    data = urlopen('https://www.googleapis.com/customsearch/v1?key={}&cx=000919482489691833361:drmpuulq2tk&q={}'.format(args.key,args.query))
    data = json.load(data)
    #print(data['items'][0]['link'])
    with open('NewsURL.txt', 'a') as f:
        for j in range(len(data['items'])):
            f.write(data['items'][j]['link']+'\n')
            print(data['items'][j]['link'])


import time
from flask import Flask, Response, request, redirect, url_for, session
from flask_cors import CORS
import json
import logging
import os
from flask_dance.contrib.google import make_google_blueprint, google
from oauthlib.oauth2.rfc6749.errors import TokenExpiredError
import re

from application_services.CommentsResource.comment_service import CommentResource
from application_services.UsersResource.user_addr_service import UserAddrResource
from application_services.UsersResource.user_service import UserResource
from application_services.AppHTTPStatus import AppHTTPStatus
# from application_services.smarty_address_service import SmartyAddressService
from database_services.RDBService import RDBService as RDBService

# from flask_dance.contrib.google import make_google_blueprint, google
# import middleware.simple_security as simple_security
from middleware.notification import NotificationMiddlewareHandler as NotificationMiddlewareHandler
from middleware.steamsignin import SteamSignIn

import middleware.security as security

logging.basicConfig(level=logging.DEBUG)

logger = logging.getLogger()
logger.setLevel(logging.INFO)

# pagination data
OFFSET = 0
MAXLIMIT = 10 #20

app = Flask(__name__)
CORS(app)
app.secret_key = "supersekrit"
os.environ['steawmpowered_key'] = 'BBC837ECFD8415A58B7575D83BCDA639'
@app.errorhandler(404)
def not_found(e):
    rsp = Response(response=json.dumps({"ERROR": "404 NOT FOUND"}, default=str, indent=4), status=404,
                   content_type="application/json")
    return rsp

@app.errorhandler(500)
def messy_error(e):
    print(e)
    rsp = Response(json.dumps({"ERROR": "500 WEIRD SERVER ERROR"}, default=str, indent=4), status=500,
                   content_type="application/json")
    return rsp

# help function for pagination
def handle_links(url, offset, limit):
    if "?" not in url:
        url += "?offset=" + str(offset) + "&limit=" + str(limit)
    else:
        if "offset" not in url:
            url = url + "&offset=" + str(offset)
        if "limit" not in url:
            url = url + "&limit=" + str(limit)
    links = []
    nexturl = re.sub("offset=\d+", "offset=" + str(offset + limit), url)
    prevurl = re.sub("offset=\d+", "offset=" + str(max(0, offset - limit)), url)
    links.append({"rel": "self", "href": url})
    links.append({"rel": "next", "href": nexturl})
    links.append({"rel": "prev", "href": prevurl})
    return links

@app.route('/')
def hello_world():
    return '<u>Hello World!</u>'

@app.route('/comments', methods=['GET', 'POST'])
def get_comments():
    if request.method == 'GET':
        offset = int(request.args.get("offset", OFFSET))
        limit = int(request.args.get("limit", MAXLIMIT))
        if limit > MAXLIMIT:
            limit = MAXLIMIT
        query_parms = dict()
        arg_list = [i for i in request.args.keys()]
        for i in arg_list:
            if i.lower() != "offset" and i.lower() != "limit":
                query_parms[i] = request.args.get(i)
        data, exception_res = CommentResource.find_by_template(query_parms, limit, offset)
        links = handle_links(request.url, offset, limit)
        if data is not None:
            res = data
        else:
            res = data
        rsp = AppHTTPStatus().format_rsp(res, exception_res, method=request.method, path=request.path)
        return rsp
    elif request.method == 'POST':
        create_data = request.form
        if create_data:
            pass
        else:
            create_data = request.json
        res, exception_res = CommentResource.create(create_data)
        rsp = AppHTTPStatus().format_rsp(res, exception_res, method=request.method, path=request.path)
        return rsp
    else:
        res = Response()
        res.headers['Access-Control-Allow-Origin'] = '*'
        return res
    pass

@app.route('/comments/<comment_id>', methods=['GET', 'PUT', 'DELETE'])
def get_comment_by_comment_id(comment_id):
    if request.method == 'GET':
        template = {"comment_id": comment_id}
        res, exception_res = CommentResource.find_by_template(template, 1, 0)
        rsp = AppHTTPStatus().format_rsp(res, exception_res, method=request.method, path=request.path)
        return rsp
    elif request.method == 'PUT':
        content = request.form['content']
        select_data = {"comment_id": comment_id}
        update_data = {"content":content}
        res, exception_res = CommentResource.update(select_data, update_data)
        rsp = AppHTTPStatus().format_rsp(res, exception_res, method=request.method, path=request.path)
        return rsp
    elif request.method == 'DELETE':
        template = {"comment_id": comment_id}
        res, exception_res = CommentResource.delete(template)
        rsp = AppHTTPStatus().format_rsp(res, exception_res, method=request.method, path=request.path)
        return rsp

@app.route('/forums/<forum_id>/comments', methods=['GET'])
def get_comments_from_forum_id(forum_id):
    if request.method == 'GET':
        template = {"forum_id": forum_id}
        offset = int(request.args.get("offset", OFFSET))
        limit = int(request.args.get("limit", MAXLIMIT))
        if limit > MAXLIMIT:
            limit = MAXLIMIT
        res, exception_res = CommentResource.find_by_template(template, limit, offset)
        rsp = AppHTTPStatus().format_rsp(res, exception_res, method=request.method, path=request.path)
        return rsp

@app.route('/comments/<comment_id>/user', methods=['GET'])
def get_linked_user(comment_id):
    if request.method == 'GET':
        template = {"comment_id": comment_id}
        res = CommentResource.find_linked_user(template)
        rsp = Response(json.dumps(res, default=str), status=200, content_type="application/json")
        return rsp

if __name__ == '__main__':
    app.run(port=8080, debug=False, host="0.0.0.0")


import numpy as np
import glob
import cv2
import time

#긴급상황호출
gin=0
def gin(_gin=1) :
    global gin
    gin=_gin
    nomal(8)

def nomal(sc) :
    sc=sc+6
    global src
    src = glob.glob('./img/man*.jpg')
    print(src)
    # 만약에 그이상의 시간을 넣었을때 예외처리
    cnt = len(src)
    if sc > cnt:
        sc = cnt - 1
    print(sc)
    sinho(sc)

def teapong() :
    global src
    src = glob.glob('./img/wind*.jpg')
    sinho()

def ston() :
    global src
    src = glob.glob('./img/ston*.jpg')
    sinho()

def sinho(sc=1) :

    gin=0
    idx = sc

    while True:

        img = cv2.imread(src[idx])

        if img is None: # 이미지가 없는 경우
            print('이미지없음')
            break
        resize_img = cv2.resize(img, dsize=(0, 0), fx=0.12, fy=0.1, interpolation=cv2.INTER_AREA)
        height, width = img.shape[:2]
        # 가로 ,세로
        mat = np.float32([[1, 0, 0], [0, 1, 1]])
        tran = cv2.warpAffine(resize_img, mat, (width, height))

        cv2.namedWindow('tran', cv2.WND_PROP_FULLSCREEN)
        cv2.setWindowProperty('tran', cv2.WND_PROP_FULLSCREEN, cv2.WINDOW_FULLSCREEN)

        cv2.imshow("tran", tran)
        if cv2.waitKey(1000) >= 0: # 1초 동안 사진보여주는데 만약에 키보드 입력이 있으면 종료
            break

        # 사진을 다 보면 첫번째 사진으로 돌아감
        idx -= 1
        if idx < 0:
            idx = sc

        #긴급 신호를 받았을시에
        if gin==1 :
            if idx<=8 :
                idx+=5
                gin=0

    cv2.destroyAllWindows()

#test
# teapong()
# ston()
#teapong()

# def test() :
#     nomal(8)
#     time.sleep(3)
#     gin()
#
#
# test()


# Trend trading strategy using moving averages
import pyupbit
import datetime
import time
import json
import pprint

# 이동 평균선 계산
def calMvAvg(ticker, timIntv, cnt):
  df = pyupbit.get_ohlcv(ticker, timIntv, cnt+1)
  mvAvg = df['close'].rolling(cnt).mean().iloc[-2]
  return mvAvg

# 매수 조건
def condBuy(ticker, timIntv, mvAvg1, mvAvg2):
  calMvAvg1 = calMvAvg(ticker, timIntv, mvAvg1)
  calMvAvg2 = calMvAvg(ticker, timIntv, mvAvg2)
  if calMvAvg1 >= calMvAvg2:
    return True
  return False

# 매도 조건
def condSell(ticker, timIntv, mvAvg1, mvAvg2):
  calMvAvg1 = calMvAvg(ticker, timIntv, mvAvg1)
  calMvAvg2 = calMvAvg(ticker, timIntv, mvAvg2)
  if calMvAvg1 <= calMvAvg2:
    return True
  return False

# 시장가 매수 시 체결 위치 / 매도 1 호가
def getMkBuyPrice(ticker):
  return pyupbit.get_orderbook(ticker)[0]["orderbook_units"][0]["ask_price"]

# 시장가 매도 시 체결 위치 / 매수 1 호가
def getMkSellPrice(ticker):
  return pyupbit.get_orderbook(ticker)[0]["orderbook_units"][0]["bid_price"]

# 거래 출력
def printHistory(areaCase, txtArea, parts):
  if areaCase == "head":
    txtArea.delete('1.0', 'end')
    txtArea.insert('end', f"\n시작시간: {parts[0]}, 코인명: {parts[1]}, 현재가: {parts[2]}, 투자금: {parts[3]}\n")
    txtArea.insert('end', f"MA: {parts[4]}, 간격: {parts[5]}, 경과: {parts[6]}\n")
    txtArea.update()
    txtArea.see('end')
  elif areaCase == "body":
    txtArea.insert('end', f"\n거래시간: {parts[0]}, 상태: {parts[1]}, 가격: {parts[2]}, 총액: {parts[3]}\n")
    txtArea.insert('end', f"주문 번호: {parts[4]}\n")
    txtArea.update()
    txtArea.see('end')
  elif areaCase == "bottom":
    txtArea.insert('end', f"\n종료시간: {parts[0]}, 시작금액: {parts[1]}, 종료금액: {parts[2]}\n")
    txtArea.insert('end', f"수익금: {parts[3]}원, 수익비율: {parts[4]}\n")
    txtArea.update()
    txtArea.see('end')
  else:
      return
  return

# 트레이드 루프 시작 종료 flag 관리
global flag
flag = False
def checkFlag():
  return flag
def startTrading():
  global flag
  flag = True
  return
def stopTrading():
  global flag
  flag = False
  return

def main(upbit, ticker, timIntv, mvAvg, amount, txtHead, txtBody, txtBottom):

  # 모든 텍스트 입력 박스 초기화
  txtHead.delete('1.0', 'end')
  txtHead.update()
  txtBody.delete('1.0', 'end')
  txtBody.update()
  txtBottom.delete('1.0', 'end')
  txtBottom.update()

  holding = False  # 현재 코인 보유 여부
  operMode = False # 시작 동시 매수 방지
  startBalance = amount # 시작 잔고
  endBalance = startBalance # 끝 잔고
  tikrBalance = 0.0 # 코인 잔고
  buyPrice = 0.0 # 매수가
  sellPrice = 0.0 # 매도가
  curPrice = 0.0 # 현재가
  fee = 0.0005 # 수수료
  setSleep = 0.8 # 과트래픽 방지
  mvAvg1 = int(mvAvg[0]) # 기준 이동평균선
  mvAvg2 = int(mvAvg[1])

  basisTime = datetime.datetime.now()
  startTime = basisTime.strftime("%H:%M:%S")

  while flag is True:
    now = datetime.datetime.now().strftime("%H:%M:%S")

    # 시작 동시 매수 방지: Sell condition은 MA1 <= MA2 이고 이 상태에서는 매수되지 않음
    if condSell(ticker, timIntv, mvAvg1, mvAvg2) is True:
      operMode = True

    # 매도
    # 해당 코인을 가지고 있고, 매도 조건이 True일 때
    if holding is True and operMode is True:
      if condSell(ticker, timIntv, mvAvg1, mvAvg2) is True:
        tikrBalance = upbit.get_balance(ticker)
        resp = upbit.sell_market_order(ticker, tikrBalance)
        uuid = resp['uuid']
        sellPrice = getMkSellPrice(ticker)
        endBalance = round((tikrBalance * sellPrice) - (tikrBalance * sellPrice * fee), 1)
        holding = False
        state = "Sold"

        transaction = [now, state, sellPrice, endBalance, uuid]
        printHistory("body", txtBody, transaction)

        time.sleep(setSleep)

    # 매수
    # 해당 코인을 가지고 있지 않고, 매수 조건이 True일 때
    if holding is False and operMode is True:
      if condBuy(ticker, timIntv, mvAvg1, mvAvg2) is True:
        resp = upbit.buy_market_order(ticker, amount)
        uuid = resp['uuid']
        buyPrice = getMkBuyPrice(ticker)
        holding = True
        state = "Bought"

        transaction = [now, state, buyPrice, endBalance, uuid]
        printHistory("body", txtBody, transaction)

        time.sleep(setSleep)

    curPrice = pyupbit.get_current_price(ticker)
    elapsedTime = (datetime.datetime.now() - basisTime)

    status = [startTime, ticker, curPrice, startBalance, mvAvg, timIntv, elapsedTime]
    printHistory("head", txtHead, status)

    time.sleep(setSleep)

  # 거래 종료 시점 보유 중일 때 전량 매도
  if flag is False and holding is True:
    now = datetime.datetime.now().strftime("%H:%M:%S")

    tikrBalance = upbit.get_balance(ticker)
    resp = upbit.sell_market_order(ticker, tikrBalance)
    uuid = resp['uuid']
    sellPrice = getMkSellPrice(ticker)
    endBalance = round((tikrBalance * sellPrice) - (tikrBalance * sellPrice * fee), 1)
    state = "Sold"

    transaction = [now, state, sellPrice, endBalance, uuid]
    printHistory("body", txtBody, transaction)

  endTime = datetime.datetime.now().strftime("%H:%M:%S")

  settlement = [endTime, startBalance, endBalance, round(endBalance - startBalance, 1), round(endBalance / startBalance, 3)]
  printHistory("bottom", txtBottom, settlement)

  return


from random import randint
from kivy.uix.boxlayout import BoxLayout
from kivy.core.audio import SoundLoader
from kivy.app import App
from kivy.config import Config
from kivy.uix.image import Image

Config.set('graphics', 'width', '360')
Config.set('graphics', 'height', '800')
Config.set('graphics', 'resizable', 'false')

number_correct_answers = 0
number_question = 1

question_list = [['https://zamanilka.ru/wp-content/uploads/2022/05/smeshariki-krosh-kartinki-23.jpg', 'Крош', 'Кролик', 'Зайка', 'Мики', 1],
                 ['https://mykaleidoscope.ru/x/uploads/posts/2022-09/1663635911_1-mykaleidoscope-ru-p-zloi-kar-karich-krasivo-1.jpg',
                     'Ворон', 'Кар-карыч', 'Каркун', 'Карыч', 2],
                 ['https://i.ytimg.com/vi/lPVtHWVwtWE/maxresdefault.jpg',
                     'Ежик', 'Колючка', 'Ежидзе', 'Клим', 1],
                 ['https://100-bal.ru/pars_docs/refs/131/130143/130143_html_371854c8.jpg', 'Филинн',
                  'Совунья', 'Сова', 'Совушка', 2],
                 ['https://phonoteka.org/uploads/posts/2023-03/1679946865_phonoteka-org-p-smeshariki-nyusha-oboi-krasivo-31.jpg',
                  'Мила', 'Нюша', 'Пигги', 'Свинка', 2],
                 ['http://edge2.dtlab.ru/image/screenshot/PSTGMLT014623.jpg',
                  'Бараш', 'Барашка', 'Баран', 'Баранидзе', 1],
                 ['https://webmg.ru/wp-content/uploads/2022/11/i-22-53.jpeg',
                  'Оленеш', 'Лоскут', 'Лосяш', 'Лось', 3],
                 ['https://mykaleidoscope.ru/x/uploads/posts/2022-09/1663252506_5-mykaleidoscope-ru-p-zloi-kopatich-oboi-5.jpg',
                  'Мишка', 'Медведь', 'Мишка фреди', 'Копатыч', 4],
                 ['https://flomaster.club/uploads/posts/2023-01/1674059143_flomaster-club-p-pin-smeshariki-risunok-pinterest-14.jpg',
                  'Пин', 'Пингвин', 'Пингвиша', 'Лоло', 1],
                 ['https://m.media-amazon.com/images/M/MV5BZjA1MDhiYmEtODk0ZS00NjAyLTgwYmItMjA0Y2M3ZGE5ZTNiXkEyXkFqcGdeQXVyNTc0NjY1ODk@._V1_FMjpg_UX1000_.jpg', 'Панди',
                  'Панда', 'Мила', 'Степаша', 1],
                 ['https://i.ytimg.com/vi/kkvOCjjzHp4/maxresdefault.jpg',
                  'Боба', 'Руби', 'Биба', 'Биби', 4],
                 ['https://avatars.dzeninfra.ru/get-zen_doc/5234097/pub_60f16a93a7eab75c27c7d17f_60f17de2cf9db26dda648fa1/scale_1200',
                  'Мышарик', 'Мышь', 'Крыс', 'Микки', 1],
                 ['https://i.ytimg.com/vi/WNzGSfqO5nE/maxresdefault.jpg',
                  'Железяка', 'Милли', 'Железная няня', 'Няня', 3],
                 ['https://vignette.wikia.nocookie.net/shararam-smeshi/images/a/a5/%D0%A5%D1%80%D1%83%D0%BC.png/revision/latest?cb=20180825013800&path-prefix=ru', 'Дракоша', 'Антошка', 'Хрум', 'Тимоша', 3],
                 ['https://sun9-66.userapi.com/impf/c626722/v626722802/162c7/3BqL9rlJZ5E.jpg?size=604x604&quality=96&sign=84cf662044a22014c6afa4f6d908a69a&type=album', 'Лягушка', 'Жаба', 'Лягуш', 'Шиша', 1]]

class MainWidget(BoxLayout):
    def new_question(self):
        img_result = None  # Initialize img_result to None

        if len(question_list) > 0:
            question = question_list[randint(0, len(question_list)-1)]
            self.ids['img_question'].source = question[0]
            self.ids['btn_answer1'].text = question[1]
            self.ids['btn_answer2'].text = question[2]
            self.ids['btn_answer3'].text = question[3]
            self.ids['btn_answer4'].text = question[4]
        else:
            text = '\n Результат: ' + str(number_correct_answers) + ' из 15.'
            self.ids['lbl_question'].text = text
            self.remove_widget(self.ids['img_question'])
            self.remove_widget(self.ids['layout_btns'])
            if number_correct_answers >= 12:
                text1 = 'Вы - Стильный бараш'
                self.ids['lbl_result'].text = text1
                self.ids['img_result'].source = 'source/Barash.jpg'
            elif number_correct_answers >= 9:
                text1 = 'Вы - Лосяш'
                self.ids['lbl_result'].text = text1
                self.ids['img_result'].source = 'source/Losyash.jpg'

            elif number_correct_answers >= 6:
                text1 = 'Вы - Ежидзе'
                self.ids['lbl_result'].text = text1
                self.ids['img_result'].source = 'source/Ezhidze.jpg'

            elif number_correct_answers >= 3:
                text1 = 'Вы - Крош'
                self.ids['lbl_result'].text = text1
                self.ids['img_result'].source = 'source/Krosh.jpg'

            elif number_correct_answers >= 0:
                text1 = 'Вы - Бунтующий ежик'
                self.ids['lbl_result'].text = text1
                self.ids['img_result'].source = 'source/Bunt.jpg'

            self.ids['img_result'].opacity = 1

    def btn_pressed(self, number_button):
        question: list
        for i in question_list:
            if (i[0] == self.ids['img_question'].source):
                question = i
                break  # Перебор списка, чтобы выбрать вопрос который выпал из списка
        global number_correct_answers
        global number_question
        if (number_button == question[5]):
            number_correct_answers += 1

        number_question += 1
        question_list.remove(question)
        self.new_question()

class MainApp(App):
    def build(self):
        app = MainWidget()
        app.new_question()  # вызываем new_question из build метода, когда окно приложения готово
        return app

if __name__ == '__main__':
    music = SoundLoader.load('Disco.mp3')
    music.volume = 0.1
    music.play()

    MainApp().run()


import logging
from persistence import get_cursor
from models import Operators

LOGGER = logging.getLogger(__name__)

def update_balance(change: float, username: str, operator: Operators):
    with get_cursor() as cursor:
        LOGGER.debug('Requested to change balance of %s by %s', username, change)
        if operator == Operators.INCREMENT:
            LOGGER.debug('Incrementing balance')
            query = "UPDATE users SET balance = balance + %s WHERE username = %s;"
        else:
            LOGGER.debug('Decrementing balance for %s by %s', username, change)
            query = "UPDATE users SET balance = balance - %s WHERE username = %s;"
        cursor.execute(query,
            (
                change,
                username
            )
        )

def update_shares_owned(change: int, username: str, symbol: str, operator: Operators):
    if operator == Operators.INCREMENT:
        LOGGER.debug('Allocating %s shares to %s for symbol %s', change, username, symbol)
        query = """INSERT INTO shares_owned_by_user (company_symbol, username, shares_owned)
                           VALUES(%s, %s, %s)
                           ON CONFLICT (company_symbol, username)
                           DO UPDATE
                           SET shares_owned = shares_owned_by_user.shares_owned + EXCLUDED.shares_owned"""
    else:
        LOGGER.debug('Deallocating %s shares to %s for symbol %s', change, username, symbol)
        query = """INSERT INTO shares_owned_by_user (company_symbol, username, shares_owned)
                    VALUES(%s, %s, %s)
                    ON CONFLICT (company_symbol, username)
                    DO UPDATE
                    SET shares_owned = shares_owned_by_user.shares_owned - EXCLUDED.shares_owned"""
    with get_cursor() as cursor:
        cursor.execute(query, (symbol, username, change))

def get_shares_portfolio(username: str):
    with get_cursor() as cursor:
        LOGGER.debug('Getting shares owned for %s', username)
        query = """SELECT company_symbol, shares_owned FROM shares_owned_by_user
                WHERE username = %s
                ORDER BY company_symbol;"""
        cursor.execute(query, (username,))
        return cursor.fetchall()


import torch
import torch.nn.functional as F

from ..builder import LOSSES
from .base import BaseWeightedLoss

@LOSSES.register_module()
class CrossEntropyLoss(BaseWeightedLoss):
    """Cross Entropy Loss.

    Support two kinds of labels and their corresponding loss type. It's worth
    mentioning that loss type will be detected by the shape of ``cls_score``
    and ``label``.
    1) Hard label: This label is an integer array and all of the elements are
        in the range [0, num_classes - 1]. This label's shape should be
        ``cls_score``'s shape with the `num_classes` dimension removed.
    2) Soft label(probablity distribution over classes): This label is a
        probability distribution and all of the elements are in the range
        [0, 1]. This label's shape must be the same as ``cls_score``. For now,
        only 2-dim soft label is supported.

    Args:
        loss_weight (float): Factor scalar multiplied on the loss.
            Default: 1.0.
        class_weight (list[float] | None): Loss weight for each class. If set
            as None, use the same weight 1 for all classes. Only applies
            to CrossEntropyLoss and BCELossWithLogits (should not be set when
            using other losses). Default: None.
    """

    def __init__(self, loss_weight=1.0, class_weight=None):
        super().__init__(loss_weight=loss_weight)
        self.class_weight = None
        if class_weight is not None:
            self.class_weight = torch.Tensor(class_weight)

    def _forward(self, cls_score, label, **kwargs):
        """Forward function.

        Args:
            cls_score (torch.Tensor): The class score.
            label (torch.Tensor): The ground truth label.
            kwargs: Any keyword argument to be used to calculate
                CrossEntropy loss.

        Returns:
            torch.Tensor: The returned CrossEntropy loss.
        """
        if cls_score.size() == label.size():
            # calculate loss for soft label

            assert cls_score.dim() == 2, 'Only support 2-dim soft label'
            assert len(kwargs) == 0, \
                ('For now, no extra args are supported for soft label, '
                 f'but get {kwargs}')

            lsm = F.log_softmax(cls_score, 1)
            if self.class_weight is not None:
                self.class_weight = self.class_weight.to(cls_score.device)
                lsm = lsm * self.class_weight.unsqueeze(0)
            loss_cls = -(label * lsm).sum(1)

            # default reduction 'mean'
            if self.class_weight is not None:
                # Use weighted average as pytorch CrossEntropyLoss does.
                # For more information, please visit https://pytorch.org/docs/stable/generated/torch.nn.CrossEntropyLoss.html # noqa
                loss_cls = loss_cls.sum() / torch.sum(
                    self.class_weight.unsqueeze(0) * label)
            else:
                loss_cls = loss_cls.mean()
        else:
            # calculate loss for hard label

            if self.class_weight is not None:
                assert 'weight' not in kwargs, \
                    "The key 'weight' already exists."
                kwargs['weight'] = self.class_weight.to(cls_score.device)
            loss_cls = F.cross_entropy(cls_score, label, **kwargs)

        return loss_cls

@LOSSES.register_module()
class BCELossWithLogits(BaseWeightedLoss):
    """Binary Cross Entropy Loss with logits.

    Args:
        loss_weight (float): Factor scalar multiplied on the loss.
            Default: 1.0.
        class_weight (list[float] | None): Loss weight for each class. If set
            as None, use the same weight 1 for all classes. Only applies
            to CrossEntropyLoss and BCELossWithLogits (should not be set when
            using other losses). Default: None.
    """

    def __init__(self, loss_weight=1.0, class_weight=None):
        super().__init__(loss_weight=loss_weight)
        self.class_weight = None
        if class_weight is not None:
            self.class_weight = torch.Tensor(class_weight)

    def _forward(self, cls_score, label, **kwargs):
        """Forward function.

        Args:
            cls_score (torch.Tensor): The class score.
            label (torch.Tensor): The ground truth label.
            kwargs: Any keyword argument to be used to calculate
                bce loss with logits.

        Returns:
            torch.Tensor: The returned bce loss with logits.
        """
        if self.class_weight is not None:
            assert 'weight' not in kwargs, "The key 'weight' already exists."
            kwargs['weight'] = self.class_weight.to(cls_score.device)
        loss_cls = F.binary_cross_entropy_with_logits(cls_score, label,
                                                      **kwargs)
        return loss_cls


from dataclasses import dataclass
from trystructure_namespace_clusters.org.oasis_open.docs.ns.emix.pkg_2011.pkg_06.power.energy_item_type import EnergyItemType

__NAMESPACE__ = "http://docs.oasis-open.org/ns/emix/2011/06/power"

@dataclass
class EnergyItem(EnergyItemType):
    class Meta:
        name = "energyItem"
        namespace = "http://docs.oasis-open.org/ns/emix/2011/06/power"