import imaplib
import email
from datetime import datetime, timedelta
from io import BytesIO

from src.components import SpeakChromium

def ReadEmails():

    # Connect to the IMAP server
    imap_server = imaplib.IMAP4_SSL("imap.example.com")
    imap_server.login("username", "password")

    # Select the inbox
    imap_server.select("inbox")

    # Search for important emails received in the last hour
    now = datetime.now()
    before = (now - timedelta(hours=1)).strftime("%d-%b-%Y")
    status, messages = imap_server.search(
        None, f'X-GM-LABELS "important" SINCE {before}')

    # Iterate over the email IDs
    for email_id in messages[0].split():
        # Fetch the email
        status, msg = imap_server.fetch(email_id, "(RFC822)")

        # Parse the email
        email_message = email.message_from_bytes(msg[0][1])

        # read the email body
        email_body = ""
        if email_message.is_multipart():
            for part in email_message.get_payload():
                if part.get_content_type() == "text/plain":
                    email_body += part.get_payload()
        else:
            email_body = email_message.get_payload()

        # read the email body
        SpeakChromium(email_body)

        # Iterate over attachments
        for part in email_message.walk():
            if part.get_content_maintype() == 'multipart':
                continue
            if part.get('Content-Disposition') is None:
                continue
            filename = part.get_filename()

            if not filename:
                continue

            # reading the attachment
            data = part.get_payload(decode=True)
            file_like = BytesIO(data)
            print(f'attachment {filename} is being processed')
            # read the name of attachments
            SpeakChromium(filename)

    # Close the IMAP connection
    imap_server.close()
    imap_server.logout()


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

__author__ = "Raul Perula-Martinez"
__copyright__ = "Social Robots Group. Robotics Lab. University Carlos III of Madrid"
__credits__ = ["Raul Perula-Martinez"]
__license__ = "LEUC3M v1.0"
__version__ = "0.0.0"
__maintainer__ = "Raul Perula-Martinez"
__email__ = "raul.perula@uc3m.es"
__status__ = "Development"

import math

from geometry_msgs.msg import PoseWithCovarianceStamped, Quaternion
from tf.transformations import quaternion_from_euler

import roslib
import rosparam
import rospy

pkg_name = "maggie_navigation_config"
roslib.load_manifest(pkg_name)

class InitialposeSender():

    """
    InitialposeSender class.
    """

    def __init__(self):
        """
        Init method.
        """

        # publishers and subscribers
        self.__initialpose_pub = rospy.Publisher('initialpose',
                                                 PoseWithCovarianceStamped,
                                                 queue_size=1,
                                                 latch=True)

    def run(self):
        """
        Spinner of the node.

        @raise KeyError: exception when dictionary has an invalid key.
        @raise rosparam.RosParamException: exception when cannot open the config file.
        """

        try:
            # read the data from the yaml file
            #             data = rosparam.load_file(roslib.packages.get_pkg_dir(pkg_name) + "/config/map_1.3.C12_initialpose.yaml",
            #                                       rospy.get_name())
            data = rosparam.load_file(roslib.packages.get_pkg_dir(pkg_name) + "/config/roboticslab_complete_clean_initialpose.yaml",
                                      rospy.get_name())
            try:
                # the place label come from the request
                place_data = data[0][0]

                # fill the msg with the data of the file
                msg = PoseWithCovarianceStamped()

                msg.header.frame_id = "/map"
                msg.header.stamp = rospy.Time.now()

                msg.pose.pose.position.x = place_data['x']
                msg.pose.pose.position.y = place_data['y']
                msg.pose.pose.position.z = 0

                msg.pose.covariance[0] = place_data['cov_x']
                msg.pose.covariance[7] = place_data['cov_y']
                msg.pose.covariance[35] = place_data['cov_th']

                # convert from (roll, pitch, yaw) to quaternion
                quat = quaternion_from_euler(0.0, 0.0, place_data['th'])
                msg.pose.pose.orientation = Quaternion(*quat.tolist())

                # publish the initial position
                self.__initialpose_pub.publish(msg)
            except KeyError:
                rospy.logerr('[INITIAL_STATUS_SENDER] Error.')
        except rosparam.RosParamException:
            rospy.logerr('[INITIAL_STATUS_SENDER] Error loading config file')

