import torch
from sklearn.metrics import f1_score
from utils import load_data, EarlyStopping
from model import HGSAGE
import pandas as pd

def score(logits, labels):
    _, indices = torch.max(logits, dim=1)
    prediction = indices.long().cpu().numpy()
    labels = labels.cpu().numpy()

    accuracy = (prediction == labels).sum() / len(prediction)
    micro_f1 = f1_score(labels, prediction, average='micro')
    macro_f1 = f1_score(labels, prediction, average='macro')

    return accuracy, micro_f1, macro_f1

def evaluate(model, g, features, labels, mask, loss_func, args, movie_nodes=None):
    model.eval()
    with torch.no_grad():
        logits = model(g, features, args)
    if movie_nodes is not None:
        logits = torch.index_select(logits, 0, movie_nodes)
        loss = loss_func(logits[mask], labels[mask])
    else:
        loss = loss_func(logits[mask], labels[mask])
    accuracy, micro_f1, macro_f1 = score(logits[mask], labels[mask])

    return loss, accuracy, micro_f1, macro_f1

def main(args):
    if args['dataset'] == 'IMDB':
        g, features, labels, num_classes, train_idx, val_idx, test_idx, train_mask, \
        val_mask, test_mask, movie_nodes = load_data(args['dataset'])
        movie_nodes = movie_nodes.to(args['device'])
    else:
        g, features, labels, num_classes, train_idx, val_idx, test_idx, train_mask, \
        val_mask, test_mask = load_data(args['dataset'])

    if hasattr(torch, 'BoolTensor'):
        train_mask = train_mask.bool()  # 布尔类型转换
        val_mask = val_mask.bool()
        test_mask = test_mask.bool()

    features = features.to(args['device'])
    labels = labels.to(args['device'])
    train_mask = train_mask.to(args['device'])
    val_mask = val_mask.to(args['device'])
    test_mask = test_mask.to(args['device'])

    model = HGSAGE(meta_paths=[['ma', 'am'], ['md', 'dm']],    # 之前构建的边: pa, ap,组合成meta-path: PAP
                   in_size=features.shape[1],
                   hidden_size=args['hidden_units'],
                   out_size=num_classes,
                   dropout=args['dropout'],
                   aggregator_type=args['aggregator_type']).to(args['device'])
    g = g.to(args['device'])

    stopper = EarlyStopping(patience=args['patience'])
    loss_fcn = torch.nn.CrossEntropyLoss()
    optimizer = torch.optim.Adam(model.parameters(), lr=args['lr'], weight_decay=args['weight_decay'])

    for epoch in range(args['num_epochs']):
        model.train()
        logits = model(g, features, args)
        if args['dataset'] == 'IMDB':
            logits = torch.index_select(logits, 0, movie_nodes)
            loss = loss_fcn(logits[train_mask], labels[train_mask])
        else:
            loss = loss_fcn(logits[train_mask], labels[train_mask])

        optimizer.zero_grad()
        loss.backward()
        optimizer.step()

        train_acc, train_micro_f1, train_macro_f1 = score(logits[train_mask], labels[train_mask])
        if args['dataset'] == 'IMDB':
            val_loss, val_acc, val_micro_f1, val_macro_f1 = evaluate(model, g, features, labels, val_mask, loss_fcn,
                                                                     args, movie_nodes)
        else:
            val_loss, val_acc, val_micro_f1, val_macro_f1 = evaluate(model, g, features, labels, val_mask, loss_fcn,
                                                                     args)
        early_stop = stopper.step(val_loss.data.item(), val_acc, model)

        print('Epoch {:d} | Train Loss {:.4f} | Train Micro f1 {:.4f} | Train Macro f1 {:.4f} | '
              'Val Loss {:.4f} | Val Micro f1 {:.4f} | Val Macro f1 {:.4f}'.format(
            epoch + 1, loss.item(), train_micro_f1, train_macro_f1, val_loss.item(), val_micro_f1, val_macro_f1))

        if early_stop:
            break

    stopper.load_checkpoint(model)
    if args['dataset'] == 'IMDB':
        test_loss, test_acc, test_micro_f1, test_macro_f1 = evaluate(model, g, features, labels, test_mask, loss_fcn,
                                                                     args, movie_nodes)
    else:
        test_loss, test_acc, test_micro_f1, test_macro_f1 = evaluate(model, g, features, labels, test_mask, loss_fcn,
                                                                     args)
    embeddings = pd.read_csv(f"{args['dataset']}_embeddings_{args['aggregator_type']}+g.csv", header=None)

    if args['dataset'] == 'IMDB':
        embeddings = embeddings.iloc[movie_nodes.cpu(), :]
    embeddings.insert(loc=len(embeddings.columns), column=len(embeddings.columns), value=labels.cpu().numpy())
    embeddings.to_csv(f"{args['dataset']}_embeddings_{args['aggregator_type']}+g.csv", index=False, header=False)
    print('Test loss {:.4f} | Test Micro f1 {:.4f} | Test Macro f1 {:.4f}'.format(
        test_loss.item(), test_micro_f1, test_macro_f1))

if __name__ == '__main__':
    import argparse

    from utils import setup

    parser = argparse.ArgumentParser(description='HGSAGE')
    parser.add_argument('-s', '--seed', type=int, default=1, help='Random seed')
    parser.add_argument('-ld', '--log-dir', type=str, default='results',
                        help='Dir for saving training results')

    args = parser.parse_args().__dict__
    args = setup(args)

    main(args)


# ---------------------------------------------------------------------------------------------------------------------#
'''
This code is to pull random tickers from the SP500, Russell, or stock exchanges.
'''
# ---------------------------------------------------------------------------------------------------------------------#

import csv
import datetime as dt
import pickle
import random
from datetime import timedelta

import alpaca_trade_api as tradeapi
import bs4 as bs
import pandas as pd
import requests
'''
from Functions.Alpaca_Key_Store import initiate_API_keys
from Functions.Stock_Filter import sift_strategy
from Functions.Stock_Filter import ticker_selection
'''

from Alpaca_Key_Store import initiate_API_keys
from Stock_Filter import sift_strategy
from Stock_Filter import ticker_selection

# ---------------------------------------------------------------------------------------------------------------------#
ALPACA_API_KEY, ALPACA_SECRET_KEY, ALPACA_API_BASE_URL = initiate_API_keys()
ALPACA_PAPER = True
api = tradeapi.REST(ALPACA_API_KEY, ALPACA_SECRET_KEY, ALPACA_API_BASE_URL, 'v2')
# ---------------------------------------------------------------------------------------------------------------------#

pd.set_option('display.max_rows', None)
pd.set_option('display.max_columns', None)

class gen_tickers():
    def __init__(self, year, smonth, mrange):
        self.year = year
        self.smonth = smonth
        # Defining start and end dates
        weekend_test = dt.date(year=int(year), month=int(smonth), day=3).weekday()
        period_start = dt.date(year=int(year), month=int(smonth), day=3)
        # ---------------------------------------------------------------

        if (weekend_test == 5):
            self.simstart = period_start + timedelta(days=2)
            self.start = period_start - timedelta(days=22)
        elif (weekend_test == 6):
            self.simstart = period_start + timedelta(days=1)
            self.start = period_start - timedelta(days=22)
        else:
            self.simstart = period_start
            self.start = period_start - timedelta(days=22)

        weekend_test = dt.date(year=int(year), month=int(smonth + mrange), day=3).weekday()
        period_end = dt.date(year=int(year), month=int(smonth + mrange), day=3)

        if (weekend_test == 5):
            self.end = period_end + timedelta(days=2)
        elif (weekend_test == 6):
            self.end = period_end + timedelta(days=1)
        else:
            self.end = period_end

    def sp500_grab(self, size):
        resp = requests.get('https://en.wikipedia.org/wiki/List_of_S%26P_500_companies')
        soup = bs.BeautifulSoup(resp.text, "lxml")
        table = soup.find('table', {'class': 'wikitable sortable'})
        tickers_trial = []
        for row in table.findAll('tr')[1:]:
            ticker = row.findAll('td')[0].text.strip()
            mapping = str.maketrans(".", "-")
            ticker = ticker.translate(mapping)
            tickers_trial.append(ticker)

        with open("sp500tickers.pickle", "wb") as f:
            pickle.dump(tickers_trial, f)

        tickers = []
        while int(len(tickers)) < int(size):
            ticker = random.choices(tickers_trial, k=1)
            try:
                data = api.polygon.historic_agg_v2(str(ticker[0]), 1, timespan='day', _from=str(self.simstart), to=str(self.end)).df
                df = data.reset_index()
                tickers.append(str(ticker[0]))
            except:
                continue

        return tickers

    def russell_grab(self, sector, strat, period, z):
        tickers = ticker_selection(self.year, sector)
        tickers = sift_strategy(strat, self.year, self.smonth, period, z, tickers)

        return tickers

    def main_exchanges(self, size):

        with open('Stock_Exchange_Lists/NYSE.csv') as csv_file:
            data_1 = csv.reader(csv_file, delimiter=",")

            companies_1 = []
            tickers_1 = []
            sector_1 = []

            for row in data_1:
                tickers_1.append(row[0])
                sector_1.append(row[5])

                tickers_1 = [x.strip(' ') for x in tickers_1]
                sector_1 = [x.strip(' ') for x in sector_1]

            while ("" in tickers_1):
                tickers_1.remove("")

        del tickers_1[0]
        del sector_1[0]

        with open('Stock_Exchange_Lists/NASDAQ.csv') as csv_file:
            data_2 = csv.reader(csv_file, delimiter=",")

            companies_2 = []
            tickers_2 = []
            sector_2 = []

            for row in data_2:
                tickers_2.append(row[0])
                sector_2.append(row[5])

                tickers_2 = [x.strip(' ') for x in tickers_2]
                sector_2 = [x.strip(' ') for x in sector_2]

            while ("" in tickers_2):
                tickers_2.remove("")

        del tickers_2[0]
        del sector_2[0]

        # Compile all tickers from each stock exchange of interest.
        total_tickers = []
        total_tickers.extend(tickers_1)
        total_tickers.extend(tickers_2)

        # Deleting any duplicates from those exchanges. Successful.
        total_tickers = list(dict.fromkeys(total_tickers))

        tickers = []
        while int(len(tickers)) < int(size):
            ticker = random.choices(total_tickers, k=1)
            try:
                data = api.polygon.historic_agg_v2(str(ticker[0]), 1, timespan='day', _from=self.simstart, to=self.end).df
                df = data.reset_index()
                tickers.append(str(ticker[0]))
            except:
                continue

        return tickers


from discord.ext import commands
import discord
from discord import Activity, ActivityType
from discord.commands import slash_command,message_command
from discord.ui import Button, View

import asyncio
from discord.utils import V
import youtube_dl
from typing import Optional
import logging
import math
from urllib import request
import youtube_dl as ytdl

from . import stats

YTDL_OPTS = {
    "default_search": "ytsearch",
    "format": "bestaudio/best",
    "audio-quality": "128K",
    "quiet": True,
    "extract_flat": "in_playlist",
    "no_playlist": True
}

# TODO: abstract FFMPEG options into their own file?
FFMPEG_BEFORE_OPTS = '-reconnect 1 -reconnect_streamed 1 -reconnect_delay_max 5'
"""
Command line options to pass to `ffmpeg` before the `-i`.

See https://stackoverflow.com/questions/43218292/youtubedl-read-error-with-discord-py/44490434#44490434 for more information.
Also, https://ffmpeg.org/ffmpeg-protocols.html for command line option reference.
"""

