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

# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: http://doc.scrapy.org/en/latest/topics/item-pipeline.html

from scrapy.selector import Selector
from requests import get
import csv
from os import mkdir

class parser(object):
    """
    First pipeline class of two.
    Parses raw html data and assigns them into variables.
    """
    def process_item(self, item, spider):
        """
        Parse raw html data from Therapist item and assign values to fields.

        Tries to find each element, if it can't, that element is assigned 0
        as a value.
        """
        # scrape profile picture
        try:
            xpath = "//div[@class='row']/div[@class='col-xs-4 col-sm-2 col-md-2 col-lg-2 photo-column no-padding-right']/span[@id='profilePhoto']/img/@src"
            profile_picture_url = Selector(text=item['html_body']).xpath(xpath).extract_first()
            item['profile_picture_binary'] = get(profile_picture_url).content
        except ValueError:
            item['profile_picture_binary'] = 0

        # definition of a bookmark so to keep from writing the same xpath
        # dozens of times for each element
        mark = "//div[@class='profile-middle profile-flag col-xs-8 col-sm-10 col-md-10 col-lg-10']/div[@class='row']/div[@class='hidden-xs col-sm-6 col-md-7 col-lg-7']"

        # name
        try:
            xpath = mark+"/h1[@itemprop='name']/text()"
            item['name'] = Selector(text=item['html_body']).xpath(xpath).extract_first().strip()
        except ValueError:
            item['name'] = 0

        # specialty title
        try:
            xpath = mark+"/div[@class='profile-title']/h2/span[1]/span/text()"
            item['specialty_title'] = Selector(text=item['html_body']).xpath(xpath).extract_first()
            if item['specialty_title']:
                item['specialty_title'] = item['specialty_title'].strip()
        except ValueError:
            item['specialty_title'] = 0

        # specialties (MD, NCC, SAP, etc...)
        try:
            xpath = mark+"/div[@class='profile-title']/h2/span[position()>1]/span/text()"
            # puts all specialties (MD, NCC, SAP, etc...) into one string separated by semicolons
            item['specialty_title2'] = ';'.join(Selector(text=item['html_body']).xpath(xpath).extract())
        except ValueError:
            item['specialty_title2'] = 0

        # website link
        try:
            xpath = "//div[@class='row']/div[@class='col-xs-4 col-sm-2 col-md-2 col-lg-2 photo-column no-padding-right']/div[@class='hidden-xs profile-buttons']/a[@class='btn btn-block hidden-xs']/@href"
            item['website_link'] = Selector(text=item['html_body']).xpath(xpath).extract_first()
        except ValueError:
            item['website_link'] = 0

        # phone number
        try:
            xpath = "//div[@class='row']/div[@class='profile-middle profile-flag col-xs-8 col-sm-10 col-md-10 col-lg-10']/div[@class='row']/div[@class='hidden-xs col-sm-6 col-md-5 col-lg-5 text-right']/div[@class='profile-phone']/span/a/text()"
            item['phone'] = Selector(text=item['html_body']).xpath(xpath).extract_first()
        except ValueError:
            item['phone'] = 0

        # re-definition of mark
        # PROBLEk
        mark = "//div[@class='row']/div[@class='col-xs-12 col-sm-10 col-md-10 col-lg-10']/div[@id='profile-content']/div[@class='row']/div[@class='col-xs-12 col-sm-8 col-md-7 col-lg-7']"

        # company name
        try:
            xpath = mark+"/div[@class='hidden-sm hidden-md hidden-lg']/div[@class='profile-address']/div[@class='address address-rank-1']/div[@itemprop='address']/text()"
            item['company_name'] = Selector(text=item['html_body']).xpath(xpath).extract_first()
            try:
                if not item['company_name'].strip():
                    item['company_name'] = 0
            except:
                pass
        except ValueError:
            item['company_name'] = 0

        # location 1
        xpath = mark+"/div[@class='hidden-sm hidden-md hidden-lg']/div[@class='profile-address']/div[@class='address address-rank-1']/div[@itemprop='address']"
        location_html = Selector(text=item['html_body']).xpath(xpath).extract_first()
        loclist = []
        xpath = "//span[@itemprop='streetAddress']/text()"
        addr1 = Selector(text=location_html).xpath(xpath).extract_first()
        try:
            addr1 = addr1.strip()
        except:
            pass
        loclist.append(addr1)
        try:
            xpath = mark+"/div[@class='hidden-sm hidden-md hidden-lg']/div[@class='profile-address']/div[@class='address address-rank-1']/div[@itemprop='address']/text()[normalize-space()]"
            loclist.append(Selector(text=item['html_body']).xpath(xpath).extract()[1])
        except IndexError:
            loclist.append('0')
        xpath = "//span[@itemprop='addressLocality']/text()"
        city = Selector(text=location_html).xpath(xpath).extract_first()
        try:
            city = city.strip()
        except:
            pass
        loclist.append(city)
        xpath = "//span[@itemprop='addressRegion']/text()"
        state = Selector(text=location_html).xpath(xpath).extract_first()
        try:
            state = state.strip()
        except:
            pass
        loclist.append(state)
        xpath = "//span[@itemprop='postalcode']/text()"
        zip_code = Selector(text=location_html).xpath(xpath).extract_first()
        try:
            zip_code = zip_code.strip()
        except:
            pass
        loclist.append(zip_code)
        newloclist = []
        for locitem in loclist:
            if not isinstance(locitem, str):
                try:
                    newlocitem = str(locitem)
                    newloclist.append(newlocitem)
                except:
                    newloclist.append('0')
            newloclist.append(locitem)
        newnewloclist = []
        for li in newloclist:
            try:
                if not li.strip() in newnewloclist and li.strip() and li.strip() != ',' and li.strip() != 'None':
                    newnewloclist.append(li.strip())
            except:
                pass
        item['location'] = ';'.join(newnewloclist)

        # location2
        xpath = mark+"/div[@class='hidden-sm hidden-md hidden-lg']/div[@class='profile-address']/div[@class='address address-rank-2']/div[@itemprop='address']"
        location_html = Selector(text=item['html_body']).xpath(xpath).extract_first()
        loclist = []
        xpath = "//span[@itemprop='streetAddress']/text()"
        addr1 = Selector(text=location_html).xpath(xpath).extract_first()
        try:
            addr1 = addr1.strip()
        except:
            pass
        loclist.append(addr1)
        try:
            xpath = mark+"/div[@class='hidden-sm hidden-md hidden-lg']/div[@class='profile-address']/div[@class='address address-rank-1']/div[@itemprop='address']/text()[normalize-space()]"
            loclist.append(Selector(text=item['html_body']).xpath(xpath).extract()[1])
        except IndexError:
            loclist.append('0')
        xpath = "//span[@itemprop='addressLocality']/text()"
        city = Selector(text=location_html).xpath(xpath).extract_first()
        try:
            city = city.strip()
        except:
            pass
        loclist.append(city)
        xpath = "//span[@itemprop='addressRegion']/text()"
        state = Selector(text=location_html).xpath(xpath).extract_first()
        try:
            state = state.strip()
        except:
            pass
        loclist.append(state)
        xpath = "//span[@itemprop='postalcode']/text()"
        zip_code = Selector(text=location_html).xpath(xpath).extract_first()
        try:
            zip_code = zip_code.strip()
        except:
            pass
        loclist.append(zip_code)
        newloclist = []
        for locitem in loclist:
            if not isinstance(locitem, str):
                try:
                    newlocitem = str(locitem)
                    newloclist.append(newlocitem)
                except:
                    newloclist.append('0')
            newloclist.append(locitem)
        newnewloclist = []
        for li in newloclist:
            try:
                if not li.strip() in newnewloclist and li.strip() and li.strip() != ',' and li.strip() != 'None':
                    newnewloclist.append(li.strip())
            except:
                pass
        # must be full address to be entered
        if len(newnewloclist) >= 3:
            item['additional_location'] = ';'.join(newnewloclist)
        else:
            item['additional_location'] = 0

        # about
        try:
            xpath = mark+"/div[@class='profile-statement']/div[@class='section profile-personalstatement']/div/text()"
            # joins each paragraph of the about section
            item['about'] = ''.join(Selector(text=item['html_body']).xpath(xpath).extract())
        except ValueError:
            item['about'] = 0

        # center pictures
        # try:
            # xpath = mark+"/div[@class='profile-gallery']/div[@class='gallery*']/div[class='sliderContainer']/div[@class='slider']/ul/li/a/@href"
            # center_pictures_urls = Selector(text=item['html_body']).xpath(xpath).extract()
            # for center_picture_url in center_pictures_urls:
                # piclist = []
                # piclist.append(get(center_picture_url).content())
                # item['center_pictures_binary_list'] = ';'.join(piclist)
        # except ValueError:
            # item['center_pictures_binary_list'] = 0

        # years in practice
        try:
            xpath = mark+"/div[@class='profile-qualifications']/ul/li[strong/text()='Years in Practice:']/text()"
            try:
                item['years_in_practice'] = Selector(text=item['html_body']).xpath(xpath).extract()[1]
            except IndexError:
                item['years_in_practice'] = 0
        except ValueError:
            item['years_in_practice'] = 0

        # school graduated
        try:
            xpath = mark+"/div[@class='profile-qualifications']/ul/li[strong/text()='School:']/text()"
            try:
                item['school'] = Selector(text=item['html_body']).xpath(xpath).extract()[1]
            except IndexError:
                item['school'] = 0
        except ValueError:
            item['school'] = 0

        # year graduated
        try:
            xpath = mark+"/div[@class='profile-qualifications']/ul/li[strong/text()='Year Graduated:']/text()"
            yr = Selector(text=item['html_body']).xpath(xpath).extract()
            try:
                yr = yr[1].strip()
                item['year_graduated'] = yr
            except:
                item['year_graduated'] = 0
        except ValueError:
            item['year_graduated'] = 0

        # license number and state
        try:
            xpath = mark+"/div[@class='profile-qualifications']/ul/li[strong/text()='License No. and State:']/text()"
            l = Selector(text=item['html_body']).xpath(xpath).extract()
            try:
                item['license_num_and_state'] = l[1].strip()
            except:
                item['license_num_and_state'] = 0
        except ValueError:
            item['license_num_and_state'] = 0

        # cost per session
        try:
            xpath = mark+"/div[@class='profile-finances']/ul/li[strong/text()='Avg Cost (per session):']/text()"
            try:
                item['cost_per_session'] = Selector(text=item['html_body']).xpath(xpath).extract()[1]
            except IndexError:
                item['cost_per_session'] = 0
        except ValueError:
            item['cost_per_session'] = 0

        # sliding scale
        try:
            xpath = mark+"/div[@class='profile-finances']/ul/li[strong/text()='Sliding Scale:']/text()"
            try:
                item['sliding_scale'] = Selector(text=item['html_body']).xpath(xpath).extract()[1]
            except:
                item['sliding_scale'] = 0
        except ValueError:
            item['sliding_scale'] = 0

        ## finances
        # accept insurance
        try:
            xpath = mark+"/div[@class='profile-finances']/ul/li[strong/text()='Accepts Insurance:']/text()"
            accepts_insurance = Selector(text=item['html_body']).xpath(xpath).extract()
            try:
                accepts_insurance_new = accepts_insurance[1].strip()
                item['accepts_insurance'] = accepts_insurance_new
            except IndexError:
                item['accepts_insurance'] = 0
        except ValueError:
            item['accepts_insurance'] = 0

        # accepted payment methods
        try:
            xpath = mark+"/div[@class='profile-finances']/ul/li[strong/text()='Accepted Payment Methods: ']/text()"
            apm = Selector(text=item['html_body']).xpath(xpath).extract()
            try:
                apm = apm[1].strip()
                new_apm = []
                for i in apm.split(","):
                    new_apm.append(i.strip())
                apm = ';'.join(new_apm)
                item['accepted_payment_methods'] = apm
            except:
                item['accepted_payment_methods'] = 0
        except ValueError:
            item['accepted_payment_methods'] = 0

        # accepted insurance plans
        try:
            xpath = mark+"/div[@class='profile-finances']/div[@class='spec-list']/div[@class='row']/div/ul/li/text()"
            # each insurance plan is separated by a semicolon
            item['accepted_insurance_plans'] = ';'.join(Selector(text=item['html_body']).xpath(xpath).extract())
        except ValueError:
            item['accepted_insurance_plans'] = 0

        # additional credentials
        try:
            xpath = mark+"/div[@class='profile-additional-credentials']/ul/li/text()"
            # each credential is separated by a semicolon
            item['additional_credentials'] = ';'.join(Selector(text=item['html_body']).xpath(xpath).extract())
        except ValueError:
            item['additional_credentials'] = 0

        # groups
        try:
            xpath = mark+"/div[@class='group-small']/h2/a/text()"
            item['groups'] = ';'.join(Selector(text=item['html_body']).xpath(xpath).extract())
        except ValueError:
            item['groups'] = 0

        # re-definition of mark
        mark = "//div[@class='row']/div[@class='col-xs-12 col-sm-10 col-md-10 col-lg-10']/div[@id='profile-content']/div[@class='row']/div[@class='col-xs-12 col-sm-4 col-md-5 col-lg-5']"

        # specialties
        try:
            xpath = mark+"/div[@class='spec-list'][1]/div[@class='row'][1]/div/ul/li/text()"
            item['specialties'] = ';'.join(Selector(text=item['html_body']).xpath(xpath).extract())
        except ValueError:
            item['specialties'] = 0

        # issues
        try:
            xpath = mark+"/div[@class='spec-list'][1]/div[@class='row'][2]/div/ul/li/text()"
            item['issues'] = ';'.join(Selector(text=item['html_body']).xpath(xpath).extract())
        except ValueError:
            item['issues'] = 0

        # mental health
        try:
            xpath = mark+"/div[@class='spec-list'][1]/div[@class='row'][3]/div/ul/li/text()"
            item['mental_health'] = ';'.join(Selector(text=item['html_body']).xpath(xpath).extract())
        except ValueError:
            item['mental_health'] = 0

        ## client focus
        # ethnicity
        try:
            xpath = mark+"/div[@class='spec-list'][2]/div[@class='spec-subcat' and strong/text()='Ethnicity:']/span/text()"
            item['ethnicity'] = Selector(text=item['html_body']).xpath(xpath).extract_first()
        except ValueError:
            item['ethnicity'] = 0
        # language
        # religion
        try:
            xpath = mark+"/div[@class='spec-list'][2]/div[@class='spec-subcat' and strong/text()='Religious Orientation:']/span/text()"
            item['religion'] = Selector(text=item['html_body']).xpath(xpath).extract_first()
        except ValueError:
            item['relgion'] = 0

        # age
        try:
            xpath = mark+"/div[@class='spec-list'][2]/div[@class='row'][1]/div/ul/li/text()"
            item['age'] = ';'.join(Selector(text=item['html_body']).xpath(xpath).extract())
        except ValueError:
            item['age'] = 0

        # categories
        try:
            xpath = mark+"/div[@class='spec-list'][2]/div[@class='row'][2]/div/ul/li/text()"
            item['categories'] = ';'.join(Selector(text=item['html_body']).xpath(xpath).extract())
        except ValueError:
            item['categories'] = 0

        ## treatment approach
        # treatment orientation
        try:
            xpath = mark+"/div[@class='spec-list'][3]/div[@class='row'][1]/div/ul/li/text()"
            item['treatment_orientation'] = ';'.join(Selector(text=item['html_body']).xpath(xpath).extract())
        except ValueError:
            item['treatment_orientation'] = 0

        # modality
        try:
            xpath = mark+"/div[@class='spec-list'][3]/div[@class='row'][2]/div/ul/li/text()"
            item['modality'] = ';'.join(Selector(text=item['html_body']).xpath(xpath).extract())
        except ValueError:
            item['modality'] = 0

        # return item to be written to csv file
        return item

    #eom
