Queer European MD passionate about IT

api_helper.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. """Get and parse Telegram API webpage."""
  2. # Standard library modules
  3. import asyncio
  4. import logging
  5. # Third party modules
  6. import aiohttp
  7. from bs4 import BeautifulSoup
  8. api_url = "https://core.telegram.org/bots/api"
  9. class TelegramApiMethod(object):
  10. """Telegram bot API method."""
  11. def __init__(self, name, description, table):
  12. """Initialize object with name, description and table data."""
  13. self._name = name
  14. self._description = description
  15. self._table = table
  16. @property
  17. def name(self):
  18. """Return method name."""
  19. return self._name
  20. @property
  21. def description(self):
  22. """Return method description."""
  23. return self._description
  24. @property
  25. def table(self):
  26. """Return method parameters table."""
  27. return self._table
  28. def get_parameters_from_table(self):
  29. """Extract parameters from API table."""
  30. result = ''
  31. if self.table is None:
  32. return "No parameters"
  33. rows = self.table.tbody.find_all('tr')
  34. if rows is None:
  35. rows = []
  36. for row in rows:
  37. result += '------\n'
  38. columns = row.find_all('td')
  39. if columns is None:
  40. columns = []
  41. for column in columns:
  42. result += f'| {column.text.strip()} |'
  43. result += '\n'
  44. result += '\n'
  45. return result
  46. async def main(loop=None, filename=None):
  47. """Get information from Telegram bot API webpage."""
  48. if loop is None:
  49. loop = asyncio.get_event_loop()
  50. async with aiohttp.ClientSession(
  51. loop=loop,
  52. timeout=aiohttp.ClientTimeout(
  53. total=100
  54. )
  55. ) as session:
  56. async with session.get(
  57. api_url
  58. ) as response:
  59. webpage = BeautifulSoup(
  60. await response.text(),
  61. "html.parser"
  62. )
  63. if filename is not None:
  64. with open(filename, 'w') as _file:
  65. _file.write(webpage.decode())
  66. for method in webpage.find_all("h4"):
  67. method_name = method.text
  68. description = ''
  69. parameters_table = None
  70. for tag in method.next_siblings:
  71. if tag.name is None:
  72. continue
  73. if tag.name == 'h4':
  74. break # Stop searching in siblings if <h4> is found
  75. if tag.name == 'table':
  76. parameters_table = tag
  77. break # Stop searching in siblings if <table> is found
  78. description += tag.get_text()
  79. if method_name and method_name[0] == method_name[0].lower():
  80. method = TelegramApiMethod(
  81. method_name, description, parameters_table
  82. )
  83. print(
  84. "NAME\n\t{m.name}\n"
  85. "DESCRIPTION\n\t{m.description}\n"
  86. f"TABLE\n\t{method.get_parameters_from_table()}\n\n".format(
  87. m=method
  88. )
  89. )
  90. if __name__ == '__main__':
  91. loop = asyncio.get_event_loop()
  92. loop.run_until_complete(main(loop=loop, filename='prova.txt'))
  93. logging.info("Done!")