async def audio_playing(ctx):
    """Verifie que le bot est en train de lire une vidéo."""
    client = ctx.guild.voice_client
    if client and client.channel and client.source:
        return True
    else:
        return False

async def in_voice_channel(ctx):
    """Verifie que le bot soit dans un channel."""
    voice = ctx.user.voice
    bot_voice = ctx.guild.voice_client
    if voice and bot_voice and voice.channel and bot_voice.channel and voice.channel == bot_voice.channel:
        return True
    else:
        return False

class Video:
    """Class containing information about a particular video."""

    def __init__(self, url_or_search, requested_by):
        """Plays audio from (or searches for) a URL."""
        with ytdl.YoutubeDL(YTDL_OPTS) as ydl:
            video = self._get_info(url_or_search)
            video_format = video["formats"][0]
            self.stream_url = video_format["url"]
            self.video_url = video["webpage_url"]
            self.title = video["title"]
            self.uploader = video["uploader"] if "uploader" in video else ""
            self.thumbnail = video[
                "thumbnail"] if "thumbnail" in video else None
            self.requested_by = requested_by

    def _get_info(self, video_url):
        with ytdl.YoutubeDL(YTDL_OPTS) as ydl:
            info = ydl.extract_info(video_url, download=False)
            video = None
            if "_type" in info and info["_type"] == "playlist":
                return self._get_info(
                    info["entries"][0]["url"])  # get info for first video
            else:
                video = info
            return video

    def get_embed(self):
        """Makes an embed out of this Video's information."""
        embed = discord.Embed(
            title=self.title, description=self.uploader, url=self.video_url)
        embed.set_footer(
            text=f"Ajouté par {self.requested_by.name}",
            icon_url=self.requested_by.avatar)
        if self.thumbnail:
            embed.set_thumbnail(url=self.thumbnail)
        return embed

class MusicInteraction(View):
    def __init__(self,music_player, url):
        super().__init__(timeout=None)
        self.player = music_player
        self.playing = True
        self.video_url = url
        self.add_item(Button(label="Voir sur Youtube",url=url,row=1))

    @discord.ui.button(label="Pause", style=discord.ButtonStyle.grey)
    async def pause_button_callback(self,button,interaction):
        if self.playing:
            button.label = "Play"
            button.style = discord.ButtonStyle.primary
        else:
            button.label = "Pause"
            button.style = discord.ButtonStyle.grey
        self.playing = not self.playing
        await interaction.response.edit_message(view=self)
        self.player._pause_audio(interaction.guild.voice_client)

    @discord.ui.button(label="Skip", style=discord.ButtonStyle.grey)
    async def skip_button_callback(self,button,interaction):
        if await audio_playing(interaction) and await in_voice_channel(interaction):
            await self.player._skip(interaction)
        else:
            if not await audio_playing(interaction):
                await interaction.response.send_message("Il n'y a pas de musique en cours.",ephemeral=True)
            elif not in_voice_channel(interaction):
                await interaction.response.send_message("Tu doit être dans le salon avec le bot.",ephemeral=True)

    @discord.ui.button(label="Loop", style=discord.ButtonStyle.grey)
    async def loop_button_callback(self,button,interaction):
        if await self.player._loop(interaction):
            if await self.player.get_loop_status(interaction):
                button.style=discord.ButtonStyle.success
            else:
                button.style=discord.ButtonStyle.grey
            await interaction.response.edit_message(view=self)
        else:
            await interaction.response.send_message("Il n'y a pas de musique en cours.")

    @discord.ui.button(label="Stop", style=discord.ButtonStyle.danger)
    async def stop_button_callback(self,button,interaction):
        await self.player._stop(interaction)

class Music(commands.Cog):

    def __init__(self, bot):
        self.bot = bot
        self.states = {}

    def get_state(self, guild):
        """Recupere l'etat de la `guild`, ou le créer."""
        if guild.id in self.states:
            return self.states[guild.id]
        else:
            self.states[guild.id] = GuildState()
            return self.states[guild.id]

    async def _stop(self, interaction):
        """Quitte le salon vocal."""
        client = interaction.guild.voice_client
        state = self.get_state(interaction.guild)
        if client and client.channel:
            await client.disconnect()
            state.playlist = []
            state.now_playing = None
        else:
            raise commands.CommandError("Pas dans un salon vocal.")

    def _pause_audio(self, client):
        if client.is_paused():
            client.resume()
        else:
            client.pause()

    async def _skip(self, interaction):
        interaction.guild.voice_client.stop()

    def _play_song(self, client, state, song):
        state.now_playing = song
        _activity = Activity(name=f"{song.title}",type=getattr(ActivityType, "listening", ActivityType.playing))
        asyncio.run_coroutine_threadsafe(self.bot.change_presence(activity=_activity),self.bot.loop)
        channel = client.channel
        source = discord.FFmpegOpusAudio(song.stream_url, before_options=FFMPEG_BEFORE_OPTS)

        def after_playing(err):
            if state.loop_flag and (len(channel.members)>1):
                next_song = state.now_playing
                self._play_song(client, state, next_song)
            elif (len(state.playlist) > 0) and (len(channel.members)>1) :
                next_song = state.playlist.pop(0)
                if state.player_message is not None:
                    asyncio.run_coroutine_threadsafe(state.player_message.edit(view=None),self.bot.loop)
                self._play_song(client, state, next_song)
                asyncio.run_coroutine_threadsafe(self.send_player(next_song,state),self.bot.loop)
            else:
                asyncio.run_coroutine_threadsafe(state.player_message.edit(view=None),self.bot.loop)
                state.now_playing = None
                asyncio.run_coroutine_threadsafe(client.disconnect(),self.bot.loop)
                asyncio.run_coroutine_threadsafe(self.bot.change_presence(activity=None),self.bot.loop)

        client.play(source, after=after_playing)

    async def _loop(self,interaction):
        if await audio_playing(interaction):
            state = self.get_state(interaction.guild)
            state.loop_flag=not state.loop_flag
            return True
        else:
            return False

    async def get_loop_status(self,interaction):
        state = self.get_state(interaction.guild)
        return state.loop_flag

    @message_command(name="Afficher la queue",guild_ids=[665676159421251587])
    async def queue(self, interaction, message):
        """Affiche la queue."""
        state = self.get_state(interaction.guild)
        await interaction.response.send_message(self._queue_text(state),ephemeral=True)

    def _queue_text(self, state):
        """Retourne le texte pour l'affichage de la queue."""
        queue = state.playlist
        if len(queue) > 0 or state.now_playing is not None:
            message = ['**Queue :**']
            message +=[f'  [P] **{state.now_playing.title}** (Ajouté par **{state.now_playing.requested_by.name}**)']
            message += [
                f"  [{index+1}] **{song.title}** (Ajouté par **{song.requested_by.name}**)"
                for (index, song) in enumerate(queue)
            ]  # add individual songs
            return "\n".join(message)
        else:
            return "La file est vide. Ajoute tes sons !"

    @slash_command(guild_ids=[665676159421251587])
    @commands.guild_only()
    async def play(self, ctx, *, url):
        """Joue l'audio de <url> (ou effectue une recherche de <url> et joue le premier résultat)."""

        client = ctx.guild.voice_client
        state = self.get_state(ctx.guild)  # get the guild's state

        if client and client.channel:
            await ctx.defer()
            state.webhook = ctx.followup
            try:
                video = Video(url, ctx.author)
            except youtube_dl.DownloadError as e:
                logging.warn(f"Error downloading video: {e}")
                await state.webhook.send("Une erreur est survenue pendant le téléchargement de la musique.",ephemeral=True)
                return
            state.playlist.append(video)
            await state.webhook.send(content=f"**{video.title}** à été ajouté à la queue")
            await state.webhook.send(embed=video.get_embed(),delete_after=5)
            stats.stats_add_music_played(ctx.user)
        else:
            if ctx.author.voice is not None and ctx.author.voice.channel is not None:
                await ctx.defer()
                state.webhook = ctx.followup
                channel = ctx.author.voice.channel
                try:
                    video = Video(url, ctx.author)
                except youtube_dl.DownloadError as e:
                    await state.webhook.send("Une erreur est survenue pendant le téléchargement de la musique.",ephemeral=True)
                    return
                client = await channel.connect()
                state.loop_flag=False #On reset le loop avant
                state.player_message = await state.webhook.send(embed=video.get_embed(),view = MusicInteraction(self,video.video_url))
                self._play_song(client, state, video)
                stats.stats_add_music_played(ctx.user)

                logging.info(f"Now playing '{video.title}'")
            else:
                await ctx.response.send_message("Tu dois être dans un salon vocal pour faire ça.",ephemeral=True)
                raise commands.CommandError("Tu dois être dans un salon vocal pour faire ça.")

    async def send_player(self,video,state):
        state.player_message = await state.webhook.send(embed=video.get_embed(),view = MusicInteraction(self,video.video_url))

def setup(bot):
    bot.add_cog(Music(bot))

class GuildState:
    """Gestion par guild."""

    def __init__(self):
        self.playlist = []
        self.now_playing = None
        self.webhook = None
        self.player_message = None
        self.loop_flag = False

    def is_requester(self, user):
        return self.now_playing.requested_by == user


#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2023/9/14 17:05
# @Author  : zzx
# @File    : simple_read.py
# @Software: PyCharm
# -*- coding: utf-8 -*-
import logging
import os
import sys

import codecs
from datetime import datetime
import platform
import re

import pandas as pd

"""
一个txt文件存储所有章节
根据 第x章 去划分文本
"""

def generate_onlyTXT():
    # 设置文件夹路径
    folder_path = "./contents"

    # 获取文件夹下所有的 TXT 文件名
    txt_files = [file for file in os.listdir(folder_path) if file.endswith(".txt")]

    # 遍历每个 TXT 文件并修改文件名
    for index, file_name in enumerate(txt_files):
        if not re.search(r'第.章', file_name):
            # 获取旧文件名的完整路径
            old_file_path = os.path.join(folder_path, file_name)

            # 修改文件名
            number = "第{}章".format(index + 1)
            new_file_name = file_name.replace(file_name, number + ' ' + file_name)  # 根据需要修改文件名的规则
            new_file_path = os.path.join(folder_path, new_file_name)

            # 重命名文件
            os.rename(old_file_path, new_file_path)

    # 获取文件夹下所有的 TXT 文件名
    txt_files = [file for file in os.listdir(folder_path) if file.endswith(".txt")]
    # 创建一个用于保存所有文件内容的新 TXT 文件
    output_file_path = "SkyrimV.txt"
    with open(output_file_path, "w", encoding="utf-8") as output_file:
        # 遍历每个 TXT 文件并将内容写入新文件
        for file in txt_files:
            file_path = os.path.join(folder_path, file)
            with open(file_path, "r", encoding="utf-8") as input_file:
                output_file.write(file)
                output_file.write(input_file.read())

    print("文件处理完成！")

