import json
from enum import Enum
from os.path import abspath
from typing import List

class Output(Enum):
    html = 1

class Config(object):
    def __init__(self,
                 subreddits: List[str],
                 hackernews: bool,
                 output: Output,
                 output_options: dict):
        self.subreddits = subreddits
        self.hackernews = hackernews
        self.output = output
        self.output_options = output_options

    @classmethod
    def from_json(cls, location: str) -> object:
        with open(abspath(location), 'r') as f:
            j = json.load(f)
            output_type = Output[j['output']['type']]
            return Config(j['subreddits'], j['hackernews'],
                          output_type, j['output']['options'])


import dash
from dash.dependencies import Input, Output
import dash_core_components as dcc
import dash_html_components as html
import plotly.graph_objs as go
from flask_sqlalchemy import SQLAlchemy
import numpy as np
import pandas as pd

TABLE_CLASSES = 'table table-hover table-sm'
GC_COLORS = {
    None: '#CCCCCC',
    'Adders': '#FFCC33',
    'Flames': '#AA77CC',
    'Maelstrom': '#FF5555',
}

app = dash.Dash()
app.server.config['SQLALCHEMY_DATABASE_URI'] = 'postgres:///gubal'
app.server.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
db = SQLAlchemy(app.server)

# Load Bootstrap
app.css.append_css({
    "external_url": "https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css",
})
app.scripts.append_script({
    "external_url": "https://code.jquery.com/jquery-3.2.1.slim.min.js",
})
app.scripts.append_script({
    "external_url": "https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js",
})
app.scripts.append_script({
    "external_url": "https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js",
})

def build_table(df, **kwargs):
    return html.Table([
        html.Thead([
            html.Tr([html.Th(col) for col in df.columns]),
        ]),
        html.Tbody([
            html.Tr([
                html.Td([df.iloc[i][col]]) for col in df.columns
            ]) for i in range(len(df))
        ]),
    ], **kwargs)

def build_gender_chart(**kwargs):
    df = pd.read_sql('SELECT gender, COUNT(*) FROM characters GROUP BY gender ORDER BY gender DESC', db.engine, index_col='gender')
    return go.Pie(labels=df.index.tolist(), values=df['count'], **kwargs)

def build_race_chart(**kwargs):
    df = pd.read_sql('SELECT race, COUNT(*) FROM characters GROUP BY race ORDER BY race DESC', db.engine, index_col='race')
    return go.Pie(labels=df.index.tolist(), values=df['count'], **kwargs)

def build_race_clan_gender_chart(**kwargs):
    df = pd.read_sql('SELECT race, clan, gender, COUNT(*) FROM characters GROUP BY race, clan, gender ORDER BY race, clan, gender DESC', db.engine, index_col=['race', 'clan', 'gender'])

    # TODO: clean up this mess
    races = df.index.get_level_values('race').unique()
    series = [{"y": [], "text": []} for i in range(4)]
    for race in races:
        race_data = df.loc[race]
        for i, clan_and_gender in enumerate(race_data.index.values):
            series[i]["text"].append(" ".join(clan_and_gender))
            series[i]["y"].append(race_data.loc[clan_and_gender]['count'])
    return [go.Bar(x=races, **s, **kwargs) for s in series]

def build_level_breakdown(**kwargs):
    df = pd.read_sql('''
        SELECT
            CASE
                WHEN level = 1 THEN 1
                WHEN level > 1 AND level < 15 THEN 2
                WHEN level = 15 THEN 15
                WHEN level > 15 AND level < 30 THEN 16
                WHEN level = 30 THEN 30
                WHEN level > 30 AND level < 50 THEN 31
                WHEN level = 50 THEN 50
                WHEN level > 50 AND level < 60 THEN 51
                WHEN level = 60 THEN 60
                WHEN level > 60 AND level < 70 THEN 61
                WHEN level = 70 THEN 70
            END level_range,
            job,
            COUNT(*)
        FROM levels
        GROUP BY level_range, job
        ORDER BY job, level_range ASC
    ''', db.engine, index_col=['level_range', 'job'])
    ranges = list(df.index.get_level_values('level_range').unique())
    jobs = list(df.index.get_level_values('job').unique())
    return [go.Bar(
        x=jobs,
        y=df.loc[rng]['count'],
        name=rng,
    ) for rng in ranges]

def build_gc_chart(**kwargs):
    df = pd.read_sql('SELECT gc, COUNT(*) FROM characters GROUP BY gc ORDER BY gc ASC', db.engine, index_col=['gc'])
    gcs = df.index.tolist()
    colors = [GC_COLORS[gc] for gc in gcs]
    return go.Pie(
        labels=gcs,
        values=df['count'],
        marker=go.Marker(
            colors=colors,
            line=go.Line(color='#FFF', width=1),
        ),
        **kwargs,
    )

def build_gc_breakdown(**kwargs):
    df = pd.read_sql('SELECT gc, gc_rank, COUNT(*) FROM characters GROUP BY gc, gc_rank ORDER BY gc, gc_rank ASC', db.engine, index_col=['gc', 'gc_rank'])
    gcs = list(df.index.get_level_values('gc').unique().dropna())
    return [go.Bar(
        y=df.loc[gc]['count'],
        name=gc,
        marker=go.Marker(color=GC_COLORS[gc]),
        **kwargs,
    ) for gc in gcs]

def build_world_breakdown(**kwargs):
    df = pd.read_sql('SELECT world, COUNT(*) FROM characters GROUP BY world ORDER BY count DESC', db.engine, index_col=['world'])
    worlds = list(df.index.values)
    return [go.Bar(
        x=worlds,
        y=df['count'],
        **kwargs,
    )]

def build_title_table(**kwargs):
    df = pd.read_sql('SELECT title, COUNT(*) FROM characters INNER JOIN character_titles ON characters.title_id = character_titles.id GROUP BY title ORDER BY count DESC LIMIT 10', db.engine)
    return build_table(df, **kwargs)

def build_top_table(col, limit=10, **kwargs):
    df = pd.read_sql('SELECT {col}, COUNT(*) FROM characters GROUP BY {col} ORDER BY count DESC LIMIT {limit}'.format(col=col, limit=limit), db.engine)
    return build_table(df, **kwargs)

def build_layout():
    return html.Div([
        html.Div([
            dcc.Graph(
                id="gender-chart",
                figure=go.Figure(
                    data=[build_gender_chart(
                        textinfo='label+percent',
                        hole=0.5,
                        marker=go.Marker(
                            colors=['#00CCFF', '#FF0099'],
                            line=go.Line(color='#FFF', width=1),
                        ),
                    )],
                    layout=go.Layout(title="Gender"),
                ),
                className='col-sm-6',
            ),
            dcc.Graph(
                id="race-chart",
                figure=go.Figure(
                    data=[build_race_chart(
                        textinfo='label+percent',
                        hole=0.5,
                        marker=go.Marker(line=go.Line(color='#FFF', width=1)),
                    )],
                    layout=go.Layout(title="Race"),
                ),
                className='col-sm-6',
            ),
        ], className='row', style={'min-height': '500px'}),
        html.Div([
            dcc.Graph(
                id="race-clan-gender-chart",
                figure=go.Figure(
                    data=[*build_race_clan_gender_chart(
                        showlegend=False,
                        hoverinfo='y+text',
                        textposition='auto',
                    )],
                    layout=go.Layout(title="Breakdown", barmode='group'),
                ),
                className='col-sm-12',
            ),
        ], className='row', style={'min-height': '500px'}),
        html.Div([
            dcc.Graph(
                id="level-breakdown",
                figure=go.Figure(
                    data=[*build_level_breakdown(
                        # showlegend=False,
                        # hoverinfo='y+text',
                        # textposition='auto',
                    )],
                    layout=go.Layout(title="Levels", barmode='group'),
                ),
                className='col-sm-12',
            ),
        ], className='row', style={'min-height': '500px'}),
        html.Div([
            html.Div([
                dcc.Graph(
                    id="gc-graph",
                    figure=go.Figure(
                        data=[build_gc_chart()],
                        layout=go.Layout(title="Grand Companies"),
                    ),
                ),
            ], className='col-sm-4'),
            html.Div([
                dcc.Graph(
                    id="gc-breakdown",
                    figure=go.Figure(
                        data=[*build_gc_breakdown()],
                        layout=go.Layout(title="Grand Company Ranks"),
                    ),
                ),
            ], className='col-sm-8'),
        ], className='row'),
        html.Div([
            dcc.Graph(
                id="world-breakdown",
                figure=go.Figure(
                    data=[*build_world_breakdown()],
                    layout=go.Layout(title="Worlds"),
                ),
                className='col-sm-12',
            ),
        ], className='row', style={'min-height': '500px'}),
        html.Div([
            html.Div([
                html.H5("Top 10 Titles"),
                build_title_table(className=TABLE_CLASSES),
            ], className='col-sm-4'),
            html.Div([
                html.H5("Top 10 First Names"),
                build_top_table('first_name', className=TABLE_CLASSES),
            ], className='col-sm-4'),
            html.Div([
                html.H5("Top 10 Last Names"),
                build_top_table('last_name', className=TABLE_CLASSES),
            ], className='col-sm-4'),
        ], className='row'),
    ], className='container')

app.layout = build_layout

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


"""
Author: Simon Lin (linwei85@gmail.com)

File: daily_reflect.py
Description: Defines the daily reflect action of Character.
This action means the character will reflect according to the chat hisotry of last day and then update his memory.
"""
import os
import sys
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), '..')))

import yaml
from util.config import configuration
from util.common_util import create_openai_client

