#reverse snake game
from winsound import PlaySound
import pygame
from pygame.locals import *         # imports certain global Variables
import time

SIZE = 16
Start = 100

class Apple:
    def __init__(self, parent_screen):
        self.parent_screen = parent_screen
        self.apple = pygame.image.load("resources/apple.jpg").convert()
        self.x = SIZE * 10
        self.y = SIZE * 5
        self.counter = 20                   #counter to count loops till movement
        self.current = 20                  #current loops since last movement
        self.direc = "down"

    def draw(self):
        self.parent_screen.blit(self.apple, (self.x, self.y))
        pygame.display.flip()

    def move_left(self):   #from normal snake
        self.direc = 'left'

    def move_right(self):
        self.direc = 'right'

    def move_up(self):
        self.direc = 'up'

    def move_down(self):
        self.direc = 'down'

    def move(self):             #movement taken from snake head
        if self.direc == 'left':
            self.x -= SIZE
        if self.direc == 'right':
            self.x += SIZE
        if self.direc == 'up':
            self.y -= SIZE
        if self.direc == 'down':
            self.y += SIZE

class Snake:
    def __init__(self, parent_screen, length):
        self.parent_screen = parent_screen
        self.head = pygame.image.load("resources/greeneye.jpeg").convert()     #importing a picture
        self.block = pygame.image.load("resources/green.jpeg").convert()

        self.length = length
        self.x = [SIZE]*length
        self.y = [SIZE]*length
        self.counter = 20                  #counter to count loops till movement
        self.current = 20                  #current loops since last movement
        self.length_increase_counter = 0

        for i in range (self.length):
            self.x.append(Start )
            self.y.append(Start + SIZE*i)

        self.direc = 'down'

    def set_direction(self, xa, ya, xb, yb):
        x = xa - xb
        y = ya - yb
        if abs(x) > abs(y):
            if x > 0:
                self.direc = "right"
            else:
                self.direc = "left"
        else:
            if y > 0:
                self.direc = "down"
            else:
                self.direc = "up"

    def move(self):
        #update body
        for i in range(self.length-1,0,-1):
            self.x[i] = self.x[i-1]
            self.y[i] = self.y[i-1]

        # update head
        if self.direc == 'left':
            self.x[0] -= SIZE
        if self.direc == 'right':
            self.x[0] += SIZE
        if self.direc == 'up':
            self.y[0] -= SIZE
        if self.direc == 'down':
            self.y[0] += SIZE

    def draw(self):
        self.parent_screen.blit(self.head, (self.x[0], self.y[0]))
        for i in range(1, self.length):
            self.parent_screen.blit(self.block, (self.x[i], self.y[i]))                         #drawing the image        ((0,0) is upper left corner)
        pygame.display.flip()                                       #update screen

    def length_increase(self):
        self.length += 1
        self.x.append(0)
        self.y.append(0)

class Game:
    def __init__(self):
        pygame.init()               #initialising the pygame module
        pygame.display.set_caption("Codebasics Snake And Apple Game")
        pygame.mixer.init()                     #initzializing sound module

        self.play_background_music()

        self.surface = pygame.display.set_mode((SIZE * 30, SIZE * 25))             #initialising the window and its properties, size in pixels
        self.snake = Snake(self.surface, 3)
        self.snake.draw()
        self.apple = Apple(self.surface)
        self.apple.draw()
        self.game_over_type = 0          # = 1 if won, = 2 if lost

    def display_score(self):
        font = pygame.font.SysFont('arial', 30)
        score = font.render(f"Score: {self.snake.length-3}", True, (255, 255, 255))
        self.surface.blit(score, (320, 20))

    def is_collision(self, x1, y1, x2, y2):
        if x1 >= x2 and x1 < x2 + SIZE:
            if y1 >= y2 and y1 < y2 + SIZE:
                return True
        return False

    def show_game_over(self):
        self.render_background()
        font = pygame.font.SysFont('arial',20)
        if self.game_over_type == 1:
            line1 = font.render(f"You won. Congratulations. Your score: {self.snake.length-3}", True, (255, 255, 255))
            self.surface.blit(line1, (50, 100))
            line2 = font.render(f"To play again press Enter. To exit press Escape", True, (255, 255, 255))
            self.surface.blit(line2, (50, 150))
        elif self.game_over_type == 2:
            line1 = font.render(f"The game is over. Your score: {self.snake.length-3}", True, (255, 255, 255))
            self.surface.blit(line1, (50, 100))
            line2 = font.render(f"To play again press Enter. To exit press Escape", True, (255, 255, 255))
            self.surface.blit(line2, (50, 150))
        else:
            line1 = font.render("Unexpected Error occurred", True, (255, 255, 255))
            self.surface.blit(line1, (50, 100))
            line2 = font.render("To play again press Enter. To exit press Escape", True, (255, 255, 255))
            self.surface.blit(line2, (50, 150))

        pygame.display.flip()

        pygame.mixer.music.pause()

    def play_sound(self, sound):
        sound = pygame.mixer.Sound(f"resources/{sound}.mp3")
        pygame.mixer.Sound.play(sound)
        pygame.mixer.Sound.set_volume(sound, 0.2)

    def play_background_music(self):
        pygame.mixer.music.load("resources/bensound-happyrock.mp3")
        pygame.mixer.music.play()
        pygame.mixer.music.set_volume(0.1)

    def render_background(self):
        bg = pygame.image.load("resources/background_green.jpeg")
        self.surface.blit(bg, (0, 0))

    def reset(self):
        self.snake = Snake(self.surface, 3)
        self.apple = Apple(self.surface)

    def play(self):
        if self.snake.current >= self.snake.counter:
            if self.snake.length_increase_counter >= 50:
                self.snake.length_increase()
                if self.snake.counter > 14:
                    self.snake.counter -= 1
                self.snake.length_increase_counter = 0

            self.snake.current = 0
            self.snake.set_direction(self.apple.x, self.apple.y, self.snake.x[0], self.snake.y[0])
            self.snake.move()
            self.snake.current = 0

        if self.apple.current >= self.apple.counter:
            self.apple.current = 0
            self.apple.move()

        if self.is_collision(self.apple.x, self.apple.y, self.snake.x[0], self.snake.y[0]):
            self.game_over_type = 2
            raise "Game over"

        for i in range(1, self.snake.length):
            if self.is_collision(self.snake.x[0], self.snake.y[0], self.snake.x[i], self.snake.y[i]):
                self.game_over_type = 1
                raise "Game over"

        self.render_background()
        self.apple.draw()
        self.snake.draw()

        self.snake.current += 1
        self.apple.current += 1
        self.snake.length_increase_counter += 1

    def run(self):
        pause = False
        running = True
        while running:
            for event in pygame.event.get():
                if event.type == KEYDOWN:
                    if event.key == K_ESCAPE:
                        running = False

                    if event.key == K_RETURN:
                        pygame.mixer.music.unpause()
                        pause = False

                    if not False:
                        if event.key == K_LEFT:
                            self.apple.move_left()
                        if event.key == K_RIGHT:
                            self.apple.move_right()
                        if event.key == K_UP:
                            self.apple.move_up()
                        if event.key == K_DOWN:
                            self.apple.move_down()

                elif event.type == QUIT:
                    running = False
            #self.play()
            try:
                if not pause:
                    self.play()
            except Exception as e:
                self.show_game_over()
                pause = True
                self.reset()

            time.sleep(0.01)

if __name__ == "__main__":
    game = Game()
    game.run()


from decimal import Context
from django.shortcuts import redirect, render
from django.http import HttpResponse
from django.contrib.auth.decorators import login_required
from django.contrib.auth.models import User
from .models import Profile
from django.contrib.auth import authenticate, login,logout,models
import http.client
from .models import Product
from .distance_calc.distance import PincodeDistance
from .models import Contact_us
from .models import Sold,Bought,ForSale

def home(request):
    if not request.user.is_authenticated:
        return redirect('/login/')
    return render(request, 'index.html')

def about(request):
    if not request.user.is_authenticated:
        return redirect('/login/')
    return render(request, 'about.html')

def contact(request):
    if not request.user.is_authenticated:
        return redirect('/login/')
    if request.method == 'POST':
        name = request.POST['name']
        phone = request.POST['phone']
        subject = request.POST['subject']
        message = request.POST['message']
        ins = Contact_us(name=name,phone=phone,subject=subject,message=message)
        ins.save()
        return redirect('/home/')
    return render(request, 'contact.html')

def trend(request):
    if not request.user.is_authenticated:
        return redirect('/login/')
    return render(request, 'pricetrend.html')

def user_analytics(request):
    if not request.user.is_authenticated:
        return redirect('/login/')
    return render(request, 'user_analytics.html')

def option(request):
    if not request.user.is_authenticated:
        return redirect('/login/')
    return render(request, 'option.html')

def view_store(request):
    if not request.user.is_authenticated:
        return redirect('/login/')
    return render(request, 'view_store.html')

def sample(request):
    if not request.user.is_authenticated:
        return redirect('/login/')
    return render(request, 'sample-inner-page.html')

def service(request):
    if not request.user.is_authenticated:
        return redirect('/login/')
    return render(request, 'services.html')

def service_detail(request):
    if not request.user.is_authenticated:
        return redirect('/login/')
    return render(request, 'service-details.html')

def sell(request):
    if not request.user.is_authenticated:
        return redirect('/login/')
    if request.method == 'POST':
       product = request.POST['product']
       quantity = request.POST['quantity']
       price = request.POST['price']
       cur_profile=Profile.objects.filter(user=request.user).first()
       phone = request.POST['phone']
       country = request.POST['Country']
       state = request.POST['State']
       district = request.POST['District']
       pin_code = request.POST['pin-code']
       prodId="prod"+str(Product.objects.all().count()+1)
       ins = Product(profile=cur_profile,prodId=prodId,product_name=product, product_quantity=quantity, product_price=price, phone_number=phone, seller_country=country, seller_state=state, seller_district=district, pin_code=pin_code)
       ins.save()
       forSale = ForSale(profile=cur_profile,prodId=prodId,product_name=product, product_quantity=quantity, product_price=price, phone_number=phone, seller_country=country, seller_state=state, seller_district=district, pin_code=pin_code)
       forSale.save()
       message=f"Congratulations! Your offer has been placed, let's hope to connect you to a buyer! Happy Trading :).\nThe details of the offer are :\nProduct Id: {prodId}\nProduct Name: {product}\n Product Quantity: {quantity} Quintal \n Product Price: Rs. {price} per Quintal"
       conn = http.client.HTTPSConnection("kptries-otp-api.herokuapp.com")
       payload = 'phone='+str(cur_profile.mobile)+'&message='+message
       headers = {
       'Content-Type': 'application/x-www-form-urlencoded'
       }
       conn.request("POST", "/message", payload, headers)
       res = conn.getresponse()
       return redirect("/option/")
    return render(request, 'sell.html')

def buy(request):
    if not request.user.is_authenticated:
        return redirect('/login/')
    if request.method == 'POST':
        name=request.POST['product']
        pincode=request.POST['pincode']
        p=PincodeDistance()
        products=Product.objects.filter(product_name=name)
        dist=[]
        for product in products:
            dis=p.getDistance(pincode,product.pin_code)
            while 1:
                if dist.count(dis) > 0:
                    dis+=1
                else:
                    break
            dist.append(dis)
        sorted_prod= [x for _,x in sorted(zip(dist,products))]
        context = {
        'products': sorted_prod,
        }
        return render(request,"view_store.html",context)
    return render(request, 'buy.html')

def confirm(request):
    if not request.user.is_authenticated:
        return redirect('/login/')
    return redirect("/confirm/")
    print(request.user)

