api.py 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. # -*- coding: utf-8 -*-
  2. from __future__ import absolute_import, unicode_literals
  3. import json
  4. def parse_line(line, category):
  5. name = line.split('](')[0][3:]
  6. description = line.split(') ')[1][2:]
  7. url = line.split('](')[1].split(') ')[0]
  8. return {
  9. 'name': name,
  10. 'description': description,
  11. 'url': url,
  12. 'category': category,
  13. }
  14. def parse_apps(readme):
  15. section = readme.split('## Apps\n\n')[1].split('\n\n## Tools')[0]
  16. subsections = section.split('### ')[1:]
  17. apps = []
  18. for subsection in subsections:
  19. split_section = subsection.split('\n\n')
  20. section_title = split_section[0]
  21. for line in split_section[1].split('\n'):
  22. app = parse_line(line, section_title)
  23. apps.append(app)
  24. return apps
  25. def parse_tools(readme):
  26. section = readme.split('## Tools\n\n')[1].split('\n\n## Resources')[0]
  27. tools = []
  28. for line in section.split('\n'):
  29. tool = parse_line(line, '')
  30. tools.append(tool)
  31. return tools
  32. def parse_readme(readme):
  33. return {
  34. 'apps': parse_apps(readme),
  35. 'tools': parse_tools(readme),
  36. }
  37. if __name__ == '__main__':
  38. readme = open('README.md', 'r').read()
  39. readme_payload = parse_readme(readme)
  40. with open('./dist/api/v1/readme.json', mode='w+', encoding='utf-8') as f:
  41. f.write(json.dumps(readme_payload, indent=True))