if __name__ == '__main__':
    try:
        # start the node
        rospy.init_node(pkg_name)

        # create and spin the node
        node = InitialposeSender()
        node.run()

        rospy.spin()
    except rospy.ROSInterruptException:
        pass


import boto3
import botocore
import os
import subprocess
import sys

import time
import threading

s3_endpoint_url = os.environ['CEPH_ENDPOINT']
s3_access_key_id = os.environ['S3_ID']
s3_secret_access_key = os.environ['S3_SECRET_KEY']
s3_bucket = 'MyBucket'

s3 = boto3.client('s3',
    '',
    use_ssl = False,
    verify = False,
    endpoint_url = s3_endpoint_url,
    aws_access_key_id = s3_access_key_id,
    aws_secret_access_key = s3_secret_access_key,
    )
s3r = boto3.resource('s3',
    '',
    use_ssl = False,
    verify = False,
    endpoint_url = s3_endpoint_url,
    aws_access_key_id = s3_access_key_id,
    aws_secret_access_key = s3_secret_access_key,)

def build_video(currSeg):

    my_bucket = s3r.Bucket(name=s3_bucket)

    if not os.path.exists("segments"):
      os.mkdir("segments")
    ffmpeg_log = open('segments/ffmpeg_log.txt', 'w')
    print(my_bucket.objects.all())

    for my_bucket_object in my_bucket.objects.all():

        print("Getting Segment from Ceph: ",my_bucket_object.key)
        filename = my_bucket_object.key
        num = filename.split("-")
        currNum = int(num[1].split(".")[0])

        if currNum > currSeg:
            s3.download_file(s3_bucket,my_bucket_object.key,"segments/"+my_bucket_object.key)
            os.system("ffmpeg -i segments/"+my_bucket_object.key+" -s 640x360 -hls_flags delete_segments+append_list+omit_endlist -hls_list_size 30 -f hls segments/out.m3u8")
            #s3.delete_object(Bucket=s3_bucket,Key=my_bucket_object.key)
            os.remove("segments/"+my_bucket_object.key)
            currSeg = currNum
        else: continue

    return currSeg

if __name__ == '__main__':

    currSeg = -1
    while(True):
        currSeg = build_video(currSeg)
        time.sleep(5)


import pandas as pd
import numpy as np
import csv

df = pd.read_csv("../../data/bases/all_projects.csv")
total_contributions = pd.read_csv("total.csv")
dict_1 = dict(
    zip(list(total_contributions["country"]), list(total_contributions["number"]))
)

loc_rejection = []
loc_acceptance = []
for key in dict_1:
    count_rejection = 0
    count_acceptance = 0
    for index, row in df.iterrows():
        if row["country"] == key:
            if row["merged"] == False:
                count_rejection += 1
            else:
                count_acceptance += 1
    loc_rejection.append(count_rejection)
    loc_acceptance.append(count_acceptance)

total_contributions["accepted"] = loc_acceptance
total_contributions["rejected"] = loc_rejection
total_contributions.to_csv("new_total.csv", index=False)
print(count_rejection)
print(count_acceptance)

# Contributos by country
# df_2 = pd.read_csv("contripercountry.csv")
# dict_2 = dict(zip(list(df_2["country"]), list(df_2["number"])))
#
# writer = csv.writer(output_file)
#
# for key in dict_1:
#     writer.writerow([key, dict_1[key], dict_2[key], dict_1[key] / dict_2[key]])


import simuPOP as sim

import random
N = 10000
pop = sim.Population(N, infoFields=['age', 'ind_id', 'father_id', 'mother_id'])
# we simulate age 0, 1, 2, 3
pop.setVirtualSplitter(sim.InfoSplitter(field='age', values=[0, 1, 2, 3]))
pop.evolve(
    initOps=[
        sim.InitSex(),
        # random assign age
        sim.InitInfo(lambda: random.randint(0, 3), infoFields='age'),
        # random genotype
        sim.InitGenotype(freq=[0.5, 0.5]),
        # assign an unique ID to everyone.
        sim.IdTagger(),
    ],
    # increase the age of everyone by 1 before mating.
    preOps=sim.InfoExec('age += 1'),
    matingScheme=sim.HeteroMating([
        # age 1, 2 will be copied
        sim.CloneMating(
            ops=[
                # This will set offspring ID
                sim.CloneGenoTransmitter(),
                # new ID for offspring in order to track pedigree
                sim.IdTagger(),
                # both offspring and parental IDs will be the same
                sim.PedigreeTagger(output='>>structured.ped'),
            ],
            subPops=[(0,1), (0,2)],
            weight=-1
        ),
        # age 2 produce offspring
        sim.RandomMating(
            ops=[
                # new ID for offspring
                sim.IdTagger(),
                # record complete pedigree
                sim.PedigreeTagger(output='>>structured.ped'),
                sim.MendelianGenoTransmitter(),   # transmit genotype
            ],
            subPops=[(0,2)]
        )]
    ),
    gen=20
)