def reflect_chat_history(my_name:str, my_profile:str, talk_to_name:str, talk_to_profile:str, chat_history, floder_template=False):
    # check the parameter wether is valid
    if not my_name or not my_profile or not talk_to_name or not talk_to_profile:
        return None

    # format the template
    with open(f"{floder_template}/gpt4/reflect.yaml", 'r') as stream:
        try:
            template = yaml.safe_load(stream)
        except yaml.YAMLError as exc:
            print(exc)

    template['talkToCharacter']['name'] = talk_to_name
    template['talkToCharacter']['profile'] = talk_to_profile
    template['myCharacter']['name'] = my_name
    template['myCharacter']['profile'] = my_profile
    template['chatHistory'] = chat_history
    prompt = template['prompt'].format(**template)

    # call openapi interface to get result
    openai = create_openai_client()
    response = openai.chat.completions.create(
            model=configuration["model"],
            messages=[
                {"role":"user", "content": prompt}
            ],
            max_tokens=int(configuration["max_tokens"])
        )

    reflect_content = response.choices[0].message.content.strip()
    return reflect_content


#!/usr/bin/python

##############################################
###Python template
###Author: Elizabeth Lee
###Date: 11/4/14
###Function: state-level excess P&I mortality rates (from Cecile) vs. CDC benchmark index

###Import data: /home/elee/Dropbox/Elizabeth_Bansal_Lab/CDC_Source/Import_Data/cdc_severity_index_long.csv, CDC_Source/Import_Data/all_cdc_source_data.csv, Census/Import_Data/totalpop_age_Census_98-14.csv, My_Bansal_Lab/Clean_Data_for_Import/ThanksgivingWeekData_cl.csv, excess mort data from Cecile

###Command Line: python ILINet_excessMort_benchmark_st_v5.py
##############################################

### notes ###
# Incidence per 100,000 is normalized by total population by second calendar year of the flu season
# 2013-14 ILINet data is normalized by estimated population size from December 2013 because 2014 estimates are not available at this time
# 2009-10 data is removed from cdc_severity_index_long.csv

### packages/modules ###
import csv
import matplotlib.pyplot as plt
import numpy as np
from itertools import product

## local modules ##
import functions_v5 as fxn

### data structures ###

### functions ###
### data files ###
ixin = open('/home/elee/Dropbox/Elizabeth_Bansal_Lab/CDC_Source/Import_Data/cdc_severity_index_long.csv','r')
ixin.readline()
ix = csv.reader(ixin, delimiter=',')

### called/local plotting parameters ###
ps = fxn.pseasons
sl = fxn.gp_ILINet_seasonlabels
fs = 24
fssml = 16
ILIcol_ePIps = fxn.gp_ILINet_colors[1:-1]

### program ###
# import data
# d_benchmark[seasonnum] = CDC benchmark index value
d_benchmark = fxn.benchmark_import(ix, 8) # no ILINet outpatient data
# import state-level excess P&I mortality rates
d_st_excessPI = fxn.excessPI_state_import()
# seasons included in excess mortality rate data
ePI_ps = sorted(set([key[0] for key in d_st_excessPI]).intersection(ps))
# state abbrs
states = sorted(set([key[1] for key in d_st_excessPI]))

# draw plot
fig1 = plt.figure()
ax1 = fig1.add_subplot(1,1,1)

# plot values
for s, col in zip(ePI_ps, ILIcol_ePIps):
	benchmark = [d_benchmark[s]] * len(states)
	excess_PI = [d_st_excessPI[(s, st)] for st in states]
	ax1.plot(benchmark, excess_PI, marker = 'o', color = col, linestyle = 'None')
	ax1.text(d_benchmark[s], -1.25, '%s' %(s), fontsize=fssml, horizontalalignment='center')
ax1.vlines([-1, 1], -20, 50, colors='k', linestyles='solid')
# ax1.hlines([-1, 1], -20, 30, colors='k', linestyles='solid')
# ax1.fill([-6, -1, -1, -6], [-1, -1, -20, -20], facecolor='blue', alpha=0.4)
# ax1.fill([-1, 1, 1, -1], [-1, -1, 1, 1], facecolor='yellow', alpha=0.4)
# ax1.fill([1, 10, 10, 1], [1, 1, 30, 30], facecolor='red', alpha=0.4)
ax1.annotate('Mild', xy=(-6.5,-1), fontsize=fssml)
ax1.annotate('Severe', xy=(8,19), fontsize=fssml)

ax1.set_ylabel('Excess P&I Mort. per 100,000', fontsize=fs)
ax1.set_xlabel(fxn.gp_benchmark, fontsize=fs)
ax1.tick_params(axis='both',labelsize=fssml)
ax1.set_xlim([-7,10])
ax1.set_ylim([-2,20])
plt.savefig('/home/elee/Dropbox/Elizabeth_Bansal_Lab/Manuscripts/Age_Severity/fluseverity_figs_v5/exploratory/ILINet_excessMort_benchmark_st.png', transparent=False, bbox_inches='tight', pad_inches=0)
plt.close()
# plt.show()


import math
from table2ascii import table2ascii, Alignment, PresetStyle
from config import defaults
from utils import *

def cidr_to_subnet_mask(cidr):
    subnet_mask = cidr * "1" + (32 - cidr) * "0"
    subnet_mask = int_to_ipv4(int(subnet_mask, 2))
    return ipv4_to_str(subnet_mask)

network = (
    input(f"Enter network with CIDR ({defaults.AVAILABLE_SUBNET}): ")
    or defaults.AVAILABLE_SUBNET
)
subnet_requirement = (
    input(f"Enter no. of hosts for each subnet (eg: {defaults.HOSTS}): ")
    or defaults.HOSTS
)
wan_links = int(
    (input(f"Enter no. of WAN links (eg: {defaults.WAN_LINKS}): ") or defaults.WAN_LINKS)
)

def calculate_subnets(network, subnet_requirement, wan_links):
    data = []
    ip, default_mask = network.split("/")
    default_mask = int(default_mask)
    ip = str_to_ipv4(ip)

    subnet_requirement = get_hosts(subnet_requirement)
    computed_subnet_size = [
        2 ** (math.floor(math.log2(i)) + 1) for i in subnet_requirement
    ]

    print(f"\nSubnetting {network} for hosts - {computed_subnet_size}")

    new_ip = ip[::]
    for i, size in enumerate(computed_subnet_size):
        new_cidr = 32 - round(math.log2(size))
        network_addr = f"{ipv4_to_str(new_ip)}/{new_cidr}"

        new_ip = shift_ipv4(new_ip, size)

        data.append(
            [
                network_addr,
                cidr_to_subnet_mask(new_cidr),
                f"{size - 2} hosts",
                f"LAN{i + 1}",
                ipv4_to_str(new_ip),
            ]
        )

    for i in range(wan_links):
        new_cidr = 32 - 2
        network_addr = f"{ipv4_to_str(new_ip)}/{new_cidr}"
        new_ip = shift_ipv4(new_ip, 4)

        data.append(
            [
                network_addr,
                cidr_to_subnet_mask(new_cidr),
                "2 hosts",
                f"WAN link{i + 1}",
                ipv4_to_str(new_ip),
            ]
        )
    return data

def print_table(data):
    output = table2ascii(
        header=[
            "Network Address",
            "Subnet Mask",
            "Usable Hosts",
            "Type",
            "Broadcast Address",
        ],
        body=data,
        style=PresetStyle.ascii_box,
    )
    print(output)

print_table(calculate_subnets(network, subnet_requirement, wan_links))


from typing import Any, List, Union, Optional
import time
import gym
import torch
import copy
import numpy as np
from easydict import EasyDict
from ding.envs import BaseEnv, BaseEnvTimestep, BaseEnvInfo
from ding.envs.common.env_element import EnvElementInfo
from ding.torch_utils import to_tensor, to_ndarray, to_list
from ding.utils import ENV_REGISTRY

def disable_gym_view_window():
    from gym.envs.classic_control import rendering
    import pyglet

    def get_window(width, height, display):
        screen = display.get_screens()
        config = screen[0].get_best_config()
        context = config.create_context(None)
        return pyglet.window.Window(
            width=width, height=height, display=display, config=config, context=context, visible=False
        )

    rendering.get_window = get_window

@ENV_REGISTRY.register('cartpole')
class CartPoleEnv(BaseEnv):

    def __init__(self, cfg: dict = {}) -> None:
        self._cfg = cfg
        self._init_flag = False
        self._replay_path = None

    def reset(self) -> torch.Tensor:
        if not self._init_flag:
            self._env = gym.make('CartPole-v0')
            if self._replay_path is not None:
                self._env = gym.wrappers.Monitor(
                    self._env, self._replay_path, video_callable=lambda episode_id: True, force=True
                )
            self._init_flag = True
        if hasattr(self, '_seed') and hasattr(self, '_dynamic_seed') and self._dynamic_seed:
            np_seed = 100 * np.random.randint(1, 1000)
            self._env.seed(self._seed + np_seed)
        elif hasattr(self, '_seed'):
            self._env.seed(self._seed)
        self._final_eval_reward = 0
        obs = self._env.reset()
        obs = to_ndarray(obs).astype(np.float32)
        return obs

    def close(self) -> None:
        if self._init_flag:
            self._env.close()
        self._init_flag = False

    def seed(self, seed: int, dynamic_seed: bool = True) -> None:
        self._seed = seed
        self._dynamic_seed = dynamic_seed
        np.random.seed(self._seed)

    def step(self, action: np.ndarray) -> BaseEnvTimestep:
        assert isinstance(action, np.ndarray), type(action)
        if action.shape == (1, ):
            action = action.squeeze()  # 0-dim tensor
        obs, rew, done, info = self._env.step(action)
        self._final_eval_reward += rew
        if done:
            info['final_eval_reward'] = self._final_eval_reward
        obs = to_ndarray(obs).astype(np.float32)
        rew = to_ndarray([rew])  # wrapped to be transfered to a Tensor with shape (1,)
        return BaseEnvTimestep(obs, rew, done, info)

    def info(self) -> BaseEnvInfo:
        T = EnvElementInfo
        return BaseEnvInfo(
            agent_num=1,
            obs_space=T(
                (4, ),
                {
                    'min': [-4.8, float("-inf"), -0.42, float("-inf")],
                    'max': [4.8, float("inf"), 0.42, float("inf")],
                    'dtype': np.float32,
                },
            ),
            # [min, max)
            act_space=T(
                (1, ),
                {
                    'min': 0,
                    'max': 2,
                    'dtype': int,
                },
            ),
            rew_space=T(
                (1, ),
                {
                    'min': 0.0,
                    'max': 1.0
                },
            ),
            use_wrappers=None,
        )

    def __repr__(self) -> str:
        return "DI-engine CartPole Env"

    def enable_save_replay(self, replay_path: Optional[str] = None) -> None:
        if replay_path is None:
            replay_path = './video'
        self._replay_path = replay_path