def confirmBuy(request):
    if not request.user.is_authenticated:
        return redirect('/login/')
    if request.method == 'GET':
        prodId=request.GET['product']
        product=Product.objects.get(prodId=prodId)
        forSale=ForSale.objects.get(prodId=prodId)
        cur_profile=Profile.objects.filter(user=request.user).first()
        buyer = Bought(profile=cur_profile,seller_name=product.profile.user.first_name,prodId=product.prodId,product_name=product.product_name, product_quantity=product.product_quantity, product_price=product.product_price, phone_number=product.phone_number, seller_country=product.seller_country, seller_state=product.seller_state, seller_district=product.seller_district, pin_code=product.pin_code)
        buyer.save()
        seller = Sold(profile=product.profile,buyer_name=cur_profile.user.first_name,buyer_phone=cur_profile.mobile,prodId=product.prodId,product_name=product.product_name, product_quantity=product.product_quantity, product_price=product.product_price, phone_number=product.phone_number, seller_country=product.seller_country, seller_state=product.seller_state, seller_district=product.seller_district, pin_code=product.pin_code)
        seller.save()
        product.product_name=product.product_name+" Sold"
        product.save()
        forSale.delete()
        message=f"Congratulations! Your order has been placed, Please connect to your seller! Happy Trading :).\nThe details of the farmer are \nFarmer Name: {product.profile.user.first_name}\n  Farmer Phone Number: {product.profile.mobile}\nProduct Id: {product.prodId}\nProduct Name: {product.product_name}\n Product Quantity: {product.product_quantity} Quintal\n Product Price: Rs. {product.product_price} per Quintal"
        conn = http.client.HTTPSConnection("kptries-otp-api.herokuapp.com")
        payload = 'phone='+str(cur_profile.mobile)+'&message='+message
        headers = {
        'Content-Type': 'application/x-www-form-urlencoded'
        }
        conn.request("POST", "/message", payload, headers)
        res = conn.getresponse()
        message=f"Congratulations! We see a buyer for your crop! Please connect to your Buyer! Happy Trading :).\nThe details of the Buyer are \nBuyer Name: {cur_profile.user.first_name}\n  Buyer Phone Number: {cur_profile.mobile}\nProduct Id: {product.prodId}\nProduct Name: {product.product_name}\n Product Quantity: {product.product_quantity} Quintal\n Product Price: Rs. {product.product_price} per Quintal"
        payload = 'phone='+str(product.profile.mobile)+'&message='+message
        conn.request("POST", "/message", payload, headers)
        res = conn.getresponse()
    return redirect("/home/")

def buyer(request):
    if not request.user.is_authenticated:
        return redirect('/login/')
    return render(request, 'buyer.html')

def profile(request):
    if not request.user.is_authenticated:
        return redirect('/login/')
    if request.method=="POST":
        mobile = request.POST.get('mobile')
        pincode=request.POST.get('pincode')
        name=request.POST.get('name')
        city=request.POST.get('city')
        user = request.user
        profile = Profile.objects.get(user = user)
        user.first_name=name
        profile.mobile=mobile
        profile.pincode=pincode
        profile.user.first_name=name
        profile.city=city
        user.save()
        profile.save()
        login(request,user)
    profile=Profile.objects.filter(user=request.user).first()
    context = {
        'profile': profile,
        }
    return render(request, 'profile.html',context)

def loginp(request):
    if request.method=="POST":
        mobile = request.POST.get('mobile')
        conn = http.client.HTTPSConnection("kptries-otp-api.herokuapp.com")
        payload = 'number='+str(mobile)
        headers = {
        'Content-Type': 'application/x-www-form-urlencoded'
        }
        conn.request("POST", "/send", payload, headers)
        res = conn.getresponse()
        data = res.read()
        print(data.decode("utf-8"))
        request.session['mobile']=mobile
        return redirect("/otp/")
    return render(request, 'login.html')

def signup(request):
    # is_auth=request.session['auth']
    # if is_auth is not "true":
        # return redirect("/login/")
    if request.method=="POST":
        mobile = request.session['mobile']
        pincode=request.POST.get('pincode')
        name=request.POST.get('name')
        city=request.POST.get('city')
        role=request.POST.get('role')
        userId="user"+str(Profile.objects.all().count()+1)
        user = User(username = userId,first_name=name)
        user.save()
        profile = Profile(user = user , mobile=mobile , pincode=pincode,role=role,city=city)
        profile.save()
        login(request,user)
        return redirect("/home/")
    return render(request, 'signup.html')

def otp(request):
    if request.method=="POST":
        mobile = request.session['mobile']
        otp=request.POST.get('OTP')
        conn = http.client.HTTPSConnection("kptries-otp-api.herokuapp.com")
        payload = 'number='+str(mobile)+'&otp='+str(otp)
        headers = {
        'Content-Type': 'application/x-www-form-urlencoded'
        }
        conn.request("POST", "/verify", payload, headers)
        res = conn.getresponse()
        data = res.read().decode('utf-8')
        if data.find("Success")!=-1:
            profile = Profile.objects.filter(mobile = mobile).first()
            if not profile:
                # request.session['auth']="true"
                return redirect("/signup/")
            login(request,profile.user)
            return redirect("/home/")
        else:
            return redirect("/invalid")
    return render(request,'otp.html')

def invalid(request):
    return render(request,'invalid.html')

def my_logout(request):
    logout(request)
    return redirect('login')

def analyticsBought(request):
    if not request.user.is_authenticated:
        return redirect('/login/')
    profile=Profile.objects.filter(user=request.user).first()
    products=Bought.objects.filter(profile=profile)
    context = {
        'products': products,
        }
    return render(request, 'analytics_buyer.html',context)

def analyticsSold(request):
    if not request.user.is_authenticated:
        return redirect('/login/')
    profile=Profile.objects.filter(user=request.user).first()
    products=Sold.objects.filter(profile=profile)
    context = {
        'products': products,
        }
    return render(request, 'analytics_seller.html',context)

def analyticsForSale(request):
    if not request.user.is_authenticated:
        return redirect('/login/')
    profile=Profile.objects.filter(user=request.user).first()
    products=ForSale.objects.filter(profile=profile)
    context = {
        'products': products,
        }
    return render(request, 'analytics_forSale.html',context)

def analyticsOption(request):
    if not request.user.is_authenticated:
        return redirect('/login/')
    return render(request, 'analytics_option.html')


from pyspark.ml import Pipeline
from pyspark.ml.evaluation import MulticlassClassificationEvaluator
from pyspark.ml.feature import VectorIndexer, IndexToString, StringIndexer

from Context import getPipelineModelFilePath
from Models.ClassificationSummaryResult import ClassificationSummaryResult
from Models.SummaryResult import SummaryResult
from SparkVersion.ml.classification.DecisionTreeClassificationModel import DecisionTreeClassificationModel
from SparkVersion.ml.classification.LogisticRegressionModel import LogisticRegressionModel
from SparkVersion.ml.classification.NaiveBayesClassificationModel import NaiveBayesClassificationModel
from SparkVersion.ml.classification.RandomForrestClassificationModel import RandomForrestClassificationModel

class Classificators:
    def __init__(self, data, train, test, maxCategories=4):
        self.data = data
        self.train = train
        self.test = test
        self.maxCategories = maxCategories

    @staticmethod
    def chooseMethod(method, hyperparameters):
        if method == "Logistic regression":
            return LogisticRegressionModel(hyperparameters).prepareModel()
        elif method == "Decision tree classifier":
            return DecisionTreeClassificationModel(hyperparameters).prepareModel()
        elif method == "Random forest classifier":
            return RandomForrestClassificationModel(hyperparameters).prepareModel()
        else:
            return NaiveBayesClassificationModel(hyperparameters).prepareModel()

    def fitAndTransform(self, regressionModel):
        pipeline = Pipeline(stages=[self.labelIndexer(), self.featureIndexer(), regressionModel, self.labelConverter()])
        model = pipeline.fit(self.train)
        predictions = model.transform(self.test)
        model.write().overwrite().save(getPipelineModelFilePath())
        return predictions

    @staticmethod
    def calculateMetrics(predictions):
        evaluator = MulticlassClassificationEvaluator(
            labelCol="indexedLabel",
            predictionCol="prediction"
        )
        accuracy = evaluator.setMetricName("accuracy").evaluate(predictions)
        precision = evaluator.setMetricName("weightedPrecision").evaluate(predictions)
        f1 = evaluator.setMetricName("f1").evaluate(predictions)
        weightedRecall = evaluator.setMetricName("weightedRecall").evaluate(predictions)
        hammingLoss = evaluator.setMetricName("hammingLoss").evaluate(predictions)
        metrics = ClassificationSummaryResult(accuracy, 1.0 - accuracy, precision, f1, weightedRecall, hammingLoss)
        return SummaryResult(classificationMetrics=metrics)

    def labelIndexer(self):
        return StringIndexer(
            inputCol="label",
            outputCol="indexedLabel"
        ).fit(self.data)

    def featureIndexer(self):
        return VectorIndexer(
            inputCol="features",
            outputCol="indexedFeatures",
            maxCategories=self.maxCategories
        ).fit(self.data)

    def labelConverter(self):
        return IndexToString(
            inputCol="prediction",
            outputCol="predictedLabel",
            labels=self.labelIndexer().labels
        )

    @staticmethod
    def indexOfFeatureIndexer():
        return 1

    @staticmethod
    def predictionColumn():
        return "predictedLabel"


# -*- coding: utf-8 -*-
# import discord
import asyncio
import requests
import distutils.util
import os
import platform

from src.trackers.COMMON import COMMON
from src.console import console

