diff --git a/Calendar/CryptoCoin.py b/Calendar/CryptoCoin.py new file mode 100644 index 0000000..3ad4f29 --- /dev/null +++ b/Calendar/CryptoCoin.py @@ -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 \ No newline at end of file diff --git a/Calendar/CryptoInterface.py b/Calendar/CryptoInterface.py index fa3b7e5..5cceb75 100644 --- a/Calendar/CryptoInterface.py +++ b/Calendar/CryptoInterface.py @@ -1,22 +1,16 @@ from DataSourceInterface import DataSourceInterface -from datetime import datetime, timezone, timedelta class CryptoInterface(DataSourceInterface): def __init__(self): - self.crypto_prices = [] + self.crypto_coins = [] def reload(self): if self.is_available() == False: return - self.crypto_prices= self.__get_prices__() - self.sort_prices() + self.crypto_coins = self.__get_coins__() - def __get_prices__(self): - raise NotImplementedError("Functions needs to be implemented") + def __get_coins__(self): + raise NotImplementedError("Function needs to be implemented") - def get_latest_prices(self): - self.crypto_prices = self.crypto_prices - return self.crypto_prices - - def sort_prices(self): - self.crypto_prices =self.crypto_prices + def get_coins(self): + return self.crypto_coins diff --git a/Calendar/CryptoItem.py b/Calendar/CryptoItem.py deleted file mode 100644 index 83109fd..0000000 --- a/Calendar/CryptoItem.py +++ /dev/null @@ -1,4 +0,0 @@ -class CryptoItem(object): - def __init__ (self): - self.name = None - self.value = None diff --git a/Calendar/CryptoListDesign.py b/Calendar/CryptoListDesign.py index dbce17a..0efb5bf 100644 --- a/Calendar/CryptoListDesign.py +++ b/Calendar/CryptoListDesign.py @@ -1,24 +1,25 @@ from DesignEntity import DesignEntity from TableDesign import TableDesign from Assets import defaultfontsize -from CryptoPrices import CryptoPrices -from settings import crypto_coins as cryptos +from GeckoCrypto import GeckoCrypto +from settings import crypto_coins class CryptoListDesign (DesignEntity): - def __init__ (self, size, coin, text_size = defaultfontsize): + def __init__ (self, size, crypto, text_size = defaultfontsize): super(CryptoListDesign, self).__init__(size) - self.coin = coin - self.__post_matrix__ = [[]] + self.crypto = crypto self.text_size = text_size def __finish_image__ (self): - self.__fill_post_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) + matrix = self.__get_matrix__() + table_design = TableDesign(self.size, matrix=matrix, col_spacing=5, fontsize = self.text_size, mask=False, truncate_rows=True) self.draw_design(table_design) - def __fill_post_matrix__ (self): - prices, coins = CryptoPrices.__get_prices__(self.coin) - for price, coin in zip(prices, coins): - row = coin + ": $" + str(price) - self.__post_matrix__[0].append(row) + def __get_matrix__ (self): + matrix = [] + coins = self.crypto.get_coins() + for coin in coins: + row = [ coin.symbol.upper(), coin.name, coin.currency + " " + str(coin.price), "% " + str(coin.day_change) ] + matrix.append(row) + return matrix diff --git a/Calendar/CryptoPrices.py b/Calendar/CryptoPrices.py deleted file mode 100644 index a31d950..0000000 --- a/Calendar/CryptoPrices.py +++ /dev/null @@ -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 diff --git a/Calendar/E-Paper.py b/Calendar/E-Paper.py index fef890a..0745d70 100644 --- a/Calendar/E-Paper.py +++ b/Calendar/E-Paper.py @@ -22,7 +22,7 @@ from AgendaListPanel import AgendaListPanel import OwmForecasts import IcalEvents import RssParserPosts -import CryptoPrices +from GeckoCrypto import GeckoCrypto all_locales = locale.locale_alias 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) events_cal = IcalEvents.IcalEvents(ical_urls, highlighted_ical_urls) rss = RssParserPosts.RssParserPosts(rss_feeds) - coin = CryptoPrices.CryptoPrices(crypto_coins) + crypto = GeckoCrypto(crypto_coins) while True: loop_timer.begin_loop() @@ -78,9 +78,9 @@ def main(): else: raise ImportError("choosen_design must be valid (" + choosen_design + ")") - debug.print_line('Getting crypto prices') - coin.reload() - design.add_crypto(coin) + debug.print_line('Fetching crypto prices from coin gecko') + crypto.reload() + design.add_crypto(crypto) debug.print_line("Fetching weather information from open weather map") owm.reload() diff --git a/Calendar/GeckoCrypto.py b/Calendar/GeckoCrypto.py new file mode 100644 index 0000000..051c5f7 --- /dev/null +++ b/Calendar/GeckoCrypto.py @@ -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 } diff --git a/Installer-with-debug.sh b/Installer-with-debug.sh index e4373a6..efa9cbb 100644 --- a/Installer-with-debug.sh +++ b/Installer-with-debug.sh @@ -21,7 +21,7 @@ if [ -z "$option" ]; then fi if [ "$option" = 3 ]; then 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 sudo rm /etc/supervisor/conf.d/E-Paper.conf fi @@ -83,10 +83,12 @@ if [ "$option" = 2 ]; then sudo pip3 install ics sudo pip3 install feedparser sudo pip3 install numpy + sudo pip3 install json pip3 install pyowm pip3 install ics pip3 install feedparser pip3 install numpy + pip3 install json echo -e "\e[1;36m"Finished installing libraries"\e[0m" fi