api.py 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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_sites(readme):
  33. section = readme.split('## Open-source sites\n\n')[1].split('\n\n## Contribute')[0]
  34. sites = []
  35. for line in section.split('\n'):
  36. site = parse_line(line, '')
  37. print(site)
  38. sites.append(site)
  39. return sites
  40. def parse_readme(readme):
  41. return {
  42. 'apps': parse_apps(readme),
  43. 'tools': parse_tools(readme),
  44. 'sites': parse_sites(readme),
  45. }
  46. if __name__ == '__main__':
  47. readme = open('README.md', 'r').read()
  48. readme_payload = parse_readme(readme)
  49. with open('./dist/api/v1/readme.json', mode='w+', encoding='utf-8') as f:
  50. f.write(json.dumps(readme_payload, indent=True))