from Tkinter import *
from PIL import ImageTk, Image
import tkMessageBox
import tkFileDialog
import recolor

class ImageRecolorUI(Frame):

    def __init__(self, parent):
        Frame.__init__(self, parent, background="white")

        self.parent = parent
        self.supportedFormats = ["jpg", "png"]
        self.colorRange = IntVar()
        self.hueValue = IntVar()
        self.hasPreviousQuery = False

        screen_width = parent.winfo_screenwidth()
        screen_height = parent.winfo_screenheight()
        self.recolorer = recolor.recolor(screen_width, screen_height)

        self.initUI()

    def initUI(self):

        self.parent.title("ImageRecolor")
        self.centerAppWindow()
        self.loadUploadButton()
        self.loadRegionSelectionButton()

    def centerAppWindow(self):
        # geometry in the form of width x height + x_offset + y_offset
        # x_offset: +ve = move right, y_offset: move down
        width = 500
        height = 500
        x_offset = (self.parent.winfo_screenwidth() - width) / 2
        y_offset = (self.parent.winfo_screenheight() - height) / 2
        self.parent.geometry("%dx%d+%d+%d" % (width, height, x_offset, y_offset))

    def loadUploadButton(self):
        uploadButton = Button(self.parent, text="Upload image", command=self.uploadImage)
        uploadButton.pack()
        separator = Label(self.parent)
        separator.pack()

    def loadRegionSelectionButton(self):
        regionButton = Button(self.parent, text="Select region", command=self.drawRegion)
        regionButton.pack()
        separator = Label(self.parent)
        separator.pack()

    def uploadImage(self):
        if (self.hasPreviousQuery):
            self.imagePanel.pack_forget()
            self.unloadInputs()

        self.imageURL = tkFileDialog.askopenfilename()
        #print(self.imageURL.split("/")[-1].split("."))
        if self.imageURL.split("/")[-1].split(".")[1] not in self.supportedFormats:
            tkMessageBox.showerror("Invalid File Format", "Please upload a JPG or PNG image.")
        else:
            uploadedImg = Image.open(self.imageURL)
            uploadedImg = uploadedImg.resize((250, 250), Image.ANTIALIAS)
            img = ImageTk.PhotoImage(uploadedImg)
            self.imagePanel = Label(self.parent, image=img)
            self.imagePanel.image = img
            self.imagePanel.pack()
            self.loadInputs()
            self.hasPreviousQuery = True

    def drawRegion(self):
        # invoke backend program and pass image URL to it
        print("Drawing region for %s" % self.imageURL)
        self.recolorer.selectRegion(self.imageURL)

    def loadInputs(self):
        self.colorRangeLabel = Label(self.parent, text="Specify color range amount: ")
        self.colorRangeLabel.pack()
        self.colorRangeEntry = Entry(self.parent, textvariable=self.colorRange)
        self.colorRangeEntry.pack()
        self.colorHueLabel = Label(self.parent, text="Specify color hue amount between [0, 359]: ")
        self.colorHueLabel.pack()
        self.colorHueEntry = Entry(self.parent, textvariable=self.hueValue)
        self.colorHueEntry.pack()
        self.submitButton = Button(self.parent, text="Submit", command=self.submitInputs)
        self.submitButton.pack()

    def unloadInputs(self):
        self.colorRangeLabel.pack_forget()
        self.colorRangeEntry.pack_forget()
        self.colorHueLabel.pack_forget()
        self.colorHueEntry.pack_forget()
        self.submitButton.pack_forget()

    def submitInputs(self):
        # send user inputs to backend program
        print("Color range specified: " + str(self.colorRange.get()))
        print("Hue value specified: " + str(self.hueValue.get()))
        self.recolorer.changeColor(self.colorRange.get(), self.hueValue.get())

def main():

    root = Tk()
    app = ImageRecolorUI(root)
    root.mainloop()

if __name__ == '__main__':
    main()


#백준 14502
from collections import deque
from itertools import combinations
def bfs(n,m,start):
  queue=deque()
  queue.append(start)
  visited[start[0]][start[1]]=True
  dx=[0,0,1,-1]
  dy=[1,-1,0,0]
  while queue:
    x,y=queue.popleft()

    for i in range(4):
      nx=x+dx[i]
      ny=y+dy[i]
      if nx>=n or ny>=m or nx<0 or ny<0:
        continue

      if visited[nx][ny]==False and graph[nx][ny]==0:
        graph[nx][ny]=2
        visited[nx][ny]==True
        queue.append((nx,ny))

  return 0

n,m=list(map(int,input().split())) #세로, 가로
graph=[]
visited=[]

for i in range(n):
  temp=list(map(int,input().split()))
  graph.append(temp)

visited=[[False]*m for _ in range(n)]
Origraph=[[0]*m for _ in range(n)]
arr=[]
for i in range(n):
  for j in range(m):
    if graph[i][j]==0:
      arr.append((i,j))
    Origraph[i][j]=graph[i][j]

ans=[]
walls=list(combinations(arr,3))
#2행 1열, 1행 2열, 4행 6열
#walls=[((2,0),(2,0),(2,0)),((1,0),(0,1),(3,5))]

for wall in walls:
  count=0
  for w in wall:
    graph[w[0]][w[1]]=1

  for i in range(n):
    for j in range(m):
      if graph[i][j]==2 and visited[i][j]==False:
        bfs(n,m,(i,j))
  for i in range(n):
    for j in range(m):
      if graph[i][j]==0:
        count+=1
  ans.append(count)
  visited=[[False]*m for _ in range(n)]
  for i in range(n):
    for j in range(m):
      graph[i][j]=Origraph[i][j]

ans=sorted(ans)
print(ans[-1])


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

"""

@author: kevin olivier

"""
import pandas as pd
import numpy as np
from sklearn.feature_selection import SelectKBest, f_classif
from sklearn.ensemble import RandomForestClassifier, GradientBoostingClassifier

class FeatureImportance:

	def __init__(self, df, resp):
		self.dataframe = df
		self.response = resp
		self.predictors = pd.Series(self.dataframe.columns)
		self._rf_imp = []
		self._boosting_imp = []
		self._rpe_imp = []
		self._kbest_imp = []
		self._rf_param = []
		self._boosting_param = []
		self._rpe_param = []
		self._kbest_param = []

	def rf(self, n_estimators=500, criterion='gini', max_features='auto'):
		""" Returns the importances calculated by a random forest classifier.

		To make the method more effective, the result is stored in a private
		property, so if it is used with the same parameters again, it will only
		have to print the result.

		Parameters:
		* n_estimators: number of trees in the forest
		* criterion: optimization criterion when building the trees.
			'gini' (default) for Gini impurity
			'entropy' for the information gain
		* max_features: number of features to select at each split

		 """
		if self._rf_param == [n_estimators, criterion, max_features]:
			return pd.DataFrame({'Predictors': self.predictors, 'RF': self._rf_imp})
		else:
			self._rf_param = [n_estimators, criterion, max_features]
			if max_features == 'auto':
				model = RandomForestClassifier(n_estimators=n_estimators, criterion=criterion, max_features=int(np.sqrt(len(self.dataframe.columns))), bootstrap=True).fit(self.dataframe, self.response)
			else:
				model = RandomForestClassifier(n_estimators=n_estimators, criterion=criterion, max_features=max_features, bootstrap=True).fit(self.dataframe, self.response)

			self._rf_imp = model.feature_importances_/model.feature_importances_.max()
			return pd.DataFrame({'Predictors': self.predictors, 'RF': self._rf_imp})

	def boosting(self, n_estimators=2000, learning_rate=.1, max_depth=1):
		""" Returns the importance calculated by a gradient boosting classifier.

		To make the method more effective, the result is stored in a private
		property, so if it is used with the same parameters again, it will only
		have to print the result.

		Parameters:
		* n_estimators: number of boosting stages to perform
		* learning_rate: coefficient by which shrink the contribution of each tree
		* max_depth: maximum depth of the individual regression estimators

		"""
		if self._boosting_param == [n_estimators, learning_rate, max_depth]:
			return pd.DataFrame({'Predictors': self.predictors, 'Boosting': self._boosting_imp})
		else:
			self._boosting_param = [n_estimators, learning_rate, max_depth]
			model = GradientBoostingClassifier(n_estimators=n_estimators, learning_rate=learning_rate, max_depth=max_depth).fit(self.dataframe, self.response)
			self._boosting_imp = model.feature_importances_/model.feature_importances_.max()
			return pd.DataFrame({'Predictors': self.predictors, 'Boosting': self._boosting_imp})

	def kbest(self, score_func=f_classif, k='all'):
		""" Returns the scores of the k best predictors according to the ANOVA
		One-way F-test.

		To make the method more effective, the result is stored in a private
		property, so if it is used with the same parameters again, it will only
		have to print the result.

		Parameters:
		* score_func: scoring function. use f_classif or chi2 for classification
		* k: number of parameters to rank. If 'all' is given, the function will
		returns the score for all predictors

		"""
		if self._kbest_param == [score_func, k]:
			return pd.DataFrame({'Predictors': self.predictors, 'KBest': self._kbest_imp})
		else:
			self._kbest_param = [score_func, k]
			kb = SelectKBest(score_func=score_func, k=k).fit(self.dataframe, self.response)
			self._kbest_imp = pd.Series(['Ranked below k']*len(self.predictors))
			self._kbest_imp[ kb.get_support() ] = kb.scores_
			return pd.DataFrame({'Predictors': self.predictors, 'KBest': self._kbest_imp})

	def rpe(self, cutoff=.90, method='pearson'):
		""" Returns a series of boolean stating whether the corresponding predictor
		remains after performing a recursive pairwise elimination.

		To make the method more effective, the result is stored in a private
		property, so if it is used with the same parameters again, it will only
		have to print the result.

		Parameters:
		* cutoff: correlation cutoff to stop the algorithm
		* method: method to compute the correlation matrix
			'peason'
			'kendall'
			'spearman'

		"""
		if self._rpe_param == [cutoff, method]:
			return pd.DataFrame({'Predictors': self.predictors, 'RPE': self._rpe_imp})
		else:
			self._rpe_param = [cutoff, method]
			rpe_list = findcorr(self.dataframe, cutoff=cutoff, method=method)
			self._rpe_imp = self.predictors.apply(lambda x: (x not in rpe_list))
			return pd.DataFrame({'Predictors': self.predictors, 'RPE': self._rpe_imp})

	def summary(self):
		""" Returns a dataframe with the result of all the methods. """
		return self.rf().merge(self.boosting(), on='Predictors').merge(self.kbest(), on='Predictors').merge(self.rpe(), on='Predictors')

	pass