#eoc

class cleaner(object):
    """
    second pipeline class that cleans item fields.

    By clean, I mean replace empty fields with zeros and strip all strings of
    trailing whitespace.
    """
    def process_item(self, item, spider):
        """
        clean fields of an item.
        """
        for k in item.fields.iterkeys():
            # if not decalred
            if k not in item:
                item[k] = '0'
            # if var is NoneType
            elif not item[k]:
                item[k] = '0'
            if isinstance(item[k], type(str)):
                # is empty string
                if not item[k]:
                    item[k] = '0'
                    try:
                        item[k] = item[k].strip()
                    except:
                        pass
            else:
                try:
                    item[k] = str(item[k]).strip()
                except:
                    pass
        return item

class writer(object):
    """
    Write all the fields of a therapist item to a line of a csv file
    """
    # name of directory to store pictures in
    pdir_name = "db_pictures"
    header = ["profile picture", "name", "specialty title", "specialties", "website link", "phone number", "comapny name", "location 1", "location 2", "about", "years in practice", "school graduated", "year graduated", "license number and state", "cost per session", "sliding scale", "accepts insurance", "accepted payment methods", "insurance plans", "additional credentials", "groups", "specialties", "issues", "mental health", "ethnicity", "religion", "age", "categories", "treatment orientation", "modality"]

    def open_spider(self, spider):
        """
        open csv file and create csv writer object when spider starts.
        write header to csv if csv is empty
        """
        # checks for header and writes one if not exists
        # tmp_file = open("db.csv", "r")
        # if not tmp_file.read():
            # tmp_file.close()
            # tmp_file = open("db.csv", "w")
            # tmp_csvf = csv.writer(tmp_file)
            # tmp_file.close()
        # make file if not exists
        self.file = open("db.csv", "wa")
        self.csvf = csv.writer(self.file)
        self.csvf.writerow(self.header)
    #eom

    def close_spider(self, spider):
        "close csv file when spider stops."
        self.file.close()
    #eom

    def process_item(self, item, spider):
        """
        write all the item fields to a csv row.

        Starts by putting all fields into a list, then it writes the row.
        if item field is a binary picture, writes picture to file, then appends
        the name of the file
        """
        row = []
        # create directory for pictures if not already created
        try:
            mkdir(self.pdir_name)
        except:
            pass
        # profile picture
        if item['profile_picture_binary'] != 0:
            profpic_name = item['name'] + "_profile_picture.jpeg"
            with open(self.pdir_name+"/"+profpic_name, "wb") as of:
                of.write(item['profile_picture_binary'])
            row.append(profpic_name)
        else:
            row.append(0)

        # name
        row.append(item['name'])
        # specialty title
        row.append(item['specialty_title'])
        # specialties
        with open("test.txt", "w") as of: of.write(str(item['specialty_title2']))
        row.append(item['specialty_title2'])
        # website link
        row.append(item['website_link'])
        # phone number
        row.append(item['phone'])
        # company name
        row.append(item['company_name'])
        # location 1
        row.append(item['location'])
        # location 2
        row.append(item['additional_location'])
        # about
        row.append(item['about'])

        # center pictures
        # center_pictures_binary_list = item['center_pictures_binary_list']
        # if center_pictures_binary_list != 0:
            # num = 1
            # picnames = []
            # for pic in item['center_pictures_binary_list'].split(';'):
                # picname = item['name'] + "gallery_pic" + str(num) + ".jpeg"
                # with open(self.pdir_name + "/" + picname, "wb") as of:
                        # of.write(pic)
                # picnames.append(picname)
            # picnames = ';'.join(picnames)
            # row.append(picnames)
        # else:
            # row.append(0)

        # years in practice
        row.append(item['years_in_practice'])
        # school graduated
        row.append(item['school'])
        # year graduated
        row.append(item['year_graduated'])
        # license number and state
        row.append(item['license_num_and_state'])
        # cost per session
        row.append(item['cost_per_session'])
        # sliding scale
        row.append(item['sliding_scale'])
        # accepts insurance
        row.append(item['accepts_insurance'])
        # accepted payment methods
        row.append(item['accepted_payment_methods'])
        # insurance plans
        row.append(item['accepted_insurance_plans'])
        # additional credentials
        row.append(item['additional_credentials'])
        # groups
        row.append(item['groups'])
        # specialties
        row.append(item['specialties'])
        # issues
        row.append(item['issues'])
        # mental health
        row.append(item['mental_health'])
        # ethnicity
        row.append(item['ethnicity'])
        # religion
        row.append(item['religion'])
        # age
        row.append(item['age'])
        # categories
        row.append(item['categories'])
        # treatment orientation
        row.append(item['treatment_orientation'])
        # modality
        row.append(item['modality'])
        ### ROW LIST DONE

        # write row to csv
        self.csvf.writerow(row)
    #eom
#eoc
#eof


#!/usr/bin/python
# -*- coding: UTF-8 -*-
###############################################################################
# Module:   WeatherMiniChallenge.py    Autor: Felipe Almeida                  #
# Start:    29-Jul-2019                Last Update: 29-Jul-2019  Version: 1.0 #
###############################################################################

import sys
import requests
import json
from datetime import datetime, timezone
import calendar

OWM_ApiKey = '11111111111111111111111111111111'
OWM_EndPoint = 'api.openweathermap.org'
OWM_Version = '2.5'
OWM_GlobalUrl = 'https://'+OWM_EndPoint+'/data/'+OWM_Version+'/'

