from django.shortcuts import render, HttpResponse, Http404
from django.core.exceptions import ObjectDoesNotExist
from MainApp.models import Item

user = {
    "name": "Иван",
    "middle": "Петрович",
    "surname": "Сидоров",
}

#items = [
#    {"id": 1, "name": "Кросовки abibas", "quantity":5},
#    {"id": 2, "name": "Куртка кожанная", "quantity":2},
#    {"id": 3, "name": "Coca-cola 1 литр", "quantity":12},
#    {"id": 4, "name": "Картофель фри", "quantity":0},
#    {"id": 5, "name": "Кепка", "quantity":124},
#]

def main(request):
    return render(request, 'index.html')

def about(request):
    text = f"""
    Имя: {user['name']}

    Отчество: {user['middle']}

    Фамилия: {user['surname']}

    """
    return HttpResponse(text)

def show_item(request, id):
    try:
        current_item = Item.objects.get(id=id)
    except ObjectDoesNotExist:
        raise Http404
    context = {
        "item": current_item
    }
    return render(request, "item.html", context)

def items_list(request):
    items = Item.objects.all()
    context = {
        "items": items
    }
    return render(request, "items.html", context)


import requests

# if __name__ == '__main__':
    # url = "https://f.video.weibocdn.com/001xiVoKgx07M0ORpxH9010412004zkY0E010.mp4?label=mp4_ld&trans_finger=81b11b8c5ffb62d33ceb3244bdd17e7b&ori=0&ps=1BVp4ysnknHVZu&Expires=1619085090&ssig=%2BrGCkMHNS1&KID=unistore,video"
    # r = requests.get(url, stream=True)
    # with open('name.mp4', "wb") as mp4:
    #     for chunk in r.iter_content(chunk_size=1024 * 1024):
    #         if chunk:
    #             mp4.write(chunk)

def download_file(url):
 local_filename = url.split('/')[-1]

 r = requests.get(url, stream=True)
 with open("/Users/cy/Downloads/video_sipder/"+local_filename, 'wb') as f:
    for chunk in r.iter_content(chunk_size=1024):
        if chunk:
            f.write(chunk)

if __name__ == '__main__':
    earl = "https://www.youtube.com/watch?v=DBYjZTdrJlA"
    download_file(earl)


# Written by Gerd Mund & Robert Logiewa

from flask import (
    render_template,
    abort,
    current_app as app,
    json,
    make_response,
)
from app.main import bp
from app.api import handlers
import pandas as pd
from app.models import (
    DataSet,
    DataSetSchema,
    FusionHypothesis,
    FusionHypothesisSchema,
    BirdMapping,
    BirdMappingSchema,
)
from app.main import plotFunction as plt
import time

@bp.route("/")
@bp.route("/index")
def index():
    """ Index page, contains general results """
    app.logger.info("Opening index page")

    app.logger.debug("Retrieving data from birds api endpoint")
    response = handlers.getBirds()

    # Make sure the api returned actual results
    if not response or response.status_code != 200 or not response.is_json:
        app.logger.warning("birds api endpoint failed")
        abort(500)

    app.logger.debug("Retrieve bird mappings from database")
    mq = BirdMapping.query.all()
    ms = BirdMappingSchema(many=True)
    m = ms.dump(mq).data

    app.logger.debug("Combine mappings and data")
    mapping = pd.DataFrame(m).set_index("id")
    amounts = pd.DataFrame(response.get_json(), index=["weight"]).T
    data = pd.concat([amounts, mapping], axis=1, sort=True).fillna(0).T

    app.logger.debug("Finalize data for index page")
    birds_data = json.loads(data.to_json(orient="columns"))
    return (render_template("index.html", birdsData=birds_data), 200)

@bp.route("birds/<string:id>")
def bird_details(id):
    """ Shows the details of a particular bird """

    # Get bird details from api
    response = handlers.getBirdDetails(id)
    if not response or response.status_code != 200 or not response.is_json:
        abort(500)

    details = response.get_json()
    sd = pd.Series(details)

    # Filter the data based on the bird id
    m = BirdMapping.query.filter_by(id=id).first()
    ms = BirdMappingSchema()
    mo = ms.dump(m).data
    sm = pd.Series(mo)

    # Finalize data for details page
    df = pd.concat([sd, sm], axis=0)
    j = json.loads(df.to_json(orient="columns"))

    plt.calcPlot(j)
    return render_template("details.html", bird=j, title=j["name"])

@bp.route("dump_database", methods=["GET"])
def dumpDatabase():
    """Dumps the entirety of the database to a json file that is then downloaded"""

    # Query all bird mappings
    m = BirdMapping.query.all()
    ms = BirdMappingSchema(many=True)
    mo = ms.dump(m).data

    # Query all bird data
    d = DataSet.query.all()
    ds = DataSetSchema(many=True)
    do = ds.dump(d).data

    # Query all hypotheses
    h = FusionHypothesis.query.all()
    dh = FusionHypothesisSchema(many=True)
    ho = dh.dump(h).data

    # Make output object
    content = {"raw_data": do, "hypotheses": ho,  "mappings": mo}

    # Convert to json and then present file to user
    response = make_response(json.dumps(content))
    response.mimetype = "application/json"
    response.headers[
        "Content-Disposition"
    ] = "attachment;filename=database.json"
    return response


from typing import List

class Solution:
    def findAnagrams(self, s: str, p: str) -> List[int]:
        if p == s: return [0]
        word = dict()
        substrings = dict()
        res = list()
        for c in p:
            word[c] = word.get(c, 0) + 1
        for i in range(len(s)):
            c = s[i]
            substrings[c] = substrings.get(c, 0) + 1
            if i >= len(p):
                c = s[i - len(p)]
                n = substrings.pop(c) - 1
                if n > 0: substrings[c] = n
            if word == substrings:
                res.append(i - len(p) + 1)
        return res


import numpy as np
from sklearn import datasets
from sklearn.model_selection import train_test_split
import matplotlib.pyplot as plt

class LinearRegression:
    def __init__(self, lr=0.001, n_iter=1000):
        """ Linear regression implementation

        Parameters
        ----------
        n_iter : int, optional (default=1000)
            The number of iterations to perform gradient descent.

        lr : float, optional (default=0.001)
            Determines the step size at each iteration while moving toward a minimum.

        """
        self.lr = lr
        self.n_iter = n_iter

    def fit(self, X_train, y_train):
        X_train, y_train = X_train.reshape([-1, X_train.shape[-1]]), y_train.reshape([-1])
        n_samples, n_features = X_train.shape[-2], X_train.shape[-1]
        self.weights = np.random.normal(0, 1, (n_features,))
        self.bias = np.random.normal(0, 1, (1,))

        for _ in range(self.n_iter):
            dw = X_train.T @ (2 * (X_train @ self.weights + self.bias - y_train)) / n_samples
            db = np.sum(2 * (X_train @ self.weights + self.bias - y_train)) / n_samples
            self.weights -= self.lr * dw
            self.bias -= self.lr * db

    def predict(self, X_test):
        return X_test @ self.weights + self.bias

    def evaluate(self, X_test, y_test, mode='mae'):
        if mode == 'mae':
            beta = 1
        elif mode == 'mse':
            beta = 2
        else:
            beta = 1
        beta = 1 if mode == 'mae' else 2
        return np.sum(np.abs(self.predict(X_test) - y_test) ** beta) / y_test.size

if __name__ == '__main__':
    X, y = datasets.make_regression(n_samples=100, n_features=1, noise=20, random_state=1234)
    X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=1234)
    mean, std = np.mean(X_train, -2), np.std(X_train, -2)
    X_train = (X_train - mean) / (std + 0.00001)
    X_test = (X_test - mean) / (std + 0.00001)
    clf = LinearRegression()
    clf.fit(X_train, y_train)
    mae = clf.evaluate(X_test, y_test)
    print(f"MAE = {mae:.4f}")
    cmap = plt.get_cmap("viridis")
    fig = plt.figure(figsize=(8, 6))
    plt.scatter(X_train, y_train, color=cmap(0.9), s=10)
    plt.scatter(X_test, y_test, color=cmap(0.5), s=10)
    plt.plot(X_test, clf.predict(X_test), color="black", linewidth=2, label="Prediction")
    plt.show()


"""Find the sum of the length of all recordings for each songwriter along with the songwriter's id."""

from pymongo import MongoClient
import sys
from pprint import pprint

client = MongoClient('localhost', int(sys.argv[1]))
db = client["A4dbEmbed"]
songwriters_recordings = db["SongwritersRecordings"]

aggregate = songwriters_recordings.aggregate([{"$unwind" : "$recordings"},
                                              {"$group" : {"_id": "$songwriter_id",
                                                           "total_length" : {"$sum" : "$recordings.length"},
                                                           "songwriter_id" : {"$first" : "$songwriter_id"}}}])

for entry in aggregate:
    pprint(entry)


# views.py
import pandas as pd
import matplotlib.pyplot as plt
from io import BytesIO
import base64
from datetime import datetime
from django.http import HttpResponse
from django.shortcuts import render
from .models import BMSSENSOR
from .models import CsvOrExcelUpload
from .forms import FuturePredictionForm
from django.contrib.auth.decorators import login_required

@login_required
def energy_prediction(request, pk):
    user = request.user
    file_data = CsvOrExcelUpload.objects.get(id=pk, user=user)

    if request.method == 'POST':
        form = FuturePredictionForm(request.POST)
        if form.is_valid():
            future_date = form.cleaned_data['future_date']
            future_time = form.cleaned_data['future_time']
            future_datetime = datetime.combine(future_date, future_time)
            if file_data.upload_file.name.endswith(('.csv', '.xls', '.xlsx')):
                df = pd.read_csv(file_data.upload_file) if file_data.upload_file.name.endswith('.csv') else pd.read_excel(file_data.upload_file)

                # Calculate COST_PER_UNIT_LIGHTING, COST_PER_UNIT_HVAC, and COST_PER_UNIT_POWER_OUTLET
                cost_per_unit_lighting = calculate_cost_per_unit(df, 'General Lighting(W/m2)')
                cost_per_unit_hvac = calculate_cost_per_unit(df, 'Cooling (Electricity)(W/m2)')
                cost_per_unit_power_outlet = calculate_cost_per_unit(df, 'System Pumps(W/m2)')

                # Connect to the BMS to get power distribution information
                df = connect_to_bms(df, request, file_data)

                # Create a prediction DataFrame for the selected future date and time
                future_prediction_df = create_future_prediction_df(df, future_datetime)

                # Apply AI-based energy optimization logic to the future prediction data
                optimized_future_df = optimize_energy_ai(future_prediction_df.copy(), cost_per_unit_lighting, cost_per_unit_hvac, cost_per_unit_power_outlet)

                # Calculate energy cost for each zone in the future
                optimized_future_df = calculate_energy_cost(optimized_future_df)

                # Calculate total cost for all zones in the future
                total_future_cost = optimized_future_df[['zone1_cost', 'zone2_cost']].sum().sum()  # Adjust column names if needed

                # Create a line plot to visualize the future prediction
                plt.plot(future_prediction_df['Date/Time'], optimized_future_df['General Lighting(W/m2)'], label='Optimized Lighting(W/m2)')
                plt.xlabel('Date/Time')
                plt.ylabel('Energy Consumption(W/m2)')
                plt.title('Future Energy Cost Prediction')
                plt.xticks(rotation=45)
                plt.legend()

                # Save the plot to a BytesIO object
                img = BytesIO()
                plt.savefig(img, format='png')
                img.seek(0)
                img_base64 = base64.b64encode(img.read()).decode()

                # Pass the plot as a base64-encoded image to the template
                context = {
                    'form': form,
                    'plot_image': f'data:image/png;base64,{img_base64}',
                    'data_table': optimized_future_df.to_html(classes='table table-striped table-bordered table-hover'),
                    'total_cost': total_future_cost
                }

                return render(request, 'energy_prediction.html', context)
    else:
        form = FuturePredictionForm()

    context = {'form': form}
    return render(request, 'energy_prediction.html', context)