from __future__ import absolute_import, print_function, division
import numpy as np
from sklearn.base import ClusterMixin, TransformerMixin

from .. import libdistance
from . import MultiSequenceClusterMixin
from ..base import BaseEstimator

__all__ = ['RegularSpatial']

#-----------------------------------------------------------------------------
# Code
#-----------------------------------------------------------------------------

class _RegularSpatial(ClusterMixin, TransformerMixin):
    """Regular spatial clustering.

    This method finds a set of cluster centers that are themselves data points,
    chosen to be approximately equally separated in conformation space with
    respect to the distance metric used. In pseudocode, the
    algorithm, from Senne et al. [1], is:

      - Initialize a list of cluster centers containing only the first data
        point in the data set
      - Iterating over all conformations in the input dataset (in order),
          * If the data point is farther than ``d_min`` from all existing
            cluster center, add it to the list of cluster centers

    Parameters
    ----------
    d_min : float
        Minimum distance between cluster centers. This parameter controls
        the number of clusters which are found.
    metric : {"euclidean", "sqeuclidean", "cityblock", "chebyshev", "canberra",
              "braycurtis", "hamming", "jaccard", "cityblock", "rmsd"}
        The distance metric to use. metric = "rmsd" requires that sequences
        passed to ``fit()`` be ```md.Trajectory```; other distance metrics
        require ``np.ndarray``s.

    References
    ----------
    .. [1] Senne, Martin, et al. J. Chem Theory Comput. 8.7 (2012): 2223-2238

    Attributes
    ----------
    cluster_center_indices_: array, [n_clusters]
        Indices of the positions chosen as cluster centers. Each entry is a
        (trajectory_index, frame_index) pair.
    cluster_centers_ : array, [n_clusters, n_features]
        Coordinates of cluster centers
    n_clusters_ : int
        The number of clusters located.
    """

    def __init__(self, d_min, metric='euclidean'):
        self.d_min = d_min
        self.metric = metric

    def fit(self, X, y=None):
        cluster_ids = [0]
        for i in range(1, len(X)):
            # distance from X[i] to each X with indices in cluster_ids
            d = libdistance.dist(
                X, X[i], metric=self.metric, X_indices=np.array(cluster_ids, dtype=np.intp))
            if np.all(d > self.d_min):
                cluster_ids.append(i)

        self.cluster_center_indices_ = cluster_ids
        self.cluster_centers_ = X[np.array(cluster_ids)]
        self.n_clusters_ = len(cluster_ids)
        return self

    def predict(self, X):
        """Predict the closest cluster each sample in X belongs to.

        In the vector quantization literature, `cluster_centers_` is called
        the code book and each value returned by `predict` is the index of
        the closest code in the code book.

        Parameters
        ----------
        X : array-like, shape = [n_samples, n_features]
            New data to predict.

        Returns
        -------
        Y : array, shape [n_samples,]
            Index of the closest center each sample belongs to.
        """
        labels, inertia = libdistance.assign_nearest(
            X, self.cluster_centers_, metric=self.metric)
        return labels

    def fit_predict(self, X, y=None):
        return self.fit(X, y=y).predict(X)

class RegularSpatial(MultiSequenceClusterMixin, _RegularSpatial, BaseEstimator):
    __doc__ = _RegularSpatial.__doc__
    _allow_trajectory = True

    def fit(self, sequences, y=None):
        """Fit the kcenters clustering on the data

        Parameters
        ----------
        sequences : list of array-like, each of shape [sequence_length, n_features]
            A list of multivariate timeseries, or ``md.Trajectory``. Each
            sequence may have a different length, but they all must have the
            same number of features, or the same number of atoms if they are
            ``md.Trajectory``s.

        Returns
        -------
        self
        """
        MultiSequenceClusterMixin.fit(self, sequences)
        self.cluster_center_indices_ = self._split_indices(self.cluster_center_indices_)
        return self

    def summarize(self):
        return """
RegularSpatial clustering
-------------------------
d_min      : {d_min}
metric     : {metric}

n_clusters : {n_clusters}
""".format(d_min=self.d_min, metric=self.metric,
           n_clusters=self.n_clusters_)


import argparse
from datetime import datetime
import os
import pickle
import torch
import json
import tqdm
from my_utils.train_utils import BatchGen, predict_squda
from docQA.docQA_model import DocQA_Model
from my_utils.evaluate import my_evaluate

# 返回embedding矩阵 和 更新参数中三个词典的长度
def load_meta(opt, squda_meta_data_path):
    with open(squda_meta_data_path, 'rb') as f:
        meta_data = pickle.load(f)
    embedding = torch.Tensor(meta_data['embedding'])
    opt['vocab_size'] = len(meta_data['vocab'])
    opt['pos_vocab_size'] = len(meta_data['vocab_tag'])
    opt['ner_vocab_size'] = len(meta_data['vocab_ner'])
    return embedding, opt

def set_args():
    parser = argparse.ArgumentParser()
    parser.add_argument('--dev_data_path', default = 'F:\\0recentwork\\SQuDA\\data\\dev-v2.0.json')    # /home/caiyinqiong/squda/data/dev-v2.0.json
    parser.add_argument('--squda_meta_data_path', default = 'output\\squda_v2.pick')   # /home/caiyinqiong/squda/output-SAN
    parser.add_argument('--train_propress_data_path', default='output\\train_preprocess.json')  # /home/caiyinqiong/squda/output-SAN
    parser.add_argument('--dev_propress_data_path', default='output\\dev_preprocess.json')  # /home/caiyinqiong/squda/output-SAN
    # parser.add_argument('--elmo_config_path', default='F:\\0recentwork\\SQuDA\\data\\elmo_2x4096_512_2048cnn_2xhighway_5.5B_options.json')
    # parser.add_argument('--elmo_weight_path', default='F:\\0recentwork\\SQuDA\\data\\elmo_2x4096_512_2048cnn_2xhighway_5.5B_weights.hdf5')
    parser.add_argument('--out_model_dir', default='docQA_output\\model')   ## output/model

    parser.add_argument('--vocab_size', default=1000)
    parser.add_argument('--pos_vocab_size', default=10)
    parser.add_argument('--ner_vocab_size', default=10)

    # train
    parser.add_argument('--batch_size', default=32) ###### 原论文32
    parser.add_argument('--epochs', default=30)     #######################################
    parser.add_argument('--learning_rate', default=0.002)  #
    parser.add_argument('--embed_tune_partial', default=1000)  ## 只调整前1000个词的embedding
    parser.add_argument('--max_len', default=15)   # 原论文 15

    ## network
    parser.add_argument('--dropout_p', type=float, default=0.3)  ## 原文0.3  应用到embed 和 所有LSTM隐单元
    parser.add_argument('--pos_dim', default=8)  ## 词性标注的特征向量长度
    parser.add_argument('--ner_dim', default=8)    ## 实体的特征向量长度
    parser.add_argument('--prealign_hidden_size', default=128)    # 对齐网络中一层全连接层的隐单元数
    parser.add_argument('--contextual_hidden_size', default=128)   # 三层BiLSTM的隐单元数  原文是128
    parser.add_argument('--label_size', default=1)

    args = parser.parse_args()
    return args

def main():
    args = set_args()
    opt = vars(args)

    print('start load data...')
    embedding, opt = load_meta(opt, args.squda_meta_data_path)
    train_data = BatchGen(args.train_propress_data_path, args.batch_size, is_train=True)
    dev_data = BatchGen(args.dev_propress_data_path, args.batch_size, is_train=False)
    print('load data end!')

    ## model define
    # param = torch.load('/home/caiyinqiong/squda/docQA_1/output_model/model_chechpoint_epoch_0.pt')
    # opt = param['config']
    # embedding = param['state_dict']['network']['lexicon_encoder.embedding.weight']
    # model = DocQA_Model(opt, embedding)
    # model.network.load_state_dict = param['state_dict']['network']
    model = DocQA_Model(opt, embedding)
    print(model.network)
    model.setup_eval_embed(embedding)  #### DocReaderModel中的方法
    print('模型总参数量：' + str(model.total_param))

    for epoch in range(args.epochs):
        print('epoch ' + str(epoch))
        train_data.reset()   ####  BatchGen中的方法，打乱batch的顺序
        start = datetime.now()

        ## train
        print('第 ' + str(epoch) + ' 轮的训练开始......')
        for i, batch in enumerate(train_data):
            model.update(batch)  #### DocReaderModel中的方法
            #break  ########################################################### debug
            remain_time = str((datetime.now() - start) / (i + 1) * (len(train_data) - i - 1)).split('.')[0]
            print('loss = {}, remain_time = {}'.format(model.train_loss.avg, remain_time))
        print('第 ' + str(epoch) + ' 轮的训练结束！ train_loss = ' + str(model.train_loss.avg))

        ## save
        model_out_file_path = os.path.join(args.out_model_dir, 'model_chechpoint_epoch_{}.pt'.format(epoch))
        model.save(model_out_file_path)

        ## predict
        print('第 ' + str(epoch) + ' 轮的预测开始......')
        answer_pred_results = predict_squda(model, dev_data)   ## 预测的结果：{uid:answer_text}
        predict_out_path = os.path.join(args.out_model_dir, 'dev_predict_out_{}.json'.format(epoch))
        with open(predict_out_path, 'w') as f:
            json.dump(answer_pred_results, f)
        print('第 ' + str(epoch) + ' 轮的预测结束！')
        exact_score, f1_score = my_evaluate(opt['dev_data_path'], answer_pred_results)
        print('第 ' + str(epoch) + ' 轮得分：exact_score=' + str(exact_score) + ', f1_score=' + str(f1_score))

        print('epoch ' + str(epoch) + ' 结束！')
    print('train done!')