class Reader:
    def __init__(self, book_name, txt_path):
        self.book_name = book_name
        self.txt_path = txt_path
        self.contents = []  # 章节名称
        self.book = self.split_book_chapter()

    def split_book_chapter(self):
        """
        读取文本文件，并将文本按章节划分
        :param txt_path:
        :return:
        """
        book_content = {}

        with codecs.open(self.txt_path, "r", encoding="utf-8") as f:
            text = f.read().strip()

            chapters = re.split("\n===第.{1,6}章===", text)
            i = 1

            for chapter in chapters:
                title = "第{}章".format(i)
                text = chapter.split("\n")
                text = "\n".join(text)
                book_content[title] = text
                i += 1

        self.contents = list(book_content.keys())
        return book_content

    def get_index(self, key_name):
        """
        根据书的章节名称，获取前一章与后一章
        :param key_name:
        :return:
        """
        this_index = self.contents.index(key_name)
        last_chapter = self.contents[this_index - 1]
        next_chapter = self.contents[this_index + 1]
        return last_chapter, next_chapter

    def start_read(self, chapter_name="第1章"):
        """
        开始阅读, 章节名称为空，且数据库中没有历史记录时，从第一章开始
        :param chapter_name: 章节名称
        :return:
        """
        chapter_name = chapter_name[0] if isinstance(chapter_name, tuple) else chapter_name

        read_content = "".join(self.book.get(chapter_name, "当前章节不存在"))
        print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"))
        print("chapter_name", chapter_name)
        text_list = read_content.split('\n')
        for text in text_list:
            limitation = 100
            if len(text) > limitation:
                text = '\n'.join([text[i:i + limitation] for i in range(0, len(text), limitation)])
            print(text)
            print("last_chapter = self.contents[this_index - 1]"
                  "next_chapter = self.contents[this_index + 1]"
                  "if platform.system().lower() == 'linux':os.system('clear')\n")
            print(
                "==== AUTHENTICATION FAILED ===\n"
                "Failed to start docker.service: Access denied\n"
                "See system logs and 'systemctl status docker.service' for details.")

        forward = ""
        while forward not in ["n", "b", "q"]:
            forward = input("""n、下一章\nb、上一章\nq、退出""")

        last_chapter_name, next_chapter_name = self.get_index(chapter_name)
        if forward == "q":
            sys.exit(0)
        # 根据命令选择上一章或者下一章，并更新历史记录
        if forward == "n":
            return next_chapter_name
        if forward == "b":
            return last_chapter_name

def start_read():
    mybook = Reader("415", "aaa.txt")
    chapter_name = "第51章"
    while True:
        if platform.system().lower() == "windows":
            os.system('cls')
        if platform.system().lower() == "linux":
            os.system('clear')
        chapter_name = mybook.start_read(chapter_name)

def more_simple_read(path="./contents/第287章 费福根卷1.txt"):
    with open(path, 'r', encoding='utf-8') as f:
        for line in f:
            line = line.strip()  # 去除行末尾的换行符和空白字符
            limitation = 80
            if len(line) > limitation:
                line = '\n'.join([line[i:i + limitation] for i in range(0, len(line), limitation)])
            print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"))
            print(line)
            print("last_chapter = self.contents[this_index - 1]"
                  "next_chapter = self.contents[this_index + 1]"
                  "if platform.system().lower() == 'linux':os.system('clear')"
                  "mybook.start_read(chapter_name)")

def print_lines(path='aaaa.txt', current_chapter=60):
    with open(path, 'r', encoding="utf-8") as file:
        lines = file.readlines()
        lines_per_batch = 500
        total_line = len(lines)
        current_line = current_chapter * lines_per_batch

        while current_line < total_line:
            print("======{},{}======".format(current_chapter, current_line))
            batch = lines[current_line:current_line + lines_per_batch]
            for line in batch:
                line = line.strip()  # 去除行末尾的换行符和空白字符
                limitation = 80
                if len(line) > limitation:
                    line = "\n[[0.4781, 0.4168, 0.6548, 0.4341, 0.4870, 0.3478, 0.7284, 0.5700, 0.4740],\n" \
                           "[0.6982, 0.5589, 0.6166, 0.6952, 0.4532, 0.5158, 0.2362, 0.3328, 0.5992]]\n\n" \
                           "last_chapter = self.contents[this_index - 1];" \
                           "next_chapter = self.contents[this_index + 1]\n" \
                           "if platform.system().lower() == 'linux':os.system('clear')\n\n" \
                           "<class 'numpy.ndarray'>\n<class 'numpy.ndarray'>\n\n" \
                           "precision: 89.81%,recall: 95.20%,F1:91.65% | precision: 96.16%,recall: 98.06%,F1:96.77%\n" \
                           "%Y-%m-%d %H:%M:%S next_chapter = self.contents[this_index + 1]\n\n"\
                        .join([line[i:i + limitation] for i in range(0, len(line), limitation)])
                print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"))
                print(line)
                print("last_chapter = self.contents[this_index - 1]"
                      "next_chapter = self.contents[this_index + 1]"
                      "if platform.system().lower() == 'linux':os.system('clear')"
                      "mybook.start_read(chapter_name)")

            user_input = input("按下字母键 'n' 并回车")
            if user_input.lower() == 'n':
                current_chapter += 1
                current_line += lines_per_batch
            elif user_input.lower() == 'b':
                current_chapter -= 1
                current_line -= lines_per_batch
                if current_chapter < 0:
                    current_chapter = 0
                    current_line = 0
            else:
                break

if __name__ == '__main__':
    # generate_onlyTXT()
    # more_simple_read()

    # start_read()

    print_lines()


import os
import ox

root_dir = os.path.normpath(os.path.abspath(os.path.dirname(__file__)))
os.chdir(root_dir)

path = '../js/'
files = sorted([
    file for file in os.listdir(path)
    if not file.startswith('.')
    and not file.startswith('oml.')
])
ox.file.write_json('../json/js.json', files, indent=4)
ox.file.write_file(
    '%soml.min.js' % path,
    '\n'.join([
        ox.js.minify(ox.file.read_file('%s%s' % (path, file)).decode('utf-8'))
        for file in files
    ])
)


import backtrader as bt

class BuyAndHold(bt.Strategy):
  def next(self):
    if self.position.size == 0:
      size = int(self.broker.getcash() / self.data)

      print(f'=== 購買 {size} 股 > 價格 {self.data.close[0]} ===')

      self.buy(size=size)


import requests
import json

def request_url(url):
    request = requests.get(url)
    data = request.json()#Json open

    country_list = []

    for i in data:
        x = i.get('name')
        country = x.get('common','-')

        #print(y)
        x = i.get('capital','-')
        capital = x[0]
        #print(y)
        area = i.get('area','-')
        #print(x)
        x = i.get('currencies','-')
        if(x != '-'):
            #currencies = str(list(x.keys())).strip('[]')
            currencies = ', '.join((list(x.keys())))#convert the dict type in list type
        else:
            currencies = '-'
        dicionario = {'Name': country, 'Capital': capital, 'Area': area, 'Currencies': currencies}
        country_list.append(dicionario)#Add object in list, on format {Name, Capital, Area, Currencies}

    return country_list #Return  Json objects by name, capital, area and currencies in list of  dictionary python format


# Licensed under a 3-clause BSD style license - see LICENSE.rst

import logging
from os.path import basename

from cotede.qc import ProfileQC

from . import fCNV
from .exceptions import CNVError

module_logger = logging.getLogger("seabird.qc")

class fProfileQC(ProfileQC):
    """Apply ProfileQC from CoTeDe straight from a file."""

    def __init__(
        self, inputfile, cfg=None, saveauxiliary=True, verbose=True, logger=None
    ):
        """ """
        self.logger = logging.getLogger(logger or "seabird.qc.fProfileQC")
        self.name = "fProfileQC"

        try:
            # Not the best way, but will work for now. I should pass
            #   the reference for the logger being used.
            profile = fCNV(inputfile)
        except CNVError as e:
            self.attributes["filename"] = basename(inputfile)
            logging.error(e.msg)
            raise

        super().__init__(profile, cfg=cfg, saveauxiliary=saveauxiliary, verbose=verbose)


"""
Kaggle 练习 -- Titanic
author: 王建坤
date: 2018-7-28
"""
import pandas as pd

# 导入数据集
train = pd.read_csv('Titanic_dataset/train.csv')
test = pd.read_csv('Titanic_dataset/test.csv')

# 查看数据集的信息
# print(train.info())
# print(test.info())

# 选择特征
selected_features = ['Pclass', 'Sex', 'Age', 'Embarked', 'SibSp', 'Parch', 'Fare']
X_train = train[selected_features]
X_test = test[selected_features]
y_train = train['Survived']

# 查看具体特征的所有值
# print(X_train['Embarked'].value_counts())
# print(X_test['Embarked'].value_counts())

# 填充缺失值，Embarked 用出现频率最高的特征值
X_train['Embarked'].fillna('S', inplace=True)
X_test['Embarked'].fillna('S', inplace=True)

# 填充缺失值，Age 和 Fare 用特征值的平均值
X_train['Age'].fillna(X_train['Age'].mean(), inplace=True)
X_test['Age'].fillna(X_test['Age'].mean(), inplace=True)
X_test['Fare'].fillna(X_test['Fare'].mean(), inplace=True)

# 查看数据集的信息
# X_train.info()

# 特征向量化
from sklearn.feature_extraction import DictVectorizer
dict_vec = DictVectorizer(sparse=False)
X_train = dict_vec.fit_transform(X_train.to_dict(orient='record'))
# 查看特征向量的特征名称
# dict_vec.feature_names_
X_test = dict_vec.transform(X_test.to_dict(orient='record'))

# 创建随机森林模型分类器
from sklearn.ensemble import RandomForestClassifier
rfc = RandomForestClassifier()

# 交叉验证的精度
from sklearn.model_selection import cross_val_score
print(cross_val_score(rfc, X_train, y_train, cv=5))

# 分类器拟合数据集
rfc.fit(X_train, y_train)
rfc_y_predict = rfc.predict(X_test)

# 保存对测试集的预测结果，以便提交
rfc_submission = pd.DataFrame({'PassengerId': test['PassengerId'], 'Survived': rfc_y_predict})
rfc_submission.to_csv('titanic_submission.csv', index=False)


from django.contrib import admin
from .models import Wallpaper
from django.contrib.auth.models import Group
class WallpaperAdmin(admin.ModelAdmin):
    list_display = ('title', 'created_date', 'is_free', 'image_thumb')
    list_filter = ('created_date', 'is_free')
    list_editable = ('is_free', )
    list_per_page = 100

admin.site.register(Wallpaper, WallpaperAdmin)
admin.site.unregister(Group)


from typing import Any, Optional

from more_itertools import consume

from .app_worker import AppWorker
from .build_jobs import (
    BaseBuildJob,
    EPUB2BuildJob,
    EPUB3BuildJob,
    EPUBType,
    PaginatedImagesBuildJob
)
from app.settings import Settings
from core.runner import pool_run

class Builder(AppWorker):
    def build_projects(
        self,
        projects: list[str],
        project_type: str,
        bundles: list[str]
    ) -> None:
        args_collection: list[tuple[Settings, str, EPUBType, list[str]]] = []
        kwargs_collection: list[dict[str, Any]] = []
        for project in projects:
            args_collection.append(
                (self.settings, project, EPUBType(project_type), bundles)
            )
            kwargs_collection.append({})
        consume(
            pool_run(
                build_epub,
                args_collection,
                kwargs_collection,
                "process",
                show_progress=True
            )
        )

def build_epub(
    settings: Settings,
    project: str,
    epub_type: EPUBType,
    bundles: list[str]
) -> None:
    build_job_types: list[type[BaseBuildJob]] = [
        EPUB2BuildJob,
        EPUB3BuildJob,
        PaginatedImagesBuildJob
    ]
    build_job: Optional[BaseBuildJob] = None
    for build_job_type in build_job_types:
        if build_job_type.epub_type is epub_type:
            build_job = build_job_type(settings, project, bundles)
    assert build_job is not None
    build_job.run()