def calculate_cost_per_unit(data, column_name):
    # Calculate cost per unit for a given column (e.g., 'General Lighting(W/m2)')
    total_cost = data[column_name].sum()
    total_energy = data[column_name].count()
    return total_cost / total_energy if total_energy != 0 else 0.0

def connect_to_bms(data, request, file_data):
    # Placeholder logic to simulate BMS sensor data retrieval
    # You should replace this with actual data retrieval logic
    try:
        # Get BMS sensor data based on user and timestamp (placeholder)
        bms_sensor = BMSSENSOR.objects.get(user=request.user, timestamp=file_data.timestamp)
        bms_data = {
            'energy_level': bms_sensor.energy_level,
            'temperature': bms_sensor.temperature
        }

        # Update office_data with BMS data (placeholder)
        data.update(bms_data)

    except BMSSENSOR.DoesNotExist:
        # Handle the case where the BMSSENSOR with the specified user and timestamp does not exist
        print("BMSSENSOR does not exist.")
    except Exception as e:
        # Handle any other unexpected exceptions
        print(f"An error occurred: {str(e)}")

    return data

def optimize_energy_ai(data, cost_per_unit_lighting, cost_per_unit_hvac, cost_per_unit_power_outlet):
    # Placeholder logic for energy optimization (adjust as needed)
    for zone, columns in zone_data.items():
        occupancy_column = columns['occupancy_column']
        lighting_column = columns['lighting_column']
        power_outlet_column = columns['power_outlet_column']
        hvac_column = columns['hvac_column']

        data = adjust_lighting(data, occupancy_column, lighting_column, cost_per_unit_lighting)
        data = adjust_hvac(data, hvac_column, cost_per_unit_hvac)
        data = adjust_power_outlets(data, occupancy_column, power_outlet_column, cost_per_unit_power_outlet)

    return data

def calculate_energy_cost(data):
    # Placeholder logic to calculate energy cost for each zone (adjust as needed)
    for zone, columns in zone_data.items():
        lighting_column = columns['lighting_column']
        power_outlet_column = columns['power_outlet_column']
        hvac_column = columns['hvac_column']

        data[f'{zone}_cost'] = (
            data[lighting_column] +
            data[power_outlet_column] +
            data[hvac_column]
        )

    return data

# Define your 'zone_data' as you did before

# Define your BMSSENSOR model

# Define other helper functions (e.g., adjust_lighting, adjust_hvac, adjust_power_outlets) as you did before


import argparse
import ast
import logging
logging.getLogger().setLevel(logging.DEBUG)
import io
import os
import pdb
import pprint
import pydoc
import random
import re
import signal
import subprocess
import sys
from time import tzset
import traceback

REAL_PRINT = print
REAL_PPRINT = pprint.pprint

class Flags(object):
    def __init__(self):
        self.parser = argparse.ArgumentParser(
            description='Rewrites a Markdown file by running the Python '
                        'code embedded within it.')
        self.parser.add_argument(
            '--overwrite',
            action='store_true',
            default=False,
            help='Overwrite the file in-place if its contents ran '
                 'successfully.')
        self.parser.add_argument(
            '--timeout_seconds',
            action='store',
            default=5,
            type=float,
            help='Kill the process with an error if the Python code in the '
                 'Markdown file hasn\'t finished executing in this time.')
        self.parser.add_argument(
            'path',
            action='store',
            default=None,
            nargs='+',
            help='Paths to the Markdown files to process.')
        self.parser.add_argument(
            '--root_dir',
            action='store',
            default='.',
            type=str,
            help='Path to the root directory of the book. Used to resolve '
                 '"python-include:" blocks in the source Markdown.')

    def parse(self):
        self.parser.parse_args(namespace=self)

FLAGS = Flags()

class MarkdownExecError(Exception):
    pass

class WrappedException(Exception):
    def __init__(self, output=None):
        super().__init__()
        self.output = output

def exec_source(path, source, context, raise_exceptions=False):
    output = io.StringIO()
    logging_handler = logging.StreamHandler(stream=output)

    def my_print(*args, **kwargs):
        kwargs['file'] = output
        REAL_PRINT(*args, **kwargs)

    def my_pprint(*args, **kwargs):
        kwargs['stream'] = output
        kwargs['width'] = 65  # Max width of monospace code lines in Word
        REAL_PPRINT(*args, **kwargs)

    def my_debug(*args, **kwargs):
        kwargs['file'] = sys.stderr
        REAL_PRINT(*args, **kwargs)

    def my_help(*args, **kwargs):
        helper = pydoc.Helper(output=output)
        return helper(*args, **kwargs)

    def my_pdb():
        # Clear any alarm clocks since we're going interactive.
        signal.alarm(0)

        p = pdb.Pdb(stdin=sys.stdin, stdout=sys.stderr)
        p.use_rawinput = True
        return p

    context['print'] = my_print
    context['pprint'] = my_pprint
    context['debug'] = my_debug
    context['help'] = my_help
    context['Pdb'] = my_pdb
    context['STDOUT'] = output
    logging.getLogger().addHandler(logging_handler)
    try:
        node = ast.parse(source, path)
        code = compile(node, path, 'exec')
        exec(code, context, context)
    except Exception as e:
        # Restore the print functions for other code in this module.
        context['print'] = REAL_PRINT

        # If there's been any output so far, print it out before the
        # error traceback is printed so we have context for debugging.
        output_so_far = output.getvalue()
        if output_so_far:
            print(output_so_far, end='', file=sys.stderr)

        if raise_exceptions:
            raise WrappedException(output_so_far) from e

        format_list = traceback.format_exception(*sys.exc_info())

        # Only show tracebacks for code that was in the target source file.
        # This way calls to parse(), compile(), and exec() are removed.
        cutoff = 0
        for cutoff, line in enumerate(format_list):
            if path in line:
                break

        formatted = ''.join(format_list[cutoff:])
        print('Traceback (most recent call last):', file=sys.stderr)
        print(formatted, end='', file=sys.stderr)
        raise MarkdownExecError(e)
    finally:
        # Restore the print function for other code in this module.
        context['print'] = REAL_PRINT
        # Disable the logging handler.
        logging.getLogger().removeHandler(logging_handler)

    return output.getvalue()

def exec_python2(source):
    source_bytes = source.encode('utf-8')
    output = subprocess.check_output(['python2.7'], input=source_bytes)
    return output.decode('utf-8')

def exec_syntax_error(source):
    source_bytes = source.encode('utf-8')
    child = subprocess.Popen(
        ['python3'],
        stdin=subprocess.PIPE,
        stderr=subprocess.PIPE)
    _, output = child.communicate(input=source_bytes)
    output = output.decode('utf-8')
    # Only return the last line. The preceeding lines will be parts of
    # the stack trace that caused the syntax error.
    return output.strip('\n').split('\n')[-1]

filename_re = re.compile('[\\.]{0,2}/[-a-zA-Z0-9_/]+(\\.py|\\.md)')

def exec_exception(path, source, context):
    try:
        exec_source(path, source, context,
                    raise_exceptions=True)
    except WrappedException as e:
        original = e.__context__
        pretty_exception = str(original)
        pretty_exception = filename_re.sub('my_code.py', pretty_exception)
        exception_line = '%s: %s' % (
            original.__class__.__name__, pretty_exception)

        if e.output:
            return '%sTraceback ...\n%s' % (
                e.output, exception_line)
        else:
            return exception_line
    else:
        assert False, 'Exception not raised'

def iterate_blocks(path, text):
    text_start = 0
    source_start = None
    block_suffix = ''
    pending_source = ''
    pending_output = ''

    # Import the __main__ module, which is this file, and run all of the
    # Markdown code as if it's part of that module. This enables modules
    # like pickle to work, which need to use import paths relative to named
    # modules in order to serialized/deserialize functions.
    import __main__
    context = __main__.__dict__

    for blocks_seen, match in enumerate(re.finditer('```', text)):
        start = match.start()
        end = match.end()

        if blocks_seen % 2 == 0:
            # Figure out the language of the opening block.
            suffix_end = text.find('\n', end)
            if suffix_end > 0:
                block_suffix = text[end:suffix_end].lower()
                source_start = end + len(block_suffix)

            # All text until the block start
            yield text[text_start:start]
        else:
            text_start = end
            source = text[source_start:start]

            # Closing block
            if block_suffix in ('python', 'python-exception'):
                # Run any pending source immediately if this is a Python
                # exception block to ensure we re-raise unexpected exceptions
                # back up instead of just printing them into output blocks.
                if block_suffix == 'python-exception':
                    if pending_source:
                        pending_output += exec_source(
                            path, pending_source, context)
                        pending_source = ''

                # Add a bunch of blank lines before the source to get the line
                # numbers to match up during ast.parse. The ast.increment_lineno
                # helper is useful, but only after parsing was successful. If
                # you want helpful error messages during parsing you need to
                # fake it.
                line_offset = text[:source_start].count('\n')
                delta_offset = line_offset - pending_source.count('\n')
                pending_source += '\n' * delta_offset

                # Accumulate all the source code to run until we reach the
                # first output block *or* the end of the file.
                pending_source += source

                yield '```%s' % block_suffix
                yield source
                yield '```'

                if block_suffix == 'python-exception':
                    pending_output += exec_exception(
                        path, pending_source, context)
                    pending_source = ''
            elif block_suffix == 'python2':
                yield '```python2'
                if not source.startswith('\n# Python 2'):
                    yield '\n# Python 2'
                yield source
                yield '```'

                pending_output += exec_python2(source)
            elif block_suffix == 'python-syntax-error':
                yield '```python-syntax-error'
                yield source
                yield '```'

                pending_output += exec_syntax_error(source)
            elif block_suffix.startswith('python-include:'):
                include_path = block_suffix[len('python-include:'):]
                file_basename = os.path.basename(include_path)
                full_path = os.path.join(FLAGS.root_dir, include_path)
                data = open(full_path, 'r').read()

                yield '```%s\n' % block_suffix
                yield '# %s\n' % file_basename
                yield data.strip()
                yield '\n```'
            else:
                if pending_source:
                    pending_output += exec_source(
                        path, pending_source, context)
                    pending_source = ''
                elif not pending_output:
                    # This handles random output blocks in the Markdown file
                    # that do not follow Python blocks. This is helpful when
                    # you're sketching out a rough draft. Just pass through
                    # whatever the field was before.
                    pending_output = source

                # Output block
                yield '```%s\n' % block_suffix
                yield pending_output.strip('\n')
                yield '\n```'

                pending_output = ''

    # What follows the very last block to the end of the file
    yield text[text_start:]

    # Run any remaining pending code to make sure it doesn't have errors.
    if pending_source:
        exec_source(path, pending_source, context)