# use a pedigree object recovered from a file saved by operator PedigreeTagger
ped = sim.loadPedigree('structured.ped')
# create a top most population, but we do not need all of them
# so we record only used individuals
IDs = [x.ind_id for x in ped.allIndividuals(ancGens=ped.ancestralGens())]
sex = [x.sex() for x in ped.allIndividuals(ancGens=ped.ancestralGens())]
# create a population, this time with genotype. Note that we do not need
# populaton structure because PedigreeMating disregard population structure.
pop = sim.Population(size=len(IDs), loci=1000, infoFields='ind_id')
# manually initialize ID and sex
sim.initInfo(pop, IDs, infoFields='ind_id')
sim.initSex(pop, sex=sex)
pop.evolve(
    initOps=sim.InitGenotype(freq=[0.4, 0.6]),
    # we do not need migration, or set number of offspring,
    # or demographic model, but we do need a genotype transmitter
    matingScheme=sim.PedigreeMating(ped,
        ops=sim.IfElse(lambda mom: mom is None,
                sim.CloneGenoTransmitter(),
                sim.MendelianGenoTransmitter())
    ),
    gen=100
)
#
print(pop.indInfo('ind_id')[:5])
print([pop.individual(x).sex() for x in range(5)])
# The pedigree object does not have population structure
print(pop.subPopSizes())


# -*- coding: utf-8 -*-
# coding: utf-8
import time
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.chrome.options import Options

class chromeDriver:
    def __init__(self):
        chrome_options = Options()
        chrome_options.add_argument("--headless")
        chrome_options.add_argument('--no-sandbox')
        chrome_options.add_argument('--disable-dev-shm-usage')
        # 한국어 변환 추가
        chrome_options.add_argument("lang=ko_KR")
        chrome_options.add_argument("user-agent=Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36")
        time.sleep(2)
        driver = webdriver.Chrome( '/data16/appscrap/chromedriver', options=chrome_options)
        time.sleep(2)
        self.driver = driver

    def get(self, url):
        self.driver.get(url)
        time.sleep(5)

    def exeScroll(self):
        no_of_pagedowns = 30
        body = self.driver.find_element_by_tag_name("body")
        while no_of_pagedowns:
            body.send_keys(Keys.PAGE_DOWN)
            time.sleep(0.2)
            no_of_pagedowns-=1

    def getHtml(self):
        return self.driver.page_source

    def close(self):
        self.driver.quit()


"""The main file."""
from views import *
from database_manager import db_manager
from img_download import img_url_handler
from json_loader import get_reddit
from json_loader import get_settings
from sub_scrape import sub_scrape
from pathlib import Path
from datetime import date
from calendar import monthrange
from time import sleep
from _thread import start_new_thread
import schedule

reddit_account = get_reddit()

def get_posts(db, subreddit, time_filter):
    img_path = 'static/images/' + subreddit + '/'
    scraper = sub_scrape(subreddit, reddit_account)
    print("Started\n Getting top posts from {} {}...\n".format(time_filter, subreddit))
    img_handler = img_url_handler(subreddit, img_path)
    scraper.get_posts(time_filter, db, img_handler)
    print('Time for next subreddit \n\n')
    return

def daily_run(db):
    """
    Connects to the database, creates a table for each subreddit
    and checks if its empty.
    """
    for each in subreddits:
        db.create_connect()
        db.create_table( each)
        db.table = each
        rows = db.is_empty()
        if rows == 0:
            get_posts(db, each, 'all')
        else:
            date_check(db, each)
        db.close_conn()
    return

def date_check(db, subreddit):
    today = date.today()
    days_in_month = monthrange(today.year, today.month)[1]
    if str(today.day) == '1':
        get_posts(db, subreddit, 'month')
    else:
        print('Time left till next reddit scrape: ', days_in_month - today.day)
    return

