fintic-tracker/test.py

153 lines
4.9 KiB
Python
Raw Normal View History

2021-04-27 18:29:14 +00:00
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
2021-05-14 12:02:22 +00:00
from pycoingecko import CoinGeckoAPI
2021-05-27 19:10:57 +00:00
from newsapi import NewsApiClient
2021-06-02 20:16:15 +00:00
import configparser
import requests
import json
2021-04-27 18:29:14 +00:00
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)
2021-06-02 20:16:15 +00:00
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+' ))
2021-04-27 18:29:14 +00:00
if __name__ == '__main__':
2021-06-02 20:16:15 +00:00
update_weather()
sys.exit()
2021-06-02 20:16:15 +00:00
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)
2021-05-27 19:10:57 +00:00
2021-04-27 18:59:25 +00:00