From 3b81be5fc1bf8afd33dfd03ba93c5d8f76728ec8 Mon Sep 17 00:00:00 2001 From: gepd Date: Sat, 11 Jul 2020 13:05:09 -0400 Subject: [PATCH] Get only the first 10 results from PIO library API Added option to get the Next and Previous Page Added string translation in language files --- languages/de.lang | 6 +++++ languages/en.lang | 6 +++++ languages/es.lang | 6 +++++ languages/fr.lang | 6 +++++ languages/it.lang | 6 +++++ languages/ko.lang | 6 +++++ languages/pl.lang | 6 +++++ languages/pt_br.lang | 6 +++++ languages/zh.lang | 6 +++++ libraries/libraries.py | 51 +++++++++++++++++++++++++++--------------- 10 files changed, 87 insertions(+), 18 deletions(-) diff --git a/languages/de.lang b/languages/de.lang index 3aded38..e42427f 100644 --- a/languages/de.lang +++ b/languages/de.lang @@ -309,6 +309,12 @@ msgstr "Hochladen per OTA ist nur Espressif (ESP8266) Plattform. Wählen Sie ein msgid "select_library" msgstr "Bibliothek auswählen" +msgid "library_page_previous_{0}" +msgstr "<< {0} Previous Page" + +msgid "library_page_next_{0}" +msgstr "Next Page {0} >>" + msgid "select_example" msgstr "Beispiel auswählen" diff --git a/languages/en.lang b/languages/en.lang index 7d14218..05f7af1 100644 --- a/languages/en.lang +++ b/languages/en.lang @@ -306,6 +306,12 @@ msgstr "Select a Port From The List" msgid "ota_error_platform" msgstr "OTA Upload is only available for Espressif (ESP8266) platform. Select other serial port or change the current environment" +msgid "library_page_previous_{0}" +msgstr "<< {0} Previous Page" + +msgid "library_page_next_{0}" +msgstr "Next Page {0} >>" + msgid "select_library" msgstr "Select a Library" diff --git a/languages/es.lang b/languages/es.lang index 30513af..a7b3734 100644 --- a/languages/es.lang +++ b/languages/es.lang @@ -309,6 +309,12 @@ msgstr "Cargas vía OTA sólo están disponibles para plataformas Espressif (ESP msgid "select_library" msgstr "Selecciona una Librería" +msgid "library_page_previous_{0}" +msgstr "<< {0} Página Anterior" + +msgid "library_page_next_{0}" +msgstr "Página Siguiente {0} >>" + msgid "select_example" msgstr "Selecciona un Ejemplo" diff --git a/languages/fr.lang b/languages/fr.lang index c636af2..5d9374e 100644 --- a/languages/fr.lang +++ b/languages/fr.lang @@ -309,6 +309,12 @@ msgstr "OTA Upload are only available for Espressif (ESP8266) platforms currentl msgid "select_library" msgstr "Select a Library" +msgid "library_page_previous_{0}" +msgstr "<< {0} Previous Page" + +msgid "library_page_next_{0}" +msgstr "Next Page {0} >>" + msgid "select_example" msgstr "Select an Example" diff --git a/languages/it.lang b/languages/it.lang index 2638272..5108378 100644 --- a/languages/it.lang +++ b/languages/it.lang @@ -309,6 +309,12 @@ msgstr "Gli aggiornamenti OTA sono al momento disponibili solo per piattaforme E msgid "select_library" msgstr "Scegli una Libreria" +msgid "library_page_previous_{0}" +msgstr "<< {0} Previous Page" + +msgid "library_page_next_{0}" +msgstr "Next Page {0} >>" + msgid "select_example" msgstr "Scegli un Esempio" diff --git a/languages/ko.lang b/languages/ko.lang index 1762580..1bade1e 100644 --- a/languages/ko.lang +++ b/languages/ko.lang @@ -309,6 +309,12 @@ msgstr "OTA Upload are only available for Espressif (ESP8266) platforms currentl msgid "select_library" msgstr "Select a Library" +msgid "library_page_previous_{0}" +msgstr "<< {0} Previous Page" + +msgid "library_page_next_{0}" +msgstr "Next Page {0} >>" + msgid "select_example" msgstr "Select an Example" diff --git a/languages/pl.lang b/languages/pl.lang index 8081654..c378459 100644 --- a/languages/pl.lang +++ b/languages/pl.lang @@ -306,6 +306,12 @@ msgstr "OTA Upload jest dostępny tylko dla platform Espressif (ESP8266)" msgid "select_library" msgstr "Wybierz Bibliotekę" +msgid "library_page_previous_{0}" +msgstr "<< {0} Previous Page" + +msgid "library_page_next_{0}" +msgstr "Next Page {0} >>" + msgid "select_example" msgstr "Wybierz Przykład" diff --git a/languages/pt_br.lang b/languages/pt_br.lang index 874c922..c6b18b5 100644 --- a/languages/pt_br.lang +++ b/languages/pt_br.lang @@ -310,6 +310,12 @@ msgstr "O upload da OTA só Está Dsponível Para a Plataforma Espressif (ESP826 msgid "select_library" msgstr "Selecione a Biblioteca" +msgid "library_page_previous_{0}" +msgstr "<< {0} Próxima página" + +msgid "library_page_next_{0}" +msgstr "Pagina anterior {0} >>" + msgid "select_example" msgstr "Selecione o Examplo" diff --git a/languages/zh.lang b/languages/zh.lang index ab79272..de7b97f 100644 --- a/languages/zh.lang +++ b/languages/zh.lang @@ -309,6 +309,12 @@ msgstr "在线上传只支持 Espressif (ESP8266) 平台, 选择其他串口或 msgid "select_library" msgstr "选择库" +msgid "library_page_previous_{0}" +msgstr "<< {0} Previous Page" + +msgid "library_page_next_{0}" +msgstr "Next Page {0} >>" + msgid "select_example" msgstr "选择示例" diff --git a/libraries/libraries.py b/libraries/libraries.py index e8437b3..a0c6591 100644 --- a/libraries/libraries.py +++ b/libraries/libraries.py @@ -40,6 +40,8 @@ def __init__(self, window=None, view=None, feedback=True): self.view = self.window.active_view() self.lib_file_path = getLibrariesFileDataPath() self.quick_list = [] + self.page = 1 + self.keyword = "" self.cwd = None self.dprint = None @@ -74,11 +76,13 @@ def download_list_async(self, keyword): Arguments: keyword {str} -- keyword to be search """ - thread = Thread(target=self.download_list, args=(keyword,)) + self.keyword = keyword + + thread = Thread(target=self.download_list) thread.start() ThreadProgress(thread, self.translate('searching'), '') - def download_list(self, keyword): + def download_list(self): """PlatformIO API Search a library in the platformio API api.platformio.org. @@ -90,7 +94,8 @@ def download_list(self, keyword): Keyword to search the library in the platformio API """ request = {} - request['query'] = keyword + request['query'] = self.keyword + request['page'] = self.page query = urlencode(request) url = 'http://api.platformio.org/lib/search?{0}'.format(query) @@ -99,26 +104,23 @@ def download_list(self, keyword): response = urlopen(req) response_list = loads(response.read().decode()) - nloop = response_list['total'] / response_list['perpage'] - if(nloop > 1): - - nloop = int(nloop) + 1 if nloop > int(nloop) else int(nloop) - for page in range(2, nloop + 1): - - request['page'] = page - query = urlencode(request) - req = Request(url, headers=get_headers()) - - response = urlopen(req) - page_next = loads(response.read().decode()) - for item_next in page_next['items']: - response_list['items'].append(item_next) + pages = response_list['total'] / response_list['perpage'] + page_previous = self.page - 1 + page_next = self.page + 1 if(len(response_list['items']) == 0): self.quick_list.append([self.translate('none_lib_found')]) else: self.quicked(response_list['items']) self.quick_list.insert(0, [self.translate('select_library').upper()]) + + if(self.page > 1): + caption = self.translate("library_page_previous_{0}", page_previous) + self.quick_list.insert(1, [caption, page_previous]) + + if(self.page < pages): + caption = self.translate("library_page_next_{0}", page_next) + self.quick_list.insert(len(self.quick_list), [caption, page_next]) quick_panel(self.quick_list, self.library_install_async) @@ -140,7 +142,10 @@ def quicked(self, source_list): frameworks = '' for framework in item['frameworks']: - frameworks += framework + ' ' + try: + frameworks += framework + ' ' + except: + frameworks += framework["name"] + ' ' info = "{0} | {1}".format(id, frameworks) quick_list.append([name, description, info]) @@ -157,9 +162,19 @@ def library_install_async(self, selected): Arguments: selected {int} -- user selection index """ + list_selection = self.quick_list[selected]; + if(selected <= 0): return + try: + page = int(list_selection[1]) + self.page = page + self.download_list_async(self.keyword) + return + except: + pass + thread = Thread(target=self.library_install, args=(selected,)) thread.start() ThreadProgress(thread, self.translate('installing'), '')