def GetWeatherForecast(v_CityId=-1):
    global OWM_ApiKey, OWM_GlobalUrl

    StrReturn = ''
    PrevWeekDay = ''

    if v_CityId == -1:
        return -1
    else:
        Url = OWM_GlobalUrl + 'forecast'
        QueryString = {
            'APPID': OWM_ApiKey,
            'id': v_CityId,
            'cnt': 40  # 5 days, 3 Hours Interval
        }
        Headers = {
            'User-Agent': "Mozilla/5.0",
            'Accept': "*/*",
            'Cache-Control': "no-cache",
            'Host': "openweathermap.org",
            'accept-encoding': "gzip, deflate",
            'Connection': "keep-alive",
            'cache-control': "no-cache"
        }
        Response = requests.request("GET", Url, headers=Headers,
                                    params=QueryString)
        jResponse = json.loads(Response.text)
        # print(json.dumps(jResponse, indent=4, sort_keys=True))
        CityName = jResponse['city']['name']+', '+jResponse['city']['country']
        for jDay in jResponse['list']:
            UnixTSDate = jDay['dt']
            Humidity = jDay['main']['humidity']
            dtDataDate = datetime.fromtimestamp(UnixTSDate, timezone.utc)
            StrWeekDay = calendar.day_name[dtDataDate.weekday()]
            StrDataHour = dtDataDate.strftime("%H")

            if (Humidity > 70):
                if (StrWeekDay == PrevWeekDay):
                    StrReturn = StrReturn+StrDataHour+','
                else:
                    StrReturn = (StrReturn[:-1] + '), ' + StrWeekDay +
                                 ' @('+StrDataHour+',')
                PrevWeekDay = StrWeekDay

    StrReturn = StrReturn[2:-1].strip() + ')'
    print('You should take an umbrella in "' + CityName +
          '" in these days/hours: ' + StrReturn)
    return 0

def GetCityId(v_CityName='São Paulo'):
    global OWM_ApiKey, OWM_GlobalUrl

    CityCode = 0
    Url = OWM_GlobalUrl + 'weather'
    QueryString = {
        'APPID': OWM_ApiKey,
        'q': v_CityName
    }
    Headers = {
        'User-Agent': "Mozilla/5.0",
        'Accept': "*/*",
        'Cache-Control': "no-cache",
        'Host': "openweathermap.org",
        'accept-encoding': "gzip, deflate",
        'Connection': "keep-alive",
        'cache-control': "no-cache"
    }
    Response = requests.request("GET", Url, headers=Headers,
                                params=QueryString)
    jResponse = json.loads(Response.text)
    try:
        if v_CityName == jResponse['name']:
            CityCode = jResponse['id']
    except:
        CityCode = -1

    return CityCode

def MainProcess():
    CityName = 'Ribeirão Preto'

    if (GetWeatherForecast(GetCityId(CityName)) == -1):
        print('Invalid City Name')
        sys.exit(1)

def main():
    try:
        MainProcess()
        sys.exit(0)
    except KeyboardInterrupt:
        print("Weather Mini Challenge Interrupted!")
        sys.exit(1)

if __name__ == "__main__":
    main()


"""Flask app for Cupcakes"""
from flask import Flask, request, redirect, render_template, flash, jsonify
from flask_debugtoolbar import DebugToolbarExtension
from models import db, connect_db, Cupcake

app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'postgresql:///cupcakes'
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
app.config['SQLALCHEMY_ECHO'] = True
app.config['SECRET_KEY'] = "SECRET!"
debug = DebugToolbarExtension(app)

connect_db(app)

@app.route("/")
def show_home_page():
    return render_template("index.html")

def serialize_cupcake(cupcake):
    """Serialize a Cupcake SQLAlchemy obj to dictionary."""

    return {
        "id": cupcake.id,
        "flavor": cupcake.flavor,
        "size": cupcake.size,
        "rating": cupcake.rating,
        "image": cupcake.image
    }

@app.route('/api/cupcakes')
def get_all_cupcakes():
    """Returns JSON data for all cupcakes"""
    cupcakes = Cupcake.query.all()
    serialized = [serialize_cupcake(c) for c in cupcakes]
    return jsonify(cupcakes=serialized)

@app.route('/api/cupcakes/<int:cupcake_id>')
def get_single_cupcake(cupcake_id):
    """Returns JSON data for single cupcake"""
    cupcake = Cupcake.query.get_or_404(cupcake_id)
    serialized = serialize_cupcake(cupcake)
    return jsonify(cupcake=serialized)

@app.route('/api/cupcakes', methods=['POST'])
def create_cupcake():
    """Creates cupcake object from data in request.
    Responds with JSON data of new cupcake."""
    flavor = request.json["flavor"]
    size = request.json["size"]
    rating = float(request.json["rating"])
    if request.json["image"]:
        image = request.json["image"]
    else:
        image = None
    new_cupcake = Cupcake(flavor=flavor, size=size, rating=rating, image=image)
    db.session.add(new_cupcake)
    db.session.commit()
    serialized = serialize_cupcake(new_cupcake)
    return ( jsonify(cupcake=serialized), 201 )

@app.route('/api/cupcakes/<int:cupcake_id>', methods=['PATCH'])
def update_cupcake(cupcake_id):
    cupcake = Cupcake.query.get_or_404(cupcake_id)
    cupcake.flavor = request.json.get("flavor") if request.json.get("flavor") else cupcake.flavor
    cupcake.size = request.json.get("size") if request.json.get("size") else cupcake.size
    cupcake.rating = float(request.json.get("rating")) if request.json.get("rating") else cupcake.rating
    cupcake.image = request.json.get("image") if request.json.get("image") else cupcake.image
    db.session.add(cupcake)
    db.session.commit()
    serialized = serialize_cupcake(cupcake)
    return jsonify(cupcake=serialized)

@app.route('/api/cupcakes/<int:cupcake_id>', methods=['DELETE'])
def delete_cupcake(cupcake_id):
    """Delete a single cupcake"""
    cupcake = Cupcake.query.get_or_404(cupcake_id)
    Cupcake.query.filter_by(id=cupcake.id).delete()
    db.session.commit()
    return jsonify({"message": "Cupcake deleted."})


from tkinter import *
from tkinter.ttk import *
from tkinter import messagebox
from align_dataset import normal_align
from create_svm import create_svm

import tkinter
import cv2
import PIL.Image, PIL.ImageTk

from torchvision import datasets, transforms

import numpy as np
import os
import sys
import torch
import pickle
import cv2
import time
import argparse
import math
from Model.MobileFacenet import MobileFaceNet

sys.path.insert(1, "MTCNN")

from mtcnn import MTCNN
from detect_face import extract_face
from mtcnn import MTCNN, detect_face

from threading import Thread

window = Tk()
window.title('Face Recognition Terminal')

photo = None
switch = 0

FONT = cv2.FONT_HERSHEY_SIMPLEX

MAX_IMGS = 100
NROF_IMGS = 0

SVM_MODEL =  "../PretrainedModel/classifier.pkl"

device = 'cuda:0' if torch.cuda.is_available() else 'cpu'
detector = MTCNN(image_size=112, device=device)

model = MobileFaceNet(512).to(torch.device("cuda:0"))
model.load_state_dict(torch.load('../PretrainedModel/model.pth'))

with open(SVM_MODEL, 'rb') as infile:
	(class_name, svm_model) = pickle.load(infile)

transform = transforms.Compose([transforms.ToTensor(), transforms.Normalize([0.5, 0.5, 0.5], [0.5, 0.5, 0.5])])

cap = cv2.VideoCapture(0)

def handleswitch():

	global switch
	switch = 1 - switch

def handleaddid():

	global switch
	global class_name, svm_model

	switch = 0
	SVM_MODEL =  "../PretrainedModel/classifier.pkl"

	if text_input.get() != '':

		add(text_input.get())
		normal_align()
		create_svm()

		with open(SVM_MODEL, 'rb') as infile:
			(class_name, svm_model) = pickle.load(infile)
		print('load done')

		return

	else:
		label.configure(text='Must Add Name of Identify')
		return

def save_face(img, save_dir):
	global NROF_IMGS, detector
	boxes, proba, points = detector.detect(img, select_largest=True, landmarks=True, proba=True)
	if boxes is not None:
		for box, point in zip(boxes, points):
			if NROF_IMGS < MAX_IMGS and proba > 0.99:
				NROF_IMGS+=1
				face = detect_face.extract_face(img, box, image_size=160)
				os.makedirs(os.path.dirname(save_dir + "%d.jpg"%(NROF_IMGS)) + "/", exist_ok=True)
				cv2.imwrite(save_dir + "%d.jpg"%(NROF_IMGS), face)
				print("Image Saved: "+ os.path.expanduser(save_dir + "%d.jpg"%(NROF_IMGS)))
	return