### source ~/pfis/venv/bin/activate

from ROOT import TFile
from root_numpy import root2array, tree2array
import numpy as np

f = TFile("ACTAA.e-s.400keV_10atm.root") # ACTAA.gammas.40keV_10atm.root
array = tree2array(f.ACTAATree)

q = np.zeros(f.ACTAATree.GetEntries())

for i in range(f.ACTAATree.GetEntries()):
    q[i] = array[i]["sdq"].sum()

import matplotlib
matplotlib.use('TkAgg')
from matplotlib import pyplot as plt

plt.plot(array[0]["sdx"],array[0]["sdy"])
plt.axis([-65,+65,-65,+65]) # 13 cm (130 mm) radius
plt.title('x-y trajectories')
plt.plot(array[1]["sdx"],array[1]["sdy"])
plt.plot(array[2]["sdx"],array[2]["sdy"])
plt.plot(array[3]["sdx"],array[3]["sdy"])
plt.plot(array[4]["sdx"],array[4]["sdy"])
plt.savefig("xy-e400keV10atm-trajectories.pdf")
plt.close()

n, bins, patches = plt.hist(q, 50)
plt.title('energysum')
plt.savefig("energysum-e400keV10atm-trajectory.pdf")


from data_preparation.dataloader import DataSetOCR
from utills.dataloader_services import *
from torch.utils.data import DataLoader
import parameters
from models import crnn
from models.mobilent_rnn import MobilenetRNN
import torch.optim as optim
from torch.autograd import Variable
from torch.nn import CTCLoss
from utills.string_label_converter import averager, StrLabelConverter
import torch.nn.functional as F
import nltk

test_dataset = DataSetOCR(
    csv_file_path='/home/aulipriya/Desktop/fake_sythetic/fake_synthetic.csv',
    root_directory='/home/aulipriya/Desktop/fake_sythetic/top_hat_5_5/')
assert test_dataset
batch = 2
dataloader_params = {
    'batch_size': batch,
    'shuffle': True,
    'collate_fn': old_collate,
    'drop_last': True

}

test_loader = DataLoader(test_dataset, **dataloader_params)

PATH = '../weights/hand_print_mobilenet_v2_24.pth'

# crnn = crnn.CRNN(parameters.max_image_height, 3, parameters.number_of_classes, 256)
crnn = MobilenetRNN(parameters.input_channels, parameters.number_of_classes, 256)
crnn = torch.nn.DataParallel(crnn)
crnn.load_state_dict(torch.load(PATH, map_location=torch.device('cpu')))
crnn.eval()

image = torch.FloatTensor(dataloader_params['batch_size'], 3, parameters.max_image_width, parameters.max_image_height)
text = torch.IntTensor(dataloader_params['batch_size'] * 5)
length = torch.IntTensor(dataloader_params['batch_size'])

with torch.no_grad():
    string_converter = StrLabelConverter()
    loss_function = CTCLoss(zero_infinity=True)
    total_cost = 0
    if torch.cuda.is_available():
        crnn.cuda()
        crnn = torch.nn.DataParallel(crnn, device_ids=range(1))
        image = image.cuda()
        text = text.cuda()
        length = length.cuda()

    counter = 0
    n_correct = 0
    for images, labels in test_loader:
        counter = counter + 1
        batch_size = images.size(0)
        loadData(image, images)
        integer_labels, label_lengths = string_converter.convert_string_to_integer(labels, [])
        loadData(text, integer_labels)
        loadData(length, label_lengths)

        output = crnn(image)
        output = F.log_softmax(output, 2)
        output_size = Variable(torch.IntTensor([output.size(0)] * batch_size))
        cost = loss_function(output, text, output_size, length) / batch_size
        total_cost = total_cost + cost.item()
        _, output = output.max(2)
        output = output.transpose(1, 0).contiguous().view(-1)
        predicted_texts = string_converter.convert_integer_to_string(output.data, output_size.data)
        ground_truth_texts = string_converter.convert_integer_to_string(text.data, length.data)
        for pred, target in zip(predicted_texts, ground_truth_texts):
            if pred == target:
                n_correct += 1
            print('Prediction : {}   GT : {}'.format(pred, target))

    average_cost = total_cost / float(counter)
    accuracy = n_correct / (batch * counter) * 100

    print('Loss {}'.format(average_cost))
    print('Accuracy {}'.format(accuracy))


import importlib.util
import timeit
import tempfile
from textwrap import dedent

# apt-get install jsonschema json-spec validictory
import fastjsonschema
import jsonschema
import validictory
from jsonspec.validators import load

NUMBER = 1000

JSON_SCHEMA = {
    '$schema': 'http://json-schema.org/draft-04/schema#',
    'type': 'array',
    'items': [
        {
            'type': 'number',
            'maximum': 10,
            'exclusiveMaximum': True,
        },
        {
            'type': 'string',
            'enum': ['hello', 'world'],
        },
        {
            'type': 'array',
            'minItems': 1,
            'maxItems': 3,
            'items': [
                {'type': 'number'},
                {'type': 'string'},
                {'type': 'boolean'},
            ],
        },
        {
            'type': 'object',
            'required': ['a', 'b'],
            'minProperties': 3,
            'properties': {
                'a': {'type': ['null', 'string']},
                'b': {'type': ['null', 'string']},
                'c': {'type': ['null', 'string'], 'default': 'abc'}
            },
            'additionalProperties': {'type': 'string'},
        },
        {'not': {'type': ['null']}},
        {'oneOf': [
            {'type': 'number', 'multipleOf': 3},
            {'type': 'number', 'multipleOf': 5},
        ]},
    ],
}

VALUES_OK = (
    [9, 'hello', [1, 'a', True], {'a': 'a', 'b': 'b', 'd': 'd'}, 42, 3],
    [9, 'world', [1, 'a', True], {'a': 'a', 'b': 'b', 'd': 'd'}, 42, 3],
    [9, 'world', [1, 'a', True], {'a': 'a', 'b': 'b', 'c': 'xy'}, 42, 3],
    [9, 'world', [1, 'a', True], {'a': 'a', 'b': 'b', 'c': 'xy'}, 'str', 5],
)

VALUES_BAD = (
    [10, 'world', [1, 'a', True], {'a': 'a', 'b': 'b', 'c': 'xy'}, 'str', 5],
    [9, 'xxx', [1, 'a', True], {'a': 'a', 'b': 'b', 'c': 'xy'}, 'str', 5],
    [9, 'hello', [], {'a': 'a', 'b': 'b', 'c': 'xy'}, 'str', 5],
    [9, 'hello', [1, 2, 3], {'a': 'a', 'b': 'b', 'c': 'xy'}, 'str', 5],
    [9, 'hello', [1, 'a', True], {'a': 'a', 'x': 'x', 'y': 'y'}, 'str', 5],
    [9, 'hello', [1, 'a', True], {}, 'str', 5],
    [9, 'hello', [1, 'a', True], {'a': 'a', 'b': 'b', 'x': 'x'}, None, 5],
    [9, 'hello', [1, 'a', True], {'a': 'a', 'b': 'b', 'x': 'x'}, 42, 15],
)

fastjsonschema_validate = fastjsonschema.compile(JSON_SCHEMA)

def fast_compiled(value, _):
    fastjsonschema_validate(value)

def fast_not_compiled(value, json_schema):
    fastjsonschema.compile(json_schema)(value)

validator_class = jsonschema.validators.validator_for(JSON_SCHEMA)
validator = validator_class(JSON_SCHEMA)

def jsonschema_compiled(value, _):
    validator.validate(value)

with tempfile.NamedTemporaryFile('w', suffix='.py') as tmp_file:
    tmp_file.write(fastjsonschema.compile_to_code(JSON_SCHEMA))
    tmp_file.flush()
    spec = importlib.util.spec_from_file_location("temp.performance", tmp_file.name)
    module = importlib.util.module_from_spec(spec)
    spec.loader.exec_module(module)

def fast_file(value, _):
    module.validate(value)

jsonspec = load(JSON_SCHEMA)

def t(func, valid_values=True):
    module = func.split('.')[0]

    setup = """from __main__ import (
        JSON_SCHEMA,
        VALUES_OK,
        VALUES_BAD,
        validictory,
        jsonschema,
        jsonspec,
        fast_compiled,
        fast_file,
        fast_not_compiled,
        jsonschema_compiled,
    )
    """

    if valid_values:
        code = dedent("""
        for value in VALUES_OK:
            {}(value, JSON_SCHEMA)
        """.format(func))
    else:
        code = dedent("""
        try:
            for value in VALUES_BAD:
                {}(value, JSON_SCHEMA)
        except:
            pass
        """.format(func))

    res = timeit.timeit(code, setup, number=NUMBER)
    print('{:<20} {:<10} ==> {:10.7f}'.format(module, 'valid' if valid_values else 'invalid', res))

print('Number: {}'.format(NUMBER))

t('fast_compiled')
t('fast_compiled', valid_values=False)

t('fast_file')
t('fast_file', valid_values=False)

t('fast_not_compiled')
t('fast_not_compiled', valid_values=False)

t('jsonschema.validate')
t('jsonschema.validate', valid_values=False)

t('jsonschema_compiled')
t('jsonschema_compiled', valid_values=False)

t('jsonspec.validate')
t('jsonspec.validate', valid_values=False)

t('validictory.validate')
t('validictory.validate', valid_values=False)


from multiprocessing import Process,Queue
from data_set.data_gen import get_batch_inception
from config.cfg import config_instace as config
import time
gen = get_batch_inception(config.batch_size,image_size=config.image_size[0])

def new_get_data(quene):
    while True:
        org_im, box, label,r_match,r_box = next(gen)
        quene.put([org_im, box, label,r_match,r_box])

numT = 4
q = Queue(numT)
ps = []
for p in range(numT):
    ps.append(Process(target=new_get_data,args=(q,)))
for pd in ps:
    pd.start()


from django.shortcuts import render, redirect, HttpResponse
from django.contrib import messages
from datetime import datetime
from .models import *
from ..log_reg.models import User

from django.http import JsonResponse

from django.views.generic import View
from django.views.generic import ListView

# Create your views here.
def index(View):
    def get(self, request):
        if 'logged_user' not in request.session:
            return redirect('/')
        else:
            context = {
                'logged_user': User.objects.get(id=request.session['logged_user']),
                'collections': Collection.objects.all().order_by("due_date"),
                'assignments': Assignment.objects.all().order_by("due_date"),

                # 'all_trips': Trip.objects.all(),
                # 'excluded_trips': Trip.objects.exclude(trips_joined=request.session['logged_user']),
            }
            test = Assignment.objects.all()
            print(test)
        return render(request, "base.html", context)

class add_assignment(View):
    def  get(self, request):
        user = User.objects.get(id=request.session['logged_user'])
        collection = Collection.objects.get(id=request.GET['collection_id'])
        this_assignment = Assignment.objects.create(
            creator=user,
            collection=collection,
            name=request.GET['assignment_name'],
            description=request.GET['description'],
            due_date=request.GET['due_date'],
            min_hours=request.GET['min_hours'],
            max_hours=request.GET['max_hours'],
            priority=request.GET['priority'],
            completed=request.GET.get('completed', False),
        )
        this_assignment.save()
        new_assignment = {
            'creator': this_assignment.creator.id,
            'collection': this_assignment.collection.name,
            'name': this_assignment.name,
            'description': this_assignment.description,
            'due_date': this_assignment.due_date,
            'min_hours': this_assignment.min_hours,
            'max_hours': this_assignment.max_hours,
            'priority': this_assignment.priority,
            'completed': this_assignment.completed,
        }
        data = {
            'new_assignment': new_assignment,
        }
        return JsonResponse(data)