class BLU():
    """
    Edit for Tracker:
        Edit BASE.torrent with announce and source
        Check for duplicates
        Set type/category IDs
        Upload
    """
    def __init__(self, config):
        self.config = config
        self.tracker = 'BLU'
        self.source_flag = 'BLU'
        self.search_url = 'https://blutopia.cc/api/torrents/filter'
        self.torrent_url = 'https://blutopia.cc/api/torrents/'
        self.upload_url = 'https://blutopia.cc/api/torrents/upload'
        self.signature = f"\n[center][url=https://blutopia.cc/forums/topics/3087]Created by L4G's Upload Assistant[/url][/center]"
        self.banned_groups = [
            '[Oj]', '3LTON', '4yEo', 'AFG', 'AniHLS', 'AnimeRG', 'AniURL', 'AROMA', 'aXXo', 'Brrip', 'CM8', 'CrEwSaDe', 'd3g', 'DeadFish', 'DNL', 'ELiTE', 'eSc', 'FaNGDiNG0', 'FGT', 'Flights',
            'FRDS', 'FUM', 'HAiKU', 'HD2DVD', 'HDS', 'HDTime', 'Hi10', 'ION10', 'iPlanet', 'JIVE', 'KiNGDOM', 'Leffe', 'LEGi0N', 'LOAD', 'MeGusta', 'mHD', 'mSD', 'NhaNc3', 'nHD', 'nikt0', 'NOIVTC',
            'nSD', 'PiRaTeS', 'playBD', 'PlaySD', 'playXD', 'PRODJi', 'RAPiDCOWS', 'RARBG', 'RDN', 'REsuRRecTioN', 'RMTeam', 'SANTi', 'SicFoI', 'SPASM', 'STUTTERSHIT', 'Telly', 'TM', 'TRiToN', 'UPiNSMOKE',
            'URANiME', 'WAF', 'x0r', 'xRed', 'XS', 'YIFY', 'ZKBL', 'ZmN', 'ZMNT',
            ['EVO', 'Raw Content Only'], ['TERMiNAL', 'Raw Content Only'], ['ViSION', 'Note the capitalization and characters used'], ['CMRG', 'Raw Content Only']
        ]

        pass

    async def upload(self, meta):
        common = COMMON(config=self.config)

        blu_name = meta['name']
        desc_header = ""
        if meta.get('webdv', False):
            blu_name, desc_header = await self.derived_dv_layer(meta)
        await common.edit_torrent(meta, self.tracker, self.source_flag)
        await common.unit3d_edit_desc(meta, self.tracker, self.signature, comparison=True, desc_header=desc_header)
        cat_id = await self.get_cat_id(meta['category'], meta.get('edition', ''))
        type_id = await self.get_type_id(meta['type'])
        resolution_id = await self.get_res_id(meta['resolution'])
        region_id = await common.unit3d_region_ids(meta.get('region'))
        distributor_id = await common.unit3d_distributor_ids(meta.get('distributor'))
        if meta['anon'] == 0 and bool(distutils.util.strtobool(str(self.config['TRACKERS'][self.tracker].get('anon', "False")))) == False:
            anon = 0
        else:
            anon = 1

        if meta['bdinfo'] != None:
            mi_dump = None
            bd_dump = open(f"{meta['base_dir']}/tmp/{meta['uuid']}/BD_SUMMARY_00.txt", 'r', encoding='utf-8').read()
        else:
            mi_dump = open(f"{meta['base_dir']}/tmp/{meta['uuid']}/MEDIAINFO.txt", 'r', encoding='utf-8').read()
            bd_dump = None
        desc = open(f"{meta['base_dir']}/tmp/{meta['uuid']}/[BLU]DESCRIPTION.txt", 'r').read()
        open_torrent = open(f"{meta['base_dir']}/tmp/{meta['uuid']}/[BLU]{meta['clean_name']}.torrent", 'rb')
        files = {'torrent': ("placeholder.torrent", open_torrent, "application/x-bittorrent")}
        data = {
            'name' : blu_name,
            'description' : desc,
            'mediainfo' : mi_dump,
            'bdinfo' : bd_dump,
            'category_id' : cat_id,
            'type_id' : type_id,
            'resolution_id' : resolution_id,
            'tmdb' : meta['tmdb'],
            'imdb' : meta['imdb_id'].replace('tt', ''),
            'tvdb' : meta['tvdb_id'],
            'mal' : meta['mal_id'],
            'igdb' : 0,
            'anonymous' : anon,
            'stream' : meta['stream'],
            'sd' : meta['sd'],
            'keywords' : meta['keywords'],
            'personal_release' : int(meta.get('personalrelease', False)),
            'internal' : 0,
            'featured' : 0,
            'free' : 0,
            'doubleup' : 0,
            'sticky' : 0,
        }
        # Internal
        if self.config['TRACKERS'][self.tracker].get('internal', False) == True:
            if meta['tag'] != "" and (meta['tag'][1:] in self.config['TRACKERS'][self.tracker].get('internal_groups', [])):
                data['internal'] = 1

        if region_id != 0:
            data['region_id'] = region_id
        if distributor_id != 0:
            data['distributor_id'] = distributor_id
        if meta.get('category') == "TV":
            data['season_number'] = meta.get('season_int', '0')
            data['episode_number'] = meta.get('episode_int', '0')
        headers = {
            'User-Agent': f'Upload Assistant/2.1 ({platform.system()} {platform.release()})'
        }
        params = {
            'api_token': self.config['TRACKERS'][self.tracker]['api_key'].strip()
        }

        if meta['debug'] == False:
            response = requests.post(url=self.upload_url, files=files, data=data, headers=headers, params=params)
            try:
                console.print(response.json())
            except:
                console.print("It may have uploaded, go check")

                return
        else:
            console.print(f"[cyan]Request Data:")
            console.print(data)
        open_torrent.close()

    async def get_cat_id(self, category_name, edition):
        category_id = {
            'MOVIE': '1',
            'TV': '2',
            'FANRES': '3'
            }.get(category_name, '0')
        if category_name == 'MOVIE' and 'FANRES' in edition:
            category_id = '3'
        return category_id

    async def get_type_id(self, type):
        type_id = {
            'DISC': '1',
            'REMUX': '3',
            'WEBDL': '4',
            'WEBRIP': '5',
            'HDTV': '6',
            'ENCODE': '12'
            }.get(type, '0')
        return type_id

    async def get_res_id(self, resolution):
        resolution_id = {
            '8640p':'10',
            '4320p': '11',
            '2160p': '1',
            '1440p' : '2',
            '1080p': '2',
            '1080i':'3',
            '720p': '5',
            '576p': '6',
            '576i': '7',
            '480p': '8',
            '480i': '9'
            }.get(resolution, '10')
        return resolution_id

    async def derived_dv_layer(self, meta):
        name = meta['name']
        desc_header = ""
        # Exit if not DV + HDR
        if not all([x in meta['hdr'] for x in ['HDR', 'DV']]):
            return name, desc_header
        import cli_ui
        console.print("[bold yellow]Generating the required description addition for Derived DV Layers. Please respond appropriately.")
        ask_comp = True
        if meta['type'] == "WEBDL":
            if cli_ui.ask_yes_no("Is the DV Layer sourced from the same service as the video?"):
                ask_comp = False
                desc_header = "[code]This release contains a derived Dolby Vision profile 8 layer. Comparisons not required as DV and HDR are from same provider.[/code]"

        if ask_comp:
            while desc_header == "":
                desc_input = cli_ui.ask_string("Please provide comparisons between HDR masters. (link or bbcode)", default="")
                desc_header = f"[code]This release contains a derived Dolby Vision profile 8 layer. Comparisons between HDR masters: {desc_input}[/code]"

        if "hybrid" not in name.lower():
            if "REPACK" in name:
                name = name.replace('REPACK', 'Hybrid REPACK')
            else:
                name = name.replace(meta['resolution'], f"Hybrid {meta['resolution']}")
        return name, desc_header

    async def search_existing(self, meta):
        dupes = []
        console.print("[yellow]Searching for existing torrents on site...")
        params = {
            'api_token' : self.config['TRACKERS'][self.tracker]['api_key'].strip(),
            'tmdbId' : meta['tmdb'],
            'categories[]' : await self.get_cat_id(meta['category'], meta.get('edition', '')),
            'types[]' : await self.get_type_id(meta['type']),
            'resolutions[]' : await self.get_res_id(meta['resolution']),
            'name' : ""
        }
        if meta['category'] == 'TV':
            params['name'] = params['name'] + f" {meta.get('season', '')}{meta.get('episode', '')}"
        if meta.get('edition', "") != "":
            params['name'] = params['name'] + f" {meta['edition']}"
        try:
            response = requests.get(url=self.search_url, params=params)
            response = response.json()
            for each in response['data']:
                result = [each][0]['attributes']['name']
                # difference = SequenceMatcher(None, meta['clean_name'], result).ratio()
                # if difference >= 0.05:
                dupes.append(result)
        except:
            console.print('[bold red]Unable to search for existing torrents on site. Either the site is down or your API key is incorrect')
            await asyncio.sleep(5)

        return dupes


import tabula
import os

cur_dir = os.path.abspath('.')
pdf_dir = '../../pdf'
csv_dir = '../../csv'

files = os.listdir(pdf_dir)
for file in files:
    name = file.split('.')[0]
    pdf_path = os.path.join(cur_dir, pdf_dir, file)
    csv_path = os.path.join(cur_dir, csv_dir, f'{name}.csv')
    try:
        tabula.convert_into(pdf_path, csv_path, output_format="csv", lattice=True, relative_area=True, pages='all')
    except KeyboardInterrupt:
        break
    except:
        # os.remove(pdf_path)
        print(pdf_path)


# Tests for Python charm helpers.

import unittest
import yaml
from testtools import TestCase

from io import StringIO

import sys
# Path hack to ensure we test the local code, not a version installed in
# /usr/local/lib.  This is necessary since /usr/local/lib is prepended before
# what is specified in PYTHONPATH.
sys.path.insert(0, 'helpers/python')
from charmhelpers.contrib import charmhelpers  # noqa