def add(name):
	nrof_lines = 256
	SAVE_DIR = "../Dataset/Processed/" + name + '/'

	if not cap.isOpened:
		raise RuntimeError("Cannot Open Video")

	add = 0

	start = time.time()
	while(add == 0):
		ret, frame = cap.read()
		img = cv2.flip(frame, 1)
		if frame is None:
			break
		(h, w) = img.shape[:2]
		radius = min(h, w)/2 - 50
		cv2.circle(img, (w//2, h//2), min(h,w), (0, 0, 0), min(h,w)+150)
		for i in range(np.int32(nrof_lines)):
			angle = 2 * math.pi * i / nrof_lines
			x, y = w//2 + math.sin(angle)*radius, h//2 + math.cos(angle)*radius
			pt1 = x, y
			pt2 = x + math.sin(angle)*10, y + math.cos(angle)*10
			cv2.line(img, tuple(np.int32(pt1)), tuple(np.int32(pt2)), (255, 255, 255), 1)

		percent = NROF_IMGS/MAX_IMGS
		nrof_big_lines = nrof_lines * percent
		n = 0
		while n < nrof_big_lines :
			angle = 2 * math.pi * n / nrof_lines
			x, y = w//2 + math.sin(angle)*radius, h//2 + math.cos(angle)*radius
			pt1 = x, y
			pt2 = x + math.sin(angle)*15, y + math.cos(angle)*15
			cv2.line(img, tuple(np.int32(pt1)), tuple(np.int32(pt2)), (255, 255, 255), 2)
			n += 1

		thread = Thread(target=save_face, args=(img, SAVE_DIR,))
		thread.start()
		start = time.time()

		boxes = detector.detect(img, select_largest=True, landmarks=False, proba=False)
		if boxes is not None:
			for box in boxes:
				cv2.putText(img, "Turn Head Around!", (10, 50), FONT, 1, (255, 255, 255), 2, cv2.LINE_AA)
				cv2.rectangle(img, tuple((np.int32(box[0]), np.int32(box[1]))), tuple((np.int32(box[2]), np.int32(box[3]))), (255, 255, 255), 1)
		if NROF_IMGS == MAX_IMGS:
			done_logo = cv2.imread("../Dataset/done.jpg")
			done_logo = cv2.resize(done_logo, (200, 200))
			done_logo = cv2.addWeighted(img[h//2-100:h//2+100,w//2-100:w//2+100,:],0.1,done_logo[0:200,0:200,:],0.9,0)
			img[h//2-100:h//2+100,w//2-100:w//2+100,:] = done_logo
			cv2.putText(img, "Press ""Q"" to Exit.", (h//2-100, w//2-100), FONT, 1, (255, 255, 255), 2, cv2.LINE_AA)
		cv2.imshow('preview', np.uint8(img))
		if cv2.waitKey(1) & 0xFF == ord('q'):
			break

	# cap.release()
	cv2.destroyAllWindows()

def Recognition(img):
	start = time.time()
	boxes, points = detector.detect(img, select_largest=True, proba=False, landmarks=True)

	if boxes is not None:
		for box in boxes:
			cv2.rectangle(img, tuple((np.int32(box[0]), np.int32(box[1]))), tuple((np.int32(box[2]), np.int32(box[3]))), (255, 255, 255), 1)
			face = extract_face(img, box, image_size=160)
			face = cv2.resize(face,(112,112))
			face = transform(face)
			face = face.type(torch.FloatTensor)
			face = torch.unsqueeze(face, 0)
			model.eval()
			with torch.no_grad():
			  face = face.to(torch.device("cuda:0"))
			  emb_buff = model(face).cpu()
			buff = emb_buff.numpy().reshape(1, -1)
			predictions = svm_model.predict_proba(buff).ravel()
			best_class_idxs = np.argmax(predictions)
			confidence = predictions[best_class_idxs]*100
			name = class_name.inverse_transform([best_class_idxs])
			predict_str = '%s: %.3f'%(name, confidence)
			print(predict_str)
			pos = tuple((np.int32(box[0]), np.int32(box[1])))
			cv2.putText(img, predict_str, pos, FONT, 0.5, (255, 255, 255), 1, cv2.LINE_AA)
	fps_str = 'fps: %d'%(1/(time.time() - start))
	cv2.putText(img, fps_str, (10, 50), FONT, 1, (255, 0, 0), 2, cv2.LINE_AA)
	return img

def Processing():

	global canvas, photo, switch

	if switch == 1:

		ret, frame = cap.read()

		frame = Recognition(frame)
		frame = cv2.resize(frame, (800,450))

		frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)

		photo = PIL.ImageTk.PhotoImage(image=PIL.Image.fromarray(frame))

		canvas.create_image(0,0, image = photo, anchor=tkinter.NW)

	elif switch == 0:

		frame = cv2.imread('nope.jpg')
		frame = cv2.resize(frame, (800,450))
		frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
		photo = PIL.ImageTk.PhotoImage(image=PIL.Image.fromarray(frame))

		canvas.create_image(0,0, image = photo, anchor=tkinter.NW)

	window.after(15, Processing)

canvas = Canvas(window, width = 800, height= 450 , bg= "gray")
canvas.pack()

label = tkinter.Label(window, text='Add Id', font=('Times New Roman', 20))
label.pack()

text_input = Entry(window, width=10)
text_input.pack()

function = Button(window,text = "Add one more Identify", command=handleaddid)
function.pack()

start = Button(window,text = "Start/Stop Get Frame", command=handleswitch)
start.pack()

Processing()

window.mainloop()


import logging

from django.conf import settings
from backend.libs.client import Client
from backend.libs.client import ClientError

DOMAIN_NAME = 'mg.taman-siswa.com'
MAILGUN_API_URL = f'https://api.mailgun.net/v3/{DOMAIN_NAME}'

logger = logging.getLogger('MAILGUN')

class MailgunClient(Client):

    def __init__(self, *args, **kwargs):
        return super().__init__(MAILGUN_API_URL, auth=('api', settings.MAILGUN_API_KEY), *args, **kwargs)

    def get_headers(self, original_headers={}):
        return original_headers

class MailgunManager:
    client = MailgunClient()

    def send_message(self,
                      sender_name,
                      sender_address,
                      recipient_address,
                      subject,
                      content,
                      bcc=[],
                      cc=[],
                      attachments=[],
                      track_open=False,
                      extra_variables={},
                      tag=None,):

        files = []
        for filename, file_handle in attachments:
            files.append(("attachment", (filename, file_handle.read())))

        sender = "%s <%s@%s>" % (sender_name, sender_address, DOMAIN_NAME)

        parameters = {
            "sender": sender,
            "from": sender,
            "to": recipient_address,
            "subject": subject,
            "html": content,
            "o:tracking": "yes" if track_open else "no",
            "o:tracking-clicks": "no",
        }

        for key in extra_variables.keys():
            parameters["v:%s" % key] = extra_variables[key]

        if tag:
            parameters["o:tag"] = tag

        try:
            response = self.client.post(
                '/messages', data=parameters, files=files)
            logger.info(
                msg=f'Mailgun Success: {sender} -> {recipient_address}')
            return response
        except ClientError as ce:
            logger.error(
                msg=f'Mailgun Error: {sender} -> {recipient_address} {str(ce)}')
            raise ce
        except Exception as e:
            logger.error(msg=f'Exception Error: {str(ce)}')
            raise e


from bs4 import BeautifulSoup
import urllib2
import time
import json

filehandle = urllib2.urlopen('http://www3.tesouro.gov.br/tesouro_direto/consulta_titulos/consultatitulos.asp')
soup = BeautifulSoup(filehandle.read())

quotes = {}

currentTime = time.strftime("%Y-%m-%d")

for tr in soup.find_all('tr') :
	columns = tr.find_all('td')
	if len(columns) > 2:
		if 'class' in columns[0].attrs and columns[0]['class'][0] == 'listing0' :
			tituloName = columns[0].contents[0]
			tituloPrice = columns[5].contents[0].replace('R$ ', '').replace('.', '').replace(',', '.')
			quotes[tituloName] = tituloPrice

quotesJson = json.dumps(quotes)
print(quotesJson)


"""
Implementazione vecchia alternativa a imdispatcher.py

Più prolissa, quindi da preferire l'altra.
C'e' l'opzione recursive in più, per ora.

    python imdispatcher.py <template-dir> <search-dir> <dst-dir> [--threshold] [--dry_run]
"""

# TODO: log ingombrante da togliere

from cv2 import error
from imfinder import bool_find, imread
from os import sys, walk
from os.path import abspath, basename, exists, isdir, join, split, splitext
import argparse
import datetime
import re
import shutil

WRONG_IMAGE_SIZE = 'wrong_image_size'
NON_MATCHING_PATHS = 'non-matching_path'
INVALID_IMAGES = 'invalid_image'
UNCLASSIFIED_IMAGES = 'unclassified_image'
DISPATCHED_IMAGES = 'dispatched_images'
TEMPLATE_MATCHING_ERROR = 'template_matching_error'

def move(src, dst, dry_run=False):
    assert exists(dst), 'dst="{}" does not exist'.format(dst)
    assert isdir(dst), 'dst="{}" is not a directory'.format(dst)
    src_dir_path, src_file_name = split(src)
    dst_file_name = src_file_name
    while True:
        dst_file_path = join(dst, dst_file_name)
        if exists(dst_file_path):
            name, ext = splitext(dst_file_name)
            dst_file_name = name + '+' + ext
        else:
            break

    if not dry_run:
        shutil.move(src, dst_file_path)

    return dst_file_path

def process_templates(templates_dirpath):
    print('process_templates', templates_dirpath)
    res = []
    for dirpath, dirs, files in walk(templates_dirpath):
        for file_name in files:
            if not file_name.startswith('template'):
                continue

            file_path = join(dirpath, file_name)
            template = imread(file_path, 0)
            assert template is not None, 'Invalid template image: {}'.format(file_path)
            label = dirpath[len(templates_dirpath):]
            res.append((file_path, template, label))
    return res

def log(start_datetime, file_path, result_flag):
    dt = start_datetime.isoformat()[:-7].replace(':', '-')
    file_name = u'image_dispatcher_{}_{}.log'.format(
        dt, result_flag)

    now_str = datetime.datetime.now().isoformat()
    with open(file_name, 'a') as fp:
        fp.write(u'{}: {}\n'.format(now_str, file_path.encode('utf-8')))

def main(templates_dirpath, root, dst,
        matching_threshold=0.9,
        dry_run=False,
        recursive=False,
        file_path_filter='jpg$|png$',
        image_size=None,
        log_file_path='log.txt'):

    res = {
        DISPATCHED_IMAGES: [],
        INVALID_IMAGES: [],
        NON_MATCHING_PATHS: [],
        UNCLASSIFIED_IMAGES: [],
        WRONG_IMAGE_SIZE: [],
    }

    if image_size:
        image_shape = tuple(reversed(image_size))

    root = abspath(root)
    dst = abspath(dst)
    file_path_compiled_regexp = re.compile(file_path_filter, re.IGNORECASE)
    n_dirs = n_files = n_images = n_filtered_images = n_dispatched = 0

    templates_data = process_templates(templates_dirpath)
    print('{} templates found'.format(len(templates_data)))

    start_datetime = datetime.datetime.now()

    try:
        for dirpath, dirs, files in walk(root, recursive=recursive):
            if dirpath == dst or dirpath == templates_dirpath:
                # we are entering in the dst dir! skip please
                print('SKIPPING dst or templates directory', dirpath)
                continue
            if basename(dirpath).startswith('dispatched'):
                print('skip', dispatched)
                continue

            print('{:,} dirs/{:,} files: {}'.format(n_dirs, n_files, dirpath))
            n_dirs += 1
            for file_name in files:
                n_files += 1
                file_path = join(dirpath, file_name)
                if not re.search(file_path_compiled_regexp, file_path):
                    #res[NON_MATCHING_PATHS].append(file_path)
                    log(start_datetime, file_path, NON_MATCHING_PATHS)
                    continue

                # Read image to classify
                image = imread(file_path, 0)

                if image is None:
                    #res[INVALID_IMAGES].append(file_path)
                    log(start_datetime, file_path, INVALID_IMAGES)
                    #print('   invalid image')
                    continue

                n_images += 1
                if image_size and image.shape != image_shape:
                    log(start_datetime, file_path, WRONG_IMAGE_SIZE)
                    continue

                n_filtered_images += 1

                dispatched = False
                for template_path, template, dest in templates_data:
                    # Template matching
                    # print('{} vs {} ({})'.format(template_path, file_path, matching_threshold))
                    try:
                        r = bool_find(template, image, threshold=matching_threshold)
                    except error as err:
                        log(start_datetime, file_path, TEMPLATE_MATCHING_ERROR)
                        print('Template matching error')
                        print(err)
                        r = False

                    if r:
                        dt_str = datetime.datetime.now().isoformat()[:-7]
                        dst_dir = join(dst, dest)

                        dst_file_path = move(file_path, dst_dir, dry_run=dry_run)

                        dispatched = True
                        n_dispatched += dispatched

                        res[DISPATCHED_IMAGES].append((file_path, dst_file_path, basename(template_path)))
                        msg = u'{}: "{}" contains "{}": >> {}'.format(
                            dt_str, file_path, basename(template_path), dst_dir)
                        print(msg)
                        print('{:,} moved/{:,} candidates/{:,} images/{:,} files'.format(
                            n_dispatched, n_filtered_images, n_images, n_files))

                        with open(log_file_path, 'a') as fp:
                            fp.write(u'{}\n'.format(msg))

                        log(start_datetime, file_path, DISPATCHED_IMAGES)

                        break

                if not dispatched:
                    log(start_datetime, file_path, UNCLASSIFIED_IMAGES)
                    sys.stdout.write('.')
                    sys.stdout.flush()
                    #res[UNCLASSIFIED_IMAGES].append(file_path)

    except KeyboardInterrupt:
        print('Interrupted by user.')

    return res

if __name__ == '__main__':
    parser = argparse.ArgumentParser(description='Image dispatcher')
    parser.add_argument('query', help='query image path to search for')
    parser.add_argument('root', default=u'.',
                        help='root directory to search in [default=%(default)s]')
    parser.add_argument('dst', default=u'../dispatched-images',
                        help='directory to move images into [default="%(default)s"]')
    parser.add_argument('--threshold', type=float, default=0.9,
                        help='template matching threshold [default=%(default)s]')
    parser.add_argument('--recursive', action='store_true', default=False,
                        help='search recursively inside `root` [default=%(default)s]')
    parser.add_argument('--dry_run', action='store_true', default=False,
                        help='do not actually move files [default=%(default)s]')
    parser.add_argument('--logfile', default='log.txt',
                        help='log file path [default=%(default)s]')
    parser.add_argument('--log', dest='loglevel', default='DEBUG',
                        help='Set the logging level [default=%(default)s]')
    args = parser.parse_args()
    print(args)
    results = main(args.query, args.root, args.dst, matching_threshold=args.threshold,
        recursive=args.recursive, dry_run=args.dry_run, log_file_path=args.logfile)

    print('{:,} images dispatched'.format(len(results[DISPATCHED_IMAGES])))

    sys.exit(0)


import logging
from functools import lru_cache
from typing import Iterator, List, Optional, Tuple

from numpy.random import Generator

from .models import DefaultTemplate, DocumentPlanNode, Message, Template
from .pipeline import NLGPipelineComponent
from .registry import Registry

log = logging.getLogger("root")

# If we're starting a new paragraph and haven't mentioned the location for more than this number of
# messages, say it again (if possible), even if it's not changed
LOC_IF_NOT_SINCE = 6

class TemplateSelector(NLGPipelineComponent):
    """
    Adds a matching Template to each Message in the DocumentPlan.

    """

    def run(
        self,
        registry: Registry,
        random: Generator,
        language: str,
        document_plan: DocumentPlanNode,
        all_messages: List[Message],
    ) -> Tuple[DocumentPlanNode]:
        """
        Run this pipeline component.
        """
        if log.isEnabledFor(logging.DEBUG):
            document_plan.print_tree()

        templates = registry.get("templates")[language.lower()]

        template_checker = TemplateMessageChecker(templates, all_messages)
        log.info("Selecting templates from {} templates".format(len(templates)))
        self._recurse(random, language, document_plan, all_messages, template_checker, None)

        return (document_plan,)

    def _recurse(
        self,
        random: Generator,
        language: str,
        this: DocumentPlanNode,
        all_messages: List[Message],
        template_checker: "TemplateMessageChecker",
        context: Optional[Message],
    ) -> Optional[Message]:
        """
        Recursively works through the tree, adding Templates to Messages.
        """
        # Check all children of this root
        for child in this.children:
            if isinstance(child, Message):
                templates = list(template_checker.all_templates_for_message(child))
                if len(templates) == 0:
                    # If there are no templates, something's gone horribly wrong
                    # The document planner should have made sure this didn't happen, but the only thing we can
                    # at this point is skip the fact
                    log.error("Found no templates to express {}".format(child))
                    raise Exception("No template for message {}".format(child))
                else:
                    random.shuffle(templates)
                    template = templates[0]
                    self._add_template_to_message(child, template, all_messages)
                    context = child
            else:
                # This child is NOT a message and we should just recurse
                context = self._recurse(random, language, child, all_messages, template_checker, context)
        return context

    @staticmethod
    def _add_template_to_message(message: Message, template_original: Template, all_messages: List[Message]) -> None:
        """
        Adds a matching template to a message, also adding the facts used by the template to the message.

        :param message: The message to be fitted with a template
        :param template_original: The template to be added to the message.
        :param all_messages: Other available messages, some of which will be needed to match possible secondary rules
               in the template.
        :return: Nothing
        """
        template = template_original.copy()
        used_facts = template.fill(message, all_messages)
        if used_facts:
            log.debug("Successfully linked template to message")
        else:
            log.error(
                "Chosen template '{}' for fact '{}' could not be used! "
                "Falling back to default templates".format(template.display_template(), message.main_fact)
            )
            template = DefaultTemplate("")
        message.template = template
        message.facts = used_facts

class TemplateMessageChecker(object):
    """
    Doesn't actually fill in templates, but just checks, for a given message (and a list of other available messages),
    whether there is a template that can be used to realise it.

    Init with templates taken from the registry for the relevant language.

    The checks are cached on message and location_required.

    """

    def __init__(self, templates: List[Template], all_messages: List[Message]) -> None:
        self.all_messages = all_messages
        self.templates = templates
        self._cache = {}

    @lru_cache(maxsize=1024)
    def exists_template_for_message(self, message: Message) -> bool:
        """
        Check for templates that apply to the given message. To make things faster, we don't try to find
        all available templates, but return as soon as we find one.
        """
        try:
            # Try getting the first template
            next(self.all_templates_for_message(message))
        except StopIteration:
            # No template found at all
            return False
        return True

    def all_templates_for_message(self, message: Message) -> Iterator[Template]:
        for template in self.templates:
            # See if the template can express this message (with the help of the other available messages)
            if template.check(message, self.all_messages):
                # Got a matching template: this message can be expressed
                yield template


import pandas as pd
import numpy as np
import datetime as dt

def calculate_ema(values: [dt.date, np.float]):
	"""This returns the value of EMA indicator for the last date provided.
	    Returns the date for which the indicator was calculated and it's value"""
	n = len(values)
	if n == 0:
		raise Exception("Cannot make average of dis.")
	alpha = 2 / (n + 1)
	factor = 1
	numerator = 0
	denominator = 0
	for i in reversed(range(0, n)):
		numerator += values[i][1] * factor
		denominator += factor
		factor *= (1 - alpha)
	return values[-1][0], numerator / denominator

def calculate_ema_range(date_values: [(dt.date, np.float)], samples_per_result: int):
	"""Returns a list of tuples of EMA indicator value and date."""
	max_index = len(date_values) - samples_per_result
	results = []
	for d in range(max_index):
		date_value = calculate_ema(date_values[0 + d: d + samples_per_result])
		results.append(date_value)
	return results


from datetime import date, timedelta

class ParameterService:

    def __init__(self,default_date_gap=10):
        self.default_date_gap = default_date_gap
        self.default_ticker = ""

    def init_params(self,date_gap=None):
        if date_gap is None:
                date_gap = self.default_date_gap
        return self.default_ticker , date.today() - timedelta(days=date_gap), date.today()

    def process_params(self,request):
        from_date = request.form.get('from_date')
        till_date = request.form.get('till_date')
        tickers = request.form.get('tickers')
        if tickers is not None:
            for separator in ":,+":
                if tickers.find(separator) >= 0:
                    tickers = tickers.split(separator)
                    break
            else:
                tickers = [tickers]

        return tickers, from_date, till_date

    def get_param(self,request,param):
        return request.form.get(param)

    def init_option_controller_params(self):
        underlyingPrice = 14.00
        days = 21
        volatility = 76.5
        interest = 1.5
        dividend = 0.0
        return underlyingPrice,days,volatility,interest,dividend

    def process_options_params(self,request):
        underlyingPrice = float(request.form.get('underlyingPrice'))
        days = int(request.form.get('days'))
        volatility = float(request.form.get('volatility'))
        interest = float(request.form.get('interest'))
        dividend = float(request.form.get('dividend'))
        return underlyingPrice,  days, volatility, interest, dividend

    def process_options_params_ajax(self,request):
        print(request.args)
        optionSide = request.args.get('optionSide')
        underlyingPrice = float(request.args.get('underlyingPrice'))
        exercisePrice = float(request.args.get('exercisePrice'))
        days = int(request.args.get('days'))
        targetPrice = float(request.args.get('targetPrice'))
        interest = float(request.args.get('interest'))
        dividend = float(request.args.get('dividend'))
        return optionSide,underlyingPrice, exercisePrice, days, targetPrice, interest, dividend


import argparse

from calculadora.calculator import Calculator

if __name__ == '__main__':
    parse = argparse.ArgumentParser()
    parse.add_argument('operation', type=str, help='mathematical operation you want to do')
    parse.add_argument('num1', type=int, help='first parameter to use in your operation')
    parse.add_argument('num2', type=int, help='second parameter to use in your operation')

    arguments = parse.parse_args()

    calculator = Calculator()

    expression = calculator.get_expression(arguments.num1, arguments.num2, arguments.operation)
    result = calculator.calculate(arguments.num1, arguments.num2, arguments.operation)

    print(f'The result  is: {expression} = {str(result)}')


import io, os
import re
from google.cloud import vision
from google.cloud import storage
from google.protobuf import json_format

def async_detect_document(gcs_source_uri, gcs_destination_uri):

    os.environ['GOOGLE_APPLICATION_CREDENTIALS'] = r'ServiceAccountToken.json'
    client = vision.ImageAnnotatorClient()

    batch_size = 100
    mime_type = 'application/pdf'
    feature = vision.types.Feature(
        type=vision.enums.Feature.Type.DOCUMENT_TEXT_DETECTION)

    # gcs_source_uri = 'gs://filename'
    gcs_source = vision.types.GcsSource(uri=gcs_source_uri)
    input_config = vision.types.InputConfig(gcs_source=gcs_source, mime_type=mime_type)

    # gcs_destination_uri = 'gs://fileDestination '
    gcs_destination = vision.types.GcsDestination(uri=gcs_destination_uri)
    output_config = vision.types.OutputConfig(gcs_destination=gcs_destination, batch_size=batch_size)

    async_request = vision.types.AsyncAnnotateFileRequest(
        features=[feature], input_config=input_config, output_config=output_config)

    operation = client.async_batch_annotate_files(requests=[async_request])
    operation.result(timeout=5000)

    storage_client = storage.Client()
    match = re.match(r'gs://([^/]+)/(.+)', gcs_destination_uri)
    bucket_name = match.group(1)
    prefix = match.group(2)
    bucket = storage_client.get_bucket(bucket_name)

    blob_list = list(bucket.list_blobs(prefix=prefix))
    print('Output files:')
    for blob in blob_list:
        print(blob.name)

    output = blob_list[0]
    json_string = output.download_as_string()
    response = json_format.Parse(
        json_string, vision.types.AnnotateFileResponse())

    response = json_format.Parse(
            json_string, vision.types.AnnotateFileResponse())
    first_page_response = response.responses[0]
    annotation = first_page_response.full_text_annotation

    print(u'Full text:')
    print(annotation.text)

files = 'file_name'
source_bucket = 'gs://cambridgefive_pdfs/'
destinations_bucket = 'gs://cambridgefive_pdfs/'
i = 0
while i < len(files):
#     async_detect_document(files[i], destinations[i])
    async_detect_document(source_bucket + files[i], source_bucket + files[i] + '.txt ')
    print(files[i])
    i+= 1


from model import common
import functools
import torch
import torch.nn as nn
import torch.nn.functional as F
from torch.nn.parameter import Parameter
import model.arch_util as mutil
def make_model(args, parent=False):
    return MARDN(args)

# branch fusion with channel-wise sub-network attention
class FusionLayer(nn.Module):
    def __init__(self, channel, reduction=16):
        super(FusionLayer, self).__init__()
        self.avg_pool1 = nn.AdaptiveAvgPool2d(1)
        self.avg_pool2 = nn.AdaptiveAvgPool2d(1)
        self.avg_pool3 = nn.AdaptiveAvgPool2d(1)
        # Squeeze and excitation
        self.se_x1 = nn.Sequential(
            nn.Conv2d(channel, channel // reduction, 1, padding=0, bias=True),
            nn.ReLU(inplace=True),
            nn.Conv2d(channel // reduction, channel, 1, padding=0, bias=True),
        )
        self.se_x2 = nn.Sequential(
            nn.Conv2d(channel, channel // reduction, 1, padding=0, bias=True),
            nn.ReLU(inplace=True),
            nn.Conv2d(channel // reduction, channel, 1, padding=0, bias=True),
        )
        self.se_x3 = nn.Sequential(
            nn.Conv2d(channel, channel // reduction, 1, padding=0, bias=True),
            nn.ReLU(inplace=True),
            nn.Conv2d(channel // reduction, channel, 1, padding=0, bias=True),
        )
        self.softmax = nn.Softmax(dim=2)
    def forward(self, x):
        if len(x) == 2:
            avg_x1 = self.se_x1(self.avg_pool1(x[0]))
            avg_x2 = self.se_x2(self.avg_pool2(x[1]))
            attens = self.softmax(torch.cat([avg_x1, avg_x2], 2))
            y = attens[:, :, 0, :].unsqueeze(2) * x[0] + attens[:, :, 1, :].unsqueeze(2) * x[1]
        elif len(x) == 3:
            avg_x1 = self.se_x1(self.avg_pool1(x[0]))
            avg_x2 = self.se_x2(self.avg_pool2(x[1]))
            avg_x3 = self.se_x3(self.avg_pool3(x[2]))
            attens = self.softmax(torch.cat([avg_x1, avg_x2, avg_x3], 2))

            y = attens[:, :, 0, :].unsqueeze(2) * x[0] + attens[:, :, 1, :].unsqueeze(2) * x[1] + \
                attens[:, :, 2, :].unsqueeze(2) * x[2]
        return y

class SpatialAttention(nn.Module):
    def __init__(self, n_feas):
        super(SpatialAttention, self).__init__()
        self.con1x1_pool = nn.Conv2d(n_feas, 1, 1)
        self.con1 = nn.Conv2d(3, 1, 1, bias=False)
        self.sig = nn.Sigmoid()

    def forward(self, x):
        avg_out = torch.mean(x, dim=1, keepdim=True)
        max_out, _ = torch.max(x, dim=1, keepdim=True)
        con_out = self.con1x1_pool(x)
        y = self.sig(self.con1(torch.cat([avg_out, max_out, con_out], 1)))

        return x*y

class ARDU(nn.Module):
    def __init__(self, nf=64, gc=32, bias=True):
        super(ARDU, self).__init__()
        # gc: growth channel, i.e. intermediate channels
        self.conv1 = nn.Conv2d(nf, gc, 3, 1, 1, bias=bias)
        self.conv2 = nn.Conv2d(nf + gc, gc, 3, 1, 1, bias=bias)
        self.conv3 = nn.Conv2d(nf + 2 * gc, gc, 3, 1, 1, bias=bias)
        self.conv4 = nn.Conv2d(nf + 3 * gc, gc, 3, 1, 1, bias=bias)
        self.conv5 = nn.Conv2d(nf + 4 * gc, nf, 3, 1, 1, bias=bias)
        self.lrelu = nn.LeakyReLU(negative_slope=0.2, inplace=True)
        self.spatialAttention = SpatialAttention(nf)
        # initialization
        mutil.initialize_weights([self.conv1, self.conv2, self.conv3, self.conv4, self.conv5], 0.1)

    def forward(self, x):
        x1 = self.lrelu(self.conv1(x))
        x2 = self.lrelu(self.conv2(torch.cat((x, x1), 1)))
        x3 = self.lrelu(self.conv3(torch.cat((x, x1, x2), 1)))
        x4 = self.lrelu(self.conv4(torch.cat((x, x1, x2, x3), 1)))
        x5 = self.conv5(torch.cat((x, x1, x2, x3, x4), 1))
        x5 = self.spatialAttention(x5)
        return x5 + x

class ARDB(nn.Module):
    '''Space-attended Residual Dense Block'''

    def __init__(self, nf, gc=32):
        super(ARDB, self).__init__()
        self.RDB1 = ARDU(nf, gc)
        self.RDB2 = ARDU(nf, gc)
        self.RDB3 = ARDU(nf, gc)
    def forward(self, x):
        out = self.RDB1(x)
        out = self.RDB2(out)
        out = self.RDB3(out)
        return out * 0.2 + x

class MARDN(nn.Module):
    def __init__(self, args, conv=common.default_conv):
        super(MARDN, self).__init__()
        n_rrdb_b1 = args.n_level1
        n_rrdb_b2 = args.n_level2
        n_rrdb_b3 = args.n_level3
        n_feats = args.n_feats
        kernel_size = 3
        self.scale = args.scale[0]

        # RGB mean for DIV2K
        self.sub_mean = common.MeanShift(args.rgb_range)
        modules_head = [conv(args.n_colors, n_feats, kernel_size)]
        # module body
        # Each branch represents a sub-network
        ARDB_block_f = functools.partial(ARDB, nf=n_feats)
        # branch I
        modules_b1s1 = []
        modules_b1s2 = []
        modules_b1s3 = []
        for _ in range(n_rrdb_b1):
            modules_b1s1.append(ARDB_block_f())
            modules_b1s2.append(ARDB_block_f())
            modules_b1s3.append(ARDB_block_f())
        # branch II
        modules_b2s2 = []
        modules_b2s3 = []
        for _ in range(n_rrdb_b2):
            modules_b2s2.append(ARDB_block_f())
            modules_b2s3.append(ARDB_block_f())
        # branch III
        modules_b3s3 = []
        for _ in range(n_rrdb_b3):
            modules_b3s3.append(ARDB_block_f())

        # fusion layer
        self.furLayerb1s3 = FusionLayer(n_feats)
        self.furLayerb2s2 = FusionLayer(n_feats)
        self.furLayerb2s3 = FusionLayer(n_feats)
        self.furLayerb3s3 = FusionLayer(n_feats)
        self.furLayerFin = FusionLayer(n_feats)
        modules_tail = [
            conv(n_feats, args.n_colors, kernel_size)]
        modules_tail_1dot5 = [
            conv(n_feats, n_feats, kernel_size),
            conv(n_feats, args.n_colors, kernel_size)]
        modules_tailx2 = [
            common.Upsampler(conv, 2, n_feats, act=False),
            conv(n_feats, args.n_colors, kernel_size)]
        modules_tailx4 = [
            common.Upsampler(conv, 4, n_feats, act=False),
            conv(n_feats, args.n_colors, kernel_size)]
        self.conv_1x1 = nn.Sequential(
            nn.Conv2d(n_feats*2, n_feats, 1, padding=0, bias=True),
            nn.ReLU(inplace=True)
        )
        self.conv_1x1_3 = nn.Sequential(
            nn.Conv2d(n_feats * 3, n_feats, 1, padding=0, bias=True),
            nn.ReLU(inplace=True)
        )
        self.head = nn.Sequential(*modules_head)
        self.modules_b1s1 = nn.Sequential(*modules_b1s1)
        self.modules_b1s2 = nn.Sequential(*modules_b1s2)
        self.modules_b1s3 = nn.Sequential(*modules_b1s3)
        self.modules_b2s2 = nn.Sequential(*modules_b2s2)
        self.modules_b2s3 = nn.Sequential(*modules_b2s3)
        self.modules_b3s3 = nn.Sequential(*modules_b3s3)
        self.add_mean = common.MeanShift(args.rgb_range, sign=1)
        # Upsample modules
        self.tail = nn.Sequential(*modules_tail)
        self.tail_1dot5 = nn.Sequential(*modules_tail_1dot5)
        self.tailx2 = nn.Sequential(*modules_tailx2)
        self.tailx4 = nn.Sequential(*modules_tailx4)
    def forward(self, x):
        x = self.sub_mean(x)
        # shallow feature extraction module
        x = self.head(x)
        # deep feature extraction module
        xh, xw = x.size(2), x.size(3)
        x_res = F.interpolate(x, size=(xh * 2, xw * 2), mode='bilinear', align_corners=False)  # bilinear
        # Stage I
        x_b1 = F.interpolate(x, size=(xh//2, xw//2), mode='bilinear', align_corners=False)   # bilinear
        x_b1 = self.modules_b1s1(x_b1)
        # Stage II
        x_b2 = self.furLayerb2s2([F.interpolate(x_b1, size=(xh, xw), mode='bilinear', align_corners=False), x])
        x_b2 = self.modules_b2s2(x_b2)
        x_b1 = self.modules_b1s2(x_b1)
        # Stage III
        x_b3 = self.furLayerb3s3([F.interpolate(x_b1, size=(xh*2, xw*2), mode='bilinear', align_corners=False),\
                                  F.interpolate(x_b2, size=(xh*2, xw*2), mode='bilinear', align_corners=False)])
        x_temp = self.furLayerb1s3([x_b1, F.interpolate(x_b2, size=(xh//2, xw//2), mode='bilinear', align_corners=False)])
        x_b2 = self.furLayerb2s3([F.interpolate(x_b1, size=(xh, xw), mode='bilinear', align_corners=False), x_b2])
        x_b1 = x_temp
        x_b3 = self.modules_b3s3(x_b3)
        x_b2 = self.modules_b2s3(x_b2)
        x_b1 = self.modules_b1s3(x_b1)

        res = self.furLayerFin([F.interpolate(x_b1, size=(2*xh, 2*xw), mode='bilinear', align_corners=False),\
                                F.interpolate(x_b2, size=(2*xh, 2*xw), mode='bilinear', align_corners=False), x_b3])
        res = res + x_res
        # Upscaling and reconstruct module
        if self.scale == 2:
            x = self.tail(res)
        elif self.scale == 3:
            x = F.interpolate(res, size=(3*xh, 3*xw), mode='bilinear', align_corners=False)
            x = self.tail_1dot5(x)
        elif self.scale == 4:
            x = self.tailx2(res)
        elif self.scale == 8:
            x = self.tailx4(res)
        x = self.add_mean(x)

        return x


from django.core.urlresolvers import reverse_lazy

from siscoban.core.mixins.views import GenericListView, GenericCreateView, GenericUpdateView, GenericDeleteView
from ..models.pay_scale import PayScale
from ..forms.pay_scale import PayScaleForm

class PayScaleListView(GenericListView):
    permission_required = 'global_permissions.diretoria'
    model = PayScale
    template_name = 'pages/pay_scale/pay_scale_list.html'
    title = 'Nova Tabela de remuneração'
    subtitle = 'Por estados'
    descriptive_title = 'Nova Tabela de remuneração por estado'

    def get_context_data(self):
        context = super(PayScaleListView, self).get_context_data()
        ufs = PayScale.objects.values('uf').all().distinct()
        context['ufs'] = ufs
        context['tables'] = PayScale.objects.all().order_by('-uf')

        try:
            tables = []
            tables_uf = []
            u = None
            for uf in ufs:
                if u is not None and u != uf['uf']:
                    tables.append(tables_uf)
                    tables_uf = []
                u = uf['uf']
                tables_uf += PayScale.objects.filter(uf=u)
                if u != uf['uf']:
                    tables.append(tables_uf)
                    tables_uf = []
            tables.append(tables_uf)
            context['tables'] = tables
        except:
            pass

        return context

class PayScaleCreateView(GenericCreateView):
    permission_required = 'global_permissions.diretoria'
    model = PayScale
    template_name = 'pages/pay_scale/pay_scale_detail.html'
    form_class = PayScaleForm
    success_url = reverse_lazy('financial:pay_scale_list')
    success_message = "Tabela de remuneração criada com sucesso"
    title = 'Nova Tabela de remuneração'
    subtitle = 'Cadastrar nova tabela de remuneração'
    descriptive_title = 'Cadastro da nova tabela de remuneração'

class PayScaleUpdateView(GenericUpdateView):
    permission_required = 'global_permissions.diretoria'
    model = PayScale
    template_name = 'pages/pay_scale/pay_scale_detail.html'
    form_class = PayScaleForm
    success_url = reverse_lazy('financial:pay_scale_list')
    success_message = "Tabela de remuneração atualizada com sucesso"
    title = 'Nova Tabela de remuneração'
    subtitle = 'Editar nova tabela de remuneração'
    descriptive_title = 'Edição da nova tabela de remuneração'

class PayScaleDeleteView(GenericDeleteView):
    permission_required = 'global_permissions.diretoria'
    model = PayScale
    success_url = reverse_lazy('financial:pay_scale_list')
    title = 'Nova Tabela de remuneração'
    subtitle = 'Remover nova tabela de remuneração'
    descriptive_title = 'Remoção da nova tabela de remuneração'


import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim
from torchvision import datasets, transforms
import numpy as np
from torch.utils.data import TensorDataset, DataLoader
import torchvision.models as models
from torch.autograd.gradcheck import zero_gradients
from PIL import Image
import sys
import os

mean = [0.485, 0.456, 0.406]
std = [0.229, 0.224, 0.225]
img_num = 200

def read_origin_data(input_dir):
    print('start reading data...')
    transform = transforms.Compose(
        [transforms.ToTensor(),
         transforms.Normalize(mean, std)])
    img_list = []
    for i in range(img_num):
        img = Image.open(os.path.join(input_dir, (3 - len(str(i))) * '0' + str(i) + '.png'))
        img = transform(img).numpy()
        img_list.append(img)
        print('img', i, 'done', end = '\r', flush = True)
    img_list = np.array(img_list)
    data = torch.FloatTensor(img_list)
    loader = DataLoader(data, batch_size = 1, shuffle = False, num_workers = 2)
    print('data read...')
    return loader

def fgsm_attack(image, epsilon, data_grad):
    sign_data_grad = data_grad.sign()
    perturbed_image = image + epsilon  *sign_data_grad
    perturbed_image = torch.clamp(perturbed_image, 0, 1)
    return perturbed_image

def plot_attack(x, num, output_dir):
#     x = np.moveaxis(x, 0, -1)
#     print(x.shape)
    x.save(os.path.join(output_dir, (3 - len(str(num))) * '0' + str(num) + '.png'))
#     new_x = np.clip((x + 1) * 255 / 2, 0, 255)
#     result = Image.fromarray(new_x.astype(np.uint8))
#     result.save('/content/ans_'+ (3 - len(str(num))) * '0' + str(num) + '.png')
def inverse_transform(t):
    return t.mul(torch.FloatTensor(std).view(3, 1, 1)).add(torch.FloatTensor(mean).view(3, 1, 1))

if __name__ == "__main__":
    model = models.resnet50(pretrained=True)
    model.eval()
    img_num = 200
    epsilon = 0.08
    data_loader = read_origin_data(sys.argv[1])
    criterion = nn.CrossEntropyLoss()
    x_adv = []
    L_inf_sum = 0.
    cnt = 0
    acc_num = 0
    for data in data_loader:
        data.requires_grad = True
        zero_gradients(data)
        output = model(data)
        init_pred = output.max(1, keepdim=True)[1]
        loss = criterion(output, init_pred[0])
        loss.backward()
        perturbed_data = data + epsilon * data.grad.sign_()
        output = model(perturbed_data)
        final_pred = output.max(1, keepdim=True)[1]
        perturbed_data = inverse_transform(perturbed_data)
        origin_data = inverse_transform(data)
        adv = perturbed_data.squeeze().detach().numpy()
        origin = origin_data.squeeze().detach().numpy()
        L_inf = np.max(adv - origin)
        L_inf = abs(L_inf) * 255
        L_inf_sum += L_inf
        new_img = torch.clamp(perturbed_data.squeeze(), 0, 1)
        img_trans = transforms.Compose([transforms.ToPILImage()])
        new_img = img_trans(new_img)
        plot_attack(new_img, cnt, sys.argv[2])
        print('\nimg num   :', cnt)
        print('previous  :', init_pred.item())
        print('after     :', final_pred.item())
#         print('real label:', labels[cnt].item())
        print('L infinity:', L_inf)
        if init_pred.item() != final_pred.item():
            acc_num += 1
        cnt += 1
        if cnt == 200:
            break
    print('====================================================================')
    print('acc rate  :', acc_num / cnt)
    print('L infinity:', L_inf_sum / cnt)


#!/usr/bin/env python
# -*- coding: utf-8 -*-

from reportlab.pdfgen.canvas import Canvas
from reportlab.lib.units import cm
from reportlab.lib import colors
from reportlab.platypus.tables import Table, TableStyle
from reportlab.platypus.flowables import Image
import cStringIO as StringIO

class PDFBase(object):
	def __init__(self, recipient, invoicenum, imagedir, currency):
		self.pdfdata = StringIO.StringIO()
		self.canvas = Canvas(self.pdfdata)

		self.recipient = recipient
		self.invoicenum = invoicenum
		self.imagedir = imagedir or '.'
		self.currency = currency or '€'

		self.preview = False

		self.canvas.setTitle("PostgreSQL Europe Invoice #%s" % self.invoicenum)
		self.canvas.setSubject("PostgreSQL Europe Invoice #%s" % self.invoicenum)
		self.canvas.setAuthor("PostgreSQL Europe")
		self.canvas._doc.info.producer = "PostgreSQL Europe Invoicing System"

	def trimstring(self, s, maxlen, fontname, fontsize):
		while len(s) > 5:
			if self.canvas.stringWidth(s, fontname, fontsize) <= maxlen:
				return s
			s = s[:len(s)-2]
		return s

	def textlines(self, t, lines):
		for l in lines.splitlines():
			t.textLine(l)

	def _pageheader(self):
		if self.preview:
			t = self.canvas.beginText()
			t.setTextOrigin(6*cm, 4*cm)
			t.setFont("Times-Italic", 70)
			t.setFillColorRGB(0.9,0.9,0.9)
			t.textLine("PREVIEW PREVIEW")
			self.canvas.rotate(45)
			self.canvas.drawText(t)
			self.canvas.rotate(-45)

		im = Image("%s/PostgreSQL_logo.1color_blue.300x300.png" % self.imagedir, width=3*cm, height=3*cm)
		im.drawOn(self.canvas, 2*cm, 25*cm)
		t = self.canvas.beginText()
		t.setFillColorRGB(0,0,0,0)
		t.setFont("Times-Roman", 10)
		t.setTextOrigin(6*cm, 27.5*cm)
		self.textlines(t,"""PostgreSQL Europe
Carpeaux Diem
13, rue du Square Carpeaux
75018 PARIS
France
""")
		self.canvas.drawText(t)

		t = self.canvas.beginText()
		t.setTextOrigin(2*cm, 23*cm)
		t.setFont("Times-Roman", 10)
		t.textLine("")
		self.textlines(t, """
Your contact: Guillaume Lelarge
Function: PostgreSQL Europe Treasurer
E-mail: treasurer@postgresql.eu
""")
		self.canvas.drawText(t)

		t = self.canvas.beginText()
		t.setTextOrigin(11*cm, 23*cm)
		t.setFont("Times-Italic", 11)
		t.textLine("To:")
		t.setFont("Times-Roman", 11)
		self.textlines(t, self.recipient)
		self.canvas.drawText(t)

		p = self.canvas.beginPath()
		p.moveTo(2*cm, 18.9*cm)
		p.lineTo(19*cm, 18.9*cm)
		self.canvas.drawPath(p)

class PDFInvoice(PDFBase):
	def __init__(self, recipient, invoicedate, duedate, invoicenum=None, imagedir=None, currency='€', preview=False, receipt=False, bankinfo=True):
		self.invoicedate = invoicedate
		self.duedate = duedate
		self.preview = preview
		self.receipt = receipt
		self.bankinfo = bankinfo
		self.rows = []

		if self.receipt:
			# Never include bank info on receipts
			self.bankinfo = False

		super(PDFInvoice, self).__init__(recipient, invoicenum, imagedir, currency)

	def addrow(self, title, cost, count=1):
		self.rows.append((title, cost, count,))

	def save(self):
		# We can fit 15 rows on one page. We might want to do something
		# cute to avoid a single row on it's own page in the future, but
		# for now, just split it evenly.
		for pagenum in range(0, (len(self.rows)-1)/15+1):
			self._pageheader()
			islastpage = (pagenum == (len(self.rows)-1)/15)

			if len(self.rows) > 15:
				suffix = " (page %s/%s)" % (pagenum+1, len(self.rows)/15+1)
			else:
				suffix = ''

			# Center between 2 and 19 is 10.5
			if self.invoicenum:
				if self.receipt:
					self.canvas.drawCentredString(10.5*cm,19*cm, "RECEIPT FOR INVOICE NUMBER %s%s" % (self.invoicenum, suffix))
				else:
					self.canvas.drawCentredString(10.5*cm,19*cm, "INVOICE NUMBER %s - %s%s" % (self.invoicenum, self.invoicedate.strftime("%B %d, %Y"),suffix))
			else:
				self.canvas.drawCentredString(10.5*cm,19*cm, "RECEIPT - %s%s" % (self.invoicedate.strftime("%B %d, %Y"), suffix))

			if pagenum == 0:
				tbldata = [["Item", "Price", "Count", "Amount"], ]
			else:
				tbldata = [["Item - continued from page %s" % pagenum, "Price", "count", "amount"], ]

			tbldata.extend([(self.trimstring(title, 10.5*cm, "Times-Roman", 10),
							 "%.2f %s" % (cost, self.currency),
							 count,
							 "%.2f %s" % ((cost * count), self.currency))
							for title,cost, count in self.rows[pagenum*15:(pagenum+1)*15]])
			style = [
					('BACKGROUND',(0,0),(3,0),colors.lightgrey),
					('ALIGN',(1,0),(3,-1),'RIGHT'),
					('LINEBELOW',(0,0),(-1,0), 2, colors.black),
					('OUTLINE', (0,0), (-1, -1), 1, colors.black),
				]
			if islastpage:
				tbldata.append(['','','Total',"%.2f %s" % (sum([cost*count for title,cost,count in self.rows]),self.currency)])
				style.append(('LINEABOVE', (-2,-1), (-1, -1), 2, colors.black))
			else:
				tbldata.append(['          Continued on page %s' % (pagenum + 2), '', '', ''])
				style.append(('ALIGN', (0, -1), (-1, -1), 'CENTER'))
				style.append(('FONT', (0, -1), (-1, -1), 'Times-Italic'))

			t = Table(tbldata, [10.5*cm, 2.5*cm, 1.5*cm, 2.5*cm])
			t.setStyle(TableStyle(style))
			w,h = t.wrapOn(self.canvas,10*cm,10*cm)
			t.drawOn(self.canvas, 2*cm, 18*cm-h)

			if self.receipt:
				self.canvas.drawCentredString(10.5*cm,17.3*cm-h, "This invoice was paid %s" % self.duedate.strftime("%B %d, %Y"))
			else:
				self.canvas.drawCentredString(10.5*cm,17.3*cm-h, "This invoice is due: %s" % self.duedate.strftime("%B %d, %Y"))

			t = self.canvas.beginText()
			t.setTextOrigin(2*cm, 5*cm)
			t.setFont("Times-Italic", 10)
			t.textLine("PostgreSQL Europe is a French non-profit under the French 1901 Law. The association is not VAT registered.")
			t.textLine("")

			if islastpage and self.bankinfo:
				t.setFont("Times-Bold", 10)
				t.textLine("Bank references / Références bancaires / Bankverbindungen / Referencias bancarias")

				t.setFont("Times-Roman", 8)
				self.textlines(t, """CCM PARIS 1-2 LOUVRE MONTORGUEIL
28 RUE ETIENNE MARCEL
75002 PARIS
FRANCE
IBAN: FR76 1027 8060 3100 0205 2290 114
BIC: CMCIFR2A
""")

			self.canvas.drawText(t)

			# Finish this page off, and optionally loop to another one
			self.canvas.showPage()

		# Last page is finished, flush the PDF output
		self.canvas.save()

		return self.pdfdata

class PDFRefund(PDFBase):
	def __init__(self, recipient, invoicedate, refunddate, invoicenum, invoiceamount, refundamount, imagedir, currency):
		self.recipient = recipient
		self.invoicedate = invoicedate
		self.refunddate = refunddate
		self.invoiceamount = invoiceamount
		self.refundamount = refundamount

		super(PDFRefund, self).__init__(recipient, invoicenum, imagedir, currency)

	def save(self):
		self._pageheader()

		self.canvas.drawCentredString(10.5*cm,19*cm, "REFUND NOTE FOR INVOICE NUMBER {0}".format(self.invoicenum))

		tbldata = [
			["Item", "Amount"],
			["Invoice total amount", "{0:.2f} {1}".format(self.invoiceamount, self.currency)],
			["Refunded amount", "-{0:.2f} {1}".format(self.refundamount, self.currency)],
			["", "{0:.2f} {1}".format(self.invoiceamount-self.refundamount, self.currency)],
		]
		style = [
			('BACKGROUND',(0,0),(1,0),colors.lightgrey),
			('ALIGN',(1,0),(1,-1),'RIGHT'),
			('LINEBELOW',(0,0),(-1,0), 2, colors.black),
			('OUTLINE', (0,0), (-1, -1), 1, colors.black),
			('LINEABOVE', (-1,-1), (-1,-1), 2, colors.black),
		]

		t = Table(tbldata, [10.5*cm, 2.5*cm, 1.5*cm, 2.5*cm])
		t.setStyle(TableStyle(style))
		w,h = t.wrapOn(self.canvas,10*cm,10*cm)
		t.drawOn(self.canvas, 2*cm, 18*cm-h)

		self.canvas.drawCentredString(10.5*cm, 17.3*cm-h, "This refund was issued {0}".format(self.refunddate.strftime("%B %d, %Y")))

		t = self.canvas.beginText()
		t.setTextOrigin(2*cm, 5*cm)
		t.setFont("Times-Italic", 10)
		t.textLine("PostgreSQL Europe is a French non-profit under the French 1901 Law. The association is not VAT registered.")
		t.textLine("")

		self.canvas.showPage()
		self.canvas.save()

		return self.pdfdata


import argparse
import socket
import struct
import threading
import sys
import logging
import time
import urllib.request, urllib.error, urllib.parse
ENCODING = 'utf-8'

def get_args(argv=None):
    #parse arguments and send to main function

    parser = argparse.ArgumentParser(description="ANONSERVER")
    parser.add_argument('-p', type=int, required=True, help='Port')
    parser.add_argument('-l', type=str, required=True, help='logFile')
    parser.add_argument('-w', type=str, required=True, help='website')
    args = parser.parse_args()
    log_file = args.l
    server_port = args.p
    website = args.w
    return server_port, log_file, website

def handshake(sock):
    #perform handshake with client

    #receive first packet
    data, addr = sock.recvfrom(1024)
    recvData, recvPayload = struct.unpack('!IIII', data[:16]), data[16:]
    logging.info(f"RECV: Sequence: {recvData[0]} Acknowlegment: {recvData[1]} Flags: ACK: N SYN: Y FIN: N")

    #send packet
    sendData = createPacket(sequence_number = 100, ack_number = recvData[0] + 1, ack = 'Y', syn = 'Y', fin = 'N', payload = b"")
    logging.info(f"SEND: Sequence: {100} Acknowlegment: {recvData[0] + 1} Flags: ACK: Y SYN: Y FIN: N")
    send_packet(sock, sendData, addr)

    #recieve second packet, handshake complete
    data, addr = sock.recvfrom(1024)
    recvData, recvPayload = struct.unpack('!IIII', data[:16]), data[16:]
    logging.info(f"RECV: Sequence: {recvData[0]} Acknowlegment: {recvData[1]} Flags: ACK: Y SYN: N FIN: N")
    print("Handshake Complete")
    logging.info(f"HANDSHAKE COMPLETE")

    return recvData[0], recvData[1], recvData[2], recvPayload, addr

def get_bit(num, i):
    #returns true if bit is 1, false if bit is 0

    return (num & (1 << i)) != 0

def print_bits(num):
    #prints number in 32 bit binary form

    print("{0:032b}".format(num))

def clear_bit(num, i):
    #sets selected bit to 0

    mask = ~(1 << i)
    return (num & mask)

def update_bit(num, i, bit):
    #updates selected bit based on user choice

    mask = ~(1 << i)
    return (num & mask) | (bit << i)

def createPacket(**kwargs):
    #creates packet to send to client

    num = 0
    s_n = kwargs['sequence_number']
    a_n = kwargs['ack_number']
    payload = kwargs['payload']
    if(kwargs['ack'] == 'Y'):
        num = update_bit(num, 2, 1)
    if(kwargs['syn'] == 'Y'):
        num = update_bit(num, 1, 1)
    if(kwargs['fin'] == 'Y'):
        num = update_bit(num, 0, 1)
    data = struct.pack('!I', s_n)
    data += struct.pack('!I', a_n)
    data += struct.pack('!I', num)
    data += struct.pack('!I', len(payload)) + payload

    return data

def send_packet(sock, data, server_addr):
    #sends packet to client

    sock.sendto(data, server_addr)

def downloadWebPage(webpage):
    #read contents of webpage and store in variable

    response = urllib.request.urlopen(webpage)
    webContent = response.read()

    return webContent

def sendFile(sock, s_n, a_n, flags, webContent, addr):
    #send file to client one piece at a time

    #open file we created in main function and send to client
    f = open('savedPage.html', 'rb')
    data = f.read(512)

    while(data):

        #send piece
        send_data = createPacket(sequence_number = a_n, ack_number = s_n + 1, ack = 'Y', syn = 'N', fin = "N", payload = data)
        if(sock.sendto(send_data, addr)):
            logging.info(f"SEND: Sequence: {a_n} Acknowlegment: {s_n + 1} Flags: ACK: Y SYN: N FIN: N")

            #receive acknowlegement that previous piece was received
            data, addr = sock.recvfrom(512)
            recvData, recvPayload = struct.unpack('!IIII', data[:16]), data[16:]
            s_n = recvData[0]
            a_n = recvData[1]
            flags = recvData[2]
            logging.info(f"RECV: Sequence: {recvData[0]} Acknowlegment: {recvData[1]} Flags: ACK: Y SYN: N FIN: N")
            data = f.read(512)

    print("Data Sent")
    #send fin packet to client
    send_data = createPacket(sequence_number = a_n, ack_number = s_n + 1, ack = 'N', syn = 'N', fin = 'Y', payload = b'')
    logging.info(f"SEND: Sequence: {a_n} Acknowlegment: {s_n + 1} Flags: ACK: N SYN: N FIN: Y")
    sock.sendto(send_data, addr)

    #receive fin packet from client
    data, addr = sock.recvfrom(512)
    recvData, recvPayload = struct.unpack('!IIII', data[:16]), data[16:]
    logging.info(f"RECV: Sequence: {recvData[0]} Acknowlegment: {recvData[1]} Flags: ACK: N SYN: N FIN: Y")
    flags = recvData[2]

    if(get_bit(flags, 0)):
        f.close()

if __name__=='__main__':
    #parse arguments, store into variables
    port, log_location, website = get_args(sys.argv[1:])
    print("Port: {}, Log location: {}, Website: {}".format(port, log_location, website))

    #logging configuration
    logging.basicConfig(filename=log_location, filemode='w', format='%(asctime)s - %(levelname)s - %(funcName)s - %(message)s', level=logging.INFO)
    logging.info("Starting ANONSERVER")
    logging.info(f"Server Port = {port}, Logfile = {log_location}, website = {website}")

    #store webpage into a file
    webpage = "http://" + website
    webContent = downloadWebPage(webpage)
    f = open('savedPage.html', 'wb')
    f.write(webContent)
    f.close()

    #grab host IP address
    IP = socket.gethostbyname(socket.gethostname())
    print(IP)
    SERVER_ADDR = (IP, port)

    #create and bind socket
    sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    sock.bind(SERVER_ADDR)

    while True:

        #perform handshake
        s_n, a_n, flags, payload, addr = handshake(sock)

        #Send the file to the client
        sendFile(sock, s_n, a_n, flags, webContent, addr)