import sys
import os
import threading
from PIL import Image, ImageDraw, ImageFont
import time
import csv

from rgbmatrix import RGBMatrix, RGBMatrixOptions
from stockTicker import StockTicker
import finnhub

import pexpect
from pycoingecko import CoinGeckoAPI
from newsapi import NewsApiClient
import configparser
import requests
import json

import sys, os
try:
	raise NotImplementedError("No error")
except Exception as e:
	exc_type, exc_obj, exc_tb = sys.exc_info()
	fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1]
	print(exc_type, fname, exc_tb.tb_lineno, exc_obj, exc_tb)
	
	
def update_weather():
    api_key = 'bd5d5096a5ba30bbcfb57ead42ab3fee'
    location = 'London'
    url = "https://api.openweathermap.org/data/2.5/weather?q={}&units=metric&appid={}".format(location, api_key)
    r = requests.get(url)
    weather = r.json()
    
    current_weather = {}
    
    coords = weather['coord']
    lat = coords['lat']
    lon = coords['lon']
    url = 'https://api.openweathermap.org/data/2.5/onecall?lat={}&units=metric&lon={}&appid={}'.format(lat, lon, api_key)
    r = requests.get(url)
    
    current_weather['main_weather'] = weather['weather'][0]['main']
    current_weather['description'] = weather['weather'][0]['description']
    current_weather['temp'] = weather['main']['temp']
    current_weather['min_temp'] = weather['main']['temp_min']
    current_weather['max_temp'] = weather['main']['temp_max']
    current_weather['feels_like'] = weather['main']['feels_like']
    current_weather['humidity'] = weather['main']['humidity']
    current_weather['clouds'] = weather['clouds']['all']
    current_weather['wind_speed'] = weather['wind']['speed']
    current_weather['wind_direction'] = weather['wind']['deg']
    current_weather['visibility'] = weather['visibility']
    current_weather['uv'] = r.json()['current']['uvi']
    
    json.dump( current_weather,  open( "csv/current_weather.json", 'w+' ))
	
	
    daily_weather = []
    daily = r.json()['daily']
    
    for day in daily:
        dct = {}
        dct['main_weather'] = day['weather'][0]['main']
        dct['description'] = day['weather'][0]['description']
        dct['min_temp'] = day['temp']['min']
        dct['min_temp'] = day['temp']['max']
        daily_weather.append(dct)
    
    
    json.dump( daily_weather,  open( "csv/daily_weather.json", 'w+' ))
		

if __name__ == '__main__':
	
    update_weather()
    sys.exit()

    api_key = 'bd5d5096a5ba30bbcfb57ead42ab3fee'
    location = 'London'
    
    url = "https://api.openweathermap.org/data/2.5/weather?q={}&units=metric&appid={}".format(location, api_key)
    r = requests.get(url)
    weather = r.json()
    
    current_weather = {}
    
    coords = weather['coord']
    lat = coords['lat']
    lon = coords['lon']
    url = 'https://api.openweathermap.org/data/2.5/onecall?lat={}&units=metric&lon={}&appid={}'.format(lat, lon, api_key)
    r = requests.get(url)
    print()
    print('main weather: ', weather['weather'][0]['main'])
    print('description: ', weather['weather'][0]['description'])
    print('temp (degrees C): ', weather['main']['temp'])
    print('min: ', weather['main']['temp_min'])
    print('max: ', weather['main']['temp_max'])
    print('feels like: ', weather['main']['feels_like'])
    print('humidity (%): ', weather['main']['humidity'])
    print('clouds: ', weather['clouds']['all'])
    print('wind speed (m/s): ', weather['wind']['speed'], 'direction:', weather['wind']['deg'])
    print('visibility (metres): ', weather['visibility'])   
    print('UV index: ', r.json()['current']['uvi'])
	
    current_weather['main_weather'] = weather['weather'][0]['main']
    current_weather['description'] = weather['weather'][0]['description']
    current_weather['temp'] = weather['main']['temp']
    current_weather['min_temp'] = weather['main']['temp_min']
    current_weather['max_temp'] = weather['main']['temp_max']
    current_weather['feels_like'] = weather['main']['feels_like']
    current_weather['humidity'] = weather['main']['humidity']
    current_weather['clouds'] = weather['clouds']['all']
    current_weather['wind_speed'] = weather['wind']['speed']
    current_weather['wind_direction'] = weather['wind']['deg']
    current_weather['visibility'] = weather['visibility']
    current_weather['uv'] = r.json()['current']['uvi']
    print()
    print(current_weather)
    
    json.dump( current_weather,  open( "csv/current_weather.json", 'w+' ))
	
	
    daily_weather = []
    daily = r.json()['daily']
    
    for day in daily:
        dct = {}
        dct['main_weather'] = day['weather'][0]['main']
        dct['description'] = day['weather'][0]['description']
        dct['min_temp'] = day['temp']['min']
        dct['min_temp'] = day['temp']['max']
        daily_weather.append(dct)
    
    
    json.dump( daily_weather,  open( "csv/daily_weather.json", 'w+' ))
		
				
    print()
    print(weather)
    print()
    print(r.json()['daily'][0])
    print()
    print(daily_weather)