class CharmHelpersTestCase(TestCase):
    """A basic test case for Python charm helpers."""

    def _patch_command(self, replacement_command):
        """Monkeypatch charmhelpers.command for testing purposes.

        :param replacement_command: The replacement Callable for
                                    command().
        """
        self.patch(charmhelpers, 'command', lambda *args: replacement_command)

    def _make_juju_status_dict(self, num_units=1,
                               service_name='test-service',
                               unit_state='pending',
                               machine_state='not-started'):
        """Generate valid juju status dict and return it."""
        machine_data = {}
        # The 0th machine is the Zookeeper.
        machine_data[0] = {'dns-name': 'zookeeper.example.com',
                           'instance-id': 'machine0',
                           'state': 'not-started'}
        service_data = {'charm': 'local:precise/{}-1'.format(service_name),
                        'relations': {},
                        'units': {}}
        for i in range(num_units):
            # The machine is always going to be i+1 because there
            # will always be num_units+1 machines.
            machine_number = i + 1
            unit_machine_data = {
                'dns-name': 'machine{}.example.com'.format(machine_number),
                'instance-id': 'machine{}'.format(machine_number),
                'state': machine_state,
                'instance-state': machine_state}
            machine_data[machine_number] = unit_machine_data
            unit_data = {
                'machine': machine_number,
                'public-address':
                '{}-{}.example.com'.format(service_name, i),
                'relations': {'db': {'state': 'up'}},
                'agent-state': unit_state}
            service_data['units']['{}/{}'.format(service_name, i)] = (
                unit_data)
        juju_status_data = {'machines': machine_data,
                            'services': {service_name: service_data}}
        return juju_status_data

    def _make_juju_status_yaml(self, num_units=1,
                               service_name='test-service',
                               unit_state='pending',
                               machine_state='not-started'):
        """Convert the dict returned by `_make_juju_status_dict` to YAML."""
        return yaml.dump(
            self._make_juju_status_dict(
                num_units, service_name, unit_state, machine_state))

    def test_make_charm_config_file(self):
        # make_charm_config_file() writes the passed configuration to a
        # temporary file as YAML.
        charm_config = {'foo': 'bar',
                        'spam': 'eggs',
                        'ham': 'jam'}
        # make_charm_config_file() returns the file object so that it
        # can be garbage collected properly.
        charm_config_file = charmhelpers.make_charm_config_file(charm_config)
        with open(charm_config_file.name) as config_in:
            written_config = config_in.read()
        self.assertEqual(yaml.dump(charm_config), written_config)

    def test_unit_info(self):
        # unit_info returns requested data about a given service.
        juju_yaml = self._make_juju_status_yaml()
        self.patch(charmhelpers, 'juju_status', lambda: juju_yaml)
        self.assertEqual(
            'pending',
            charmhelpers.unit_info('test-service', 'agent-state'))

    def test_unit_info_returns_empty_for_nonexistent_service(self):
        # If the service passed to unit_info() has not yet started (or
        # otherwise doesn't exist), unit_info() will return an empty
        # string.
        juju_yaml = "services: {}"
        self.patch(charmhelpers, 'juju_status', lambda: juju_yaml)
        self.assertEqual(
            '', charmhelpers.unit_info('test-service', 'state'))

    def test_unit_info_accepts_data(self):
        # It's possible to pass a `data` dict, containing the parsed
        # result of juju status, to unit_info().
        juju_status_data = yaml.safe_load(
            self._make_juju_status_yaml())
        self.patch(charmhelpers, 'juju_status', lambda: None)
        service_data = juju_status_data['services']['test-service']
        unit_info_dict = service_data['units']['test-service/0']
        for key, value in unit_info_dict.items():
            item_info = charmhelpers.unit_info(
                'test-service', key, data=juju_status_data)
            self.assertEqual(value, item_info)

    def test_unit_info_returns_first_unit_by_default(self):
        # By default, unit_info() just returns the value of the
        # requested item for the first unit in a service.
        juju_yaml = self._make_juju_status_yaml(num_units=2)
        self.patch(charmhelpers, 'juju_status', lambda: juju_yaml)
        unit_address = charmhelpers.unit_info(
            'test-service', 'public-address')
        self.assertEqual('test-service-0.example.com', unit_address)

    def test_unit_info_accepts_unit_name(self):
        # By default, unit_info() just returns the value of the
        # requested item for the first unit in a service. However, it's
        # possible to pass a unit name to it, too.
        juju_yaml = self._make_juju_status_yaml(num_units=2)
        self.patch(charmhelpers, 'juju_status', lambda: juju_yaml)
        unit_address = charmhelpers.unit_info(
            'test-service', 'public-address', unit='test-service/1')
        self.assertEqual('test-service-1.example.com', unit_address)

    def test_get_machine_data(self):
        # get_machine_data() returns a dict containing the machine data
        # parsed from juju status.
        juju_yaml = self._make_juju_status_yaml()
        self.patch(charmhelpers, 'juju_status', lambda: juju_yaml)
        machine_0_data = charmhelpers.get_machine_data()[0]
        self.assertEqual('zookeeper.example.com', machine_0_data['dns-name'])

    def test_wait_for_machine_returns_if_machine_up(self):
        # If wait_for_machine() is called and the machine(s) it is
        # waiting for are already up, it will return.
        juju_yaml = self._make_juju_status_yaml(machine_state='running')
        self.patch(charmhelpers, 'juju_status', lambda: juju_yaml)
        machines, time_taken = charmhelpers.wait_for_machine(timeout=1)
        self.assertEqual(1, machines)

    def test_wait_for_machine_times_out(self):
        # If the machine that wait_for_machine is waiting for isn't
        # 'running' before the passed timeout is reached,
        # wait_for_machine will raise an error.
        juju_yaml = self._make_juju_status_yaml()
        self.patch(charmhelpers, 'juju_status', lambda: juju_yaml)
        self.assertRaises(
            RuntimeError, charmhelpers.wait_for_machine, timeout=0)

    def test_wait_for_machine_always_returns_if_running_locally(self):
        # If juju is actually running against a local LXC container,
        # wait_for_machine will always return.
        juju_status_dict = self._make_juju_status_dict()
        # We'll update the 0th machine to make it look like it's an LXC
        # container.
        juju_status_dict['machines'][0]['dns-name'] = 'localhost'
        juju_yaml = yaml.dump(juju_status_dict)
        self.patch(charmhelpers, 'juju_status', lambda: juju_yaml)
        machines, time_taken = charmhelpers.wait_for_machine(timeout=1)
        # wait_for_machine will always return 1 machine started here,
        # since there's only one machine to start.
        self.assertEqual(1, machines)
        # time_taken will be 0, since no actual waiting happened.
        self.assertEqual(0, time_taken)

    def test_wait_for_machine_waits_for_multiple_machines(self):
        # wait_for_machine can be told to wait for multiple machines.
        juju_yaml = self._make_juju_status_yaml(
            num_units=2, machine_state='running')
        self.patch(charmhelpers, 'juju_status', lambda: juju_yaml)
        machines, time_taken = charmhelpers.wait_for_machine(num_machines=2)
        self.assertEqual(2, machines)

    def test_wait_for_unit_returns_if_unit_started(self):
        # wait_for_unit() will return if the service it's waiting for is
        # already up.
        juju_yaml = self._make_juju_status_yaml(
            unit_state='started', machine_state='running')
        self.patch(charmhelpers, 'juju_status', lambda: juju_yaml)
        charmhelpers.wait_for_unit('test-service', timeout=0)

    def test_wait_for_unit_raises_error_on_error_state(self):
        # If the unit is in some kind of error state, wait_for_unit will
        # raise a RuntimeError.
        juju_yaml = self._make_juju_status_yaml(
            unit_state='start-error', machine_state='running')
        self.patch(charmhelpers, 'juju_status', lambda: juju_yaml)
        self.assertRaises(RuntimeError, charmhelpers.wait_for_unit,
                          'test-service', timeout=0)

    def test_wait_for_unit_raises_error_on_timeout(self):
        # If the unit does not start before the timeout is reached,
        # wait_for_unit will raise a RuntimeError.
        juju_yaml = self._make_juju_status_yaml(
            unit_state='pending', machine_state='running')
        self.patch(charmhelpers, 'juju_status', lambda: juju_yaml)
        self.assertRaises(RuntimeError, charmhelpers.wait_for_unit,
                          'test-service', timeout=0)

    def test_wait_for_relation_returns_if_relation_up(self):
        # wait_for_relation() waits for relations to come up. If a
        # relation is already 'up', wait_for_relation() will return
        # immediately.
        juju_yaml = self._make_juju_status_yaml(
            unit_state='started', machine_state='running')
        self.patch(charmhelpers, 'juju_status', lambda: juju_yaml)
        charmhelpers.wait_for_relation('test-service', 'db', timeout=0)

    def test_wait_for_relation_times_out_if_relation_not_present(self):
        # If a relation does not exist at all before a timeout is
        # reached, wait_for_relation() will raise a RuntimeError.
        juju_dict = self._make_juju_status_dict(
            unit_state='started', machine_state='running')
        units = juju_dict['services']['test-service']['units']
        # We'll remove all the relations for test-service for this test.
        units['test-service/0']['relations'] = {}
        juju_dict['services']['test-service']['units'] = units
        juju_yaml = yaml.dump(juju_dict)
        self.patch(charmhelpers, 'juju_status', lambda: juju_yaml)
        self.assertRaises(
            RuntimeError, charmhelpers.wait_for_relation, 'test-service',
            'db', timeout=0)

    def test_wait_for_relation_times_out_if_relation_not_up(self):
        # If a relation does not transition to an 'up' state, before a
        # timeout is reached, wait_for_relation() will raise a
        # RuntimeError.
        juju_dict = self._make_juju_status_dict(
            unit_state='started', machine_state='running')
        units = juju_dict['services']['test-service']['units']
        units['test-service/0']['relations']['db']['state'] = 'down'
        juju_dict['services']['test-service']['units'] = units
        juju_yaml = yaml.dump(juju_dict)
        self.patch(charmhelpers, 'juju_status', lambda: juju_yaml)
        self.assertRaises(
            RuntimeError, charmhelpers.wait_for_relation, 'test-service',
            'db', timeout=0)

    def test_wait_for_page_contents_returns_if_contents_available(self):
        # wait_for_page_contents() will wait until a given string is
        # contained within the results of a given url and will return
        # once it does.
        # We need to patch the charmhelpers instance of urlopen so that
        # it doesn't try to connect out.
        test_content = "Hello, world."
        self.patch(charmhelpers, 'urlopen',
                   lambda *args: StringIO(test_content))
        charmhelpers.wait_for_page_contents(
            'http://example.com', test_content, timeout=0)

    def test_wait_for_page_contents_times_out(self):
        # If the desired contents do not appear within the page before
        # the specified timeout, wait_for_page_contents() will raise a
        # RuntimeError.
        # We need to patch the charmhelpers instance of urlopen so that
        # it doesn't try to connect out.
        self.patch(charmhelpers, 'urlopen',
                   lambda *args: StringIO("This won't work."))
        self.assertRaises(
            RuntimeError, charmhelpers.wait_for_page_contents,
            'http://example.com', "This will error", timeout=0)

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


#!/usr/bin/python -tt
from werkzeug.wrappers import Response, Request
from werkzeug.serving import run_simple
import posixpath, urllib, os, mimetypes, json, sqlite3
from operator import attrgetter
import get_events, datetime
import combine
import time

from feed.date.rfc3339 import *

def manage_db():
	conn = sqlite3.connect('example.db')
	conn.row_factory = sqlite3.Row
	c = conn.cursor()
	yield c
	conn.commit()
	conn.close()

def application(environ, start_response):
	request = Request(environ)
	path = translate_path(request.path)
	try:
		f = open(path,'rb')
	except IOError:
		if "login" in request.path:
			response = Response()
			response.status_code = 301
			response.location = get_events.login()
			return response(environ, start_response)
		if "callback" in request.path:
			email = get_events.callback(request.args.get("code"))
			response = Response()
			response.status_code = 301
			response.location = "/static/app.html"
			response.set_cookie("blah",email,expires=datetime.datetime.now() + datetime.timedelta(days=1))
			return response(environ, start_response)
		if "addtask" in request.path:
			deadline = tf_from_timestamp(request.form["deadline"]) * 1000
			name = request.form["name"]
			hours = int(request.form["hours"])
			minutes = int(request.form["minutes"])
			duration = 3600*hours + 60*minutes
			repeat = request.form["repeat"]
			new_task = {"summary":name,"deadline":deadline,"duration":duration,"repeat":repeat}
			email = request.cookies.get("blah")
			for c in manage_db():
				c.execute("select * from events where email=?",(email,))
				curr_tasks = []
				result = c.fetchone()
				if result['tasks'] is not None:
					curr_tasks = json.loads(result['tasks'])
				curr_tasks.append(new_task)
				curr_tasks.sort(key=lambda x: x["duration"], reverse=True)
				curr_tasks.sort(key=lambda x: x["deadline"])
				c.execute("insert or replace into events (tasks,email,data) values (:tasks,:email,(select data from events where email=:email))",{"email":email,"tasks":json.dumps(curr_tasks)})
			response = Response("a")
			return response(environ, start_response)
		if "removetasks" in request.path:
			position = int(request.form["position"])
			email = request.cookies.get("blah")
			response = Response()
			for c in manage_db():
				c.execute("select * from events where email=?",(email,))
				result = c.fetchone()
				if result['tasks'] is not None:
					curr_tasks = json.loads(result['tasks'])
					del curr_tasks[position]
					c.execute("insert or replace into events (tasks,email,data) values (:tasks,:email,(select data from events where email=:email))",{"email":email,"tasks":json.dumps(curr_tasks)})
				else:
					response.status_code = 404
			return response(environ, start_response)
		if "edittask" in request.path:
			deadline = tf_from_timestamp(request.form["deadline"]) * 1000
			name = request.form["name"]
			hours = int(request.form["hours"])
			minutes = int(request.form["minutes"])
			duration = 3600*hours + 60*minutes
			position = int(request.form["position"])
			repeat = request.form["repeat"]
			new_task = {"summary":name,"deadline":deadline,"duration":duration,"repeat":repeat}
			email = request.cookies.get("blah")
			response = Response()
			for c in manage_db():
				c.execute("select * from events where email=?",(email,))
				result = c.fetchone()
				curr_tasks = json.loads(result['tasks'])
				curr_tasks[position] = new_task
				c.execute("insert or replace into events (tasks,email,data) values (:tasks,:email,(select data from events where email=:email))",{"email":email,"tasks":json.dumps(curr_tasks)})
			return response(environ, start_response)
		if "loadtasks" in request.path:
			email = request.cookies.get("blah")
			for c in manage_db():
				c.execute("select tasks from events where email=?",(email,))
				result = c.fetchone()
			tasks = json.loads(result['tasks'])
			currtime = time.time() * 1000
			for task in tasks:
				if task['deadline'] < currtime or task['duration'] == 0:
					task['showAlertPane'] = True
			response = Response(json.dumps(tasks), mimetype="application/json")
			return response(environ, start_response)
		if "loadall" in request.path:
			email = request.cookies.get("blah")
			for c in manage_db():
				c.execute("select * from events where email=?",(email,))
				result = c.fetchone()
				events = json.loads(result['data'])
				tasks = json.loads(result['tasks'])
			currtime = time.time() * 1000
			tasks = [task for task in tasks if not (task['deadline'] < currtime or task['duration'] == 0)]
			events = [combine.Thing(x) for x in events]
			tasks = [combine.Thing(x) for x in tasks]
			events = sorted(events,key=lambda k:k.start)
			tasks = sorted(tasks,key=lambda k:k.start)
			toReturn = combine.combine(events,tasks)
			toReturn = [x.__dict__ for x in toReturn]
			globalError = False
			for elem in toReturn:
				if elem["type"] != 3:
					if elem["start"] < currtime:
						globalError = True
			response = Response(json.dumps({"error":globalError,"data":toReturn}), mimetype="application/json")
			return response(environ, start_response)
		if "startstop" in request.path:
			email = request.cookies.get("blah")
			position = int(request.form["position"])
			taskInProgress = False
			for c in manage_db():
				c.execute("select tasks from events where email=?",(email,))
				tasks = json.loads(c.fetchone()['tasks'])
				for task in tasks:
					if "startedTime" in task:
						taskInProgress = True
						task["duration"] = max(task["duration"] - (time.time() - task["startedTime"]),0)
						del task["startedTime"]
						c.execute("insert or replace into events (tasks,email,data) values (:tasks,:email,(select data from events where email=:email))",{"email":email,"tasks":json.dumps(tasks)})
						break
				if not taskInProgress:
					tasks[position]["startedTime"] = time.time()
					c.execute("insert or replace into events (tasks,email,data) values (:tasks,:email,(select data from events where email=:email))",{"email":email,"tasks":json.dumps(tasks)})
			response = Response()
			return response(environ, start_response)
		# if not, 404 this sucker
		response = Response()
		response.status_code = 404
		return response(environ, start_response)
	response = Response(f.read(), mimetype=mimetypes.guess_type(request.path)[0])
	return response(environ, start_response)

