Skip to content

Commit

Permalink
Get only the first 10 results from PIO library API
Browse files Browse the repository at this point in the history
Added option to get the Next and Previous Page
Added string translation in language files
  • Loading branch information
gepd committed Jul 11, 2020
1 parent 58f0e53 commit 3b81be5
Show file tree
Hide file tree
Showing 10 changed files with 87 additions and 18 deletions.
6 changes: 6 additions & 0 deletions languages/de.lang
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down
6 changes: 6 additions & 0 deletions languages/en.lang
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down
6 changes: 6 additions & 0 deletions languages/es.lang
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down
6 changes: 6 additions & 0 deletions languages/fr.lang
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down
6 changes: 6 additions & 0 deletions languages/it.lang
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down
6 changes: 6 additions & 0 deletions languages/ko.lang
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down
6 changes: 6 additions & 0 deletions languages/pl.lang
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down
6 changes: 6 additions & 0 deletions languages/pt_br.lang
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down
6 changes: 6 additions & 0 deletions languages/zh.lang
Original file line number Diff line number Diff line change
Expand Up @@ -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 "选择示例"

Expand Down
51 changes: 33 additions & 18 deletions libraries/libraries.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand All @@ -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)
Expand All @@ -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)

Expand All @@ -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])
Expand All @@ -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'), '')
Expand Down

0 comments on commit 3b81be5

Please sign in to comment.