This commit is contained in:
Maximilian Giller 2019-05-19 00:22:24 +02:00
parent ccf28e52b7
commit ba8c95ff67
8 changed files with 100 additions and 63 deletions

10
Calendar/CryptoCoin.py Normal file
View file

@ -0,0 +1,10 @@
class CryptoCoin(object):
def __init__ (self):
self.name = None
self.symbol = None
self.price = None
self.day_change = None
self.currency = None
self.datetime = None
self.fetch_datetime = None

View file

@ -1,22 +1,16 @@
from DataSourceInterface import DataSourceInterface from DataSourceInterface import DataSourceInterface
from datetime import datetime, timezone, timedelta
class CryptoInterface(DataSourceInterface): class CryptoInterface(DataSourceInterface):
def __init__(self): def __init__(self):
self.crypto_prices = [] self.crypto_coins = []
def reload(self): def reload(self):
if self.is_available() == False: if self.is_available() == False:
return return
self.crypto_prices= self.__get_prices__() self.crypto_coins = self.__get_coins__()
self.sort_prices()
def __get_prices__(self): def __get_coins__(self):
raise NotImplementedError("Functions needs to be implemented") raise NotImplementedError("Function needs to be implemented")
def get_latest_prices(self): def get_coins(self):
self.crypto_prices = self.crypto_prices return self.crypto_coins
return self.crypto_prices
def sort_prices(self):
self.crypto_prices =self.crypto_prices

View file

@ -1,4 +0,0 @@
class CryptoItem(object):
def __init__ (self):
self.name = None
self.value = None

View file

@ -1,24 +1,25 @@
from DesignEntity import DesignEntity from DesignEntity import DesignEntity
from TableDesign import TableDesign from TableDesign import TableDesign
from Assets import defaultfontsize from Assets import defaultfontsize
from CryptoPrices import CryptoPrices from GeckoCrypto import GeckoCrypto
from settings import crypto_coins as cryptos from settings import crypto_coins
class CryptoListDesign (DesignEntity): class CryptoListDesign (DesignEntity):
def __init__ (self, size, coin, text_size = defaultfontsize): def __init__ (self, size, crypto, text_size = defaultfontsize):
super(CryptoListDesign, self).__init__(size) super(CryptoListDesign, self).__init__(size)
self.coin = coin self.crypto = crypto
self.__post_matrix__ = [[]]
self.text_size = text_size self.text_size = text_size
def __finish_image__ (self): def __finish_image__ (self):
self.__fill_post_matrix__() matrix = self.__get_matrix__()
table_design = TableDesign(self.size, line_spacing=2, col_spacing=3, matrix=self.__post_matrix__, fontsize = self.text_size, mask=False, wrap=True, truncate_rows=True) table_design = TableDesign(self.size, matrix=matrix, col_spacing=5, fontsize = self.text_size, mask=False, truncate_rows=True)
self.draw_design(table_design) self.draw_design(table_design)
def __fill_post_matrix__ (self): def __get_matrix__ (self):
prices, coins = CryptoPrices.__get_prices__(self.coin) matrix = []
for price, coin in zip(prices, coins): coins = self.crypto.get_coins()
row = coin + ": $" + str(price) for coin in coins:
self.__post_matrix__[0].append(row) row = [ coin.symbol.upper(), coin.name, coin.currency + " " + str(coin.price), "% " + str(coin.day_change) ]
matrix.append(row)
return matrix

View file

@ -1,29 +0,0 @@
from CryptoInterface import CryptoInterface
from datetime import datetime, timedelta, date
import CryptoItem
import urllib.request
import json
import math
class CryptoPrices(CryptoInterface):
def __init__(self, coins):
self.coins = coins
super(CryptoPrices, self).__init__()
def is_available(self):
if len(self.coins) > 0 and len(self.coins) < 8:
return True
else:
return False
def __get_prices__(self):
price=[]
name=[]
for coin in self.coins:
data = urllib.request.urlopen("https://api.coingecko.com/api/v3/simple/price?ids="+coin+"&vs_currencies=USD").read()
dataJSON = json.loads(data.decode('utf-8'))
raw = dataJSON[coin]["usd"]
price.append(math.ceil(raw*100)/100)
name.append(coin)
return price,name

View file