if __name__ == "__main__":
    main()


import sys
import json
import time
import collections
import heapq
import random
import itertools
import csv
import math
import pandas as pd
from pyspark import SparkContext
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression
from sklearn.metrics import mean_squared_error
from xgboost import XGBRegressor

folder, testpath, outpath = sys.argv[-3], sys.argv[-2], sys.argv[-1]

sc = SparkContext(appName="task2_3")
start = time.time()
'''
item_base
'''
def load1(x):
    ids = x.split(',')
    return ((str(ids[0]), str(ids[1])), float(ids[2]))
def load2(x):
    ids = x.split(',')
    return (str(ids[0]), str(ids[1]))
#data
train = sc.textFile(folder+'/yelp_train.csv').filter(lambda x: x != ('user_id,business_id,stars'))\
            .map(load1).groupByKey().mapValues(lambda x: sum(x)/len(x))
test = sc.textFile(testpath).filter(lambda x: x != ('user_id,business_id,stars'))\
            .map(load2)
#map with data
busi_u_map = train.map(lambda x: (x[0][1], x[0][0]))\
            .groupByKey().mapValues(set).collectAsMap()
user_b_map = train.map(lambda x: (x[0][0], x[0][1]))\
            .groupByKey().mapValues(set).collectAsMap()
busi_ur_map = train.map(lambda x: (x[0][1], (x[0][0], x[1])))\
            .groupByKey().mapValues(dict).collectAsMap()
user_br_map = train.map(lambda x:(x[0][0], (x[0][1], x[1])))\
            .groupByKey().mapValues(dict).collectAsMap()
b_avg_map = train.map(lambda x:(x[0][1], (x[1], 1)))\
            .reduceByKey(lambda a, b: (a[0]+b[0], a[1]+b[1]))\
            .map(lambda x: (x[0], x[1][0]/x[1][1])).collectAsMap()

pearcorr = {}
#pearson correlation
def pear_corr(x):
    b1, b2 = x
    sameU = busi_u_map[b1] & busi_u_map[b2]
    L = len(sameU)
    if L == 0: return 0
    star1 = [busi_ur_map[b1][u] for u in sameU]
    star2 = [busi_ur_map[b2][u] for u in sameU]
    avg1 = sum(star1)/len(star1)
    avg2 = sum(star2)/len(star2)
    top1 = [(i-avg1) for i in star1]
    top2 = [(i-avg2) for i in star2]
    top = sum([(a*b) for a, b in zip(top1, top2)])
    down = math.sqrt(sum([i**2 for i in top1]))*math.sqrt(sum([j**2 for j in top2]))
    res = top/down if down != 0 else 0
    #(b1, b2, corrs)
    return res

neigN = 30
#predict
def item_predict(x):
    '''
    p = sum(r*w)/sum(abs(w))
    '''
    u, b = x
    if b not in busi_ur_map: return (3.84, 0)
    otherb = user_b_map[u]
    heap = []
    top, down = 0, 0
    mark = False
    for ob in otherb:
        if (ob, b) not in pearcorr:
            ans = pear_corr((b, ob))
            pearcorr[(b, ob)] = ans
            pearcorr[(ob, b)] = ans
        w = pearcorr[(b,ob)]
        heap.append((w, user_br_map[u][ob]))
    if len(heap) < 10:
        for w, r in heap:
            if w < 0:
                w = -w
                w = w**2.5
                top += ((6-r)*w)
                down += w
            elif w > 0:
                w = w**2.5
                top += (r*w)
                down += w
    else:
        for w, r in heap:
            if w > 0:
                w = w**2.5
                top += (r*w)
                down += w
    ans = 0
    if down == 0: ans = b_avg_map[b]
    elif mark: ans = top/down
    else: ans = ((top/down)+b_avg_map[b])/2
    return (ans, len(heap))

pred_map = test.map(lambda x: ((x[0],x[1]), item_predict(x))).collectAsMap()

'''
model_base
'''
#data
train_df = pd.read_csv(folder+'/yelp_train.csv')
b_file = folder+'/business.json'
busi = sc.textFile(b_file).map(lambda x: json.loads(x))\
        .map(lambda x:(x['business_id'], x['review_count'],x['stars'],x['attributes'])).collect()
b_df = pd.DataFrame(busi, columns=['business_id','b_review_count','b_star','attributes'])

#attr
attr = pd.DataFrame(b_df['attributes'].to_dict()).T
attr = attr.drop(columns=['BusinessParking', 'Ambience', 'BestNights', 'GoodForMeal', 'HairSpecializesIn', 'Music'])
cols_to_split = ['AgesAllowed', 'Alcohol', 'BYOBCorkage', 'NoiseLevel', 'RestaurantsAttire', 'Smoking', 'WiFi']
new_cat = pd.concat([pd.get_dummies(attr[col], prefix=col, prefix_sep='_') for col in cols_to_split], axis=1)
attr = pd.concat([attr, new_cat], axis=1)
attr.drop(cols_to_split, inplace=True, axis=1)
del(new_cat)
attr2 = attr
attr2 = attr2.fillna(0.5).applymap(lambda x: 1 if x != 0.5 else x).applymap(lambda x: 0 if x == 'False' else x)
b_df2 = b_df.merge(attr2, left_index=True, right_index=True)
b_df2 = b_df2.drop(columns=['attributes'])
b_df2.set_index('business_id', inplace=True)

group = train_df
group.set_index(['user_id', 'business_id'], inplace=True)
group.sort_index(inplace=True)

combine = pd.merge(group, b_df2, left_index=True, right_index=True)
combine.head()
del(train_df)

u_file = folder+'/user.json'
user = sc.textFile(u_file).map(lambda x: json.loads(x))\
        .map(lambda x:(x['user_id'], x['review_count'],x['useful'],x['funny'],x['cool'],x['fans'],x['average_stars'])).collect()
u_df = pd.DataFrame(user, columns=['user_id','u_review_count','useful','funny','cool','fans','u_star'])
del(u_file)
u_df2 = u_df
u_df2.set_index('user_id', inplace=True)

com2 = combine
com2 = pd.merge(com2, u_df2, how='left', left_index=True, right_index=True)

test_df = pd.read_csv(testpath)
test_df=test_df.drop(columns=['stars'])
test_df.set_index(['user_id', 'business_id'], inplace=True)
test_df.sort_index(inplace=True)
#preprocess test data
test2 = pd.merge(test_df, b_df2, how='left', left_index=True, right_index=True)
test2 = pd.merge(test2, u_df2, how='left', left_index=True, right_index=True)

y = com2.stars
X = com2.drop(columns=['stars'])
t_Xtest = test2

xgb = XGBRegressor(n_estimators=100, learning_rate=0.1, random_state=1)
xgb.fit(X, y)
y_xgb= xgb.predict(t_Xtest)

def together_predict(x):
    u, b = x
    ansT, L = item_predict(x)
    queryD = test2.query('user_id== @u and business_id == @b')
    ansM = xgb.predict(queryD)[0]
    out = 0
    if L > 10: out = 0.5*ansT+0.5*ansM
    elif L > 5: out = 0.4*ansT+0.6*ansM
    elif L > 3: out = 0.2*ansT+0.8*ansM
    elif L > 2: out = 0.1*ansT+0.9*ansM
    else: out = ansM
    return out

def write(x):
    return ','.join(str(d) for d in x)

'''
hybrid
'''
test_df['prediction']=y_xgb
test_df = test_df.reset_index()

test_df['prediction'] = test_df.apply(lambda x: 0.2*pred_map[(x.user_id, x.business_id)][0]+0.8*x.prediction if pred_map[(x.user_id, x.business_id)][1] > 20 else 0.99*x.prediction+0.01*pred_map[(x.user_id, x.business_id)][0], axis = 1)

test_df.to_csv(outpath, index=False)
#header = sc.parallelize(["user_id,business_id,prediction"])
#join = header.union(pred)
#join.repartition(1).saveAsTextFile(outpath)

print("--- %s seconds ---" % (time.time() - start))


__author__ = 'Larry A. Hartman'
__company__ = 'Janus Research'

import configparser
import logging
import os
from common import file_ops
from datetime import *

logfile = 'januswm-capture'
logger = logging.getLogger(logfile)