def print_iter(it, path, overwrite):
    output = sys.stdout
    if overwrite:
        output = io.StringIO()
    try:
        for text in it:
            print(text, end='', file=output)
    except:
        raise
    else:
        if overwrite:
            open(path, 'w', encoding='utf-8').write(output.getvalue())

def main():
    FLAGS.parse()

    for path in FLAGS.path:
        # Always use the same seed so multiple runs of the same Markdown files
        # will always produce the same output results.
        random.seed(1234)

        # Always pretend that we're running in Pacific Time
        os.environ['TZ'] = 'US/Pacific'
        tzset()

        # Kill the process if it's been running for longer than the timeout.
        signal.alarm(FLAGS.timeout_seconds)

        text = open(path, encoding='utf-8').read()
        it = iterate_blocks(path, text)
        try:
            print_iter(it, path, FLAGS.overwrite)
        except MarkdownExecError:
            return 1

    return 0

if __name__ == '__main__':
    sys.exit(main())


from flask import Flask, render_template, redirect, session, request
from user_model import User

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

@app.route("/")
def root():
    return redirect("/users")

@app.route("/users")
def users():
    all_users = User.all_users() # returns a list of user objects
    return render_template("all.html", all_users = all_users)

@app.route("/users/new")
def users_new():
    return render_template("create.html")

@app.route("/user/create", methods=["post"])
def user_create():
    print(request.form)
    user_id = User.add_user(request.form)
    return redirect("/users")

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


def speak(text):
    import win32com.client as wincl
    speak1 = wincl.Dispatch("SAPI.SpVoice")
    if int(text) > 5:
        speak1.speak("Bester Mann hat ")
        speak1.speak(text)
        speak1.speak("eingetragen. ")
    else:
        speak1.speak("Zu wenige... SO EINEN HALS!!!! ")

def suche():
    import pyautogui
    import datetime
    import time
    from selenium.webdriver.chrome.options import Options
    from selenium.common.exceptions import NoSuchElementException
    from selenium.webdriver.common.keys import Keys
    from selenium.webdriver.support import expected_conditions as EC
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support.ui import WebDriverWait
    from selenium import webdriver
    from pymongo import MongoClient
    chrome_options = Options()

    # chrome_options.add_argument("--headless")
    driver = webdriver.Chrome(chrome_options=chrome_options)  # docker port anstatt lokal
    client = MongoClient("192.168.100.5", 27017, maxPoolSize=None)
    db2 = client['gemeinden']
    collection = db2['gemeinden']
    while True:
        cursor = collection.find({'telefon': {"$exists": False}, "zahl":"1"}, no_cursor_timeout=True, limit=1)
        a = collection.count({'telefon': {"$exists": False}, "zahl":"1"})
        print(a)
        for i in cursor:
            suchbegriff = i['Anschrift der Gemeinde']
            suchbegriff2 = i['Ort']
            suchbegriff3 = suchbegriff + " " + suchbegriff2 + " telefon"

            google_url = "https://www.google.de/search?q="
            google_url = google_url + suchbegriff3 + " impressum"
            driver.get(google_url)
            driver.switch_to.window(driver.window_handles[-1])
            print(suchbegriff, suchbegriff2)
            nummer = input("Bitte nummer eingeben\n\n")

            nummer = nummer.strip()
            if nummer:
                collection.update_many({'Anschrift der Gemeinde': i['Anschrift der Gemeinde'], 'Ort': i['Ort']}, {"$set": {'telefon': nummer}})
                ausgefuellt = a - collection.count({'telefon': {"$exists": False}})
                #	03 99 98 - 15 30speak(ausgefuellt)
                print(ausgefuellt, "Eingetragen.\n")
                print(collection.count({'telefon': {"$exists": False}}), "verbleibend.")

suche()


from django.db.models.signals import post_save
from django.dispatch import receiver

from .models import MLMClient, MLMTransaction, get_or_create_mlm_config
from .utils.base import get_system_client

from django.conf import settings

CREATION_DESCRIPTION = getattr(
    settings, "CREATION_DESCRIPTION", "Client account creation."
)

@receiver(post_save, sender=MLMClient)
def execute_subscription_operation(sender, instance, created, *args, **kwargs):
    # Notice that we're checking for 'created' here. We only want to do this
    # the first time the 'User' instance is created. If the save that caused
    # this signal to be run was an update action, we know the user already
    # has a profile.
    if instance and created:

        # We make transactions only if the client has parent and has been created by an admin
        if instance.parent and instance.created_by:

            if instance.created_by.user.is_mlm_staff:
                instance.is_active = True
                instance.save()

                config = get_or_create_mlm_config()
                # We debit the Client from the subscription amount
                tr1 = MLMTransaction.objects.make_debit(
                    instance,
                    instance.subscription_amount,
                    MLMTransaction.FOR_AFFILIATION,
                    CREATION_DESCRIPTION,
                    initiated_by=instance.created_by,
                )

                # We credit the system from the subscription amount
                system_client = get_system_client()
                tr2 = MLMTransaction.objects.make_credit(
                    system_client,
                    instance.subscription_amount,
                    MLMTransaction.FOR_AFFILIATION,
                    CREATION_DESCRIPTION,
                    initiated_by=instance.created_by,
                )

                ### Here Verify if there is a pair, then credit the parent
                # tr3 = MLMTransaction.objects.make_credit(
                #     instance.parent,
                #     config.upline_commissions,
                #     transaction_type=MLMTransaction.FOR_AFFILIATION,
                #     initiated_by=instance.created_by,
                # )
            else:
                instance.is_active = False
                instance.save()
        elif instance.parent and not instance.created_by:
            instance.is_active = False
            instance.save()
        else:
            instance.is_active = True
            instance.save()


from flask import abort, jsonify, request

from app import app
from models import User

@app.route("/login", methods=["POST"])
def user_login():
    if not request.is_json:
        abort(400, "The request payload is not in JSON format")
    else:
        try:
            data = request.get_json()
            if "email" in data.keys():
                user = User.query.filter_by(email=data["email"]).first()
                if user is None:
                    abort(404)
                else:
                    return jsonify(user.serialize())
            else:
                abort(422)
        except Exception as e:
            return abort(e.code, e)


import pygame
import constants

class Ball(pygame.sprite.Sprite):

    def __init__(self):

        #https://www.pygame.org/docs/ref/sprite.html
        pygame.sprite.Sprite.__init__(self)

        self.image = pygame.image.load('brickblocks/start.png')
        self.rect = self.image.get_rect()

        self.rect.centerx = constants.WIDTH / 2
        self.rect.centery = constants.LARGE / 2

        #add start speed
        self.speed = [3, 3]

    def updateBallPosition(self):

        if self.rect.right >= constants.WIDTH or self.rect.left <= 0:
            self.speed[0] = -self.speed[0]
        if self.rect.top <= 0:
            self.speed[1] = -self.speed[1]

        self.rect.move_ip((self.speed))


import os
os.environ['KERAS_BACKEND'] = 'tensorflow'
import tensorflow as tf
config = tf.ConfigProto()
config.gpu_options.per_process_gpu_memory_fraction=0.05
session = tf.Session(config=config)

from deep_sort import generate_detections
from vehicle_vgg import vehicle_encoder

from deep_sort.deep_sort import nn_matching
from deep_sort.deep_sort.tracker import Tracker
from deep_sort.application_util import preprocessing as prep
from deep_sort.application_util import visualization
from deep_sort.deep_sort.detection import Detection

import keras
import numpy as np

import matplotlib.pyplot as plt

class deepsort_rbc():
	def __init__(self):
		#loading this encoder is slow, should be done only once.
		#self.encoder = generate_detections.create_box_encoder("deep_sort/resources/networks/mars-small128.ckpt-68577")
		self.encoder = vehicle_encoder('weights/vehicle_vgg.h5')
		self.metric = nn_matching.NearestNeighborDistanceMetric("cosine",.5 , 100)
		self.tracker= Tracker(self.metric)

	def reset_tracker(self):
		self.tracker= Tracker(self.metric)

	#Older yolo used to give y,x,h,w.
	"""
	def format_yolo_output( self,out_boxes):
		out_boxes=np.array([out_boxes[:,1],out_boxes[:,0],\
			out_boxes[:,3]-out_boxes[:,1],out_boxes[:,2]-out_boxes[:,0]])
		out_boxes=out_boxes.T
		return out_boxes
	"""

	#Current Yolo gives x_center,y_center,w,h
	#Deep sort needs the format `top_left_x, top_left_y, width,height

	def format_yolo_output( self,out_boxes):
		for b in range(len(out_boxes)):
			out_boxes[b][0] = out_boxes[b][0] - out_boxes[b][2]/2
			out_boxes[b][1] = out_boxes[b][1] - out_boxes[b][3]/2
		return out_boxes

	def run_deep_sort(self, frame, out_scores, out_boxes, out_classes):
		out_boxes = self.format_yolo_output(out_boxes)

		if out_boxes==[]:
			self.tracker.predict()
			trackers = self.tracker.tracks
			return trackers

		detections = np.array(out_boxes)
		#features = self.encoder(frame, detections.copy())
		features = self.encoder.extract_features(frame,detections)

		#print(frame.shape)

		detections = [Detection(bbox, score, feature,classname) \
					for bbox,score, feature,classname in\
				zip(detections,out_scores, features,out_classes)]

		outboxes = np.array([d.tlwh for d in detections])

		outscores = np.array([d.confidence for d in detections])
		indices = prep.non_max_suppression(outboxes, 0.8,outscores)

		detections = [detections[i] for i in indices]
		self.tracker.predict()
		self.tracker.update(detections)
		trackers = self.tracker.tracks

		return trackers


from PIL import Image

# https://pillow.readthedocs.io/en/5.1.x/reference/Image.html