@ -22,7 +22,7 @@ from AgendaListPanel import AgendaListPanel
import OwmForecasts import OwmForecasts
import IcalEvents import IcalEvents
import RssParserPosts import RssParserPosts
import CryptoPrices from GeckoCrypto import GeckoCrypto
all_locales = locale.locale_alias all_locales = locale.locale_alias
if language.lower() not in all_locales.keys(): if language.lower() not in all_locales.keys():
@ -62,7 +62,7 @@ def main():
owm = OwmForecasts.OwmForecasts(location, api_key, paid_api=owm_paid_subscription) owm = OwmForecasts.OwmForecasts(location, api_key, paid_api=owm_paid_subscription)
events_cal = IcalEvents.IcalEvents(ical_urls, highlighted_ical_urls) events_cal = IcalEvents.IcalEvents(ical_urls, highlighted_ical_urls)
rss = RssParserPosts.RssParserPosts(rss_feeds) rss = RssParserPosts.RssParserPosts(rss_feeds)
coin = CryptoPrices.CryptoPrices(crypto_coins) crypto = GeckoCrypto(crypto_coins)
while True: while True:
loop_timer.begin_loop() loop_timer.begin_loop()
@ -78,9 +78,9 @@ def main():
else: else:
raise ImportError("choosen_design must be valid (" + choosen_design + ")") raise ImportError("choosen_design must be valid (" + choosen_design + ")")
debug.print_line('Getting crypto prices') debug.print_line('Fetching crypto prices from coin gecko')
coin.reload() crypto.reload()
design.add_crypto(coin) design.add_crypto(crypto)
debug.print_line("Fetching weather information from open weather map") debug.print_line("Fetching weather information from open weather map")
owm.reload() owm.reload()

63
Calendar/GeckoCrypto.py Normal file
View file

@ -0,0 +1,63 @@
from CryptoInterface import CryptoInterface
from datetime import datetime
from CryptoCoin import CryptoCoin
from urllib.request import urlopen
import json
import math
api_test_url = "https://www.coingecko.com"
api_url = "https://api.coingecko.com/api/v3/"
api_metadata_url = api_url + "coins/list"
api_price_url = api_url + "simple/price"
price_currency = "usd"
price_currency_sign = "$"
class GeckoCrypto(CryptoInterface):
def __init__(self, coins):
self.coin_names = coins
self.metadata = None
super(GeckoCrypto, self).__init__()
def is_available(self):
return True
try:
urlopen(api_test_url)
return True
except:
return False
def __get_coins__(self):
self.__prepare_metadata__()
coins = []
for name in self.coin_names:
try:
data = urlopen(api_price_url + "?include_24hr_change=true&ids=" + self.metadata[name]['id'] + "&vs_currencies=" + price_currency).read()
dataJSON = json.loads(data.decode('utf-8'))
raw = dataJSON[name][price_currency]
price = math.ceil(raw*100) / 100
change = dataJSON[name]['usd_24h_change']
coins.append(self.__build_coin__(name, price, change))
except:
print("Gecko-Error [" + name + "]")
return coins
def __build_coin__(self, name, value, change):
coin = CryptoCoin()
coin.name = self.metadata[name]['name']
coin.day_change = round(change, 2)
coin.price = value
coin.datetime = datetime.now()
coin.fetch_datetime = datetime.now()
coin.currency = price_currency_sign
coin.symbol = self.metadata[name]['symbol']
return coin
def __prepare_metadata__(self):
self.metadata = None
data = urlopen(api_metadata_url).read()
dataJSON = json.loads(data.decode('utf-8'))
self.metadata = { coin['id'].lower() : coin for coin in dataJSON if coin['id'].lower() in self.coin_names }

View file

@ -21,7 +21,7 @@ if [ -z "$option" ]; then
fi fi
if [ "$option" = 3 ]; then if [ "$option" = 3 ]; then
echo -e "Removing the E-Paper software now..." echo -e "Removing the E-Paper software now..."
pip3 uninstall feedparser -y && sudo pip3 uninstall feedparser -y && pip3 uninstall numpy -y && sudo pip3 uninstall numpy -y && pip3 uninstall Pillow -y && sudo pip3 uninstall Pillow -y && sudo pip3 uninstall pyowm -y&& sudo pip3 uninstall ics -y && pip3 uninstall pyowm -y && pip3 uninstall ics -y && sudo apt-get remove supervisor -y && sudo apt-get clean && sudo apt-get autoremove -y pip3 uninstall json -y && sudo pip3 uninstall json -y && pip3 uninstall feedparser -y && sudo pip3 uninstall feedparser -y && pip3 uninstall numpy -y && sudo pip3 uninstall numpy -y && pip3 uninstall Pillow -y && sudo pip3 uninstall Pillow -y && sudo pip3 uninstall pyowm -y&& sudo pip3 uninstall ics -y && pip3 uninstall pyowm -y && pip3 uninstall ics -y && sudo apt-get remove supervisor -y && sudo apt-get clean && sudo apt-get autoremove -y
if [ -e /etc/supervisor/conf.d/E-Paper.conf ]; then if [ -e /etc/supervisor/conf.d/E-Paper.conf ]; then
sudo rm /etc/supervisor/conf.d/E-Paper.conf sudo rm /etc/supervisor/conf.d/E-Paper.conf
fi fi
@ -83,10 +83,12 @@ if [ "$option" = 2 ]; then
sudo pip3 install ics sudo pip3 install ics
sudo pip3 install feedparser sudo pip3 install feedparser
sudo pip3 install numpy sudo pip3 install numpy
sudo pip3 install json
pip3 install pyowm pip3 install pyowm
pip3 install ics pip3 install ics
pip3 install feedparser pip3 install feedparser
pip3 install numpy pip3 install numpy
pip3 install json
echo -e "\e[1;36m"Finished installing libraries"\e[0m" echo -e "\e[1;36m"Finished installing libraries"\e[0m"
fi fi