class CaptureCfg(object):
    """
    Class attributes of configuration settings for capture operations
    """
    def __init__(
        self,
        core_cfg: any
    ) -> None:
        """
        Sets object properties either directly or via ConfigParser from ini file

        :param core_cfg: any
        """
        # Define image sequence, dtg, and name for captured image
        core_path_dict = core_cfg.get(attrib='core_path_dict')
        cfg_url_dict = core_cfg.get(attrib='cfg_url_dict')

        self.img_seq = file_ops.f_request(
            file_cmd='data_read',
            file_name=cfg_url_dict['seq'],
            num_bytes=7
        )
        self.img_orig_dtg = datetime.today().strftime('%Y-%m-%d_%H%M')
        img_core_name = self.img_orig_dtg + '_' + self.img_seq
        img_name = img_core_name + '.jpg'

        err_xmit_name = 'errs_' + img_core_name + '.txt'

        self.img_url = os.path.join(
            core_path_dict['img'],
            img_name
        )

        self.err_xmit_url = os.path.join(
            core_path_dict['xmit'],
            err_xmit_name
        )

        self.ini_file = cfg_url_dict['capt']
        self.config = configparser.ConfigParser()
        self.config.read_file(f=open(self.ini_file))

        self.img_capt_dict = {
            'exec_interval': self.config.getint(
                'Capture_Settings',
                'execution_interval'
            ),
            # Image capture frequency in minutes, minimum = 'exec_interval' and maximum = 1440
            'img_capt_freq': self.config.getint(
                'Capture_Settings',
                'image_capture_freq'
            ),
            # Image capture frequency in minutes, minimum = 'exec_capt_freq' and maximum = 1440
            'img_xmit_freq': self.config.getint(
                'Capture_Settings',
                'image_xmit_freq'
            )
        }

        # LED configuration dictionary
        self.led_cfg_dict = {
            'count': 2,             # Number of LED pixels.
            'pin': 18,              # GPIO pin connected to the pixels (18 uses PWM!).
            'freq_hz': 800000,      # LED signal frequency in hertz (usually 800khz)
            'dma': 10,              # DMA channel to use for generating signal (try 10)
            'brightness': 255,      # Set to 0 for darkest and 255 for brightest
            'invert': False,        # True to invert the signal (when using NPN transistor level shift)
            'channel': 0            # set to '1' for GPIOs 13, 19, 41, 45 or 53
        }

        # LED rgbw settings dictionary
        self.led_set_dict = {
            'r': 100,
            'g': 100,
            'b': 100,
            'w': 255
        }

        # Py Camera configuration dictionary
        # PiCam v2 modes:
        # 1  1920x1080
        # 2  3280x2464
        # 5  1640x922
        # 6  1280x720
        # VGA 640x480
        # PAL 768x576
        # SVGA 800x600
        # XGA  1024x768
        self.cam_cfg_dict = {
            'mode': 2,
            'width': 1536,
            'height': 1536,
            'quality': 20,
            'shutter': 100000,              # Microseconds, 0 = auto
            'sharpness': 100,               # -100 to 100, 0 = default
            'saturation': 0,                # -100 to 100, 0 = default
            'rotation': 180,                # 0 to 359 degrees
            'exposure': 'antishake'         # exposure setting
        }

    def get(
        self,
        attrib: str
    ) -> any:
        """
        Gets configuration attributes

        :param attrib: str

        :return: any
        """
        if attrib == 'img_seq':
            return self.img_seq
        elif attrib == 'img_orig_dtg':
            return self.img_orig_dtg
        elif attrib == 'img_url':
            return self.img_url
        elif attrib == 'err_xmit_url':
            return self.err_xmit_url
        elif attrib == 'img_capt_dict':
            return self.img_capt_dict
        elif attrib == 'led_cfg_dict':
            return self.led_cfg_dict
        elif attrib == 'led_set_dict':
            return self.led_set_dict
        elif attrib == 'cam_cfg_dict':
            return self.cam_cfg_dict

    def set(
        self,
        section: str,
        attrib: str,
        value: str
    ) -> (bool, str):
        """
        Sets configuration attributes by updating ini file

        :param section: str
        :param attrib: str
        :param value: str
        :return: set_err: bool
        """
        set_err = False
        valid_section = True
        valid_option = True
        valid_value = True
        log = ''

        if section == 'Capture_Settings':
            if attrib == 'execution_interval':
                try:
                    if int(value) < 0:
                        log = 'Attribute value {0} is less than 1: {1}.'.format(attrib, value)
                        logger.error(msg=log)
                        log1 = 'Retaining previous value.'.format(attrib)
                        logger.warning(msg=log1)
                        valid_value = False

                    elif (int(value) > 1) and (int(value) < 5):
                        log = 'Attribute value {0} is greater than 1 and less than 5: {1}.'.format(attrib, value)
                        logger.error(msg=log)
                        log1 = 'Retaining previous value.'.format(attrib)
                        logger.warning(msg=log1)
                        valid_value = False

                    elif int(value) > 1440:
                        log = 'Attribute value {0} is greater than 1440: {1}.'.format(attrib, value)
                        logger.error(msg=log)
                        log1 = 'Retaining previous value.'.format(attrib)
                        logger.warning(msg=log1)
                        valid_value = False

                    else:
                        if not (int(value) == 5) and \
                                not (int(value) == 6) and \
                                not (int(value) == 10) and \
                                not (int(value) == 12) and \
                                not (int(value) == 15) and \
                                not (int(value) == 20) and \
                                not (int(value) == 30):
                            log = 'Attribute value {0} is not a valid value (5, 6, 10, 12, 15, 20, 30): {1}.'.\
                                format(attrib, value)
                            logger.error(msg=log)
                            log1 = 'Retaining previous value.'.format(attrib)
                            logger.warning(msg=log1)
                            valid_value = False

                except ValueError:
                    log = 'Attribute value {0} is not an integer: {1}.'.format(attrib, value)
                    logger.error(msg=log)
                    log1 = 'Retaining previous value.'.format(attrib)
                    logger.warning(msg=log1)
                    valid_value = False

            elif attrib == 'image_capture_freq':
                try:
                    if int(value) < 0:
                        log = 'Attribute value {0} is less than 0: {1}.'.format(attrib, value)
                        logger.error(msg=log)
                        log1 = 'Retaining previous value.'.format(attrib)
                        logger.warning(msg=log1)
                        valid_value = False

                    elif (int(value) > 1) and (int(value) < self.img_capt_dict['exec_interval']):
                        log = 'Attribute value {0} is between 1 and {1}: {2}.'. \
                            format(attrib, self.img_capt_dict['exec_interval'], value)
                        logger.error(msg=log)
                        log1 = 'Retaining previous value.'.format(attrib)
                        logger.warning(msg=log1)
                        valid_value = False

                    elif int(value) % self.img_capt_dict['exec_interval']:
                        log = 'Attribute value {0} is not divisible by execution interval: {1}.'.format(attrib, value)
                        logger.error(msg=log)
                        log1 = 'Retaining previous value.'.format(attrib)
                        logger.warning(msg=log1)
                        valid_value = False

                    elif int(value) > 1440:
                        log = 'Attribute value {0} is greater than 1440: {1}.'.format(attrib, value)
                        logger.error(msg=log)
                        log1 = 'Retaining previous value.'.format(attrib)
                        logger.warning(msg=log1)
                        valid_value = False

                except ValueError:
                    log = 'Attribute value {0} is not an integer: {1}.'.format(attrib, value)
                    logger.error(msg=log)
                    log1 = 'Retaining previous value.'.format(attrib)
                    logger.warning(msg=log1)
                    valid_value = False

            elif attrib == 'image_xmit_freq':
                try:
                    if int(value) < 0:
                        log = 'Attribute value {0} is less than 0: {1}.'.format(attrib, value)
                        logger.error(msg=log)
                        log1 = 'Retaining previous value.'.format(attrib)
                        logger.warning(msg=log1)
                        valid_value = False

                    elif (int(value) > 1) and (int(value) < self.img_capt_dict['img_capt_freq']):
                        log = 'Attribute value {0} is between 1 and {1}: {2}.'. \
                            format(attrib, self.img_capt_dict['exec_interval'], value)
                        logger.error(msg=log)
                        log1 = 'Retaining previous value.'.format(attrib)
                        logger.warning(msg=log1)
                        valid_value = False

                    elif int(value) % self.img_capt_dict['img_capt_freq']:
                        log = 'Attribute value {0} is not divisible by execution interval: {1}.'.format(attrib, value)
                        logger.error(msg=log)
                        log1 = 'Retaining previous value.'.format(attrib)
                        logger.warning(msg=log1)
                        valid_value = False

                    elif int(value) > 1440:
                        log = 'Attribute value {0} is greater than 1440: {1}.'.format(attrib, value)
                        logger.error(msg=log)
                        log1 = 'Retaining previous value.'.format(attrib)
                        logger.warning(msg=log1)
                        valid_value = False

                except ValueError:
                    log = 'Attribute value {0} is not an integer: {1}.'.format(attrib, value)
                    logger.error(msg=log)
                    log1 = 'Retaining previous value.'.format(attrib)
                    logger.warning(msg=log1)
                    valid_value = False

            else:
                valid_option = False

        else:
            valid_section = False
            valid_option = False

        if valid_section:
            if valid_option:
                if valid_value:
                    try:
                        self.config.set(
                            section=section,
                            option=attrib,
                            value=value
                        )
                        self.config.write(
                            fp=open(
                                file=self.ini_file,
                                mode='w',
                                encoding='utf-8'
                            ),
                            space_around_delimiters=True
                        )
                        log = 'ConfigParser successfully set and wrote options to file.'
                        logger.info(msg=log)
                    except Exception as exc:
                        log = 'ConfigParser failed to set and write options to file.'
                        logger.error(msg=log)
                        logger.error(msg=exc)
                        print(log)
                        print(exc)

            else:
                set_err = True
                log = 'ConfigParser failed to set and write options to file, invalid option.'
                logger.error(msg=log)

        else:
            set_err = True
            log = 'ConfigParser failed to set and write options to file, invalid section.'
            logger.error(msg=log)

        return set_err, log


import json
import boto3

def handler(event, context):
    s3_client = boto3.client('s3')
    s3_clientobj = s3_client.get_object(Bucket='tp-data-bucket', Key='tp-data.txt')

    for line in s3_clientobj['Body'].iter_lines():
        object = json.loads(line)
        print(f"Name: {object['name']['s']} ID: {object['id']['n']}")
    print("Welcome to TechPrimers")
    print("Lambda execution Completed...!")


# importing the modules
import os

import requests
from bs4 import BeautifulSoup

def extract_title(url):
    # target url
    #url = 'https://dahd.nic.in/division/administration/vigilance-apar-cell'
    # making requests instance
    reqs = requests.get(url)
    # using the BeautifulSoup module
    soup = BeautifulSoup(reqs.text, 'html.parser')
    # displaying the title
    print("Title of the page is : ")
    for title in soup.find_all('title'):
        print(title.get_text())
    # Finding all meta tags present, stored in a list format
    # meta_tag = soup.findAll('meta')
    # Looping the meta tag list
    # print(meta_tag)
    # for x in meta_tag:
    # print(x.attrs['keywords'])
    # print('-----------------')

if __name__ == '__main__':
    #script_name = os.path.basename(__file__)
    urls_file = open('crawler_output/url_list.txt', 'r')
    urls_all = urls_file.readlines()
    for url in urls_all:
        print(url)
        extract_title(url.strip())
    urls_file.close()