class PNGSplit():
    def __init__(self):
        print("hi")
        get_image("pan.png")

    def get_image(image_path):
        """Get a numpy array of an image so that one can access values[x][y]."""
        image = Image.open(image_path, 'r')
        width, height = image.size
        pixel_values = list(image.getdata())
        if image.mode == 'RGBA':
            channels = 4
        elif image.mode == 'L':
            channels = 1
        else:
            print("Unknown mode: %s" % image.mode)
            return None
        pixel_values = numpy.array(pixel_values).reshape((width, height, channels))
        return pixel_values

PNGSplit()


import glob, os
import pathlib
import time
import random
import discord
from dotenv import load_dotenv

AUDIO_PATH = str(pathlib.Path(__file__).parent.absolute())
AUDIO_PATH = AUDIO_PATH.replace("\\", "/") + "/audio_files/"
FFMPEG_PATH = "C:/ffmpeg/bin/ffmpeg.exe"
load_dotenv()
CLIENT = discord.Client()
TOKEN = os.getenv("DISCORD_TOKEN")

def get_audio_files():
    """
    Get a list of audio files under the audio_files directory
    :return: dict, contains file names as well as track length
    """
    os.chdir(AUDIO_PATH)
    audio_tracks = {}
    return [file for file in glob.glob("*.mp3")]

async def play_audio(context):
    """
    Play an audio file from the audio_files directory
    :param context: Discord Context
    """
    user = context.author
    voice_channel = user.voice.channel  # get users curr voice channel
    vc = await context.author.voice.channel.connect()  # join channel
    if vc is None:
        print("Not a valid voice channel, user may not be in a channel")

    vc.play(discord.FFmpegPCMAudio(executable=FFMPEG_PATH,
                                   source=AUDIO_PATH + random.choice(get_audio_files())))

    time.sleep(1)
    while vc.is_playing():
        time.sleep(.5)
    await vc.disconnect()

@CLIENT.event
async def on_message(context):
    if context.author == CLIENT.user:  # if msg is from the bot
        return
    elif context.content.lower() == "!chip":
        await play_audio(context)
    else:
        print(context)

CLIENT.run(TOKEN)


"""
@Datetime： 2021/6/17 20:00
@Author： Sjh
@IDE： PyCharm
"""
#导入包
from hyperlpr import *
import numpy
from PIL import Image, ImageDraw, ImageFont
#导入OpenCV库
import cv2

#第一种方式:加入中文 https://blog.csdn.net/qq_41895190/article/details/90301459
def image_add_text(img1, text, left, top, text_color, text_size):
    # 判断图片是否为ndarray格式，转为RGB图片
    if isinstance(img1, numpy.ndarray):
        image = Image.fromarray(cv2.cvtColor(img1, cv2.COLOR_BGR2RGB))
    # 创建一个可以在给定图像上绘图的对象
    draw = ImageDraw.Draw(image)
    # 参数依次为 字体、字体大小、编码
    font_style = ImageFont.truetype("font/simsun.ttc", text_size, encoding='utf-8')
    # 参数依次为位置、文本、颜色、字体
    draw.text((left, top), text, text_color, font=font_style)
    # 图片转换为opencv格式
    return cv2.cvtColor(numpy.asarray(image), cv2.COLOR_RGB2BGR)

#定义显示窗口的大小
cv2.namedWindow("enhanced",0)
cv2.resizeWindow("enhanced", 640, 480)

#读入图片
image = cv2.imread(r"D:\Project\Monet_traffic\data\photo\1.png")

# #改变图片大小
# image = cv2.resize(image,None,fx=0.5,fy=0.5)

#识别结果
print(HyperLPR_plate_recognition(image))

#识别信息
xinxi = HyperLPR_plate_recognition(image)[0][0]
conf = HyperLPR_plate_recognition(image)[0][1]
location = HyperLPR_plate_recognition(image)[0][2]
print(xinxi)
print(location)

# opencv添加文字
# font = cv2.FONT_HERSHEY_SIMPLEX
# # print(xinxi.split('J')[0])
# if xinxi.split('J')[0] == "川":
#     xinxi = "川" + xinxi.split('J')[0]
# # cv2.putText(image, xinxi, (600, 500), font, 1, (0, 0, 255), 3, cv2.LINE_AA)

#转换为PIL的image格式,使用PIL绘制文字,再转换为OpenCV的图片格式
image = image_add_text(image, xinxi,location[0], location[1], (255, 0, 0), 50)

#画框
cv2.rectangle(image, (location[0], location[1]), (location[2], location[3]), (0, 255, 0), 2)

#展示
cv2.imshow("image",image)
cv2.waitKey(0)

#方法二:https://blog.csdn.net/qq_44740544/article/details/106177945
# #读入单张图片：加中文
# image = cv2.imread(r"D:\Project\Monet_traffic\data\photo\1.png")
#
# # 判断图片是否为ndarray格式，转为RGB图片
# if (isinstance(image, numpy.ndarray)):
#     img = Image.fromarray(cv2.cvtColor(image, cv2.COLOR_BGR2RGB))
#
# draw = ImageDraw.Draw(img)
# # 参数依次为 字体、字体大小、编码
#
# fontStyle = ImageFont.truetype("font/simsun.ttc", 20, encoding="utf-8")
# # 参数依次为位置、文本、颜色、字体
# draw.text((10, (1* 50) + 23), '这是', (255, 255, 255), font=fontStyle)
#
# # 转回BGR图片、ndarray格式
# image = cv2.cvtColor(numpy.asarray(img), cv2.COLOR_RGB2BGR)
#
# cv2.imshow("image",image)
#
# cv2.waitKey(0)

#车牌识别:https://huchenyang.net/py/80.html
#添加中文的思路
"""
因为使用cv2.putText() 只能显示英文字符，中文会出现乱码问题，
因此使用PIL在图片上绘制添加中文，可以指定字体文件。
大体思路：
OpenCV图片格式转换成PIL的图片格式；
"""


import sys, os, io, atexit
input = lambda : sys.stdin.readline().rstrip('\r\n')
stdout = io.BytesIO()
sys.stdout.write = lambda s : stdout.write(s.encode("ascii"))
atexit.register(lambda : os.write(1, stdout.getvalue()))

N, M, B = map(int, input().split())
block = []

for _ in range(N):
    block.append(list(map(int, input().split())))

ans = int(1e9)
glevel = 0

for i in range(257):
    use_block = 0
    take_block = 0
    for x in range(N):
        for y in range(M):
            if block[x][y] > i:
                take_block += block[x][y] - i
            else:
                use_block += i - block[x][y]
    if use_block > take_block + B:
        continue

    count = take_block * 2 + use_block

    if count <= ans:
        ans = count
        glevel = i

print(ans, glevel)


import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation
from mpl_toolkits.mplot3d import Axes3D

#creates the coordinate system and items for the solar system
class SolarSystem():
    def __init__(self):
        self.size = 1000
        self.planets = []
        self.fig = plt.figure()
        self.ax = Axes3D(self.fig,auto_add_to_figure=False)#, auto_add_to_figure=False)
        self.fig.add_axes(self.ax)
        self.dT = 1

    def add_planet(self, planet):
        self.planets.append(planet)

    def update_planets(self):
        self.ax.clear() ###Remove the previous drawing
        for planet in self.planets:
            planet.move()
            planet.draw()

    def fix_axes(self):
        self.ax.set_xlim((-self.size/2, self.size/2))
        self.ax.set_ylim((-self.size/2, self.size/2))
        self.ax.set_zlim((-self.size/2, self.size/2))

    def gravity_planets(self):
        for i, first in enumerate(self.planets):
            for second in self.planets[i+1:]:
                first.gravity(second)

class Planet():
    def __init__(self, SolarSys, mass, position=(0,0,0), velocity=(0,0,0)):
        self.SolarSys = SolarSys
        self.mass = mass
        self.position = position
        self.velocity = velocity
        self.SolarSys.add_planet(self)
        self.color = "black"

    def move(self): # updates its position and velocity
        self.position = (
            self.position[0]+self.velocity[0]*SolarSys.dT,
            self.position[1]+self.velocity[1]*SolarSys.dT,
            self.position[2]+self.velocity[2]*SolarSys.dT,
        )

    def draw(self): ###Plot Position
        self.SolarSys.ax.plot(*self.position, marker="o", markersize=10, color=self.color)

    def gravity(self, other): ###evaluate the force
        distance = np.subtract(other.position, self.position)
        distanceMag = np.linalg.norm(distance)
        distanceUnit = np.divide(distance,distanceMag)
        forceMag = self.mass*other.mass/(distanceMag**2)
        force = np.multiply(distanceUnit, forceMag)
        switch = 1
        for body in self, other:
            acceleration = np.divide(force, body.mass)
            acceleration = np.multiply(force, SolarSys.dT*switch)
            body.velocity = np.add(body.velocity, acceleration)
            switch *= -1

class Sun(Planet): ###Sun Class is herited from the Planet class
    def __init__(self, SolarSys, mass=1000, position=(0,0,0), velocity=(0,0,0)):
        super(Sun, self).__init__(SolarSys, mass, position, velocity)
        self.color = "yellow"

    def move(self):
        self.position = self.position

SolarSys = SolarSystem()

#Defining planets
planet1 = Planet(SolarSys, mass=100, position=(-50,-200,0), velocity=(-4,-5,0))
planet2 = Planet(SolarSys, mass=100, position=(50,-200,0), velocity=(-4,5,0))
planet3 = Planet(SolarSys, mass=200, position=(0,500,0), velocity=(4,0,0))
#sun = Sun(SolarSys)

def animate(i):
    print("The frame is:",i)
    SolarSys.gravity_planets()
    SolarSys.update_planets()
    SolarSys.fix_axes()

anim = animation.FuncAnimation(SolarSys.fig, animate, frames=100, interval=1)

writervieo = animation.FFMpegWriter(fps=120)

plt.show()


import bpy

from bpy_extras.io_utils import ImportHelper
from bpy.props import StringProperty, BoolProperty, EnumProperty
from bpy.types import Operator

import pathlib
import os
import re

ENUM_Axis = [("X","X","X"),("Y","Y","Y"),("Z","Z","Z"),("NEGATIVE_X","-X","-X"),("NEGATIVE_Y","-Y","-Y"),("NEGATIVE_Z","-Z","-Z")]

def recursive_collect_file_by_format(root_directory, format):

    collected_files = []
    items = os.walk(root_directory)

    for (path, directories, files) in items:
        for file in files:
            if file.endswith(".obj"):
                collected_files.append(path+"/"+file)

    return collected_files

ENUM_Filter_Mode = [("INCLUDE","Include","Include"),("EXCLUDE","Exclude","Exclude"), ("REGEX", "Regex", "Regex")]