# def get_collection_form(request):
#     context = {
#         'collections': Collection.objects.all().order_by("due_date"),
#     }
#     return render(request, 'collection_form.html', context)

# def get_assignment_form(request):
#     context = {
#         'assignments': Assignment.objects.all().order_by("due_date"),
#     }
#     return render(request, 'assignment_form.html', context)

# def add_collection(request):
#     errors = Collection.objects.collection_validator(request.POST)

#     if errors:
#         for k, v in errors.items():
#             messages.error(request, v)
#         return redirect('/main/index/')
#     else:
#         user = User.objects.get(id=request.session['logged_user'])
#         this_collection = Collection.objects.create(
#             name=request.POST['collection_name'],
#             description=request.POST['description'],
#             due_date=request.POST['due_date'],
#             priority=request.POST['priority'],
#             completed=request.POST.get('completed', False),
#             creator=user,
#         )
#         this_collection.save()
#         context = {
#             'collection': this_collection,
#         }
#         return render(request, "add_collection_form.html", context)

# def add_assignment(request):
#     errors = Assignment.objects.assignment_validator(request.POST)

#     if errors:
#         for k, v in errors.items():
#             messages.error(request, v)
#         return redirect('/main/index/')
#     else:
#         # possible Checkbox Bug Fix
#         # if request.POST['completed'] = "on":
#         #     completed=True
#         user = User.objects.get(id=request.session['logged_user'])
#         collection = Collection.objects.get(id=request.POST['collection_id'])
#         this_assignment = Assignment.objects.create(
#             creator=user,
#             collection=collection,
#             name=request.POST['assignment_name'],
#             description=request.POST['description'],
#             due_date=request.POST['due_date'],
#             min_hours=request.POST['min_hours'],
#             max_hours=request.POST['max_hours'],
#             priority=request.POST['priority'],
#             completed=request.POST.get('completed', False),
#         )
#         this_assignment.save()
#         context ={
#             'assignment': this_assignment
#         }
#         return render(request, 'add_assignment_form.html', context)

# def update_assignment(request, assignment_id):
#     errors = Assignment.objects.assignment_validator(request.POST)

#     if errors:
#         for k, v in errors.items():
#             messages.error(request, v)
#         return redirect('/main/index/')
#     else:
#         assignment_form = Assignment.objects.get(id=request.POST['assignment_id'])
#         new_collection_id = Collection.objects.get(id=request.POST['collection'])
#         assignment_form.collection=new_collection_id,
#         assignment_form.name=request.POST['assignment_name'],
#         assignment_form.description=request.POST['description'],
#         assignment_form.due_date=request.POST['due_date'],
#         assignment_form.min_hours=request.POST['min_hours'],
#         assignment_form.max_hours=request.POST['max_hours'],
#         assignment_form.priority=request.POST['priority'],
#         assignment_form.completed=request.POST.get('completed', False)
#         assignment_form.save()
#         success = True
#         console.log(success)
#         context = {
#             'assignments': Assignment.objects.all().order_by("due_date"),
#         }
#         return render(request, 'assignment_form.html', context)

# def update_collection(request):
#     pass


from typing import Optional, Union, List
import math

import tensorflow as tf

from tensorflow.keras.layers import Conv3D

from svr.scene_reconstruction.model.layers.layer_interface import LayerInterface
from svr.scene_reconstruction.model.layers.pad_layer import PadLayer

class Conv3DLayer(LayerInterface):

    def __init__(self, amount_of_filter: int, filter_size: Optional[Union[int, List[int]]] = None, dil_value: int = 1, name: str = ""):
        super(LayerInterface, self).__init__(name=name)
        self.use_reflective_padding_3d = LayerInterface.settings("3d_layers/use_reflective_padding_3D")

        if filter_size is None:
            filter_size = LayerInterface.settings("TreeModel/normal_filter_size")

        kernel_regularizer = None
        if LayerInterface.settings("3d_layers/Inception3DLayer/regularizer_scale") > 1e-13:
            kernel_regularizer = tf.keras.regularizers.L2(LayerInterface.settings("3d_layers/Inception3DLayer/regularizer_scale"))

        padding_type = "same"
        if not isinstance(filter_size, list) and not isinstance(filter_size, tuple):
            filter_size = (filter_size, filter_size, filter_size)
        if self.use_reflective_padding_3d:
            padding_type = "valid"
            paddings = [int(math.floor(filter_val * 0.5) * dil_value) for filter_val in filter_size]
            self.pad_layer = PadLayer(paddings, name="PadLayer")
        filter_size = tuple(filter_size)

        self.layer = Conv3D(filters=amount_of_filter, kernel_size=filter_size,
                            padding=padding_type, dilation_rate=(dil_value, dil_value, dil_value),
                            kernel_regularizer=kernel_regularizer, bias_regularizer=kernel_regularizer,
                            name=f"inception_layer_dil_{dil_value}_filters_{amount_of_filter}")

    def call(self, input_tensor, training=False):
        x = input_tensor
        if self.use_reflective_padding_3d:
            x = self.pad_layer(x)
        return self.layer(x)


from gql import gql, Client
from gql.transport.aiohttp import AIOHTTPTransport
from collections import OrderedDict
from operator import getitem
import time, math

########## Configuration String ##########

GRAPHQL_URL = "https://defi-kingdoms-community-api-gateway-co06z8vi.uc.gateway.dev/graphql"
ADDRESS  = "0xD688F76E844f2982a776cBf59235fa2aB79cF726"
HERO_SHOWN = 6 #number of heroes shown in results in each category
TQ_HEROES = ['1353','1759','73659','180273','43371','700','58270','63009','102956','177124','23915'] #heroes id which falls under training quests category

##########################################

# Select your transport with a defined url endpoint
transport = AIOHTTPTransport(url=GRAPHQL_URL)

# Create a GraphQL client using the defined transport
client = Client(transport=transport, fetch_schema_from_transport=True)

# Provide a GraphQL query
graphQuery = """
{
heroes(where: {owner: "ADDRESS"}) {
    xp
    level
    id
    staminaFullAt
    stamina
    currentQuest
    mainClass
    profession
    }
}
"""

def getXpNeeded(currentLevel):
  xpNeeded = 0
  nextLevel = currentLevel + 1;

  if currentLevel < 6:
    xpNeeded = nextLevel * 1000
  elif currentLevel < 9:
    xpNeeded = 4000 + (nextLevel - 5) * 2000
  elif currentLevel < 16:
    xpNeeded = 12000 + (nextLevel - 9) * 4000
  elif currentLevel < 36:
    xpNeeded = 40000 + (nextLevel - 16) * 5000
  elif currentLevel < 56:
      xpNeeded = 140000 + (nextLevel - 36) * 7500
  elif currentLevel >= 56:
      xpNeeded = 290000 + (nextLevel - 56) * 10000;
  return xpNeeded

def getCurrentStamina(id, staminaFullAt,stamina):
  ts = time.time()
  currentStamina = stamina - ((staminaFullAt-ts) / (20*60))
  return currentStamina

def getTimerCD(stamina):
    if stamina < 20:
        return ' (' + str(math.ceil((20 - stamina) * 20)) + ' min)'
    else:
        return ''

def printDetails(heroes,entries):
    for x in range(min(len(heroes),entries)):
      print(str(heroes[x]['id']) , ', ',
      str(heroes[x]['xpLeft']) , ', ' ,
      '{0:.2f}'.format(heroes[x]['currentStamina']) ,
      getTimerCD(heroes[x]['currentStamina']) , ', ',
      str(heroes[x]['status']), ', ',
      heroes[x]['mainClass'] , ' (' , heroes[x]['profession'] ,')' ,
      sep='')

graphQuery = graphQuery.replace("ADDRESS",ADDRESS)

query = gql(
    graphQuery
)

# Execute the query on the transport
result = client.execute(query)
for item in result['heroes']:
    xpNeeded = getXpNeeded(item['level'])
    xpLeft = xpNeeded - item ['xp']
    currentStamina = getCurrentStamina(item['id'], item['staminaFullAt'], item['stamina'])
    item.update( {"xpNeeded":xpNeeded})
    item.update( {"xpLeft":xpLeft})
    item.update( {"currentStamina":currentStamina})
    if item['currentQuest'] == '0x0000000000000000000000000000000000000000':
        item.update( {"status":"idle"})
    else:
        item.update( {"status":"questing"})
    if item['id'] in TQ_HEROES:
        item.update( {"isTQ":"true"})
    else:
        item.update( {"isTQ":"false"})

print("=== Experience  ===")
print("ID , XP NEEDED , CURRENT STAMINA , STATUS , HERO CLASS (PROFESSION)")
heroes = result['heroes']
heroes.sort(key=lambda x: x.get('xpLeft'))
printDetails(heroes,HERO_SHOWN)

print("\n\n=== Gardening Heroes  ===")
TimerQuestHeroes = list(filter(lambda x: x.get('currentQuest') == '0x0000000000000000000000000000000000000000' and x.get('profession') in ('gardening') and x.get('isTQ') == 'false', heroes))
TimerQuestHeroes.sort(key=lambda x: x.get('currentStamina'), reverse=True)
printDetails(TimerQuestHeroes,HERO_SHOWN)

print("\n\n=== Mining Heroes  ===")
TimerQuestHeroes = list(filter(lambda x: x.get('currentQuest') == '0x0000000000000000000000000000000000000000' and x.get('profession') in ('mining') and x.get('isTQ') == 'false', heroes))
TimerQuestHeroes.sort(key=lambda x: x.get('currentStamina'), reverse=True)
printDetails(TimerQuestHeroes,HERO_SHOWN)

print("\n\n=== Fishing Heroes  ===")
idleHeroes = list(filter(lambda x: x.get('currentQuest') == '0x0000000000000000000000000000000000000000' and x.get('profession') in ('fishing') and x.get('isTQ') == 'false', heroes))
idleHeroes.sort(key=lambda x: x.get('currentStamina'), reverse=True)
printDetails(idleHeroes,HERO_SHOWN)

print("\n\n=== Foraging Heroes  ===")
idleHeroes = list(filter(lambda x: x.get('currentQuest') == '0x0000000000000000000000000000000000000000' and x.get('profession') in ('foraging') and x.get('isTQ') == 'false', heroes))
idleHeroes.sort(key=lambda x: x.get('currentStamina'), reverse=True)
printDetails(idleHeroes,HERO_SHOWN)

print("\n\n=== Training Quest Heroes  ===")
idleHeroes = list(filter(lambda x: x.get('currentQuest') == '0x0000000000000000000000000000000000000000' and x.get('isTQ') == 'true', heroes))
idleHeroes.sort(key=lambda x: x.get('currentStamina'), reverse=True)
printDetails(idleHeroes,HERO_SHOWN)


# _*_ coding:utf-8 _*_
import tensorflow as tf
import config
import network
import os