from __future__ import annotations

import ast
import asyncio
import time
from dataclasses import dataclass
from itertools import chain
from pathlib import Path
from typing import TYPE_CHECKING, Any, Iterator, List, Optional, cast

from robotcode.core.async_tools import check_canceled, check_canceled_sync, threaded
from robotcode.core.dataclasses import CamelSnakeMixin
from robotcode.core.logging import LoggingDescriptor
from robotcode.core.uri import Uri
from robotcode.jsonrpc2.protocol import rpc_method
from robotcode.language_server.common.lsp_types import DocumentUri, Position, Range, TextDocumentIdentifier
from robotcode.language_server.robotframework.configuration import RobotConfig
from robotcode.language_server.robotframework.utils.async_ast import AsyncVisitor

from .protocol_part import RobotLanguageServerProtocolPart

if TYPE_CHECKING:
    from robotcode.language_server.robotframework.protocol import RobotLanguageServerProtocol

@dataclass
class GetAllTestsParams(CamelSnakeMixin):
    workspace_folder: str
    paths: Optional[List[str]] = None
    suites: Optional[List[str]] = None

@dataclass
class GetTestsParams(CamelSnakeMixin):
    text_document: TextDocumentIdentifier
    base_name: Optional[str]

@dataclass
class GetTestsFromDocumentParams(CamelSnakeMixin):
    text_document: TextDocumentIdentifier

@dataclass
class TestItem(CamelSnakeMixin):
    type: str
    id: str
    label: str
    longname: str
    uri: Optional[DocumentUri] = None
    children: Optional[List[TestItem]] = None
    description: Optional[str] = None
    range: Optional[Range] = None
    tags: Optional[List[str]] = None
    error: Optional[str] = None

class FindTestCasesVisitor(AsyncVisitor):
    async def get(self, source: DocumentUri, model: ast.AST, base_name: Optional[str]) -> List[TestItem]:
        self._results: List[TestItem] = []
        self.source = source
        self.path = Uri(source).to_path().resolve()
        self.base_name = base_name
        await self.visit(model)
        return self._results

    async def visit_Section(self, node: ast.AST) -> None:  # noqa: N802
        from robot.parsing.model.blocks import TestCaseSection

        if isinstance(node, TestCaseSection):
            await self.generic_visit(node)

    async def visit_TestCase(self, node: ast.AST) -> None:  # noqa: N802
        from robot.parsing.model.blocks import TestCase
        from robot.parsing.model.statements import Tags

        test_case = cast(TestCase, node)
        longname = f"{self.base_name}.{test_case.name}" if self.base_name else test_case.name
        self._results.append(
            TestItem(
                type="test",
                id=f"{self.path};{longname};{test_case.lineno}",
                longname=longname,
                label=test_case.name,
                uri=self.source,
                range=Range(
                    start=Position(line=test_case.lineno - 1, character=test_case.col_offset),
                    end=Position(
                        line=(test_case.end_lineno if test_case.end_lineno != -1 else test_case.lineno) - 1,
                        character=test_case.end_col_offset if test_case.end_col_offset != -1 else test_case.col_offset,
                    ),
                ),
                tags=[str(tag) for tag in chain(*[tags.values for tags in test_case.body if isinstance(tags, Tags)])],
            )
        )

class DiscoveringProtocolPart(RobotLanguageServerProtocolPart):
    _logger = LoggingDescriptor()

    def __init__(self, parent: RobotLanguageServerProtocol) -> None:
        super().__init__(parent)
        self._patch()

    def _patch(self) -> None:
        from robot.api.parsing import File, get_model
        from robot.running import TestSuite
        from robot.running.builder.builders import RobotParser, TestSuiteBuilder

        from robotcode.language_server.robotframework.utils.version import get_robot_version

        def get_document_text(source: str) -> str:
            check_canceled_sync()

            if self.parent.loop:
                doc = self.parent.documents.get_sync(Uri.from_path(source).normalized())
                if doc is not None:
                    return doc.text()

            return source

        def get_source(self: Any, source: str) -> str:
            return get_document_text(source)

        RobotParser._get_source = get_source

        orig = RobotParser._build

        def my_get_model_v4(source: str, data_only: bool = False, curdir: Optional[str] = None) -> Any:
            check_canceled_sync()
            try:
                return get_model(source, data_only, curdir)
            except (SystemExit, KeyboardInterrupt):
                raise
            except BaseException as e:
                ex = e
                self._logger.critical(lambda: f"Can't parse {source}: {ex}")
                return File(source=source)

        def my_get_model_v6(
            source: str, data_only: bool = False, curdir: Optional[str] = None, lang: Any = None
        ) -> Any:
            check_canceled_sync()
            try:
                return get_model(source, data_only, curdir, lang)
            except (SystemExit, KeyboardInterrupt):
                raise
            except BaseException as e:
                ex = e
                self._logger.critical(lambda: f"Can't parse {source}: {ex}")
                return File(source=source)

        my_get_model = my_get_model_v4 if get_robot_version() < (6, 0) else my_get_model_v6

        def build(
            self: Any,
            suite: TestSuite,
            source: str,
            defaults: Any,
            model: Any = None,
            get_model: Any = my_get_model,
        ) -> TestSuite:
            check_canceled_sync()

            return orig(self, suite, source, defaults, model, get_model)

        RobotParser._build = build

        def _validate_test_counts(self: Any, suite: TestSuite, multisource: bool = False) -> None:
            # we don't need this
            pass

        TestSuiteBuilder._validate_test_counts = _validate_test_counts

    async def get_config(self, workspace_uri: str) -> Optional[RobotConfig]:
        folder = self.parent.workspace.get_workspace_folder(workspace_uri)
        if folder is None:
            return None

        return await self.parent.workspace.get_configuration(RobotConfig, folder.uri)

    @rpc_method(name="robot/discovering/getTestsFromWorkspace", param_type=GetAllTestsParams)
    @threaded()
    async def get_tests_from_workspace(
        self,
        workspace_folder: str,
        paths: Optional[List[str]] = None,
        suites: Optional[List[str]] = None,
        *args: Any,
        **kwargs: Any,
    ) -> List[TestItem]:
        from robot.output.logger import LOGGER
        from robot.running import TestCase, TestSuite
        from robot.running.builder.builders import TestSuiteBuilder

        from robotcode.language_server.robotframework.utils.version import get_robot_version

        async def generate(suite: TestSuite) -> TestItem:
            children: List[TestItem] = []

            await check_canceled()

            test: TestCase
            for test in suite.tests:
                children.append(
                    TestItem(
                        type="test",
                        id=f"{Path(test.source).resolve() if test.source is not None else ''};"
                        f"{test.longname};{test.lineno}",
                        label=test.name,
                        longname=test.longname,
                        uri=str(Uri.from_path(test.source)) if test.source else None,
                        range=Range(
                            start=Position(line=test.lineno - 1, character=0),
                            end=Position(line=test.lineno - 1, character=0),
                        ),
                        tags=list(test.tags),
                    )
                )

            for s in suite.suites:
                children.append(await generate(s))

            return TestItem(
                type="suite",
                id=f"{Path(suite.source).resolve() if suite.source is not None else ''};{suite.longname}",
                label=suite.name,
                longname=suite.longname,
                uri=str(Uri.from_path(suite.source)) if suite.source else None,
                children=children,
                range=Range(
                    start=Position(line=0, character=0),
                    end=Position(line=0, character=0),
                )
                if suite.source
                else None,
            )

        self._logger.info("Wait for workspace documents loaded...")

        await self.parent.robot_workspace.documents_loaded.wait()

        start = time.monotonic()

        self._logger.info("Start discovering tests")

        workspace_path = Uri(workspace_folder).to_path()
        canceled = False
        LOGGER._cache_only = True
        try:
            config = await self.get_config(workspace_folder)
            rpa_mode = config.get_rpa_mode() if config is not None else None
            languages = await self.parent.documents_cache.get_workspace_languages(workspace_folder)
            parsers = config.parsers if config is not None else None

            if paths is None and config is not None:
                paths = config.paths

            if paths and len(paths):

                def normalize_paths(paths: List[str]) -> Iterator[str]:
                    for path in paths:
                        p = Path(path)

                        if not p.is_absolute():
                            p = Path(workspace_path, p)

                        if p.exists():
                            yield str(p)

                def nonexisting_paths(paths: List[str]) -> Iterator[str]:
                    for path in paths:
                        p = Path(path)

                        if not p.is_absolute():
                            p = Path(workspace_path, p)

                        if not p.exists():
                            yield str(p)

                valid_paths = list(normalize_paths(paths))

                if get_robot_version() >= (6, 1):
                    builder = TestSuiteBuilder(
                        included_suites=suites if suites else None,
                        rpa=rpa_mode,
                        lang=languages,
                        parsers=parsers,
                    )
                elif get_robot_version() >= (6, 0):
                    builder = TestSuiteBuilder(
                        included_suites=suites if suites else None,
                        rpa=rpa_mode,
                        lang=languages,
                    )
                else:
                    builder = TestSuiteBuilder(included_suites=suites if suites else None, rpa=rpa_mode)

                suite: Optional[TestSuite] = builder.build(*valid_paths) if valid_paths else None
                suite_item = [await generate(suite)] if suite else []

                return [
                    TestItem(
                        type="workspace",
                        id=str(Path.cwd().resolve()),
                        label=Path.cwd().name,
                        longname=Path.cwd().name,
                        uri=str(Uri.from_path(Path.cwd())),
                        children=[
                            *suite_item,
                            *[
                                TestItem(
                                    type="error",
                                    id=f"{i};ERROR",
                                    longname="error",
                                    label=i,
                                    error=f"Parsing '{i}' failed: File or directory to does not exist.",
                                )
                                for i in nonexisting_paths(paths)
                            ],
                        ],
                    )
                ]

            if get_robot_version() >= (6, 1):
                builder = TestSuiteBuilder(
                    included_suites=suites if suites else None,
                    rpa=rpa_mode,
                    lang=languages,
                    parsers=parsers,
                )
            elif get_robot_version() >= (6, 0):
                builder = TestSuiteBuilder(included_suites=suites if suites else None, rpa=rpa_mode, lang=languages)
            else:
                builder = TestSuiteBuilder(included_suites=suites if suites else None, rpa=rpa_mode)
            return [await generate(builder.build(str(workspace_path)))]
        except (SystemExit, KeyboardInterrupt):
            raise
        except asyncio.CancelledError:
            canceled = True
            self._logger.info("Tests discovery canceled")
            raise
        except BaseException as e:
            ex = e
            self._logger.info(lambda: f"Failed to discover tests: {ex}")
            return [
                TestItem(
                    type="error",
                    id=str(Uri.from_path(Path.cwd().resolve())),
                    longname="error",
                    label=Path.cwd().name,
                    error=str(e),
                )
            ]
        finally:
            if not canceled:
                self._logger.info(lambda: f"Tests discovery took {time.monotonic() - start}s")

    @rpc_method(name="robot/discovering/getTestsFromDocument", param_type=GetTestsParams)
    @threaded()
    async def get_tests_from_document(
        self, text_document: TextDocumentIdentifier, base_name: Optional[str], *args: Any, **kwargs: Any
    ) -> List[TestItem]:
        try:
            return await FindTestCasesVisitor().get(
                text_document.uri,
                await self.parent.documents_cache.get_model(
                    await self.parent.documents.get_or_open_document(
                        Uri(text_document.uri).to_path(), language_id="robotframework"
                    )
                ),
                base_name,
            )
        except (asyncio.CancelledError, SystemExit, KeyboardInterrupt):
            raise
        except BaseException:
            return []