def translate_path(path):
	# abandon query parameters
	path = path.split('?',1)[0]
	path = path.split('#',1)[0]
	path = posixpath.normpath(urllib.unquote(path))
	words = path.split('/')
	words = filter(None, words)
	path = os.getcwd()
	for word in words:
		drive, word = os.path.splitdrive(word)
		head, word = os.path.split(word)
		if word in (os.curdir, os.pardir): continue
		path = os.path.join(path, word)
	return path

"""
def guess_type(path):
   extensions_map = mimetypes.types_map.copy()
   extensions_map.update({
        '': 'application/octet-stream', # Default
        '.c': 'text/plain',
        '.h': 'text/plain',
        })
   base, ext = posixpath.splitext(path)
   print base,ext
   if ext in extensions_map:
	   return extensions_map[ext]
   ext = ext.lower()
   if ext in extensions_map:
	   return extensions_map[ext]
   else:
	   return extensions_map['']
"""

if __name__ == '__main__':
	run_simple('0.0.0.0', 80, application, use_debugger=True, use_reloader=True)


from channels.generic.websocket import AsyncWebsocketConsumer
from dotenv import load_dotenv
from deepgram import Deepgram
from typing import Dict

import os

load_dotenv()

class TranscriptConsumer(AsyncWebsocketConsumer):
   dg_client = Deepgram(os.getenv('DEEPGRAM_API_KEY'))

   async def get_transcript(self, data: Dict) -> None:
       if 'channel' in data:
           transcript = data['channel']['alternatives'][0]['transcript']

           if transcript:
               await self.send(transcript)

   async def connect_to_deepgram(self):
       try:
           self.socket = await self.dg_client.transcription.live({'punctuate': True, 'interim_results': False})
           self.socket.registerHandler(self.socket.event.CLOSE, lambda c: print(f'Connection closed with code {c}.'))
           self.socket.registerHandler(self.socket.event.TRANSCRIPT_RECEIVED, self.get_transcript)

       except Exception as e:
           raise Exception(f'Could not open socket: {e}')

   async def connect(self):
       await self.connect_to_deepgram()
       await self.accept()

   async def disconnect(self, close_code):
       await self.channel_layer.group_discard(
           self.room_group_name,
           self.channel_name
       )

   async def receive(self, bytes_data):
       self.socket.send(bytes_data)


"""
This script has the only porpouse of creating CSV's containing the pixel information of
each face from the folder of train and test data.
"""

# L I B R A R I E S
import numpy as np
import pandas as pd
import cv2 as cv
from glob import glob

# F U N C T I O N S
def images_to_dataframe(label,file_path):
    dataframe = pd.DataFrame()
    count = 1
    list_all_images = glob(pathname=file_path)
    for image in list_all_images:
        img = cv.imread(image)
        img_gray = cv.cvtColor(img, cv.COLOR_BGR2GRAY)
        row = np.append(label,img_gray.flatten())
        row = row.reshape(1,-1)
        dataframe = dataframe.append(pd.DataFrame(row))
        print(f'{count} out of {len(list_all_images)} from {file_path}')
        count += 1
    return dataframe

def split_train_test_dataframes(dataframe, train_prop=0.75, test_prop=0.25):
    """
    ...
    """

    validate_prop = train_prop + test_prop
    if validate_prop != 1:
        print('train_prop + test_prop should greater than zero. Instead is {}'.format(validate_prop))
        return None,None,None

    n_obs = dataframe.shape[0]

    idx_train = int(n_obs * train_prop)

    df_train = dataframe.iloc[ :idx_train, : ]
    df_test = dataframe.iloc[ idx_train: , : ]

    return df_train, df_test

def create_final_dataframe(list_dataframes):
    df = pd.concat(list_dataframes)
    df = df.sample(frac=1).reset_index(drop=True)
    return df

# C O D E
file_path_positive = '../data/faces/positives/*.jpg'
file_path_negative = '../data/faces/negatives/*.jpg'

file_path_train_data = '../data/training_data.csv'
file_path_test_data = '../data/testing_data.csv'

# Create positive and negative dataframes
df_positive = images_to_dataframe(label=1,file_path=file_path_positive)
print(' ')
df_negative = images_to_dataframe(label=0, file_path=file_path_negative)
print(' ')

# Create train, test and validation data
df_positive_train, df_positive_test = split_train_test_dataframes(dataframe=df_positive)
df_negative_train, df_negative_test = split_train_test_dataframes(dataframe=df_negative)

# Concatenate into three dataframes
df_train = create_final_dataframe(list_dataframes=[df_positive_train, df_negative_train])
df_test = create_final_dataframe(list_dataframes=[df_positive_test, df_negative_test])

df_train.to_csv(file_path_train_data, index=False, header=False)
print('Training data -> DONE')
df_test.to_csv(file_path_test_data, index= False, header= False)
print('Test data -> DONE')


import torch
import numpy as np
import matplotlib.pyplot as plt
import torchmetrics
import torch.nn as nn
import torch.nn.functional as F

device = torch.device("cuda" if torch.cuda.is_available() else "cpu")

def process_image(img):
    tmp = img
    if tmp.shape[1] == 13:
        tmp = convert_to_segmentation(tmp)
    elif tmp.shape[1] == 4 or tmp.shape[1] == 1:
        tmp = tmp[:,:3,:,:].permute(0,2,3,1)
    return tmp.cpu()

def show_result(G, x_, y_, num_epoch):
    preds = process_image(G(x_))
    x = process_image(x_)
    y = process_image(y_)
    fig, ax = plt.subplots(x_.shape[0], 3, figsize=(6,10))
    for i in range(x_.shape[0]):
        ax[i, 0].get_xaxis().set_visible(False)
        ax[i, 0].get_yaxis().set_visible(False)
        ax[i, 1].get_xaxis().set_visible(False)
        ax[i, 1].get_yaxis().set_visible(False)
        ax[i, 2].get_xaxis().set_visible(False)
        ax[i, 2].get_yaxis().set_visible(False)
        ax[i, 0].cla()
        ax[i, 0].imshow(x[i])
        ax[i, 1].cla()
        ax[i, 1].imshow(preds[i])
        ax[i, 2].cla()
        ax[i, 2].imshow(torch.round(y[i] * 12))

    plt.tight_layout()
    label_epoch = 'Epoch {0}'.format(num_epoch)
    fig.text(0.5, 0, label_epoch, ha='center')
    label_input = 'Input'
    fig.text(0.18, 1, label_input, ha='center')
    label_output = 'Output'
    fig.text(0.5, 1, label_output, ha='center')
    label_truth = 'Ground truth'
    fig.text(0.81, 1, label_truth, ha='center')

    plt.show()

def convert_to_one_hot(labels, classes=13, scale_values=True):
    """
    Convert a segmentation image to one hot encoding

    @params
    labels: (batch_size, height, width) or (batch_size, 1, height, width)
    classes: number of classes
    scale_values: if True, labels is multiplied by classes then rounded to the nearest integer before converting to one hot encoding.

    @note
    labels must be integers between 0 and classes-1 unless scale_values is True.
    If scale_values is True, labels must be between 0 and 1,
    and will be multiplied by classes before being converted to one hot encoding.

    @return
    one_hot_images: (batch_size, num_classes, height, width)
    """

    if len(labels.shape) == 4:
        labels = labels.squeeze(1)

    if scale_values:
        labels = torch.round(labels * (classes-1))

    one_hot = torch.nn.functional.one_hot(labels.long(), num_classes=classes)
    one_hot = torch.permute(one_hot, [0, 3, 1, 2]).float()

    return one_hot

def convert_to_segmentation(one_hot_images):
    """
    Convert a one hot encoding image to segmentation image

    @params
    one_hot_images: (batch_size, num_classes, height, width)

    @return
    segmentation_images: (batch_size, 1, height, width)
    """
    return torch.argmax(one_hot_images, dim=1)

def meanIoU(pred, target, classes):
    """
    Give the mean IoU of the image and the mask

    @params
        img: (batch_size, classes, height, width)
        mask: (batch_size, classes, height, width)
        classes: number of classes

    @note it is required that the values in img and mask and intergers between 0 and classes-1

    @returns
        mean IoU
    """

    # convert pred to segmentation and back to one hot encoding
    pred = convert_to_one_hot(convert_to_segmentation(pred), classes=classes, scale_values=False)

    intersection = torch.sum(pred * target, dim=(2, 3))
    union = torch.sum(pred, dim=(2, 3)) + torch.sum(target, dim=(2, 3)) - intersection
    iou = (intersection + 1e-8) / (union + 1e-8)
    iou = torch.mean(iou, dim=1)
    return torch.mean(iou)

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

    def forward(self, pred, target):
        pred = F.softmax(pred, dim=1)
        intersection = torch.sum(pred * target, dim=(1, 2, 3))
        union = torch.sum(pred, dim=(1, 2, 3)) + torch.sum(target, dim=(1, 2, 3))
        dice = (2 * intersection ) / (union + 1e-8)
        dice = torch.mean(1 - dice)
        return dice


"""
Module for the word select pane displayed on the left side of the search results window.
Displays the text from the search window and highlights the words not found in the search query.
"""

import re

from aqt.qt import *
from anki.hooks import runHook

from .. import utils

