main.py 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. import aiohttp
  2. import asyncio
  3. from bs4 import BeautifulSoup
  4. from pyfzf.pyfzf import FzfPrompt
  5. HREFX = 'https://pypi.org/search/?q='
  6. PYPIHREF = 'https://pypi.org'
  7. async def check(x, y):
  8. for i in range(len(y)):
  9. if x == y[i]:
  10. return i
  11. break
  12. else:
  13. continue
  14. return 404 # Ошибка
  15. async def get_html(href):
  16. async with aiohttp.ClientSession() as session:
  17. async with session.get(url=href) as resp:
  18. return await resp.read()
  19. async def main():
  20. modulename = input('Введите название модуля который хотите найти: ').replace(' ', '+')
  21. href = f'{HREFX}{modulename}'
  22. html = await get_html(href)
  23. soup = BeautifulSoup(html, 'lxml')
  24. packages = soup.find('ul', class_='unstyled')
  25. lis = packages.find_all('li')
  26. hrefsofresults = []
  27. namesofresults = []
  28. for li in lis:
  29. a = li.find('a')
  30. ahref = a.get('href')
  31. hrefsofresults.append(PYPIHREF + ahref)
  32. spanname = a.find('span', class_='package-snippet__name').text
  33. namesofresults.append(spanname)
  34. fzf = FzfPrompt()
  35. selectedmodule = fzf.prompt(namesofresults)[0]
  36. i = await check(selectedmodule, namesofresults)
  37. href = hrefsofresults[i]
  38. html = await get_html(href)
  39. soup = BeautifulSoup(html, 'lxml')
  40. header_pip_instructions = soup.find('p', class_='package-header__pip-instructions')
  41. comand = header_pip_instructions.find('span', id='pip-command').text
  42. print('============================\n\nКомманда для установки:')
  43. print(f'---- {comand}\n\n============================\n')
  44. releasedp = soup.find('p', class_='package-header__date')
  45. releaseddate = releasedp.find('time').text
  46. print('Последнее обновление модуля:')
  47. print(f'{releaseddate}\n\n============================\n')
  48. try:
  49. homepagea = soup.find('a', class_='vertical-tabs__tab vertical-tabs__tab--with-icon vertical-tabs__tab--condensed')
  50. homepage = homepagea.get('href')
  51. print('Ссылка на домашнюю страницу:')
  52. print(f'---- {homepage}')
  53. except:
  54. print('У данного модуля нет домашней страницы!')
  55. if __name__ == '__main__':
  56. asyncio.run(main())