diff --git a/Calendar/AgendaListPanel.py b/Calendar/AgendaListPanel.py index b222d45..44cd6bf 100644 --- a/Calendar/AgendaListPanel.py +++ b/Calendar/AgendaListPanel.py @@ -5,13 +5,12 @@ from settings import general_settings, line_thickness from PIL import ImageDraw from Assets import colors from RssPostListDesign import RssPostListDesign -from CryptoListDesign import CryptoListDesign agenda_ypadding = 5 weatherheader_height = 0.113 seperator_width = line_thickness infolist_size = (1, 0.24) -infolist_padding = 0 +infolist_padding = 2 class AgendaListPanel (PanelDesign): '''Lists upcoming events in chronological order and groups them by days''' @@ -41,24 +40,9 @@ class AgendaListPanel (PanelDesign): self.__draw_seperator__(1-infolist_size[1], colors["fg"]) - def add_tasks (self, tasks): + def add_taks (self, tasks): pass - def add_crypto (self, crypto): - if general_settings["info-area"] != "crypto": - return - - self.info_size = self.__abs_pos__(infolist_size) - pos = (0, self.size[1] - self.info_size[1] + infolist_padding) - - list = CryptoListDesign(self.info_size, crypto) - height = list.get_estimated_height() - list.pos = (pos[0], pos[1] + (self.info_size[1] - height)) - self.draw_design(list) - - self.info_size = (self.size[0], height) - self.__draw_seperator__(list.pos[1] / self.size[1], colors["fg"]) - def __finish_panel__(self): self.__draw_calendar__() if general_settings["weather-info"]: diff --git a/Calendar/CryptoCoin.py b/Calendar/CryptoCoin.py deleted file mode 100644 index 3ad4f29..0000000 --- a/Calendar/CryptoCoin.py +++ /dev/null @@ -1,10 +0,0 @@ -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 5cceb75..fa3b7e5 100644 --- a/Calendar/CryptoInterface.py +++ b/Calendar/CryptoInterface.py @@ -1,16 +1,22 @@ from DataSourceInterface import DataSourceInterface +from datetime import datetime, timezone, timedelta class CryptoInterface(DataSourceInterface): def __init__(self): - self.crypto_coins = [] + self.crypto_prices = [] def reload(self): if self.is_available() == False: return - self.crypto_coins = self.__get_coins__() + self.crypto_prices= self.__get_prices__() + self.sort_prices() - def __get_coins__(self): - raise NotImplementedError("Function needs to be implemented") + def __get_prices__(self): + raise NotImplementedError("Functions needs to be implemented") - def get_coins(self): - return self.crypto_coins + 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 diff --git a/Calendar/CryptoItem.py b/Calendar/CryptoItem.py new file mode 100644 index 0000000..83109fd --- /dev/null +++ b/Calendar/CryptoItem.py @@ -0,0 +1,4 @@ +class CryptoItem(object): + def __init__ (self): + self.name = None + self.value = None diff --git a/Calendar/CryptoListDesign.py b/Calendar/CryptoListDesign.py index 949e8a4..cb1ae0a 100644 --- a/Calendar/CryptoListDesign.py +++ b/Calendar/CryptoListDesign.py @@ -1,36 +1,27 @@ from DesignEntity import DesignEntity from TableDesign import TableDesign from Assets import defaultfontsize -from GeckoCrypto import GeckoCrypto -from settings import crypto_coins +from CryptoPrices import CryptoPrices +from settings import crypto_coins as cryptos -xpadding = 5 class CryptoListDesign (DesignEntity): - def __init__ (self, size, crypto, text_size = defaultfontsize): + def __init__ (self, size, coin, text_size = defaultfontsize): super(CryptoListDesign, self).__init__(size) - self.crypto = crypto + self.coin = coin + self.__post_matrix__ = [] self.text_size = text_size - self.matrix = self.__get_matrix__() def __finish_image__ (self): - col_spacing = 10 - if len(self.matrix) > 0: - col_spacing = (self.size[0] / len(self.matrix[0])) * 0.5 - - table_design = TableDesign(self.size, matrix=self.matrix, col_spacing=col_spacing, fontsize = self.text_size, mask=False, truncate_rows=True) - table_design.pos = (xpadding, 0) + self.__fill_post_matrix__() + table_design = TableDesign(self.size, column_horizontal_alignments = ["Right"], line_spacing=2, col_spacing=3, matrix=self.__post_matrix__, fontsize = self.text_size, mask=False, wrap=True, truncate_rows=True) self.draw_design(table_design) - 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 + def format_price(self,prices,coins,daychange): + return [ '', coins,' | ','$',prices,' | %',daychange] - def get_estimated_height(self): - line_height = self.text_size * 1.25 - height = line_height * len(self.matrix) - return height + def __fill_post_matrix__ (self): + prices, coins, daychanges = CryptoPrices.__get_prices__(self.coin) + for price, coin, daychange in zip(prices, coins, daychanges): + row=self.format_price(str(price), coin, str(daychange)) + self.__post_matrix__.append(row) diff --git a/Calendar/CryptoPrices.py b/Calendar/CryptoPrices.py new file mode 100644 index 0000000..7c4256c --- /dev/null +++ b/Calendar/CryptoPrices.py @@ -0,0 +1,32 @@ +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=[] + daychange=[] + for coin in self.coins: + data = urllib.request.urlopen("https://api.coingecko.com/api/v3/simple/price?ids="+coin+"&vs_currencies=USD&include_market_cap=false&include_24hr_vol=false&include_24hr_change=true&include_last_updated_at=false").read() + dataJSON = json.loads(data.decode('utf-8')) + raw = dataJSON[coin]["usd"] + raw24h = dataJSON[coin]["usd_24h_change"] + price.append(math.ceil(raw*100)/100) + daychange.append(math.ceil(raw24h*100)/100) + name.append(coin) + return price,name,daychange diff --git a/Calendar/DayListPanel.py b/Calendar/DayListPanel.py index e271208..6530ce9 100644 --- a/Calendar/DayListPanel.py +++ b/Calendar/DayListPanel.py @@ -10,7 +10,6 @@ from DayRowDesign import DayRowDesign from RssPostListDesign import RssPostListDesign from CryptoListDesign import CryptoListDesign from settings import line_thickness -from math import ceil todayheader_pos = (0,0) todayheader_size = (1,0.25) @@ -49,16 +48,14 @@ class DayListPanel (PanelDesign): for row in self.__day_rows__: row.add_rssfeed(rss) if general_settings["info-area"] is "rss": - self.__day_rows__ = self.__day_rows__[:-infoarea_replacedrowscount] self.__draw_rss_infoarea__(rss) def add_crypto (self, crypto): + for row in self.__day_rows__: + row.add_crypto(crypto) if general_settings["info-area"] is "crypto": self.__draw_crypto_infoarea__(crypto) - def add_tasks (self, tasks): - pass - def __draw_rss_infoarea__ (self, rss): height = infoarea_replacedrowscount * self.dayrow_size[1] * self.size[1] - rss_y_padding ypos = self.size[1] - height @@ -76,12 +73,8 @@ class DayListPanel (PanelDesign): pos = (0, ypos) design = CryptoListDesign(size, crypto) - acutal_height = design.get_estimated_height() - design.pos = (pos[0], pos[1] + (height - acutal_height)) + design.pos = pos self.draw_design(design) - - replaced_rows = ceil(acutal_height / (self.dayrow_size[1] * self.size[1])) - self.__day_rows__ = self.__day_rows__[:-replaced_rows] def __draw_day_rows__ (self): following_days = self.__get_following_days__() @@ -104,6 +97,9 @@ class DayListPanel (PanelDesign): row_height = max_area_height / self.dayrow_count self.dayrow_size = (1, row_height / self.size[1]) + if general_settings["info-area"] in ["rss"] or ["crypto"]: + self.dayrow_count -= infoarea_replacedrowscount + def __get_following_days__(self): following_days = [] for i in range(self.dayrow_count): @@ -117,7 +113,7 @@ class DayListPanel (PanelDesign): def __draw_lines__(self): positions = [] - for i in range(len(self.__day_rows__)): + for i in range(self.dayrow_count + 1): positions.append(self.__get_day_row_pos__(i)[1]) for ypos in positions: line_start = (0, ypos) diff --git a/Calendar/DayRowDesign.py b/Calendar/DayRowDesign.py index 7f6d44d..63eac57 100644 --- a/Calendar/DayRowDesign.py +++ b/Calendar/DayRowDesign.py @@ -37,6 +37,9 @@ class DayRowDesign (DesignEntity): def add_rssfeed (self, rss): pass + + def add_crypto (self, crypto): + pass def __draw_event_list__ (self, calendar): number_width = daynumber_y_size[0] * self.size[1] diff --git a/Calendar/DayViewPanel.py b/Calendar/DayViewPanel.py index a35447c..124f21f 100644 --- a/Calendar/DayViewPanel.py +++ b/Calendar/DayViewPanel.py @@ -49,7 +49,7 @@ class DayViewPanel (PanelDesign): self.__draw_rss_feed__(rss) self.__draw_infoarea_line__() - def add_crypto (self, crypto): + def add_cryptofeed (self, crypto): if general_settings["info-area"] == "crypto": self.__draw_crypto_feed__(crypto) self.__draw_infoarea_line__() @@ -77,8 +77,7 @@ class DayViewPanel (PanelDesign): pos = (0, self.size[1] - size[1]) crypto = CryptoListDesign(size, crypto) - acutal_height = crypto.get_estimated_height() - crypto.pos = (pos[0], pos[1] + (height - acutal_height)) + crypto.pos = pos self.draw_design(crypto) @@ -91,7 +90,7 @@ class DayViewPanel (PanelDesign): events.pos = pos self.draw_design(events) - def add_tasks (self, tasks): + def add_taks (self, tasks): pass def __finish_panel__ (self): diff --git a/Calendar/E-Paper.py b/Calendar/E-Paper.py index 19db1a1..fef890a 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 GeckoCrypto +import CryptoPrices 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) - crypto = GeckoCrypto.GeckoCrypto(crypto_coins) + coin = CryptoPrices.CryptoPrices(crypto_coins) while True: loop_timer.begin_loop() @@ -78,6 +78,10 @@ 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 weather information from open weather map") owm.reload() design.add_weather(owm) @@ -90,10 +94,6 @@ def main(): rss.reload() design.add_rssfeed(rss) - debug.print_line('Fetching crypto prices from coin gecko') - crypto.reload() - design.add_crypto(crypto) - debug.print_line("\nStarting to render") for i, output in enumerate(output_adapters): try: diff --git a/Calendar/GeckoCrypto.py b/Calendar/GeckoCrypto.py deleted file mode 100644 index 3f19d17..0000000 --- a/Calendar/GeckoCrypto.py +++ /dev/null @@ -1,62 +0,0 @@ -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://api.coingecko.com/api/v3/ping" -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_ids = coins - self.metadata = None - super(GeckoCrypto, self).__init__() - - def is_available(self): - try: - urlopen(api_test_url) - return True - except: - return False - - def __get_coins__(self): - self.__prepare_metadata__() - coins = [] - for id in self.coin_ids: - try: - data = urlopen(api_price_url + "?include_24hr_change=true&ids=" + self.metadata[id]['id'] + "&vs_currencies=" + price_currency).read() - dataJSON = json.loads(data.decode('utf-8')) - raw = dataJSON[id][price_currency] - price = math.ceil(raw*100) / 100 - change = dataJSON[id]['usd_24h_change'] - - coins.append(self.__build_coin__(id, price, change)) - except: - print("Gecko-Error [" + id + "]") - return coins - - def __build_coin__(self, id, value, change): - coin = CryptoCoin() - - coin.name = self.metadata[id]['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[id]['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_ids } diff --git a/Calendar/MonthOvPanel.py b/Calendar/MonthOvPanel.py index b377a32..b580056 100644 --- a/Calendar/MonthOvPanel.py +++ b/Calendar/MonthOvPanel.py @@ -71,9 +71,6 @@ class MonthOvPanel (PanelDesign): if general_settings["info-area"] is "crypto": self.__draw_crypto_post_list_to_bottom__(crypto) - def add_tasks (self, tasks): - pass - def add_calendar (self, calendar): if general_settings["highlight-event-days"]: month_events = list(set([ (event.begin_datetime.day, event.begin_datetime.month, event.begin_datetime.year) for event in calendar.get_month_events()])) @@ -95,10 +92,8 @@ class MonthOvPanel (PanelDesign): month_pos = self.__abs_pos__(monthovposition) month_height = self.month_block.get_real_height() size = (self.size[0], self.size[1] - (month_pos[1] + month_height + self.weather_header_height)) - info_list = CryptoListDesign(size, crypto) - list_height = info_list.get_estimated_height() - info_list.pos = (int(month_pos[0]), month_pos[1] + month_height + self.weather_header_height + (size[1] - list_height)) + info_list.pos = (int(month_pos[0]), month_pos[1] + month_height + self.weather_header_height) self.draw_design(info_list) def __draw_event_list_to_bottom__ (self, calendar): diff --git a/Calendar/MonthViewPanel.py b/Calendar/MonthViewPanel.py index 4fbab24..294520d 100644 --- a/Calendar/MonthViewPanel.py +++ b/Calendar/MonthViewPanel.py @@ -8,7 +8,6 @@ from TableDesign import TableDesign from DayBoxDesign import DayBoxDesign from RssPostListDesign import RssPostListDesign from WeatherHeaderDesign import WeatherHeaderDesign -from CryptoListDesign import CryptoListDesign weather_height = 0.113 info_height = 0.25 @@ -33,7 +32,7 @@ class MonthViewPanel (PanelDesign): def __init_sizes__(self): self.weather_height = 0 self.info_height = 0 - if general_settings["info-area"] in ["events", "rss", "crypto"]: + if general_settings["info-area"] in ["events", "rss"]: self.info_height = info_height if general_settings["weather-info"]: self.weather_height = weather_height @@ -75,21 +74,9 @@ class MonthViewPanel (PanelDesign): rss.pos = pos self.draw_design(rss) - def add_tasks (self, tasks): + def add_taks (self, tasks): pass - def add_crypto (self, crypto): - if general_settings["info-area"] == "crypto": - self.__draw_crypto__(crypto) - - def __draw_crypto__(self, crypto): - size = (self.size[0], self.size[1] * self.info_height) - pos = (0, self.size[1] - size[1]) - - crypto = CryptoListDesign(size, crypto) - crypto.pos = (pos[0],pos[1] + (size[1] - crypto.get_estimated_height())) - self.draw_design(crypto) - def __finish_panel__(self): self.__draw_days__() diff --git a/Calendar/PanelDesign.py b/Calendar/PanelDesign.py index 361d57c..53e65dc 100644 --- a/Calendar/PanelDesign.py +++ b/Calendar/PanelDesign.py @@ -10,19 +10,16 @@ class PanelDesign (DesignEntity): self.start_timestamp = datetime.now() def add_weather (self, weather): - raise NotImplementedError("Function needs to be implemented") + raise NotImplementedError("Functions needs to be implemented") def add_calendar (self, calendar): - raise NotImplementedError("Function needs to be implemented") + raise NotImplementedError("Functions needs to be implemented") def add_rssfeed (self, rss): - raise NotImplementedError("Function needs to be implemented") + raise NotImplementedError("Functions needs to be implemented") - def add_tasks (self, tasks): - raise NotImplementedError("Function needs to be implemented") - - def add_crypto (self, crypto): - raise NotImplementedError("Function needs to be implemented") + def add_taks (self, tasks): + raise NotImplementedError("Functions needs to be implemented") def __finish_panel__(self): pass diff --git a/Calendar/settings.py.sample b/Calendar/settings.py.sample index 46e0db6..e77bed7 100644 --- a/Calendar/settings.py.sample +++ b/Calendar/settings.py.sample @@ -11,7 +11,7 @@ rss_feeds = [ "http://feeds.bbci.co.uk/news/world/rss.xml#" ] -crypto_coins = [ +crypto_coins = [ #limit 8 coins @ font_size =14 "bitcoin", "litecoin", "ethereum", diff --git a/Installer-with-debug.sh b/Installer-with-debug.sh index efa9cbb..e4373a6 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 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 + 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,12 +83,10 @@ 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