class WordSelect(QTextBrowser):
    """
    Text browser that accepts a string and a word model that defines what should be highlighted.
    Highlighting done via CSS.
    """

    STYLE = """
    body {
        font-size: 11pt;
        line-height: 1.5;
    }
    a {
        color: black;
        text-decoration: none !important;
    }
    .unknown {
        background: lightgreen;
    }
    """

    def __init__(self, text, word_model, parent=None):
        super().__init__(parent)
        self.text = text
        self.word_model = word_model
        self.build()
        self.setOpenLinks(False)
        self.anchorClicked.connect(self.intercept_click)

    def build(self):
        self.html = self.build_html()
        self.setText(self.html)

    def build_html(self):
        """
        Build the html which includes the stylesheet and the text.
        Use anchor  tags for new words.
        Use classname for CSS targeting/coloring and use href attr. for word string.
        On  link click, look at link's href to determine word clicked on.
        Somewhat of a hack, but also very simple.
        """

        html = ""
        tokens = utils.split_words(self.text)
        for token in tokens:
            if not utils.is_word(token):
                # Not a word, don't allow clicking
                # Render double spacing correctly in HTML
                token = re.sub(r"\n{2,}", "

", token)
                html += token
                continue
            known = self.word_model[token]["known"]
            if known:
                html += f"{token}"
            else:
                html += f" {token} "
        html += ""
        return html

    def set_word_model(self, word_model):
        self.word_model = word_model

    def intercept_click(self, link):
        """
        Listen for anchor  tag click, get word by looking at the href value.
        """

        word = link.toString()
        note_ids = self.word_model[word]["note_ids"]
        known = self.word_model[word]["known"]
        runHook("load_word", word, note_ids, known)

    def ignore_word(self, word):
        """
        Ignore word by changing the data model and re-rendering the html.
        """

        for token in self.word_model:
            if token.lower() == word.lower():
                self.word_model[token]["known"] = True
        self.build()


from fabric.contrib.files import append, exists, sed
from fabric.api import env, local, run
import random

REPO_URL = 'https://github.com/tuvttran/TDD_Python.git'

def deploy():
    site_folder = f'/home/{env.user}/sites/{env.host}'
    source_folder = site_folder + '/source'
    _create_directory_structure_if_necessary(site_folder)
    _get_latest_source(source_folder)
    _update_settings(source_folder, env.host)
    _update_virtualenv(source_folder)
    _update_static_files(source_folder)
    _update_database(source_folder)

def _create_directory_structure_if_necessary(site_folder):
    for subfolder in ('database', 'static', 'virtualenv', 'source'):
        run(f'mkdir -p {site_folder}/{subfolder}')

def _get_latest_source(source_folder):
    if exists(source_folder + '/.git'):
        run(f'cd {source_folder} && git fetch')
    else:
        run(f'git clone {REPO_URL} {source_folder}')
    current_commit = local("git log -n 1 --format=%H", capture=True)
    run(f'cd {source_folder} && git reset --hard {current_commit}')

def _update_settings(source_folder, site_name):
    settings_path = source_folder + '/superlists/settings.py'
    sed(settings_path, "DEBUG = True", "DEBUG = False")
    sed(
        settings_path,
        'ALLOWED_HOSTS =.+$',
        f'ALLOWED_HOSTS = ["{site_name}"]'
    )
    secret_key_file = source_folder + '/superlists/secret_key.py'
    if not exists(secret_key_file):
        chars = 'abcdefghijklmnopqrstuvwxyz0123456789!@#$%^&*(-_=+)'
        key = ''.join(random.SystemRandom().choice(chars) for _ in range(50))
        append(secret_key_file, f'SECRET_KEY = "{key}"')
    append(settings_path, '\nfrom .secret_key import SECRET_KEY')

def _update_virtualenv(source_folder):
    virtualenv_folder = source_folder + '/../virtualenv'
    if not exists(virtualenv_folder + '/bin/pip'):
        run(f'python3.6 -m venv {virtualenv_folder}')
    run(f'{virtualenv_folder}/bin/pip install -r {source_folder}/requirements.txt')

def _update_static_files(source_folder):
    run(
        f'cd {source_folder}'
        ' && ../virtualenv/bin/python manage.py collectstatic --noinput'
    )

def _update_database(source_folder):
    run(
        f'cd {source_folder}'
        ' && ../virtualenv/bin/python manage.py migrate --noinput'
    )


import pymongo
import json
from subscription import settings
import pymysql
from subscription.items import WeiboSubscription
from subscription.items import WechatSubscription

class WeiboMongoDao(object):
    def __init__(self):
        self.client = pymongo.MongoClient(settings.MONGO_HOST, settings.MONGO_PORT)
        self.db_auth = self.client.admin
        self.db_auth.authenticate(settings.MONGO_USERNAME, settings.MONGO_PSD)
        self.db = self.client[settings.MONGO_DB_NAME]
        self.collection = self.db[settings.MONGO_WEIBO_COLLECTION_NAME]

    def update_post_many(self, posts):
        for post in posts:
            self.collection.update({'_id': post['_id']}, post)

    def find_weibo_by_send_flag(self, send_flag):
        result = []
        cursor = self.collection.find({'send_flag': send_flag}).sort('mblog.created_at')
        for c in cursor:
            result.append(c)
        return result

    def insert_one(self, item):
        weibo_json = json.loads(item['json'])
        inserted = self.collection.find_one({'itemid': weibo_json['itemid']})

        if inserted is None:
            self.collection.insert_one(weibo_json)

class WechatMongoDao(object):
    def __init__(self):
        self.client = pymongo.MongoClient(settings.MONGO_HOST, settings.MONGO_PORT)
        self.db_auth = self.client.admin
        self.db_auth.authenticate(settings.MONGO_USERNAME, settings.MONGO_PSD)
        self.db = self.client[settings.MONGO_DB_NAME]
        self.collection = self.db[settings.MONGO_WECHAT_COLLECTION_NAME]

    def update_post_many(self, posts):
        for post in posts:
            self.collection.update({'_id': post['_id']}, post)

    def find_wechat_by_send_flag(self, send_flag):
        result = []
        cursor = self.collection.find({'send_flag': send_flag}).sort('datetime')
        for c in cursor:
            result.append(c)
        return result

    def find_wechat_by_id(self, id):
        document = self.collection.find_one({'_id': id})
        return document

    def insert_one(self, item):
        inserted = self.collection.find_one({'_id': item['_id']})

        if inserted is None:
            self.collection.insert_one(item)

class SubscriptionDao(object):
    def __init__(self):
        self.db = pymysql.connect(settings.MYSQL_HOST, settings.MYSQL_USER_NAME,
                                  settings.MYSQL_USER_PSD, settings.MYSQL_DB_NAME,
                                  charset='utf8', use_unicode=True)

    # 获取所有正在追踪的微博uid
    def get_all_uids(self):
        with self.db.cursor() as cursor:
            sql = 'SELECT uid, comment FROM weibo_subscription WHERE status = 0;'
            cursor.execute(sql)
            all = cursor.fetchall()
            result = []
            for i in all:
                item = WeiboSubscription()
                item['uid'] = i[0]
                item['comment'] = i[1]
                result.append(item)
            return result

    def get_all_wechat(self):
        with self.db.cursor() as cursor:
            sql = 'SELECT wechat_num, comment FROM weixin_subscription WHERE status = 0;'
            cursor.execute(sql)
            all = cursor.fetchall()
            result = []
            for i in all:
                item = WechatSubscription()
                item['wechat_num'] = i[0]
                item['comment'] = i[1]
                result.append(item)
            return result

    # 插入邮件发送记录
    def insert_mail_log(self, to_mail, from_mail, context, user_id, send_timestamp):
        with self.db.cursor() as cursor:
            sql = 'INSERT email_log (from_mail, to_mail, user_id, send_timestamp, context) ' \
                  'VALUE (%s,%s,%s,%s,%s);'
            cursor.execute(sql, (from_mail, to_mail, user_id, send_timestamp, context))
            self.db.commit()

    def close(self):
        self.db.close()


#(©)Codexbotz

from pyrogram import __version__
from bot import Bot
from config import OWNER_ID
from pyrogram.types import Message, InlineKeyboardMarkup, InlineKeyboardButton, CallbackQuery

@Bot.on_callback_query()
async def cb_handler(client: Bot, query: CallbackQuery):
    data = query.data
    if data == "about":
        await query.message.edit_text(
            text = f"○ ᴄʀᴇᴀᴛᴏʀ : sᴀɴᴅʏ\n○ ʟᴀɴɢᴜᴀɢᴇ : ᴘʏᴛʜᴏɴ𝟹\n○ ʟɪʙʀᴀʀʏ : ᴘʏʀᴏɢʀᴀᴍ ᴀsʏɴᴄɪᴏ {__version__}\n○ Channel : @MensBotz\n○ Support Group : @MensSupportt\n\n🔋 ᴘᴏᴡᴇʀᴇᴅ ʙʏ :- @MensBotZ",
            disable_web_page_preview = True,
            reply_markup = InlineKeyboardMarkup(
                [
                    [
                        InlineKeyboardButton("🔒 Close", callback_data = "close")
                    ]
                ]
            )
        )
    elif data == "close":
        await query.message.delete()
        try:
            await query.message.reply_to_message.delete()
        except:
            pass


from flask import Flask

app=Flask(__name__)

@app.route('/')
def index():
    # return '{"a":"a","b":"b"}'
    return  '''
    <note>
    <from>John</from>
    <heading>Reminder</heading>
    <body>Dont forget the meeting!</body>
    </note>
    '''

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


import unittest
from itertools import izip
from os.path import join

import geopandas as gpd

from context import src
from src import GetIncidentCounts

class TestGetIncidentCounts(unittest.TestCase):
    test_2006_fp = join('TestIncFiles', 'test_2006.csv')

    def test_get_call_data(self):
        pts, call_types = GetIncidentCounts.get_call_data(self.test_2006_fp)

        exp_pts = [
            (-80.93953700, 35.23977900),
            (-80.88772400, 35.21648000),
            (-80.90232300, 35.27923800),
            (-80.86900700, 35.20734900),
            (-80.91755900, 35.20787200),
        ]

        exp_calls = [
            u'611 Dispatched & canceled en route',
            u'321 EMS call, excluding vehicle accident with inju',
            u'531 Smoke or odor removal',
            u'321 EMS call, excluding vehicle accident with inju',
            u'321 EMS call, excluding vehicle accident with inju'
        ]
        pts = [(pt.x, pt.y) for pt in pts]
        for pt, exp_pt in izip(pts, exp_pts):
            self.assertAlmostEqual(pt[0], exp_pt[0], delta=0.0000001)
            self.assertAlmostEqual(pt[1], exp_pt[1], delta=0.0000001)

        self.assertListEqual(call_types, exp_calls)

class TestAverageCounts(unittest.TestCase):
    def test_avg_counts1(self):
        count_dict = \
            {0: {'2016':
                {
                    315: 5,
                    316: 35,
                    317: 65
                },
                '2015':
                    {
                        315: 10,
                        316: 40,
                        317: 70
                    },
                '2014':
                    {
                        315: 15,
                        316: 45,
                        317: 75
                    },
                '2013':
                    {
                        315: 20,
                        316: 50,
                        317: 80
                    },
                '2012':
                    {
                        315: 25,
                        316: 55,
                        317: 85,
                        318: 5
                    },
            }
            }

        year = '2016'
        all_inc_types = {315, 316, 317, 318}
        avg_counts = GetIncidentCounts.average_counts(count_dict, year, all_inc_types)
        exp_dict = \
            {0:
                {'2016':
                    {
                        315: 15,
                        316: 45,
                        317: 75,
                        318: 1
                    }
                }
            }
        self.assertDictEqual(avg_counts, exp_dict)

class TestDataFrame(unittest.TestCase):
    def test_generated_df_integrated(self):
        test_file = join('TestShpFile', 'MecklenburgCounty_NC_tract', 'MecklenburgCounty_NC_tract.shp')
        dem_gdf = gpd.read_file(test_file)
        exp_rec_nums_2006 = [207, 99, 44, 41, 35]
        exp_inc_types_2006 = ['611 Dispatched & canceled en route',
                              '321 EMS call, excluding vehicle accident with inju',
                              '531 Smoke or odor removal',
                              '321 EMS call, excluding vehicle accident with inju',
                              '321 EMS call, excluding vehicle accident with inju'
                              ]

        years = ['2006']
        base_shape = 'TRACTCE'
        all_counts, all_inc_types = GetIncidentCounts.get_all_counts(geo_df=dem_gdf, base_shape=base_shape,
                                                                     log_folder='TestIncFiles', years=years)
        geo_df = GetIncidentCounts.generate_count_gdf(geo_df=dem_gdf, base_shape=base_shape, all_counts=all_counts)

        self.assertSetEqual(all_inc_types, set(exp_inc_types_2006))
        for rn, it in izip(exp_rec_nums_2006, exp_inc_types_2006):
            val = geo_df.loc[rn, it]
            self.assertEqual(val, 1)

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


import setuptools

NAME = 'augmentation'
VERSION = '0.0.1'
DESCRIPTION = 'Exploring image augmentation'
AUTHOR = 'greyhypotheses'
URL = 'https://github.com/greyhypotheses/augmentation'
PYTHON_REQUIRES = '=3.7.9'

with open('README.md') as f:
    readme_text = f.read()

setuptools.setup()(
    name=NAME,
    version=VERSION,
    description=DESCRIPTION,
    long_description=readme_text,
    author=AUTHOR,
    url=URL,
    python_requires=PYTHON_REQUIRES,
    package_dir={'': 'src'},
    packages=setuptools.find_packages('src', exclude=['docs', 'tests'])
)


#!/usr/bin/env python

from khemia import MolReader
import argparse

def main():
    parser = argparse.ArgumentParser(description='Convert molecule file')
    parser.add_argument('--in', '-i', type=str, required=True, dest='iname', help='Input file')
    args = parser.parse_args()

    reader = MolReader(args.iname)

    count = 0
    error = 0
    for mol in reader:
        if mol is None:
            error += 1
        else:
            count += 1
    reader.close()

    print('mol: %d, error: %d' % (count, error))

if __name__ == '__main__':
    main()


from typing import Optional

# Definition for a binary tree node.
class TreeNode:
    def __init__(self, val=0, left=None, right=None):
        self.val = val
        self.left = left
        self.right = right

class Solution:
    def __init__(self, diameter=0) -> None:
        self.diameter = diameter

    # Calculates depth at a specefic node and updates the diameter
    def find_diam(self, root: Optional[TreeNode]) -> int:
        if not root:
            return 0

        left_depth = self.find_diam(root.left)
        right_depth = self.find_diam(root.right)

        self.diameter = max(left_depth + right_depth, self.diameter)

        return 1 + max(left_depth, right_depth)

    def diameterOfBinaryTree(self, root: Optional[TreeNode]) -> int:
        if not root:
            return 0

        self.find_diam(root)

        return self.diameter

root = TreeNode(1)
node2 = TreeNode(1)
node3 = TreeNode(1)
node4 = TreeNode(1)
node5 = TreeNode(1)
node6 = TreeNode(1)
node7 = TreeNode(1)
node8 = TreeNode(1)
node9 = TreeNode(1)
node10 = TreeNode(1)
node11 = TreeNode(2)

root.right = node2
node2.right = node3
node3.right = node4
node4.right = node5
node5.right = node6
node6.right = node7
node7.right = node8
node8.right = node9
node9.right = node10
node10.left = node11

print(Solution().diameterOfBinaryTree(root))  # 10


#-*-coding:utf-8-*-
#Lenet-5 model

import torch

import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim

class LeNet(nn.Module):
    def __init__(self,num_class):
        super(LeNet,self).__init__()
        #input[N,1,28,28]
        #self.conv1 = nn.Conv2d(1,6,5)  # gray image
        self.conv1 = nn.Conv2d(3, 6, 5)
        self.avepool = nn.AvgPool2d(2,2)
        self.conv2 = nn.Conv2d(6,16,5)
        #flatten[N,400]
        #self.fc1 = nn.Linear(400,120)
        self.fc1 = nn.Linear(44944, 120)
        self.fc2 = nn.Linear(120,84)
        self.fc3 = nn.Linear(84,num_class)
    def forward(self,x):
        str0 = x
        x = F.relu(self.conv1(x))
        x = self.avepool(x)
        x = F.relu(self.conv2(x))
        x = self.avepool(x)
        # (64,16,53,53)
        x = x.view((-1,44944))
        x = F.relu(self.fc1(x))
        x = F.relu(self.fc2(x))
        #x = F.softmax(self.fc3(x), dim=1)
        x = self.fc3(x)
        return x


import hypchat
import signal
import time
import logging
import subprocess

logging.basicConfig(
    format='%(asctime)s : %(levelname)s : %(message)s', level=logging.DEBUG)

bot_name = '/pyexec'
room_name = 'YOUR_ROOM_NAME'
api_key = 'YOUR_APIV2_KEY'

def timeout(a, b):
    raise RuntimeError

def subprocess_exec(src):

    signal.signal(signal.SIGALRM, timeout)
    signal.alarm(10)

    try:
        logging.debug('attempting to run %s' % src)
        cmd = 'python -c \'%s\'' % src.replace("\'", "\"")
        p = subprocess.Popen(cmd, stdout=subprocess.PIPE, shell=True)
        out, err = p.communicate()

        if out:
            result = out
        if err:
            result = err

    except SyntaxError as e:
        logging.debug('SyntaxError')

        result += str(e)
    except RuntimeError:
        logging.debug('ran too long')
        result += "Ran too long"

    signal.alarm(0)
    return result

def get_room(name, all_rooms):
    for room in all_rooms:
        if room['name'] == name:
            return room

def eval_bot_mentions(all_messages):
    for message in all_messages:
        message_text = message['message']
        if message_text.startswith(bot_name + ' '):
            logging.debug(message)
            text = message_text.replace(bot_name + ' ', '').strip()
            yield message['from']['mention_name'], text

if __name__ == "__main__":
    hc = hypchat.HypChat(api_key)
    logging.debug('connected to hipchat')

    all_rooms = hc.rooms(max_results=1000)['items']
    target_room = get_room(room_name, all_rooms)
    logging.debug('located %s Lounge room' % (room_name))

    after_message_id = target_room.latest(maxResults=1)['items'][0]['id']
    logging.debug('starting from id %s' % after_message_id)

    while True:
        try:
            logging.debug('fetching new messages')
            new_messages = target_room.latest(
                not_before=after_message_id)['items']
        except hypchat.requests.HttpServiceUnavailable:
            time.sleep(15)
            continue

        after_message_id = new_messages[-1]['id']
        for partner, text in eval_bot_mentions(new_messages[1:]):
            out = subprocess_exec("%s" % text)
            if out:
                logging.debug('trying to send %s/%s/%s' % (partner, text, out))

                target_room.message("@%s>>> %s\n%s" % (partner, text, out))
        time.sleep(5)


import tensorflow as tf
import numpy as np
from tqdm import tqdm
import os
from sklearn.utils import shuffle
from PIL import Image

def load_smaple(sample_dir,shuffle_flag = True):

    print("loading dataset...")
    lfilenames = []
    labelnames = []

    for dirpath,dirnames,filenames in os.walk(sample_dir):
        for filename in filenames:
            filepath = os.sep.join([dirpath,filename])
            lfilenames.append(filepath)
            labelname = dirpath.split("\\")[-1]
            labelnames.append(labelname)

    lab = list(sorted(set(labelnames)))
    labdict = dict(zip(lab,list(range(len(lab)))))
    labels = [labdict[i] for i in labelnames]

    if shuffle_flag:
        return shuffle(np.asarray(lfilenames),np.asarray(labels)),np.asarray(lab)
    else:
        return (np.asarray(lfilenames),np.asarray(labels)),np.asarray(lab)

dir_path = "man_woman"
(filenames,labels),_ = load_smaple(dir_path,False)

def make_TFRec(filenames,labels):
    #1.创建writer
    writer = tf.python_io.TFRecordWriter("mydata.tfrecords")
    for i in tqdm(range(len(labels))):
        image = Image.open(filenames[i])
        img = image.resize((256,256))
        img_raw = img.tobytes()
        #2.读取到的内容转化为tfrecords格式
        example = tf.train.Example( #example
                  features = tf.train.Features(#features
                      feature = {"label": tf.train.Feature(int64_list = tf.train.Int64List(value = [labels[i]])),
                                 "img_raw": tf.train.Feature(bytes_list = tf.train.BytesList(value = [img_raw]))}))#feature字典
        writer.write(example.SerializeToString())#序列化压缩
    writer.close()

make_TFRec(filenames,labels)

def read_and_decode(filenames,flag="train",batch_size=3):
    #1.读取文件生成队列
    if flag == "train":
        filename_queue = tf.train.string_input_producer(filenames)
    else:
        filename_queue = tf.train.string_input_producer(filenames,num_epochs=1,shuffle=False)
    #2.从队列读取example
    reader = tf.TFRecordReader()
    _,serialized_example = reader.read(filename_queue)
    #3.将example解析为features
    features = tf.parse_single_example(serialized_example,
                                       features = {"label":tf.FixedLenFeature([],tf.int64),
                                                   "img_raw":tf.FixedLenFeature([],tf.string)})
    #4.将features解析为图片数据
    image = tf.decode_raw(features['img_raw'],tf.uint8)
    image = tf.reshape(image,[256,256,3])
    label = tf.cast(features['label'],tf.int32)

    if flag == "train":
        image = tf.cast(image,tf.float32) * (1./255) - 0.5
        img_batch,label_batch = tf.train.batch([image,label],batch_size=batch_size,capacity=20)
        return img_batch,label_batch

    return image,label
TFRecordfilnames = ["mydata.tfrecords"]
image,label = read_and_decode(TFRecordfilnames,flag='test')

save_image_path = "show\\"
if tf.gfile.Exists(save_image_path):
    tf.gfile.DeleteRecursively(save_image_path)
tf.gfile.MakeDirs(save_image_path)

with tf.Session() as sess:
    sess.run(tf.local_variables_initializer())

    coord = tf.train.Coordinator()
    threads = tf.train.start_queue_runners(coord=coord)

    myset = set([])
    try:
        i = 0
        while True:
            example,example_label = sess.run([image,label])
            example_label = str(example_label)
            if example_label not in myset:
                myset.add(example_label)
                tf.gfile.MakeDirs(save_image_path+example_label)
            img = Image.fromarray(example,'RGB')
            img.save(save_image_path+example_label+"\\"+str(i)+'_Label_'+'.jpg')
            print(i)
            i += 1
    except tf.errors.OutOfRangeError:
        print('Done Test -- epoch limit reached')
    finally:
        coord.request_stop()
        coord.join(threads)
        print("stop()")


#-*-coding: utf-8 -*-
import sys
import xml.sax
from xml.sax.handler import ContentHandler

class MyHandler(ContentHandler):    #핸들러를 상속받는다.
    _name_node = ['friends', 'address', 'name']     #엘리먼트 목록지정
    _address_node=['friends','address','addr']
    def __init__(self, writer = sys.stdout):    #표준 출력을 지정 및 초기화
        self.writer = writer
        self._node = []
        ContentHandler.__init__(self)

    def startElement(self, name, attrs):    #ContentHandler의 재정의 메소드(=오버라이딩한 메소드)
        self._node.append(name)

    def endElement(self, name):     #ContentHandler의 재정의 메소드
        self._node.pop()

    def characters(self, data):     #ContentHandler의 재정의 메소드
        if self._node == self._name_node:
            self.writer.write(u'name:%s\n' % data)
        elif self._node == self._address_node:
            self.writer.write(u'address:%s\n' %data)
            self.writer.write(u'------------\n')
    #위에 start~, end~, characters메소드들은 명시적으로 호출하는 명령문이 없어, 자동 호출됨 ->callback 메소드
    #시작 태그를 만나면, 종료 태그를 만나면, 데이터를 만나면 자동 호출됨

if __name__ == "__main__":
    parser = xml.sax.make_parser()      #SAX SMLReader 객체를 만들어 반환한다
    parser.setContentHandler(MyHandler())
    parser.parse("myfriend.xml")


import json
import jwt
import requests
from datetime     import datetime

from django.http  import JsonResponse
from django.views import View

from .models     import User
from my_settings import SECRET_KEY

class KakaoSigninView(View):
    def get(self, request):
        try:
            access_token = request.headers.get("Authorization")

            if not access_token:
                return JsonResponse({"message" : "NEED_TOKEN"}, status = 400)

            response = requests.get(
                "https://kapi.kakao.com/v2/user/me",
                headers = {"Authorization" : f"Bearer {access_token}"},
                timeout = 5
            )

            if not response.status_code == 200:
                raise ConnectionError

            profile_json  = response.json()

            kakao_id      = profile_json.get("id")
            kakao_account = profile_json.get("kakao_account")
            name          = kakao_account["profile"]["nickname"]
            thumbnail     = kakao_account["profile"]["thumbnail_image_url"]
            month         = kakao_account["birthday"][0:2]
            day           = kakao_account["birthday"][2:4]
            birthday      = datetime.strptime(f"2000-{month}-{day}", "%Y-%m-%d").date() if kakao_account["birthday_needs_agreement"] else None

            user, created = User.objects.get_or_create(
                kakao     = kakao_id,
                defaults  = {
                    "name"      : name,
                    "thumbnail" : thumbnail,
                    "birthday"  : birthday,
                    "is_host"   : False
            })

            user.name      = name
            user.thumbnail = thumbnail
            user.birthday  = birthday
            user.save()

            token = jwt.encode({"id" : user.id}, SECRET_KEY, algorithm = "HS256")
            return JsonResponse({"message" : "SUCCESS", "token" : token}, status = 200)

        except KeyError:
            return JsonResponse({"message" : "KEY_ERROR"}, status = 400)

        except ConnectionError:
            return JsonResponse({"message" : "KAKAO_ERROR"}, status = 408)


"""Tests of argument validation"""

import pytest

from hipscat_import.index.arguments import IndexArguments

def test_none():
    """No arguments provided. Should error for required args."""
    with pytest.raises(ValueError):
        IndexArguments()

def test_empty_required(tmp_path, small_sky_object_catalog):
    """*Most* required arguments are provided."""
    ## Input path is missing
    with pytest.raises(ValueError, match="input_catalog_path"):
        IndexArguments(
            indexing_column="id",
            output_path=tmp_path,
            output_artifact_name="small_sky_object_index",
        )

    ## Input path is bad
    with pytest.raises(ValueError, match="input_catalog_path"):
        IndexArguments(
            input_catalog_path="/foo",
            indexing_column="id",
            output_path=tmp_path,
            output_artifact_name="small_sky_object_index",
            overwrite=True,
        )

    ## Indexing column is required.
    with pytest.raises(ValueError, match="indexing_column "):
        IndexArguments(
            input_catalog_path=small_sky_object_catalog,
            indexing_column="",
            output_path=tmp_path,
            output_artifact_name="small_sky_object_index",
            overwrite=True,
        )

def test_invalid_paths(tmp_path, small_sky_object_catalog):
    """Required arguments are provided, but paths aren't found."""
    ## Prove that it works with required args
    IndexArguments(
        input_catalog_path=small_sky_object_catalog,
        indexing_column="id",
        output_path=tmp_path,
        output_artifact_name="small_sky_object_index",
    )
    ## Input path is invalid catalog
    with pytest.raises(ValueError, match="input_catalog_path not a valid catalog"):
        IndexArguments(
            input_catalog_path="path",
            indexing_column="id",
            output_path=f"{tmp_path}/path",
            output_artifact_name="small_sky_object_index",
        )

def test_good_paths(tmp_path, small_sky_object_catalog):
    """Required arguments are provided, and paths are found."""
    tmp_path_str = str(tmp_path)
    args = IndexArguments(
        input_catalog_path=small_sky_object_catalog,
        indexing_column="id",
        output_path=tmp_path,
        output_artifact_name="small_sky_object_index",
    )
    assert args.input_catalog_path == small_sky_object_catalog
    assert str(args.output_path) == tmp_path_str
    assert str(args.tmp_path).startswith(tmp_path_str)

def test_column_inclusion_args(tmp_path, small_sky_object_catalog):
    """Test errors for healpix partitioning arguments"""
    with pytest.raises(ValueError, match="one of"):
        IndexArguments(
            input_catalog_path=small_sky_object_catalog,
            indexing_column="id",
            output_path=tmp_path,
            output_artifact_name="small_sky_object_index",
            include_hipscat_index=False,
            include_order_pixel=False,
        )
    _ = IndexArguments(
        input_catalog_path=small_sky_object_catalog,
        indexing_column="id",
        output_path=tmp_path,
        output_artifact_name="small_sky_object_index",
        overwrite=True,
        include_hipscat_index=True,
        include_order_pixel=True,
    )

    _ = IndexArguments(
        input_catalog_path=small_sky_object_catalog,
        indexing_column="id",
        output_path=tmp_path,
        output_artifact_name="small_sky_object_index",
        overwrite=True,
        include_hipscat_index=True,
        include_order_pixel=False,
    )
    _ = IndexArguments(
        input_catalog_path=small_sky_object_catalog,
        indexing_column="id",
        output_path=tmp_path,
        output_artifact_name="small_sky_object_index",
        overwrite=True,
        include_hipscat_index=False,
        include_order_pixel=True,
    )

def test_compute_partition_size(tmp_path, small_sky_object_catalog):
    """Test validation of compute_partition_size."""
    with pytest.raises(ValueError, match="compute_partition_size"):
        IndexArguments(
            input_catalog_path=small_sky_object_catalog,
            indexing_column="id",
            output_path=tmp_path,
            output_artifact_name="small_sky_object_index",
            compute_partition_size=10,  ## not a valid join option
        )

def test_to_catalog_info(small_sky_object_catalog, tmp_path):
    """Verify creation of catalog parameters for index to be created."""
    args = IndexArguments(
        input_catalog_path=small_sky_object_catalog,
        indexing_column="id",
        output_path=tmp_path,
        output_artifact_name="small_sky_object_index",
        include_hipscat_index=True,
        include_order_pixel=True,
    )
    catalog_info = args.to_catalog_info(total_rows=10)
    assert catalog_info.catalog_name == args.output_artifact_name
    assert catalog_info.total_rows == 10

def test_provenance_info(small_sky_object_catalog, tmp_path):
    """Verify that provenance info includes index-specific fields."""
    args = IndexArguments(
        input_catalog_path=small_sky_object_catalog,
        indexing_column="id",
        output_path=tmp_path,
        output_artifact_name="small_sky_object_index",
        include_hipscat_index=True,
        include_order_pixel=True,
    )

    runtime_args = args.provenance_info()["runtime_args"]
    assert "input_catalog_path" in runtime_args


"""This will convert a polyglot book to a list of move sequences and corresponding probabilities
given that moves are selected in proportion to their polyglot weight (i.e., win-rate)

E.g.:
"""

import chess
import chess.polyglot
from collections import namedtuple
import fire
import math
import numpy as np

Entry = namedtuple('Entry', 'move weight')

def dfs(reader, board, depth, ll=0):
    """Yield a list of move-sequences and corresponding log-likelihoods"""
    total_weight = 0
    entries = []
    for pentry in reader.find_all(board):
        total_weight += pentry.weight
        entries.append(Entry(pentry.move(), pentry.weight))
    for entry in entries:
        board.push(entry.move)
        ll_cur = ll + math.log(entry.weight/total_weight)
        if depth<=1:
            moves = ' '.join(move.uci() for move in board.move_stack)
            yield((moves, ll_cur))
        else:
            yield from dfs(reader, board, depth-1, ll_cur)
        board.pop()

def main(book_filename, length):
    """Output a list of fixed-length move-sequences and corresponding probabilities
    in a polyglot opening book

    book_filename: Filename of polyglot opening book
    length: Length of move sequences to extract
    """
    assert(length>=1)
    with chess.polyglot.open_reader("bookfish.bin") as reader:
        moves, lls = zip(*dfs(reader, chess.Board(), length))
    lls = np.array(lls)  # log likelihoods
    ls = np.exp(lls - max(lls))  # likelihoods
    ps = ls/sum(ls)  # probabilities
    seqs = zip(ps, moves)
    seqs = sorted(seqs, key=lambda it: it[0], reverse=True)
    for item in seqs:
        print("{:.9e} {}".format(*item))

# main("bookfish.bin", 8)
fire.Fire(main)


from transformers import AutoModelForSequenceClassification, AutoTokenizer
import torch
from torch.utils.data import Dataset, DataLoader

# Load pre-trained model and tokenizer
model_name = 'bert-base-uncased'
model = AutoModelForSequenceClassification.from_pretrained(model_name, num_labels=2)
tokenizer = AutoTokenizer.from_pretrained(model_name)

# Define your custom dataset
class CustomDataset(Dataset):
    def __init__(self, texts, labels):
        self.texts = texts
        self.labels = labels

    def __getitem__(self, idx):
        encoding = tokenizer(self.texts[idx], truncation=True, padding=True, return_tensors='pt')
        label = torch.tensor(self.labels[idx])
        return encoding, label

    def __len__(self):
        return len(self.texts)

# Load your custom dataset
train_texts = [
    "I loved the movie. The storyline was captivating and the acting was brilliant.",
    "The movie was hilarious and had great performances. A must-watch comedy.",
    "The film was a mix of romance and action. It kept me engaged throughout.",
    "The movie had stunning visuals and impressive cinematography. A visual treat.",
    "The film was a psychological thriller with a twist ending. It kept me on the edge of my seat."
]

train_labels = [
    ["Positive"],
    ["Positive", "Funny"],
    ["Positive", "Romance", "Action"],
    ["Positive", "Visuals"],
    ["Positive", "Thriller"]
]

train_dataset = CustomDataset(train_texts, train_labels)
train_dataloader = DataLoader(train_dataset, batch_size=16, shuffle=True)

# Fine-tune the model
optimizer = torch.optim.AdamW(model.parameters(), lr=1e-5)
loss_fn = torch.nn.CrossEntropyLoss()

device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
model.to(device)

model.train()
for epoch in range(3):  # Adjust the number of epochs as needed
    running_loss = 0.0
    for inputs, labels in train_dataloader:
        inputs = {key: value.to(device) for key, value in inputs.items()}
        labels = labels.to(device)

        optimizer.zero_grad()
        outputs = model(**inputs)
        loss = loss_fn(outputs.logits, labels)
        loss.backward()
        optimizer.step()

        running_loss += loss.item()

    epoch_loss = running_loss / len(train_dataloader)
    print(f"Epoch {epoch + 1} Loss: {epoch_loss:.4f}")

# Save the fine-tuned model
model.save_pretrained('fine_tuned_model')
tokenizer.save_pretrained('fine_tuned_model')