{'TestCaseSection': 'robot.parsing.model.blocks.TestCaseSection', 'TestCase': 'robot.parsing.model.blocks.TestCase', 'Tags': 'robot.parsing.model.statements.Tags'}


"""
This file is imported with some modification from gdown repo, many thanks and support to them.
gdown repo: https://github.com/wkentaro/gdown
"""
from __future__ import print_function

import glob
import json
import os
import os.path as osp
import re
import shutil
import sys
import tempfile
import textwrap
import time

import requests
import six
import tqdm

from .parse_url import parse_url

CHUNK_SIZE = 512 * 1024  # 512KB
home = osp.expanduser("~")

if hasattr(textwrap, "indent"):
    indent_func = textwrap.indent
else:

    def indent_func(text, prefix):
        def prefixed_lines():
            for line in text.splitlines(True):
                yield (prefix + line if line.strip() else line)

        return "".join(prefixed_lines())

def get_url_from_gdrive_confirmation(contents):
    url = ""
    for line in contents.splitlines():
        m = re.search(r'href="(\/uc\?export=download[^"]+)', line)
        if m:
            url = "https://docs.google.com" + m.groups()[0]
            url = url.replace("&", "&")
            break
        m = re.search("confirm=([^;&]+)", line)
        if m:
            confirm = m.groups()[0]
            url = re.sub(
                r"confirm=([^;&]+)", r"confirm={}".format(confirm), url
            )
            break
        m = re.search('"downloadUrl":"([^"]+)', line)
        if m:
            url = m.groups()[0]
            url = url.replace("\\u003d", "=")
            url = url.replace("\\u0026", "&")
            break
        m = re.search('
(.*)
', line)
        if m:
            error = m.groups()[0]
            raise RuntimeError(error)
    if not url:
        raise RuntimeError(
            "Cannot retrieve the public link of the file. "
            "You may need to change the permission to "
            "'Anyone with the link', or have had many accesses."
        )
    return url

def download(
    url=None,
    output=None,
    quiet=False,
    proxy=None,
    speed=None,
    use_cookies=True,
    verify=True,
    id=None,
    fuzzy=False,
    resume=False,
):
    """Download file from URL.

    Parameters
    ----------
    url: str
        URL. Google Drive URL is also supported.
    output: str
        Output filename. Default is basename of URL.
    quiet: bool
        Suppress terminal output. Default is False.
    proxy: str
        Proxy.
    speed: float
        Download byte size per second (e.g., 256KB/s = 256 * 1024).
    use_cookies: bool
        Flag to use cookies. Default is True.
    verify: bool or string
        Either a bool, in which case it controls whether the server’s TLS
        certificate is verified, or a string, in which case it must be a path
        to a CA bundle to use. Default is True.
    id: str
        Google Drive's file ID.
    fuzzy: bool
        Fuzzy extraction of Google Drive's file Id. Default is False.
    resume: bool
        Resume the download from existing tmp file if possible.
        Default is False.

    Returns
    -------
    output: str
        Output filename.
    """
    if not (id is None) ^ (url is None):
        raise ValueError("Either url or id has to be specified")
    if id is not None:
        url = "https://drive.google.com/uc?id={id}".format(id=id)

    url_origin = url
    sess = requests.session()
    res = None
    # Load cookies
    cache_dir = osp.join(home, ".cache", "gdown")
    if not osp.exists(cache_dir):
        os.makedirs(cache_dir)
    cookies_file = osp.join(cache_dir, "cookies.json")
    if osp.exists(cookies_file) and use_cookies:
        with open(cookies_file) as f:
            cookies = json.load(f)
        for k, v in cookies:
            sess.cookies[k] = v

    if proxy is not None:
        sess.proxies = {"http": proxy, "https": proxy}
        print("Using proxy:" + proxy)

    gdrive_file_id, is_gdrive_download_link = parse_url(url, warning=not fuzzy)

    if fuzzy and gdrive_file_id:
        # overwrite the url with fuzzy match of a file id
        url = "https://drive.google.com/uc?id={id}".format(id=gdrive_file_id)
        url_origin = url
        is_gdrive_download_link = True

    headers = {
        "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36"  # NOQA
    }

    while True:
        try:
            if id is None:
                res = sess.get(url,headers=headers, stream=True, timeout= (20,5))
            else:
                res = sess.get(url, headers=headers, stream=True, verify=verify, timeout = 30)

        except requests.exceptions.ProxyError as e:
            print("An error has occurred using proxy")
            return "Error"
        except requests.exceptions.ConnectionError as e:
            print("An error has occurred connectiong to download server")
            return "Error"
        except requests.exceptions.ReadTimeout as e:
            print("An error has occurred connectiong to download server")
            return "Error"

        # Save cookies
        with open(cookies_file, "w") as f:
            cookies = [
                (k, v)
                for k, v in sess.cookies.items()
                if not k.startswith("download_warning_")
            ]
            json.dump(cookies, f, indent=2)

        if "Content-Disposition" in res.headers:
            # This is the file
            break
        if not (gdrive_file_id and is_gdrive_download_link):
            break

        # Need to redirect with confirmation
        try:
            url = get_url_from_gdrive_confirmation(res.text)
        except RuntimeError as e:
            print("Access denied with the following error:")
            error = "\n".join(textwrap.wrap(str(e)))
            error = indent_func(error, "\t")
            print("\n"+ error+"\n")
            print("You may still be able to access the file from the browser")
            print("\n\t"+url_origin+"\n")
            return "Error"

    if gdrive_file_id and is_gdrive_download_link:
        m = re.search('filename="(.*)"', res.headers["Content-Disposition"])
        filename_from_url = m.groups()[0]
    else:
        filename_from_url = osp.basename(url)

    if output is None:
        output = filename_from_url

    output_is_path = isinstance(output, six.string_types)
    if output_is_path and output.endswith(osp.sep):
        if not osp.exists(output):
            os.makedirs(output)
        output = osp.join(output, filename_from_url)

    if output_is_path:
        existing_tmp_files = glob.glob("{}*".format(output))
        if resume and existing_tmp_files:
            tmp_file = existing_tmp_files[0]
        else:
            resume = False
            tmp_file = tempfile.mktemp(
                suffix=tempfile.template,
                prefix=osp.basename(output),
                dir=osp.dirname(output),
            )
            tmp_file = osp.join(osp.dirname(output),osp.basename(output))
            print(tmp_file)
        f = open(tmp_file, "wb")
    else:
        tmp_file = None
        f = output
    print(f.tell())
    if tmp_file is not None and f.tell() != 0:
        headers["Range"] = "bytes={}-".format(f.tell())
        try:
            if id is None:
                res = sess.get(url, stream=True, timeout = (20,5))
            else:
                res = sess.get(url, headers=headers, stream=True, verify=verify, timeout = 30)

        except requests.exceptions.ConnectionError as e:
            print("An error has occurred connectiong to download server")
            return "Error"
        except requests.exceptions.ReadTimeout as e:
            print("An error has occurred connectiong to download server")
            return "Error"

    if not quiet:
        print("Downloading started ...")
        if resume:
            print("Resume: ", tmp_file)
        print("From: "+ url_origin)
        print(
            "To: "+ osp.abspath(output) if output_is_path else output,)

    try:
        total = res.headers.get("Content-Length")
        if total is not None:
            total = int(total)
        if not quiet:
            pbar = tqdm.tqdm(total=total, unit="B", unit_scale=True)
        t_start = time.time()
        for chunk in res.iter_content(chunk_size=CHUNK_SIZE):
            if time.time() - t_start > 50:
                return "Error"
            f.write(chunk)
            if not quiet:
                pbar.update(len(chunk))
            if speed is not None:
                elapsed_time_expected = 1.0 * pbar.n / speed

                elapsed_time = time.time() - t_start
                if elapsed_time < elapsed_time_expected:
                    time.sleep(elapsed_time_expected - elapsed_time)
            t_start = time.time()

        if not quiet:
            pbar.close()
        if tmp_file:
            f.close()
            shutil.move(tmp_file, output)
    except IOError as e:
        print(e)
        return "Error"
    except requests.exceptions.RequestException as e:  # This is the correct syntax
            return "Error"

    except requests.exceptions.ConnectionError as e:
        print("An error has occurred connectiong to download server")
        return "Error"
    except requests.exceptions.ReadTimeout as e:
        print("An error has occurred connectiong to download server")
        return "Error"
    finally:
        sess.close()

    return output