class BATCHIMPORT_OT_Recursive_Import_OBJ(Operator):
    """Recursive Import OBJ"""
    bl_idname = "batch_import.recursive_import_obj"  # important since its how bpy.ops.import_test.some_data is constructed
    bl_label = "Batch Import OBJ"

    bl_options = {'UNDO', 'REGISTER'}
    # filename_ext = ".obj"
    # filename_ext = "."

    filter_glob: StringProperty(
        default="*.obj",
        options={'HIDDEN'},
        maxlen=255,  # Max internal buffer length, longer would be clamped.
    )

    directory: bpy.props.StringProperty()
    use_collection: bpy.props.BoolProperty(default=False, name="Use Collection")
    new_collection: bpy.props.BoolProperty(default=True, name="New Collection")
    collection_name: bpy.props.StringProperty(default="Import OBJ", name="Collection Name")
    file_name_as_collection: bpy.props.BoolProperty()

    filter_use: bpy.props.BoolProperty(default=False, name="Use Filter")
    filter_case_sensitive: bpy.props.BoolProperty(default=False, name="Case Sensitive")
    filter_text: bpy.props.StringProperty(name="Filter")
    filter_mode: bpy.props.EnumProperty(items=ENUM_Filter_Mode, name="Filter Mode")

    clamp_size: bpy.props.FloatProperty(default=0.0, min=0.0, name="Clamp Bounding")
    forward_axis: bpy.props.EnumProperty(default="X", items=ENUM_Axis, name="Axis Forward")
    up_axis: bpy.props.EnumProperty(default="Y", items=ENUM_Axis, name="Up")
    import_vertex_groups: bpy.props.BoolProperty(default=False, name="Vertex Groups")
    validate_meshes: bpy.props.BoolProperty(default=False, name="Validate Meshes")

    def invoke(self, context, event):

        if self.directory:
            self.collection_name = pathlib.Path(self.directory).stem
        else:
            self.collection_name = "Collection"

        context.window_manager.fileselect_add(self)
        return {"RUNNING_MODAL"}

    def draw(self, context):
        layout = self.layout

        box = layout.box()
        box.label(text="Batch Options", icon="MOD_ARRAY")

        box.label(text="Collection", icon="OUTLINER_COLLECTION")
        box.prop(self, "use_collection")
        if self.use_collection:
            row = box.row(align=True)
            if self.new_collection:
                row.prop(self, "collection_name", icon="OUTLINER_COLLECTION", text="")
            else:
                row.prop_search(self, "collection_name", bpy.data, "collections", icon="OUTLINER_COLLECTION", text="")

            row.prop(self, "new_collection", text="", icon="PLUS")
            box.prop(self, "file_name_as_collection", text="File Name As Collection", icon="CURRENT_FILE")

        box.separator()

        box.label(text="Filter", icon="FILTER")
        box.prop(self, "filter_use")

        if self.filter_use:
            col = box.column(align=True)
            row = col.row()
            row.prop(self, "filter_mode", expand=True)
            col.prop(self, "filter_text", text="", icon="FILTER")
            if self.filter_mode in ["INCLUDE", "EXCLUDE"]:
                box.prop(self, "filter_case_sensitive")

        box = layout.box()
        box.label(text="Transform", icon="OBJECT_DATA")

        box.prop(self, "clamp_size")
        box.prop(self, "forward_axis")
        box.prop(self, "up_axis")

        box = layout.box()
        box.label(text="Options", icon="EXPORT")
        box.prop(self, "import_vertex_groups")
        box.prop(self, "validate_meshes")

    def execute(self, context):

        files = recursive_collect_file_by_format(self.directory, ".obj")
        filtered_files = files

        original_objects = [object for object in bpy.data.objects]
        collection = None

        if self.filter_use:

            filtered_files = []

            for file in files:
                if self.filter_mode == "INCLUDE":

                    filename = os.path.basename(file)

                    if self.filter_case_sensitive:
                        if self.filter_text in filename:
                            filtered_files.append(file)
                    else:
                        if self.filter_text.lower() in filename.lower():
                            filtered_files.append(file)

                if self.filter_mode == "EXCLUDE":

                    if self.filter_case_sensitive:
                        if not self.filter_text in file:
                            filtered_files.append(file)
                    else:
                        if not self.filter_text.lower() in file.lower():
                            filtered_files.append(file)

                if self.filter_mode == "REGEX":
                    regex_match = bool(re.search(self.filter_text, file))
                    if regex_match:
                        filtered_files.append(file)

        self.report({"INFO"}, "Found " + str(len(filtered_files)) + " files ")
        print("Found " + str(len(filtered_files)) + " files")

        imported_files = {}

        for object in context.selected_objects:
            object.select_set(False)

        if self.use_collection:
            if len(filtered_files) > 0:
                collection = create_collection(self.collection_name, not self.new_collection, context.collection)

        for file in filtered_files:

            # imported_files[file] = list(context.selected_objects)

            print("Importing " + file)
            self.report({"INFO"}, "Importing" + file)
            bpy.ops.wm.obj_import(filepath=file, clamp_size=self.clamp_size, forward_axis=self.forward_axis, up_axis=self.up_axis, import_vertex_groups=self.import_vertex_groups, validate_meshes=self.validate_meshes)

            imported_object = [obj for obj in bpy.data.objects if obj not in original_objects]
            imported_files[file] = imported_object

            original_objects = list(bpy.data.objects)

            if self.use_collection:

                if collection:
                    move_objects_to_collection(imported_object, collection)

                    if self.file_name_as_collection:
                        file_collection = create_collection(os.path.relpath(file, self.directory), True, collection)
                        move_objects_to_collection(imported_object, file_collection)

        print("-----")
        print("Imported Objects: ")
        for file in imported_files:
            print("-----")
            print("    " + file)
            for obj in imported_files[file]:
                print("        " + obj.name)

        return {"FINISHED"}

def move_to_collection(obj, collection):

    for col in obj.users_collection:
        col.objects.unlink(obj)

    collection.objects.link(obj)

def move_objects_to_collection(objs, collection):

    for obj in objs:
        move_to_collection(obj, collection)

def create_collection(name, use_exist, parent_collection):

    context = bpy.context
    collection = None
    isNew = True

    if use_exist:
        collection = bpy.data.collections.get(name)
        isNew = False

    if not collection:
        collection = bpy.data.collections.new(name)
        isNew = True

    if isNew:
        if not parent_collection.get(collection.name):
            parent_collection.children.link(collection)

    return collection

classes = [BATCHIMPORT_OT_Recursive_Import_OBJ]

def register():

    for cls in classes:
        bpy.utils.register_class(cls)

    # bpy.types.TOPBAR_MT_file_import.append(menu_func_import)

def unregister():
    for cls in classes:
        bpy.utils.unregister_class(cls)

    # bpy.types.TOPBAR_MT_file_import.remove(menu_func_import)

if __name__ == "__main__":
    register()


"""This module contains the DirectScrolledWindowFrame class.

It will create a scrolled frame with an extra frame on top of it
"""

__all__ = ['DirectScrolledWindowFrame']

from panda3d.core import *
from direct.gui import DirectGuiGlobals as DGG
from direct.gui.DirectFrame import DirectFrame
from direct.gui.DirectButton import DirectButton
from direct.gui.DirectScrolledFrame import DirectScrolledFrame

class DirectScrolledWindowFrame(DirectScrolledFrame):
    """
    A moveable window with a scrolled content frame
    """
    def __init__(self, parent = None, **kw):
        optiondefs = (
            # Define type of DirectGuiWidget
            # The height of the area to drag the widget around
            ('dragAreaHeight',              0.1, None),
            ('resortOnDrag',               True, None),
            ('showClose',                  True, None),
            ('closeButtonPosition',     'Right', None),
            ('closeButtonScale',           0.05, None)
            )
        # Merge keyword options with default options
        self.defineoptions(kw, optiondefs)

        # Initialize superclasses
        DirectScrolledFrame.__init__(self, parent)

        # Call option initialization functions
        self.initialiseoptions(DirectScrolledWindowFrame)

        self.dragDropTask = None

        b = self.bounds
        c = self.createcomponent(
            'dragFrame', (), 'dragFrame',
            DirectFrame,
            # set the parent of the frame to this class
            (self,),
            state=DGG.NORMAL,
            suppressMouse=True,
            frameColor=(0.5,0.5,0.5,1),
            relief=1,
            pos=(0,0,b[3]),
            # set the size
            frameSize=(b[0],b[1],0, self['dragAreaHeight']))

        c.bind(DGG.B1PRESS, self.dragStart)
        c.bind(DGG.B1RELEASE, self.dragStop)

        scale = self['closeButtonScale']
        pos = (0,0,self['dragAreaHeight']*0.5)
        if self['closeButtonPosition'] == 'Right':
            pos = (b[1]-scale*0.5,0,self['dragAreaHeight']*0.5)
        elif self['closeButtonPosition'] == 'Left':
            pos = (b[0]+scale*0.5,0,self['dragAreaHeight']*0.5)
        closeBtn = self.createcomponent(
            'closeButton', (), 'closeButton',
            DirectButton,
            (c,),
            text='x',
            scale=scale,
            pos=pos,
            command=self.destroy)

    def dragStart(self, event):
        """
        Start dragging the window around
        """
        if self.dragDropTask is not None:
            # remove any existing tasks
            taskMgr.remove(self.dragDropTask)

        if self['resortOnDrag']:
            self.reparentTo(self.parent, 0)

        # get the windows position as seen from render2d
        vWidget2render2d = self.getPos(render2d)
        # get the mouse position as seen from render2d
        vMouse2render2d = Point3(event.getMouse()[0], 0, event.getMouse()[1])
        # calculate the vector between the mosue and the window
        editVec = Vec3(vWidget2render2d - vMouse2render2d)
        # create the task and store the values in it, so we can use it in there
        self.dragDropTask = taskMgr.add(self.dragTask, self.taskName("dragDropTask"))
        self.dragDropTask.editVec = editVec
        self.dragDropTask.mouseVec = vMouse2render2d

    def dragTask(self, t):
        """
        Calculate the new window position ever frame
        """
        # chec if we have the mouse
        mwn = base.mouseWatcherNode
        if mwn.hasMouse():
            # get the mouse position
            vMouse2render2d = Point3(mwn.getMouse()[0], 0, mwn.getMouse()[1])
            # calculate the new position using the mouse position and the start
            # vector of the window
            newPos = vMouse2render2d + t.editVec
            # Now set the new windows new position
            self.setPos(render2d, newPos)
        return t.cont

    def dragStop(self, event):
        """
        Stop dragging the window around
        """
        # kill the drag and drop task
        taskMgr.remove(self.dragDropTask)


from django.http import HttpResponse, JsonResponse
from django.views.decorators.csrf import csrf_exempt
from rest_framework import status
from rest_framework.decorators import api_view
from rest_framework.parsers import JSONParser
from rest_framework.response import Response
from django.contrib.auth.hashers import make_password
import string
import random
import datetime
import math
from .models import AccountGeneric
from .serializers import AccountGenericSerializer
from ..account.models import LoggInBasic, AccountBasic
from ..account.serializers import AccountSerializer
from ..post.serializers import PostSerializer