def train(args, sess, model):
    # Adam optimizers are used instead of AdaDelta
    # parser.add_argument('--learning_rate', dest='learning_rate', default=0.001, help='learning rate of the optimizer')
    # parser.add_argument('--momentum', dest='momentum', default=0.5, help='momentum of the optimizer')
    """
    :param args: 预先设置的一些参数，学习率
    :param sess:
    :param model: 模型
    :return:
    """
    d_optimizer = tf.train.AdamOptimizer(args.learning_rate, beta1=args.momentum, name="AdamOptimizer_D").minimize(model.d_loss, var_list=model.d_vars)
    c_optimizer = tf.train.AdamOptimizer(args.learning_rate, beta1=args.momentum, name="AdamOptimizer_C").minimize(model.recon_loss, var_list=model.c_vars)

    global_optimizer = tf.train.AdamOptimizer(args.learning_rate, beta1=args.momentum, name="AdamOptimizer_C").minimize(model.loss_all, var_list=model.c_vars)

    epoch = 0
    step = 0
    global_step = 0

    # saver
    # 创建saver对象实例用来保存训练好的模型和调用已保存的模型
    saver = tf.train.Saver()

    # 默认continue_training设置的是false
    if args.continue_training:
        tf.local_variables_initializer().run()
        last_ckpt = tf.train.latest_checkpoint(args.checkpoints_path)
        saver.restore(sess, last_ckpt)
        ckpt_name = str(last_ckpt)
        print ("Loaded model file from " + ckpt_name)
        epoch = int(ckpt_name.split('-')[-1])
    else:
        # init = tf.initialize_all_variables()
        '''替换上面的函数,作用是返回一个初始化所有全局变量的操作(op),要是你把图投放到一个session后,你可以通过run
        操作来初始化所有的全局变量,本质相当于variable_initializers(global_variables())
        '''
        tf.global_variables_initializer().run()
        '''
        返回一个初始化所有局部变量的操作(op),要是你把图投放到一个session之中,你就能通过run这个操作来初始化所有的
        局部变量,本质相当有variable_initliazers(local_variables())
        '''
        tf.local_variables_initializer().run()
    '''
    tensorflow的session是支持多线程的,可以在同一个session中创建多个线程,并行执行.在Session中的所有线程都必须能够
    同步终止,异常都必须能正确捕获并报告,session终止的时候,队列必须能被正确的关闭.tensorflow提供了两个类来实现对
    session中多线程的管理:tf.Coordinator和tf.QueueRunner,这两个类往往一起使用
    Coordinator类用来管理在Session中的多线程,可以用来同时停止多个工作线程并且向那个在等待所有工作线程终止的程序报告
    异常,该线程捕获到这个异常之后就会终止所有的线程.使用tf.train.Coordinator()来创建一个线程管理器(协调器)对象
    QueueRunner类来启动tensor的入队线程,可以用来启动多个工作线程同时将多个tensor推入到文件名称队列中,具体的执行函数是
    tf.train.start_queue_runners,只有调用该函数之后才会真正把tensor推入到内存序列中,供计算单元调用,否则会由于内存
    序列为空,数据流程图会处于一直等待状态
    https://blog.csdn.net/dcrmg/article/details/79780331
    '''
    coord = tf.train.Coordinator()
    threads = tf.train.start_queue_runners(sess=sess, coord=coord)
    # summary init
    all_summary = tf.summary.merge([model.recon_loss_sum,
                                    model.d_loss_sum,
                                    model.loss_all_sum,
                                    model.input_img_sum,
                                    model.real_img_sum,
                                    model.recon_img_sum,
                                    model.g_local_imgs_sum,
                                    model.r_local_imgs_sum])
    writer = tf.summary.FileWriter(args.graph_path, sess.graph)

    # training starts here

    # first train completion network
    while epoch < args.train_step:

        # Training Stage 1 (Completion Network)
        if epoch < args.Tc:
            summary, c_loss, _ = sess.run([all_summary, model.recon_loss, c_optimizer])
            if epoch%200 == 0:
                writer.add_summary(summary, global_step)
                global_step += 1
            print ("Epoch [%d] Step [%d] C Loss: [%.4f]" % (epoch, step, c_loss))
        elif epoch < args.Tc + args.Td:
            # Training Stage 2 (Discriminator Network)
            summary, d_loss, _ = sess.run([all_summary, model.d_loss, d_optimizer])
            if epoch%200 == 0:
                writer.add_summary(summary, global_step)
                global_step += 1
            print ("Epoch [%d] Step [%d] D Loss: [%.4f]" % (epoch, step, d_loss))
        else:
            # Training Stage 3 (Completion Network)
            summary, g_loss, _ = sess.run([all_summary, model.loss_all, global_optimizer])
            if epoch%200 == 0:
                writer.add_summary(summary, global_step)
                global_step += 1
            print ("Epoch [%d] Step [%d] G Loss: [%.4f]" % (epoch, step, g_loss) )
        # Check Test image results every time epoch is finished
        if step*args.batch_size >= model.data_count:
            saver.save(sess, args.checkpoints_path + "/model")
            # global_step=epoch)

            # res_img = sess.run(model.test_res_imgs)

            # save test img result
            # img_tile(epoch, args, res_img)
            step = 0
            epoch += 1

        step += 1
    coord.request_stop()
    coord.join(threads)
    sess.close()
    print("Done.")

def main(args):
    run_config = tf.ConfigProto()
    run_config.gpu_options.allow_growth = True

    # create graph, images, and checkpoints folder if they don't exist
    if not os.path.exists(args.checkpoints_path):
        os.makedirs(args.checkpoints_path)
    if not os.path.exists(args.graph_path):
        os.makedirs(args.graph_path)
    if not os.path.exists(args.images_path):
        os.makedirs(args.images_path)

    with tf.Session(config=run_config) as sess:
        # network模块中有network对象，用来定义网络结构
        model = network.network(args)

        print('Start Training...')
        train(args, sess, model)

main(config.args)

# Still Working....


import argparse
from datetime import datetime, timedelta
import torch
from torch import nn
import torch.backends.cudnn as cudnn
from torch.utils.tensorboard import SummaryWriter
from tqdm import tqdm
import model.RAIN as net
from dataset.data_generator_RAIN import DataGenerator
from utils.utils import get_checkpoints
from utils.timer import timeit

print("Device name: {}".format(torch.cuda.get_device_name(0)))
start_time = datetime.now()
max_duration = 24 * 3600 - 5 * 60
print("torch version: {}".format(torch.__version__))
print("device count: {}".format(torch.cuda.device_count()))
print('device name: {}'.format(torch.cuda.get_device_name(0)))

cudnn.benchmark = True

torch.autograd.set_detect_anomaly(True)

def adjust_learning_rate(optimizer, iteration_count):
    """Imitating the original implementation"""
    lr = args.lr / (1.0 + args.lr_decay * iteration_count)
    for param_group in optimizer.param_groups:
        param_group['lr'] = lr

parser = argparse.ArgumentParser()
# Basic options
parser.add_argument('--content', type=str, default='bssfp',
                    help='the modality for content')  # bssfp, t2 or lge
parser.add_argument('--style', type=str, default='t2',
                    help='the modality for content')

parser.add_argument('--lr', type=float, default=1e-4)
parser.add_argument('--lr_decay', type=float, default=5e-5)
parser.add_argument('--batch_size', type=int, default=16)
parser.add_argument('--ns', type=int, default=700)
parser.add_argument('--epochs', type=int, default=200)
parser.add_argument('--augmentation', action='store_true')
parser.add_argument('--lw', action='store_true')
parser.add_argument('--crop', type=int, default=224)
parser.add_argument('--vgg', help='the path to the directory of the weight', type=str,
                    default='pretrained/vgg_normalised.pth')
parser.add_argument('--style_weight', type=float, default=1.0)
parser.add_argument('--content_weight', type=float, default=1.0)
parser.add_argument('--latent_weight', type=float, default=1.0)
parser.add_argument('--recons_weight', type=float, default=5.0)
parser.add_argument('--save_every_epochs', type=int, default=0)
args = parser.parse_args()
print('weight_dir: {}'.format(args.vgg))

def get_appendix():
    appendix = args.content + "2" + args.style + '.lr{}'.format(args.lr) + \
               '.sw{}'.format(args.style_weight) + '.cw{}'.format(args.content_weight) + \
               '.lw{}'.format(args.latent_weight) + '.rw{}'.format(args.recons_weight)
    if args.augmentation:
        appendix += '.aug'
    return appendix

appendix = get_appendix()
print(appendix)
device = torch.device('cuda')