def flaskThread():
        app.run(host='0.0.0.0', use_reloader=False, debug=False, threaded=True)

start_new_thread(flaskThread, ())
schedule.every().day.at('12:00').do(daily_run, db_m)

while True:
    schedule.run_pending()
    sleep(1)


def main():
    N = int(input())
    for _ in range(N):
        nums = list(map(int, input().split()))
        cnt = 0
        for i in range(10):
            if nums[i] == (i + 1) % 5:
                cnt += 1
        if cnt == 8:
            print(_ + 1)

if __name__ == "__main__":
    main()


import pandas as pd
import statsmodels.api as sm
import matplotlib.pyplot as plt
import numpy as np

df = pd.read_csv('../file/user_balance_table_all.csv', index_col='user_id', names=['user_id', 'report_date', 'tBalance', 'yBalance', 'total_purchase_amt', 'direct_purchase_amt', 'purchase_bal_amt', 'purchase_bank_amt', 'total_redeem_amt', 'consume_amt', 'transfer_amt', 'tftobal_amt', 'tftocard_amt', 'share_amt', 'category1', 'category2', 'category3', 'category4'
], parse_dates=[1])

df['report_date'] = pd.to_datetime(df['report_date'], errors='coerce')
df['total_purchase_amt'] = pd.to_numeric(df['total_purchase_amt'], errors='coerce')
df['total_redeem_amt'] = pd.to_numeric(df['total_redeem_amt'], errors='coerce')
df['purchase_bank_amt'] = pd.to_numeric(df['purchase_bank_amt'], errors='coerce')

df = df.groupby('report_date').sum()
ts = df['total_purchase_amt']
ts = ts['2014-04-01':'2014-06-30']

model=sm.tsa.statespace.SARIMAX(endog=ts,order=(1,1,0),seasonal_order=(0,0,0,7),trend='c',enforce_invertibility=False)
results=model.fit()
print(results.summary())
print(results.predict())

predict_ts = results.predict()

predict_ts.plot(label='predicted')
ts.plot(label='original')
plt.legend(loc='best')
plt.show()


#!/usr/bin/python3

"""
Module 2-matrix_divided
module contsains one method that takes in a matrix and
returns a matrix whose elements have been divided by div
"""

def matrix_divided(matrix, div):
    """
        Return matrix divided by div to the round of 2
    """
    if div == 0:
        raise ZeroDivisionError("division by zero")
    if not isinstance(div, (int, float)):
        raise TypeError("div must be a number")
    if type(matrix) is not list or len(matrix) == 0 or len(matrix[0]) == 0:
        msg = "matrix must be a matrix (list of lists) of integers/floats"
        raise TypeError(msg)
    n_matrix = []
    n_list = []
    samelen = len(matrix[0])

    for lists in matrix:
        if type(lists) is not list:
            raise TypeError(msg)
        if len(lists) != samelen:
            raise TypeError("Each row of the matrix must have the same size")
        for i in lists:
            if not isinstance(i, (int, float)):
                raise TypeError(msg)
            n_list.append(round(i/div, 2))
        n_matrix.append(n_list)
    return n_matrix


import sqlalchemy
from sqlalchemy.ext.automap import automap_base
from sqlalchemy.orm import Session
from sqlalchemy import create_engine, func

import pandas as pd

from flask import Flask, jsonify, render_template
from flask_cors import CORS

from config import pwd
import json

rds_connection_string = f"postgres:{pwd}@localhost:5432/Proj2_AirlineAccidents"
engine = create_engine(f'postgresql://{rds_connection_string}')

# reflect an existing database into a new model
Base = automap_base()
# reflect the tables
Base.prepare(engine, reflect=True)

# Save reference to the table
airplane_events = Base.classes.airplane_events_clean

app = Flask(__name__)
CORS(app)

# Flask Routes to render HTML
@app.route("/")
def home():
    """Return the homepage."""
    return render_template("index.html")