@csrf_exempt
@api_view(['POST'])
def get_profile(request, username):
    data = request.data
    if len(data.keys() & {'token'}) >= 1:
        token = request.data['token']
        try:
            my_account = LoggInBasic.objects.get(token=token).account
            account = AccountBasic.objects.get(pk=username)
            account_generic = AccountGeneric.objects.get(pk=account)
            is_followed = account_generic.followers.filter(
                name=my_account.name).exists()
            data = {'name': account.name,
                    'username': username, 'bio': account.bio, 'nfollowers': account_generic.followers.count(), 'nfollowings': account_generic.followings.count(), 'npost': account_generic.posts.count(), 'profile': account.profile, 'isfollowed': is_followed}
            return Response({'msg': data}, status.HTTP_200_OK)
        except LoggInBasic.DoesNotExist:
            return Response({'msg': 'invalid token'}, status.HTTP_406_NOT_ACCEPTABLE)
        except AccountBasic.DoesNotExist:
            return Response({'msg': 'invalid username'}, status.HTTP_406_NOT_ACCEPTABLE)
    return Response({'msg': 'invalid data'}, status.HTTP_406_NOT_ACCEPTABLE)

@csrf_exempt
@api_view(['POST'])
def follow(request):
    data = request.data
    if len(data.keys() & {'token', 'username'}) >= 2:
        token = request.data['token']
        try:
            username = data['username']
            my_account = LoggInBasic.objects.get(token=token).account
            account = AccountBasic.objects.get(pk=username)
            account_generic = AccountGeneric.objects.get(pk=account)
            my_account_generic = AccountGeneric.objects.get(pk=my_account)
            if (account_generic.followers.filter(username=my_account.username).exists()):
                account_generic.followers.remove(my_account)
                my_account_generic.followings.remove(account)
                account_generic.save()
                my_account_generic.save()
                return Response({'msg': 'successfully removed'}, status.HTTP_200_OK)
            my_account_generic.followings.add(account)
            account_generic.followers.add(my_account)
            account_generic.save()
            my_account_generic.save()
            return Response({'msg': 'successfully added'}, status.HTTP_200_OK)
        except LoggInBasic.DoesNotExist:
            return Response({'msg': 'invalid token'}, status.HTTP_406_NOT_ACCEPTABLE)
        except AccountBasic.DoesNotExist:
            return Response({'msg': 'invalid username'}, status.HTTP_406_NOT_ACCEPTABLE)
    return Response({'msg': 'invalid data'}, status.HTTP_406_NOT_ACCEPTABLE)

@csrf_exempt
@api_view(['POST'])
def get_followers_followings(request):
    data = request.data
    if len(data.keys() & {'token', 'username'}) >= 2:
        token = request.data['token']
        try:
            username = data['username']
            my_account = LoggInBasic.objects.get(token=token).account
            account = AccountBasic.objects.get(pk=username)
            account_generic = AccountGeneric.objects.get(pk=account)
            my_account_generic = AccountGeneric.objects.get(pk=my_account)
            followers = AccountSerializer(account_generic.followers, many=True)
            followings = AccountSerializer(
                account_generic.followings, many=True)
            return Response({'msg': {'followers': followers.data, 'followings': followings.data}}, status.HTTP_200_OK)
        except LoggInBasic.DoesNotExist:
            return Response({'msg': 'invalid token'}, status.HTTP_406_NOT_ACCEPTABLE)
        except AccountBasic.DoesNotExist:
            return Response({'msg': 'invalid username'}, status.HTTP_406_NOT_ACCEPTABLE)
    return Response({'msg': 'invalid data'}, status.HTTP_406_NOT_ACCEPTABLE)

@csrf_exempt
@api_view(['POST'])
def get_posts_username(request):
    data = request.data
    if len(data.keys() & {'token', 'username'}) >= 2:
        token = request.data['token']
        try:
            username = data['username']
            my_account = LoggInBasic.objects.get(token=token).account
            account = AccountBasic.objects.get(pk=username)
            account_generic = AccountGeneric.objects.get(pk=account)
            my_account_generic = AccountGeneric.objects.get(pk=my_account)
            posts = account_generic.posts
            dates = []
            for p in posts.all():
                dates.append(get_correct_time(p.date_post))
            return Response({'msg': {'posts': PostSerializer(posts, many=True).data, 'date': dates}}, status.HTTP_200_OK)
        except LoggInBasic.DoesNotExist:
            return Response({'msg': 'invalid token'}, status.HTTP_406_NOT_ACCEPTABLE)
        except AccountBasic.DoesNotExist:
            return Response({'msg': 'invalid username'}, status.HTTP_406_NOT_ACCEPTABLE)
    return Response({'msg': 'invalid data'}, status.HTTP_406_NOT_ACCEPTABLE)

def return_delta_time(time):
    return (datetime.datetime.now(datetime.timezone.utc) - time).seconds

def get_correct_time(time):
    dseconds = return_delta_time(time)
    date = 'Just know'
    if dseconds > 60 and dseconds <= 3600:
        date = str(math.floor(dseconds/60)) + ' mins ago'
    elif dseconds > 3600:
        date = str(math.floor(dseconds / 3600)) + ' hours age'
    return(date)


# -*- coding: utf-8 -*-
"""
Created on Sat Sep  5 20:29:36 2020

@author: soumyajyoti
"""

import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim
import torchvision
import torchvision.transforms as transforms
import torchvision.datasets as datasets
from torch.autograd import Variable
from torch.utils.data import DataLoader
import matplotlib.pyplot as plt
import numpy as np
import torchvision.models as models
from argparse import ArgumentParser
import os

parser = ArgumentParser()

parser.add_argument('--path', required=True, type=str) ## Root Path ##
parser.add_argument('--dataset_name', required=True, type=str) ## Dataset Name(Example: breakhis, jucyt,bi) ##
parser.add_argument('--dist', required=True, type=str) ## Data Type(Example: Original, ROI) ##
parser.add_argument('--model_name', required=True, type=str) ## CNN Model Name(Example: resnet18. densenet161, inceptionv3)
parser.add_argument('--no_class', required=True, type=int) ## No of Classes ##
parser.add_argument('--epoch', required=True, type=int) ## No of Epoch ##

args = parser.parse_args()

# Paths

train_path = args.path+'/'+args.dataset_name+'/'+args.dist+'/train/'
validation_path=args.path+'/'+args.dataset_name+'/'+args.dist+'/validation/'

print(train_path,validation_path)

# Data Loading

if args.model_name=='resnet18':
    h,w=224,224
    net= models.resnet18()
    net.fc=nn.Linear(net.fc.in_features,args.no_class)
elif args.model_name=='resnet34':
    h,w=224,224
    net= models.resnet34()
    net.fc=nn.Linear(net.fc.in_features,args.no_class)
elif args.model_name=='resnet50':
    h,w=224,224
    net= models.resnet50()
    net.fc=nn.Linear(net.fc.in_features,args.no_class)
elif args.model_name=='resnet101':
    h,w=224,224
    net= models.resnet101()
    net.fc=nn.Linear(net.fc.in_features,args.no_class)
elif args.model_name=='resnet152':
    h,w=224,224
    net= models.resnet152()
    net.fc=nn.Linear(net.fc.in_features,args.no_class)
elif args.model_name=='densenet121':
    h,w=224,224
    net= models.densenet121()
    net.classifier=nn.Linear(net.classifier.in_features,args.no_class)
elif args.model_name=='densenet161':
    h,w=224,224
    net= models.densenet161()
    net.classifier=nn.Linear(net.classifier.in_features,args.no_class)
elif args.model_name=='densenet169':
    h,w=224,224
    net= models.densenet169()
    net.classifier=nn.Linear(net.classifier.in_features,args.no_class)
elif args.model_name=='densenet201':
    h,w=224,224
    net= models.densenet201()
    net.classifier=nn.Linear(net.classifier.in_features,args.no_class)

elif args.model_name=='inception-v3':
    h,w=299,299
    net= models.inception_v3()
    net.AuxLogits.fc = nn.Linear(768,args.no_class)
    net.fc = nn.Linear(2048,args.no_class)

# TRANSFORM
transform =transforms.Compose([
           transforms.Resize((h,w)),
           transforms.ToTensor(),
		   transforms.Normalize(mean = [ 0.5, 0.5, 0.5 ],
				        std = [ 0.5, 0.5, 0.5]),])

# Loading data from a image folder

train_data = datasets.ImageFolder(train_path ,transform)
validation_data = datasets.ImageFolder(validation_path , transform)

train_loader = DataLoader(train_data, batch_size=8, shuffle=True, num_workers=2)

validation_loader = DataLoader(validation_data , batch_size=1, shuffle=True ,num_workers=2)

classes = ('B','M')

net = net.cuda()
criterion = nn.NLLLoss(size_average = True)
optimizer = optim.Adam(net.parameters(),lr=0.0001)

train_loss_vs_epoch=[]
validation_loss_vs_epoch=[]
save_path=os.makedirs(args.path+'/model')
print("Start Training for "+args.model_name)
print(net)

# Training the network

min_validation = 9999
for epoch in range(args.epoch) :
    train_loss = 0.0

    for i,batch in enumerate(train_loader,0):
        net=net.train(True) # Training Mode

        inputs, labels = batch
        inputs, labels = inputs.cuda(), labels.cuda()

        inputs, labels = Variable(inputs), Variable(labels)

        optimizer.zero_grad()

        outputs = net(inputs)
        loss = criterion(F.log_softmax(outputs), labels)

        loss.backward()
        optimizer.step()

        train_loss += loss.item()
    train_loss_vs_epoch.append(train_loss/len(train_loader))

    validation_loss = 0.0
    for batch in validation_loader :
        net = net.train(False)
        inputs, labels = batch

        inputs, labels = inputs.cuda(),labels.cuda();
        inputs, labels = Variable(inputs), Variable(labels)
        outputs = net(inputs)
        loss = criterion(outputs, labels)
        validation_loss += loss.item()

    validation_loss_vs_epoch.append(validation_loss/len(validation_loader))

    plt.plot(train_loss_vs_epoch,'r',validation_loss_vs_epoch,'b')
    plt.savefig('plot'+args.model_name+"_"+args.dataset_name+"_"+args.dist+"_"+'.png')

    if validation_loss < min_validation :
        min_validation = validation_loss
        torch.save(net,save_path+'/'+args.model_name+"_"+args.dataset_name+"_"+args.dist+"_"+'.pt');
        print('model saved')

    print('[epoch: %d] train loss: %.6f, val loss= %0.6f' %(epoch+1, train_loss/len(train_loader), validation_loss/len(validation_loader)))

print('Finished Training')


### PURPOSE
'''
The purpose of this script is to house certain utility functions that are applicable to a wide
range of functions and different scripts.

'''

# Import Libraries
import os
from datetime import datetime
import pandas as pd