@timeit
def main():
    decoder = net.decoder
    vgg = net.encoder
    fc_encoder = net.fc_encoder
    fc_decoder = net.fc_decoder

    vgg.load_state_dict(torch.load(args.vgg))
    vgg = nn.Sequential(*list(vgg.children())[:31])

    start_epoch = 0
    if args.lw:
        decoder_model_dir = 'weights/decoder.{}.pt'.format(appendix)
        fc_encoder_dir = 'weights/fc_encoder.{}.pt'.format(appendix)
        fc_decoder_dir = 'weights/fc_decoder.{}.pt'.format(appendix)
        decoder.load_state_dict(torch.load(decoder_model_dir)['model_state_dict'])
        fc_encoder.load_state_dict(torch.load(fc_encoder_dir)['model_state_dict'])
        fc_decoder.load_state_dict(torch.load(fc_decoder_dir)['model_state_dict'])
        start_epoch = torch.load(decoder_model_dir)['epoch']
    print('start epoch: {}'.format(start_epoch))
    network = net.Net(vgg, decoder, fc_encoder, fc_decoder)
    network.train()
    network.to(device)

    decoder_checkpoint, fc_encoder_checkpoint, fc_decoder_checkpoint = get_checkpoints(apdx=appendix,
                                                                                n_epochs=args.epochs,
                                                                                save_every_epochs=args.save_every_epochs)

    content_iter = iter(
        DataGenerator(modality=args.content, crop_size=args.crop, n_samples=args.ns, batch_size=args.batch_size,
                      augmentation=args.augmentation))
    style_iter = iter(
        DataGenerator(modality=args.style, crop_size=args.crop, n_samples=args.ns, batch_size=args.batch_size,
                      augmentation=args.augmentation))

    optimizer = torch.optim.Adam(network.decoder.parameters(), lr=args.lr)
    optimizer_fc_encoder = torch.optim.Adam(network.fc_encoder.parameters(), lr=args.lr)
    optimizer_fc_decoder = torch.optim.Adam(network.fc_decoder.parameters(), lr=args.lr)

    loss_c_mean_list, loss_s_mean_list, loss_l_mean_list, loss_r_mean_list = [], [], [], []
    decoder_lr = []
    fc_encoder_lr = []
    fc_decoder_lr = []
    max_epoch_time = 0
    for i in tqdm(range(start_epoch, args.epochs)):
        epoch_start = datetime.now()
        loss_c_list, loss_s_list, loss_l_list, loss_r_list = [], [], [], []

        adjust_learning_rate(optimizer, iteration_count=i)
        adjust_learning_rate(optimizer_fc_encoder, iteration_count=i)
        adjust_learning_rate(optimizer_fc_decoder, iteration_count=i)

        decoder_lr.append(optimizer.param_groups[0]['lr'])
        fc_encoder_lr.append(optimizer_fc_encoder.param_groups[0]['lr'])
        fc_decoder_lr.append(optimizer_fc_decoder.param_groups[0]['lr'])

        for (content_images, _), (style_images, _) in zip(content_iter, style_iter):
            for param in network.fc_encoder.parameters():
                param.requires_grad = True
            for param in network.fc_decoder.parameters():
                param.requires_grad = True
            loss_c, loss_s, loss_l, loss_r = network.forward(torch.from_numpy(content_images).cuda(),
                                                             torch.from_numpy(style_images).cuda())

            # collect losses
            loss_c_list.append(loss_c.item())
            loss_s_list.append(loss_s.item())
            loss_l_list.append(loss_l.item())
            loss_r_list.append(loss_r.item())

            loss_l = args.latent_weight * loss_l
            loss_r = args.recons_weight * loss_r
            loss_fc = loss_l + loss_r
            optimizer_fc_encoder.zero_grad()
            optimizer_fc_decoder.zero_grad()
            loss_fc.backward(retain_graph=True)

            for param in network.fc_encoder.parameters():
                param.requires_grad = False
            for param in network.fc_decoder.parameters():
                param.requires_grad = False

            loss_c = args.content_weight * loss_c
            loss_s = args.style_weight * loss_s
            optimizer.zero_grad()
            loss_de = loss_c + loss_s
            loss_de.backward()
            optimizer.step()
            optimizer_fc_encoder.step()
            optimizer_fc_decoder.step()
            print("{}, {}, {}, {}, {}".format("0:>12".format('epoch: {}'.format(i)), loss_c.item(), loss_s.item(),
                                              loss_l.item(), loss_r.item()))

        mean_loss_c = sum(loss_c_list) / len(loss_c_list)
        mean_loss_s = sum(loss_s_list) / len(loss_s_list)
        mean_loss_l = sum(loss_l_list) / len(loss_l_list)
        mean_loss_r = sum(loss_r_list) / len(loss_r_list)
        loss_c_mean_list.append(mean_loss_c)
        loss_s_mean_list.append(mean_loss_s)
        loss_l_mean_list.append(mean_loss_l)
        loss_r_mean_list.append(mean_loss_r)
        if (datetime.now() - start_time).seconds > max_duration - max_epoch_time:
            i = args.epochs - 1

        monitor = args.latent_weight * mean_loss_l + args.recons_weight * mean_loss_r + \
                  args.content_weight * mean_loss_c + args.style_weight * mean_loss_s
        monitor = round(monitor, 3)

        decoder_checkpoint.step(monitor=monitor, model=network.decoder, epoch=i + 1)
        fc_encoder_checkpoint.step(monitor=monitor, model=network.fc_encoder, epoch=i + 1)
        fc_decoder_checkpoint.step(monitor=monitor, model=network.fc_decoder, epoch=i + 1)

        if (datetime.now() - start_time).seconds > max_duration - max_epoch_time:
            print("training time elapsed: {}".format(datetime.now() - start_time))
            print("max_epoch_time: {}".format(timedelta(seconds=max_epoch_time)))
            break
        max_epoch_time = max((datetime.now() - epoch_start).seconds, max_epoch_time)

    writer = SummaryWriter(comment=appendix + 'Scr{}'.format(decoder_checkpoint.best_result))
    print("write a training summary")
    i = 1
    for loss_c, loss_s, loss_l, loss_r, d_lr, fc_e_lr, fc_d_lr in zip(loss_c_mean_list, loss_s_mean_list,
                                                                      loss_l_mean_list, loss_r_mean_list,
                                                                      decoder_lr, fc_encoder_lr, fc_decoder_lr):
        writer.add_scalars('Loss', {'Content': loss_c, 'Style': loss_s, 'KL': loss_l, 'Reconstruct': loss_r,
                                    'Combine': args.latent_weight * loss_l + args.recons_weight * loss_r +
                                               args.content_weight * loss_c + args.style_weight * loss_s}, i)
        writer.add_scalars('Lr', {'Decoder': d_lr, 'FC_encoder': fc_e_lr, 'FC_decoder': fc_d_lr}, i)
        i += 1
    writer.close()

if __name__ == '__main__':
    main()
    print('Time elapsed: {}'.format(datetime.now() - start_time))
    print('program finish')


from typing import Union
from .abstracts import BaseDetector
from .wrappers import (
    MediapipeDetector,
    Ssd300x300Detector,
    RetinafaceDetector
)

class DetectorFactory:
    __detector_map = {
        "mediapipe": MediapipeDetector,
        "ssd": Ssd300x300Detector,
        "ssd300x300": Ssd300x300Detector,
        "retinaface": RetinafaceDetector,
    }
    @staticmethod
    def get_detector_class(detector_name: str):
        detector_name = detector_name.lower()
        if detector_name not in DetectorFactory.__detector_map:
            raise ValueError("Detector not found")
        return DetectorFactory.__detector_map[detector_name]
    @staticmethod
    def get_detector(detector_name: str) -> BaseDetector:
        return DetectorFactory.get_detector_class(detector_name)()
    @staticmethod
    def check_detector(detector: Union[str, BaseDetector]) -> BaseDetector:
        if not isinstance(detector, (str, BaseDetector)):
            raise ValueError("Detector must be string or simple-face detector instance")
        if isinstance(detector, str):
            detector = DetectorFactory.get_detector(detector)
        return detector


from googletrans import Translator
import json

def run(file_name):
    translator = Translator()
    with open(file_name, "r+") as file:
        data = file.read()
        data = json.loads(data)['data']
        for d in data:
            paragraphs = d['paragraphs']
            for page in paragraphs:
                context = page['context']

                context_trans = translator.translate(context, src='en', dest='vi')
                print(context_trans.text)

if __name__ == '__main__':
    run("dataset/train-v2.0.json")


import argparse
import os
import sys
from datetime import datetime

import requests
import piexif
from rich.progress import track
from rich.console import Console

from aulaclient import AulaClient

class Arguments:
    def __init__(self):
        parser = argparse.ArgumentParser(description='Download images from aula.dk.')
        parser.add_argument(
            '--outputFolder',
            required=True,
            default='output',
            help='Download images in this folder')
        parser.add_argument(
            '--cutoffDate',
            required=True,
            help='Download images posted on or after this date (format: "YYYY-MM-DD")')
        parser.add_argument(
            '--tags',
            required=False,
            nargs='+',
            help='Download images having at least one of these tags')
        args = parser.parse_args()

        self.cutoff_date = datetime.fromisoformat(args.cutoffDate).date()
        self.tags_to_find = args.tags
        self.output_directory = args.outputFolder

    def print_arguments(self):
        param_style = "cyan"
        console.print("Parameters:", style=param_style)
        console.print(f"  outputDirectory: {self.output_directory}", style=param_style)
        console.print(f"  cutoffDate: {self.cutoff_date.strftime('%Y-%m-%d')}", style=param_style)
        if self.tags_to_find:
            console.print(f"  tags: {self.tags_to_find}", style=param_style)
        console.print()

class AlbumToDownload:
    def __init__(self, name, album_type, creation_date, pictures):
        self.name = name
        self.album_type = album_type
        self.creation_date = creation_date
        self.pictures = pictures

    def __str__(self):
        return f"Type: {self.album_type}, CreationDate: {self.creation_date}, '\
            'Pictures: {len(self.pictures)}, Name: {self.name}"

def parse_date(date_string):
    return datetime.strptime(date_string.split('T')[0], '%Y-%m-%d').date()

def parse_datetime(date_string):
    return datetime.strptime(date_string, '%Y-%m-%dT%H:%M:%S%z')

def clean_title(title):
    return title.strip().replace('/', '_').replace(':', '_').replace('?', '_')

def picture_has_tags(picture, tags):
    if not picture['tags']:
        return False

    picture_tags = list(map(lambda t: t['name'], picture['tags']))
    for tag in tags:
        if tag in picture_tags:
            return True
    return False

def add_exif_creation_time(image_path, creation_time):
    is_jpeg = image_path.lower().endswith(('.jpg', '.jpeg'))
    if is_jpeg:
        zeroth_ifd = {
            piexif.ImageIFD.DateTime: creation_time.strftime('%Y:%m:%d %H:%M:%S'),
        }
        exif_ifd = {
            piexif.ExifIFD.DateTimeOriginal: creation_time.strftime('%Y:%m:%d %H:%M:%S'),
        }
        exif_dict = {"0th": zeroth_ifd, "Exif": exif_ifd}
        exif_bytes = piexif.dump(exif_dict)
        piexif.insert(exif_bytes, image_path)

def get_albums_from_gallery(institution_profile_ids, cutoff_date):
    print('Get Albums...')
    additional_params = {'limit': 1000}
    albums_to_download = []
    albums = client.get_albums(institution_profile_ids, additional_params)
    albums_with_id = filter_list_with_property(albums, 'id')
    for album in albums_with_id:
        creation_date = parse_date(album['creationDate'])
        is_album_created_after_cutoff_date = creation_date >= cutoff_date
        if not is_album_created_after_cutoff_date:
            continue
        pictures = client.get_pictures(institution_profile_ids, album['id'], additional_params)
        if pictures:
            name = clean_title(album['title'])
            album = AlbumToDownload(name, "Album", creation_date, pictures)
            albums_to_download.append(album)
    return albums_to_download

def filter_list_with_property(list_to_filter, property_name: str):
    return list(filter(lambda a: a[property_name] is not None, list_to_filter))

def get_albums_from_posts(institution_profile_ids, children_ids, cutoff_date):
    print('Get Posts...')
    additional_params = {'limit': 1000}
    albums_to_download = []
    get_posts_institution_profile_ids = institution_profile_ids + children_ids
    posts = client.get_posts(get_posts_institution_profile_ids, additional_params)
    for post in posts:
        creation_date = parse_date(post['publishAt'])
        is_post_after_cutoff_date = creation_date >= cutoff_date
        if not is_post_after_cutoff_date:
            continue
        attachments_with_media = filter_list_with_property(post['attachments'], 'media')
        if attachments_with_media:
            name = clean_title(post['title'])
            pictures = list(map(lambda x: x['media'], attachments_with_media))
            if pictures:
                album = AlbumToDownload(name, "Post", creation_date, pictures)
                albums_to_download.append(album)
    return albums_to_download

def get_albums_from_messages(cutoff_date):
    print('Get Threads...')
    albums_to_download = []
    threads = get_threads(cutoff_date)
    for thread in threads:
        thread_id = thread['id']
        thread_latest_message_time = parse_datetime(thread['latestMessage']['sendDateTime'])
        is_thread_updated_after_cutoff_date = thread_latest_message_time.date() >= cutoff_date
        if not is_thread_updated_after_cutoff_date:
            break  # Following threads won't be updated either, exit
        messages_with_attachments = get_messages_with_attachments_in_thread(thread_id)
        for message_index, message in enumerate(messages_with_attachments):
            attachments_with_media = filter_list_with_property(message['attachments'], 'media')
            name = f"{clean_title(thread['subject'])}_{message_index:02d}"
            creation_date = parse_date(thread['startedTime'])
            pictures = list(map(lambda x: x['media'], attachments_with_media))
            if pictures:
                album = AlbumToDownload(name, "Message", creation_date, pictures)
                albums_to_download.append(album)
    return albums_to_download

def get_threads(cutoff_date):
    threads_page_param = {'page': 0}
    threads_response = client.get_threads(threads_page_param)
    threads = threads_response['threads']
    while threads_response['moreMessagesExist']:
        threads_page_param['page'] += 1
        threads_response = client.get_threads(threads_page_param)
        threads += threads_response['threads']
        last_thread_update_time = parse_datetime(threads[-1]['latestMessage']['sendDateTime'])
        if last_thread_update_time.date() < cutoff_date:
            break
    return threads

def get_messages_with_attachments_in_thread(thread_id):
    page_param = {'page': 0}
    messages_response = client.get_messages_for_thread(thread_id, page_param)
    messages = messages_response['messages']
    while messages_response['moreMessagesExist']:
        page_param['page'] += 1
        messages_response = client.get_messages_for_thread(thread_id, page_param)
        messages += messages_response['messages']
    messages_with_attachments = list(filter(lambda x: x['hasAttachments'], messages))
    return messages_with_attachments