@app.route("/api")
def Accidents():
     # Create our session (link) from Python to the DB
     session = Session(engine)

    # # Perform a query to retrieve the accident data
     accident_data = session.query(airplane_events.event_id, airplane_events.investigation_type, airplane_events.event_date, airplane_events.location,\
         airplane_events.latitude, airplane_events.longitude, airplane_events.airport_code, airplane_events.airport_name, airplane_events.injury_severity, \
         airplane_events.aircraft_damage, airplane_events.aircraft_category, airplane_events.make, airplane_events.flight_purpose, airplane_events.air_carrier, \
         airplane_events.fatalities, airplane_events.injuries, airplane_events.uninjured, airplane_events.weather_condition, airplane_events.flight_phase).\
         order_by(airplane_events.event_date).all()

     session.close()

     accident_df = pd.DataFrame(accident_data, columns =['event_id', 'investigation_type', 'event_date', 'location', 'latitude','longitude', 'airport_code', \
         'airport_name', 'injury_severity', 'aircraft_damage', 'aircraft_category', 'make', 'flight_purpose', 'air_carrier', 'fatalities', 'injuries', 'uninjured', \
         'weather_condition', 'flight_phase'])

     #Need to convert latitude and longitude to string (stored as decimal in DB)
     accident_df['latitude'] = accident_df['latitude'].astype(str)
     accident_df['longitude'] = accident_df['longitude'].astype(str)

     accident_dict = accident_df.to_dict()
     return jsonify(accident_dict)

@app.route("/monthly")
def Monthly():
    monthly_accidents = engine.execute ('''Select Extract (Month from event_date) AS monthid
		                                    , TO_CHAR (event_date, 'Month') AS monthname
		                                    , count (event_id) as accidents
		                                    , sum (fatalities) as fatalities
	                                        from	airplane_events_clean
                                            group by Extract (Month from event_date), TO_CHAR (event_date, 'Month')
                                            order by Extract (Month from event_date)''')
    results= []
    results = [list(row) for row in monthly_accidents]
    test = []
    for x in range(0, len(results)):
        result_dict = {'monthid':results[x][0], 'monthname': results[x][1], 'accidents': results[x][2], 'fatalities': results[x][3] }
        test.append(result_dict)
    return jsonify(test)

@app.route("/by_airport")
def by_airport():
    accidents_by_airport = engine.execute('''SELECT aec.airport_code, ac.airport_name, ac.latitude, ac.longitude, ac.city, ac.country
                                            , count (aec.event_id) as accidents_total, sum(aec.fatalities) as fatalities_total
                                        FROM airplane_events_clean aec
                                        inner join airport_coordinates ac on
                                                aec.airport_code = ac.airport_code
                                        group by aec.airport_code, ac.airport_name, ac.latitude, ac.longitude, ac.city, ac.country
                                        having sum(aec.fatalities) > 0''')
    results= []
    results = [list(row) for row in accidents_by_airport]
    test = []
    for x in range(0, len(results)):
        result_dict = {'airport_code':results[x][0], 'airport_name': results[x][1], 'latitude': (str(results[x][2])), 'longitude': (str(results[x][3])), 'accidents': results[x][4], 'fatalities': results[x][5]}

        test.append(result_dict)
    return jsonify(test)

@app.route("/weather_impact")
def Weather():
    fatality_flight_phase = engine.execute('''SELECT flight_phase, SUM(uninjured) as UNINJURED, SUM(FATALITIES) as FATALITIES, SUM(injuries) as INJURIES
                            FROM airplane_events_clean
                            GROUP BY flight_phase
                            ORDER BY flight_phase''')
    results= []
    results = [list(row) for row in fatality_flight_phase]
    test = []
    for x in range(0, len(results)):
        result_dict = {'Flight Phase':results[x][0], 'Uninjured': results[x][1], 'Injuries': results[x][3], 'Fatalities': results[x][2] }
        test.append(result_dict)
    return jsonify(test)

@app.route("/weather_flight_impact")
def Weather_flight_phase():
    weather_flight_phase = engine.execute('''SELECT flight_phase, airplane_events_clean.weather_condition, SUM(FATALITIES) as FATALITIES, SUM(injuries) as INJURIES FROM airplane_events_clean
        WHERE weather_condition = 'IMC' or weather_condition = 'VMC'
        GROUP BY flight_phase, weather_condition
        ORDER BY flight_phase;''')
    results= []
    results = [list(row) for row in weather_flight_phase]
    test = []
    for x in range(0, len(results)):
        result_dict = {'Flight Phase':results[x][0], 'Weather Condition': results[x][1], 'Fatal': results[x][2], 'Injuries': results[x][3] }
        test.append(result_dict)
    return jsonify(test)

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