# Modules For Sending Emails
import smtplib
from email.message import EmailMessage
from email.mime.application import MIMEApplication
from email.utils import COMMASPACE, formatdate
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.base import MIMEBase
from email import encoders

# OUTPUT FUNCTIONS____________________________________________________________________

def write_to_excel(dataframe, filename, target_dir, add_datetime):
    '''Inputs:  dataframe, filename, target_dir'''
    os.chdir(target_dir)

    # Add datetime to filename or not
    if add_datetime == True:
        filename = filename + '_' + str(datetime.today())
    else:
        filename = filename

    writer = pd.ExcelWriter(filename+'.xlsx')
    dataframe.to_excel(writer, 'Data')
    print('Dataframe {} has been written to {}'.format(filename, target_dir))
    writer.save()

# REPORT FUNCTIONS___________________________________________________________________

def progress_recorder(Count_obj, range_value_obj):
    '''
    Inputs
        Count_obj:  This is the current count of the for loop, i.e.
        the current count of the number of pages scraped.
        range_value_obj: This represents the integer value of the last
        page of the web page minus the first, so the total number of pages to scrape.
    '''
    if Count_obj == 1:
        print('Scraping started')
    elif Count_obj == round(range_value_obj * 0.01, 0):
        print('1% Complete')
    elif Count_obj == round(range_value_obj * 0.05, 0):
        print('5% Complete')
    elif Count_obj == round(range_value_obj * 0.1,0):
        print('10% Complete')
    elif Count_obj == round(range_value_obj * 0.15,0):
        print('15% Completed')
    elif Count_obj == round(range_value_obj * 0.20,0):
        print('20% Complete')
    elif Count_obj == round(range_value_obj * 0.25,0):
        print('25% Completed')
    elif Count_obj == round(range_value_obj * 0.3,0):
        print('30% Completed')
    elif Count_obj == round(range_value_obj * 0.35,0):
        print('35% Completed')
    elif Count_obj == round(range_value_obj * 0.4,0):
        print('40% Completed')
    elif Count_obj == round(range_value_obj * 0.5,0):
        print('50% Completed')
    elif Count_obj == round(range_value_obj * 0.6,0):
        print('60% Completed')
    elif Count_obj == round(range_value_obj * 0.7,0):
        print('70% Completed')
    elif Count_obj == round(range_value_obj * 0.8,0):
        print('80% Completed')
    elif Count_obj == round(range_value_obj * 0.9,0):
        print('90% Completed')
    elif Count_obj == round(range_value_obj * 1.0,0):
        print('RRRRRR!!!!!!.......Scraping 100% Complete!!')
        print('Ok to proceed to the next job!\n')
    return None

# PROGRESS REPORT - Page numbers on first run----------------------------------------------

def pre_run_scraper_status_report(Run_type, End_page):
    '''Input:  Run_type, End_page'''
    if Run_type == 'Start_from_last_page':
        print('Scraper starting from the last page scraped, which is {}'.format(End_page))
    elif Run_type == 'Reset':
        print('''The user has selected the reset run-option.  The scraper will
                proceed to delete all prior data and start to scrape from page 0''')

    return None

# PROGRESS REPORT - Information After Run--------------------------------------------------

def get_num_pages_scraped(mydb, Beginning_page):
    '''
    Function:           Get count of rows in table where page_number is great than our start
                        page.  This will tell us how many pages were scraped
    Substring_index:    https://dev.mysql.com/doc/refman/8.0/en/
                        string-functions.html#function_substring-index
    Sql command:        Iterate dictionary, return value associated with key "Count(*)"'''
    mycursor = mydb.cursor(dictionary = True)
    sql_command = '''
        SELECT  COUNT(*)
        FROM SCA_DATA3_TEST
        WHERE SUBSTRING_INDEX(page_number, '=', -1) > {}'''.format(Beginning_page)
    mycursor.execute(sql_command)
    for x in mycursor:
        return x['COUNT(*)']

def get_df_data_companies_sued(mydb, Beginning_page):

    mycursor = mydb.cursor(dictionary = True)
    sql_command = '''
        SELECT  *
        FROM SCA_DATA3_TEST
        WHERE SUBSTRING_INDEX(page_number, '=', -1) > {}'''.format(Beginning_page)

    df = pd.read_sql_query(sql_command, mydb)

    return df

def driver_function_post_run_scraper_report(mydb, Beginning_page, End_page, report_output):
    '''
    Input:      mydb:  db connection
                Return_value:  Either number of pages scraped or df_companies_sued
    Output:     Return df & write to Excel or standard output.

    Functions:
                1.) Define first and last pages scraped
                2.) Define cases scraped - return dataframe
                    Derived values:  try to create a key of the attributes
                3.) If Starr is on the account (tbd)
                4.) Prediction (tbd)
                5.) Similar cases (tbd)
    '''

    # Define Target Dir
    target_output_dir = '/home/ccirelli2/Desktop/Programming/SCA_Web_scaper/Output'

    # Print Results to Standard Output
    if report_output == 'print_results':
        df_companies_sued = get_df_data_companies_sued(mydb, Beginning_page)
        # For now lets just print the results
        print('\n Progress Report:')
        print('Number of pages scraped {}\n'.format(get_num_pages_scraped(mydb, Beginning_page)))
        print('The following companies were added to the database: {}'.format(
            ', '.join([x.split("\n")[0] for x in df_companies_sued['defendant_name']])))

    # Generate Dataframe & Write to Excel
    elif report_output == 'dataframe_w_results':
        df_companies_sued = get_df_data_companies_sued(mydb, Beginning_page)
        write_to_excel(df_companies_sued, 'SCA_Scraper_Results', target_output_dir,
                       add_datetime = False)
        return df_companies_sued

    # Return Dataframe Filename + Path
    elif report_output == 'dataframe_filename_plus_path':
        return target_output_dir + '/SCA_Scraper_Results.xlsx'

    # Generate Text File for Body of Email

    elif report_output == 'email_text_body':
        # Source Url
        source_url = 'http://securities.stanford.edu/'
        # Generate Dataframe
        df_companies_sued = get_df_data_companies_sued(mydb, Beginning_page)
        num_companies_sued = len(df_companies_sued['defendant_name'])
        # Create Text File
        filename = 'Email_body.txt'
        Email_body = open('Email_body.txt', 'w')
        # Create Subject Line & Title
        Email_body.write('Intellisurance Securities Class Action Scraper Report \r')
        Email_body.write('Report Date                   => {} \r'.format(datetime.today()))
        Email_body.write('Number of Companies In Report => {} \r'.format(num_companies_sued))
        Email_body.write('Source                        => {} \n\n'.format(source_url))

        # Add Content For Each Company Sued
        for row in df_companies_sued.itertuples():
            # Identify Values
            defendant_name  = str(row[2])
            link            = str(row[1])
            date_filed      = str(row[5])
            case_summary    = str(row[6])
            # Write to File
            Email_body.write('Defendant Name => {}'.format(defendant_name))
            Email_body.write('Date Filed     => {}\r'.format(date_filed))
            Email_body.write('Link to case   => {}\r'.format(link))
            Email_body.write('Case Summary:  => {}\r'.format(case_summary))
            Email_body.write('_________________________________________________________\n\n')

        # Add Copyright Disclosure At the End
        Copyright_disclosure ='''\r
        \n\n\n------------------------------------------------------------------------------
        Please be advised that the content of this report is sources from the Stanford Law
        Securities Class Action Web Page and is for academic purposes only.  In now way does
        the author warrant the accuracy of the information presented, its validity nor is the
        author responsible for its use by recipients of this report.
        ------------------------------------------------------------------------------------
        '''
        Email_body.write(Copyright_disclosure)

        # Close Email Body
        Email_body.close()

        # Return Path + Filename
        return  target_output_dir + '/' + filename

    # END DRIVER FUNCTION__________________________________________________________________

def email_no_attachment(from_address, to_address, timeout_sec, password, message):
    '''Should probably add each of these objects as inputs'''

    # Objects:
    gmail_server = 'smtp.gmail.com'
    port         = 465
    message = 'Subject: Test subject line'
    # Create Secure Connection to Gmail Server
    server = smtplib.SMTP_SSL(gmail_server, port ,timeout = timeout_sec)
    print('Connection Created to Gmail Server \n')
    server.login(from_address, password)
    print('Log-on successful to gmail address {}'.format(from_address))
    server.sendmail(
        from_address,
        to_address,
        message)
    print('Email successfully sent')
    server.quit()

def email_with_attachments(password, toaddr, subject, body, attachment_filename):

    # Define To & From Addresses
    fromaddr = 'intellisurance@gmail.com'

    # Create Instance Message Object
    msg = MIMEMultipart()

    # Define Attributes
    msg['From'] = fromaddr
    msg['To'] = toaddr
    msg['Subject'] = subject
    msg.attach(MIMEText(body, 'plain')) # what does this do?

    # File Specification & Encoding
    attachment = open(attachment_filename, 'rb')
    part = MIMEBase('application', 'octet-stream')
    part.set_payload((attachment).read())
    encoders.encode_base64(part)
    part.add_header('Content-Disposition', 'attachment; filename = {}'.format(attachment_filename))
    msg.attach(part)

    # Login to Server & Send Message
    server = smtplib.SMTP_SSL('smtp.gmail.com', 465, timeout = 5)
    server.login('intellisurance@gmail.com', password)
    text = msg.as_string()              # I guess we are converting the entire message to str
    server.sendmail(fromaddr, toaddr, text)
    server.quit()

    return print('\nEmail Successfully Sent to:  {}'.format(toaddr))


import matplotlib.pyplot as plt
import numpy as np
data = np.loadtxt('data_lambda1vsangle')
lambda1 = 0.5*(data[:,2]+data[:,3])
angle = 0.5*(180-data[:,0]+data[:,1])*np.pi/180.
cosangle = np.cos(angle)
sinangle = np.sin(angle)
anglefunction = sinangle/np.power(cosangle,0.33)
plt.scatter(anglefunction, lambda1, s=30, facecolors='none',edgecolors='k')
plt.axis([0,1.5,0,160])
plt.xlabel(r'$\frac{\sin\phi}{\cos^\frac{1}{3}\phi}$',fontsize=20)
plt.ylabel(r'$\lambda_1$',fontsize=20)
plt.gcf().subplots_adjust(bottom = 0.15)
plt.savefig('lambdavsangle.png')
plt.show()


__revision__ = "$Rev: 2809 $"
__date__ = "$Date: 2008-05-10 03:15:14 -0300 (sab, 10 mai 2008) $"
__author__ = "$Author: johann $"