def get_image_data(album: AlbumToDownload, output_directory, file_data):
    album_directory_name = album.creation_date.strftime('%Y%m%d') + ' ' + album.name
    album_directory_path = os.path.join(output_directory, album_directory_name)
    image_creation_time = datetime.strptime(file_data['created'], '%Y-%m-%dT%H:%M:%S%z')
    image_response = requests.get(file_data['url'], timeout=30)

    if image_creation_time.date() == album.creation_date:
        image_directory_path = album_directory_path
    else:
        folder_name = image_creation_time.strftime('%Y%m%d')
        image_directory_path = os.path.join(album_directory_path, folder_name)

    return {
        "image_directory_path": image_directory_path,
        "image_content": image_response.content,
        "image_creation_time": image_creation_time
    }

def main():
    args = Arguments()
    args.print_arguments()

    try:
        profiles = client.get_profiles()
    except PermissionError as error:
        console.print(error, style="red")
        console.print("Could not get profiles, exiting.", style="red")
        sys.exit()

    institution_ids = list(map(lambda p: p['id'], profiles[0]['institutionProfiles']))
    children_ids = list(map(lambda p: p['id'], profiles[0]['children']))

    albums_to_download = []
    albums_to_download += get_albums_from_gallery(institution_ids, args.cutoff_date)
    albums_to_download += get_albums_from_posts(institution_ids, children_ids, args.cutoff_date)
    albums_to_download += get_albums_from_messages(args.cutoff_date)

    print('Download Pictures...')
    for album in track(albums_to_download, "Albums to download..."):
        if album.creation_date < args.cutoff_date:
            continue
        print('>', album, end=' ', flush=True)
        for picture in album.pictures:
            tags_are_found_or_ignored = (not args.tags_to_find
                                         or picture_has_tags(picture, args.tags_to_find))
            if tags_are_found_or_ignored:
                file_data = picture['file']
                image_data = get_image_data(album, args.output_directory, file_data)

                os.makedirs(image_data["image_directory_path"], exist_ok=True)
                image_path = os.path.join(image_data["image_directory_path"], file_data['name'])
                with open(image_path, "wb") as image_file:
                    image_file.write(image_data["image_content"])
                add_exif_creation_time(image_path, image_data["image_creation_time"])

if __name__ == '__main__':
    console = Console()
    client = AulaClient()
    main()


import os
from config import TOKEN, URL, url_davis, mbar_mmHg, mph_ms, in_mm
import flask
from flask import Flask, request
import telebot
import json
import urllib.request
import datetime
import pytz

#import requests

bot = telebot.TeleBot(TOKEN)
server = Flask(__name__)

def json_getweather():
    tzkiev = pytz.timezone('Europe/Kiev')
    date_now = datetime.datetime.now(tzkiev)
    data = urllib.request.urlopen(url_davis).read().decode()
    obj =  json.loads(data)
    observation_time = obj['observation_time_rfc822']
    lastupdated_time = datetime.datetime.strptime(observation_time, '%a, %d %b %Y %H:%M:%S %z')
    delta_time = date_now - lastupdated_time
    if delta_time.seconds < 3800:
        try:
            pressure = round(float(obj['pressure_mb']) * mbar_mmHg)
            temperature = float(obj['temp_c'])
            wind_speed = round(float(obj['wind_mph']) * mph_ms,1)
            wind_direction = obj['wind_dir']
            humidity = obj['relative_humidity']
            rain_ratehr = round(float(obj['davis_current_observation']['rain_rate_in_per_hr']) * in_mm,2)
            rain_dayin = round(float(obj['davis_current_observation']['rain_day_in']) * in_mm,2)
            if wind_direction == "North":
                wind_dir_uk = "ÐŸÑ–Ð²Ð½Ñ–Ñ‡Ð½Ð¸Ð¹"
            elif wind_direction == "South":
                wind_dir_uk = "ÐŸÑ–Ð²Ð´ÐµÐ½Ð½Ð¸Ð¹"
            elif wind_direction == "West":
                wind_dir_uk = "Ð—Ð°Ñ…Ñ–Ð´Ð½Ð¸Ð¹"
            elif wind_direction == "East":
                wind_dir_uk = "Ð¡Ñ…Ñ–Ð´Ð½Ð¸Ð¹"
            elif wind_direction == "Northeast" or wind_direction == "North-northeast" or wind_direction == "East-northeast":
                wind_dir_uk = "ÐŸÑ–Ð²Ð½Ñ–Ñ‡Ð½Ð¾-Ð¡Ñ…Ñ–Ð´Ð½Ð¸Ð¹"
            elif wind_direction == "Northwest" or wind_direction == "North-northwest" or wind_direction == "West-northwest":
                wind_dir_uk = "ÐŸÑ–Ð²Ð½Ñ–Ñ‡Ð½Ð¾-Ð—Ð°Ñ…Ñ–Ð´Ð½Ð¸Ð¹"
            elif wind_direction == "Southeast" or wind_direction == "South-southeast" or wind_direction == "East-southeast":
                wind_dir_uk = "ÐŸÑ–Ð²Ð´ÐµÐ½Ð½Ð¾-Ð¡Ñ…Ñ–Ð´Ð½Ð¸Ð¹"
            elif wind_direction == "Southwest" or wind_direction == "South-southwest" or wind_direction == "West-southwest":
                wind_dir_uk = "ÐŸÑ–Ð²Ð´ÐµÐ½Ð½Ð¾-Ð—Ð°Ñ…Ñ–Ð´Ð½Ð¸Ð¹"
            collecting_data ='ÐŸÐ¾Ð³Ð¾Ð´Ð° Ñ�Ñ‚Ð°Ð½Ð¾Ð¼ Ð½Ð° '+lastupdated_time.strftime("%H:%M\n")+'======================\n''Ð¢ÐµÐ¼Ð¿ÐµÑ€Ð°Ñ‚ÑƒÑ€Ð°: '+str(temperature)+' â„ƒ\n''Ð¢Ð¸Ñ�Ðº: '+str(pressure)+' Ð¼Ð¼ Ñ€Ñ‚. Ñ�Ñ‚.\n''Ð’Ð¾Ð»Ð¾Ð³Ñ–Ñ�Ñ‚ÑŒ: '+str(humidity)+'%\n''Ð¨Ð²Ð¸Ð´ÐºÑ–Ñ�Ñ‚ÑŒ Ð²Ñ–Ñ‚Ñ€Ñƒ: '+str(wind_speed)+' Ð¼/Ñ�\n''Ð�Ð°Ð¿Ñ€Ñ�Ð¼Ð¾Ðº Ð²Ñ–Ñ‚Ñ€Ñƒ: '+str(wind_dir_uk)+'\n''ÐžÐ¿Ð°Ð´Ð¸: '+str(rain_dayin)+' Ð¼Ð¼/Ð´ÐµÐ½ÑŒ, '+str(rain_ratehr)+' Ð¼Ð¼/Ð³Ð¾Ð´.'
        except KeyError:
                collecting_data = "â�—ï¸�ÐœÐµÑ‚ÐµÐ¾Ñ�Ñ‚Ð°Ð½Ñ†Ñ–Ñ� OfflineðŸ’¤\n"+"Ð· " +lastupdated_time.strftime("%d/%m/%Y %H:%M")
    else:collecting_data = "â�—ï¸�ÐœÐµÑ‚ÐµÐ¾Ñ�Ñ‚Ð°Ð½Ñ†Ñ–Ñ� OfflineðŸ’¤\n"+"Ð· " +lastupdated_time.strftime("%d/%m/%Y %H:%M")
    return collecting_data

keyboard = telebot.types.ReplyKeyboardMarkup(resize_keyboard=True)
keyboard.row('ÐžÑ‚Ñ€Ð¸Ð¼Ð°Ñ‚Ð¸ Ð¿Ð¾Ð³Ð¾Ð´Ñƒ \u26F7ðŸ�‚ \u2603')

#markup = telebot.types.InlineKeyboardMarkup()
#26F7
#stringList = {"Name": "John", "Language": "Python", "API": "pyTelegramBotAPI"}
#crossIcon = u"\u26C5"
#for key, value in stringList.items():
#    markup.add(telebot.types.InlineKeyboardButton(text=value, callback_data="['value', '" + value + "', '" + key + "']"),
#               telebot.types.InlineKeyboardButton(text=crossIcon, callback_data="['key', '" + key + "']"))

@bot.message_handler(commands=['start','help'])
def start(message):
    #bot.reply_to(message, 'ÐŸÑ€Ð¸Ð²Ñ–Ñ‚, ' + message.from_user.first_name)
    bot.send_message(message.chat.id, ' \u2744 Ð’Ð°Ñ� Ð²Ñ–Ñ‚Ð°Ñ” Ð¿Ð¾Ð³Ð¾Ð´Ð½Ñ–Ð¹ Ð±Ð¾Ñ‚ Ð“Ð›Ðš Ð—Ð°Ñ…Ð°Ñ€ Ð‘ÐµÑ€ÐºÑƒÑ‚\n\n''Ð©Ð¾Ð± Ð¾Ñ‚Ñ€Ð¸Ð¼Ð°Ñ‚Ð¸ Ñ–Ð½Ñ„Ð¾Ñ€Ð¼Ñ–Ñ†Ñ–ÑŽ Ñ‚Ð¸Ñ�Ð½Ñ–Ñ‚ÑŒ ÐºÐ½Ð¾Ð¿ÐºÑƒ "ÐžÑ‚Ñ€Ð¸Ð¼Ð°Ñ‚Ð¸ Ð¿Ð¾Ð³Ð¾Ð´Ñƒ", Ñ�ÐºÐ° Ð·Ð½Ð°Ñ…Ð¾Ð´Ð¸Ñ‚ÑŒÑ�Ñ� Ð²Ð½Ð¸Ð·ÑƒðŸ‘‡\n', reply_markup=keyboard)

@bot.message_handler(func=lambda message: True, content_types=['text'])
def echo_message(message):
    collecting_data = json_getweather()
    #bot.reply_to(message, message.text)
    bot.send_message(message.chat.id, 'Ð’Ñ–Ñ‚Ð°ÑŽ ' + message.from_user.first_name + '\n'+collecting_data, reply_markup=keyboard)

@server.route("/" + TOKEN, methods=['POST'])
def getMessage():
    bot.process_new_updates([telebot.types.Update.de_json(request.stream.read().decode("utf-8"))])
    return "!", 200

@server.route("/")
def webhook():
    bot.remove_webhook()
    bot.set_webhook(url=URL + TOKEN)
    return "!", 200
if __name__ == "__main__":
    server.run(host="0.0.0.0", port=int(os.environ.get('PORT', 5000)))
    #server.run(threaded=True)


from pathlib import Path

from armory.data.utils import maybe_download_weights_from_s3
from armory.logs import log

PARENT_DIR = Path(__file__).parent

def get_path(filename) -> str:
    """
    Get the absolute path of the provided file name. Ordering priority is:
        1) Check directly for provided filepath
        2) Load from parent directory
        3) Attempt to download from s3 as a weights file
    """
    filename = Path(filename)
    if filename.is_file():
        return str(filename)
    filepath = PARENT_DIR / filename
    if filepath.is_file():
        return str(filepath)

    return maybe_download_weights_from_s3(filename)