import urllib
from datetime import datetime, timedelta
from django.db import transaction
from django.http import HttpResponseRedirect
from django.template import RequestContext
from django.shortcuts import render_to_response
from django.utils.text import capfirst
from django.utils.safestring import mark_safe
from django.utils.translation import ugettext_lazy as _
from django.conf import settings
from shotserver04.common import int_or_none, last_poll_timeout, error_page
from shotserver04.common.object_cache import preload_foreign_keys
from shotserver04.start.models import NewsItem
from shotserver04.start.forms.url import UrlForm
from shotserver04.start.forms.browsers import BrowsersForm
from shotserver04.start.forms.features import FeaturesForm
from shotserver04.start.forms.options import OptionsForm
from shotserver04.start.forms.special import SpecialForm
from shotserver04.platforms.models import Platform
from shotserver04.factories.models import Factory
from shotserver04.browsers.models import BrowserGroup, Browser
from shotserver04.websites.models import Website
from shotserver04.requests.models import RequestGroup, Request
from shotserver04.sponsors.models import Sponsor

BROWSER_COLUMNS = 5
SELECTOR_TEMPLATE = u"""
%s
""".strip()

def start(http_request):
    """
    Front page with URL input, browser chooser, and options.
    """
    if (http_request.user.is_anonymous() and
        hasattr(settings, 'ALLOW_ANONYMOUS_REQUESTS') and
        not settings.ALLOW_ANONYMOUS_REQUESTS):
        url = '/'
        if http_request.META['QUERY_STRING']:
            url += '?' + http_request.META['QUERY_STRING']
        return error_page(http_request, _("login required"),
            _("Anonymous screenshot requests are not allowed."),
            u'%s' % (
                urllib.quote(url.encode('utf-8')),
                _("Please log in with your username and password.")))
    # Initialize forms.
    post = http_request.POST or None
    url_form = UrlForm(post)
    features_form = FeaturesForm(post)
    options_form = OptionsForm(post)
    special_form = SpecialForm(post)
    # Get available choices from database, with correct translations.
    active_factories = Factory.objects.filter(
        last_poll__gte=last_poll_timeout())
    active_browsers = Browser.objects.filter(
        factory__in=active_factories,
        active=True)
    if not active_browsers:
        return error_page(http_request, _("out of service"),
            _("No active screenshot factories."),
            _("Please try again later."))
    features_form.load_choices(active_browsers)
    options_form.load_choices(active_factories)
    # Validate posted data.
    valid_post = (url_form.is_valid() and
                  options_form.is_valid() and
                  features_form.is_valid() and
                  special_form.is_valid())
    # Preload some database entries for browser forms
    preload_foreign_keys(active_browsers,
                         factory=active_factories,
                         factory__operating_system=True,
                         browser_group=True,
                         engine=True)
    # Select browsers according to GET request
    selected_browsers = None
    if 'browsers' in http_request.GET:
        selected_browsers = http_request.GET['browsers'].split()
    # Browser forms for each platform.
    browser_forms = []
    for platform in Platform.objects.all():
        browser_form = BrowsersForm(active_browsers, platform,
                                    post, selected_browsers)
        browser_form.platform_name = \
            unicode(platform).lower().replace(' ', '-')
        if browser_form.is_bound:
            browser_form.full_clean()
        if browser_form.fields:
            browser_forms.append(browser_form)
        valid_post = valid_post and browser_form.is_valid()
    browser_forms[0].is_first = True
    browser_forms[-1].is_last = True
    priority = 0
    if valid_post:
        if (url_form.cleaned_data['shocksite_keywords'] >
            settings.SHOCKSITE_KEYWORDS_ALLOWED):
            # Ignore screenshot requests for shock sites.
            priority = -url_form.cleaned_data['shocksite_keywords']
        elif 'shotserver04.priority' in settings.INSTALLED_APPS:
            # Get priority processing for domain or user.
            from shotserver04.priority import domain_priority, user_priority
            priority = max(domain_priority(url_form.cleaned_data['domain']),
                           user_priority(http_request.user))
        usage_limited = check_usage_limits(
            http_request, priority,
            url_form.cleaned_data['website'],
            url_form.cleaned_data['domain'])
        if usage_limited:
            valid_post = False
    if not valid_post:
        # Show HTML form.
        if 'url' in http_request.GET:
            url_form.fields['url'].initial = http_request.GET['url']
        multi_column(browser_forms)
        selectors = mark_safe(',\n'.join([
            SELECTOR_TEMPLATE % (plus_minus, capfirst(text))
            for text, plus_minus in selector_pairs(browser_forms)]))
        news_list = NewsItem.objects.all()[:10]
        sponsors_list = Sponsor.objects.filter(front_page=True)
        show_special_form = http_request.user.is_authenticated()
        return render_to_response('start/start.html', locals(),
            context_instance=RequestContext(http_request))
    # Create screenshot requests and redirect to website overview.
    expire = datetime.now() + timedelta(minutes=30)
    values = {
        'website': url_form.cleaned_data['website'],
        'ip': http_request.META['REMOTE_ADDR'],
        'user': None,
        }
    if http_request.user.is_authenticated():
        values['user'] = http_request.user
    values.update(options_form.cleaned_data)
    values.update(features_form.cleaned_data)
    values.update(special_form.cleaned_data)
    match_values = {}
    for key in values:
        if values[key] is None:
            match_values[key + '__isnull'] = True
        else:
            match_values[key] = values[key]
    existing = RequestGroup.objects.filter(
        expire__gte=datetime.now(), **match_values).order_by('-submitted')
    if (len(existing) and
        existing[0].request_set.filter(screenshot__isnull=True).count()):
        # Previous request group is still pending, reuse it.
        request_group = existing[0]
        request_group.update_fields(expire=expire)
        if priority > request_group.priority:
            request_group.update_fields(priority=priority)
    else:
        request_group = RequestGroup.objects.create(
            expire=expire, priority=priority, **values)
    for browser_form in browser_forms:
        create_platform_requests(
            request_group, browser_form.platform, browser_form, priority)
    # Make sure that the redirect will show the new request group
    transaction.commit()
    # return render_to_response('debug.html', locals(),
    #     context_instance=RequestContext(http_request))
    return HttpResponseRedirect(values['website'].get_absolute_url())

def previous_websites(requests):
    """
    Show only websites with most requests.
    """
    requests = requests[:200] # Limit sort effort.
    preload_foreign_keys(requests, request_group__website=True)
    websites = set()
    for request in requests:
        website = request.request_group.website
        if hasattr(website, 'request_count'):
            website.request_count += 1
        else:
            website.request_count = 0
        websites.add(website)
    websites = list(websites)
    if len(websites) <= 1:
        return websites
    websites.sort(key=lambda website: -website.request_count)
    return websites[:10] # Show only the most useful results.

def check_usage_limit(result, http_request, message,
                      solution, solution_url, max_requests, **kwargs):
    """
    Check a specific usage limit.
    """
    if not http_request.user.is_anonymous():
        kwargs['request_group__user'] = http_request.user
    if not kwargs:
        kwargs['request_group__ip'] = http_request.META['REMOTE_ADDR']
    yesterday = datetime.now() - timedelta(hours=24)
    kwargs['request_group__submitted__gte'] = yesterday
    requests = Request.objects.filter(**kwargs)
    count = requests.count()
    if count > max_requests:
        if 'request_group__website__domain' in kwargs:
            domain = kwargs['request_group__website__domain']
        result['message'] = message % locals()
        result['solution'] = solution
        result['solution_url'] = solution_url
        result['websites'] = previous_websites(requests)

def check_usage_limits(http_request, priority, website, domain):
    """
    Make sure that the usage limits aren't exceeded.
    """
    website_messages = [
_("There were already %(count)d screenshot requests for this website today."),
_("You have already requested %(count)d screenshots for this website today.")]
    domain_messages = [
_("There were already %(count)d screenshot requests for %(domain)s today."),
_("You have already requested %(count)d screenshots for %(domain)s today.")]
    user_messages = [
_("There were already %(count)d screenshot requests from your IP today."),
_("You have already requested %(count)d screenshots today.")]
    email = settings.ADMINS[0][0]
    solutions = [_("Please create a user account."),
                 _("Please sign up for priority processing."),
                 _("Please write to %(email)s.") % locals()]
    solution_urls = ['/accounts/email/',
                     '/priority/',
                     'mailto:%s' % settings.ADMINS[0][1]]
    index = 1
    if http_request.user.is_anonymous():
        index = 0
    elif priority:
        index = 2
    result = {}
    check_usage_limit(result, http_request,
                      website_messages[min(1, index)],
                      solutions[index], solution_urls[index],
                      settings.MAX_WEBSITE_REQUESTS_PER_DAY[index],
                      request_group__website=website)
    if result:
        return result
    check_usage_limit(result, http_request,
                      domain_messages[min(1, index)],
                      solutions[index], solution_urls[index],
                      settings.MAX_DOMAIN_REQUESTS_PER_DAY[index],
                      request_group__website__domain=domain)
    if result:
        return result
    check_usage_limit(result, http_request,
                      user_messages[min(1, index)],
                      solutions[index], solution_urls[index],
                      settings.MAX_USER_REQUESTS_PER_DAY[index])
    return result

def multi_column(browser_forms):
    """
    Arrange browsers in multiple columns per platform.
    """
    groups = [[form.column_length(), form] for form in browser_forms]
    allow_columns = BROWSER_COLUMNS
    if len(browser_forms) > 3:
        allow_columns += 1
    for total_columns in range(len(browser_forms), allow_columns):
        groups.sort()
        length, form = groups[-1]
        if length <= 4:
            break
        form.columns += 1
        groups[-1][0] = form.column_length()

def selector_pairs(browser_forms):
    """
    Links to select or unselect all browsers.
    """
    total = sum([len(form.fields) for form in browser_forms])
    yield (_('all'), '+' * total)
    yield (_('none'), '-' * total)
    start = 0
    for form in browser_forms:
        length = len(form.fields)
        end = start + length
        yield (form.platform.name,
               '-' * start + '+' * length + '-' * (total - end))
        start = end
    yield ('Gecko', selector_func(browser_forms, lambda field:
        field.browser.engine.name == 'Gecko'))
    yield ('KHTML/WebKit', selector_func(browser_forms, lambda field:
        field.browser.engine.name in ('KHTML', 'AppleWebKit')))

def selector_func(browser_forms, func):
    result = []
    for form in browser_forms:
        for name, field in form.fields.items():
            if func(field):
                result.append('+')
            else:
                result.append('-')
    return ''.join(result)

def create_platform_requests(request_group, platform, browser_form,
                             priority=0):
    """
    Create screenshots requests for selected browsers on one platform.
    """
    platform_lower = platform.name.lower().replace(' ', '-')
    for name in browser_form.fields:
        if not browser_form.cleaned_data[name]:
            continue # Browser not selected
        first_part, browser_name, major, minor = name.split('_')
        if first_part != platform_lower:
            continue # Different platform
        browser_group = BrowserGroup.objects.get(name__iexact=browser_name)
        Request.objects.get_or_create(
            request_group=request_group,
            platform=platform,
            browser_group=browser_group,
            major=int_or_none(major),
            minor=int_or_none(minor),
            priority=priority,
            )