api.py 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. # -*- coding: utf-8 -*-
  2. from __future__ import absolute_import, unicode_literals
  3. import json
  4. def parse_line(line, category):
  5. print(line)
  6. name = line.split('](')[0][3:]
  7. url = line.split('](')[1].split(')')[0]
  8. description = '' if line[-1] == ')' else line.split(') ')[1][2:]
  9. return {
  10. 'name': name,
  11. 'description': description,
  12. 'url': url,
  13. 'category': category,
  14. }
  15. def parse_apps(readme):
  16. section = readme.split('## Apps\n\n')[1].split('\n\n## Tools')[0]
  17. subsections = section.split('### ')[1:]
  18. apps = []
  19. for subsection in subsections:
  20. split_section = subsection.split('\n\n')
  21. section_title = split_section[0]
  22. for line in split_section[1].split('\n'):
  23. app = parse_line(line, section_title)
  24. apps.append(app)
  25. return apps
  26. def parse_tools(readme):
  27. section = readme.split('## Tools\n\n')[1].split('\n\n## Resources')[0]
  28. tools = []
  29. for line in section.split('\n'):
  30. tool = parse_line(line, '')
  31. tools.append(tool)
  32. return tools
  33. def parse_resources(readme):
  34. section = readme.split('## Resources\n\n')[1].split('\n\n## Community')[0]
  35. subsections = section.split('### ')[1:]
  36. resources = []
  37. for subsection in subsections:
  38. split_section = subsection.split('\n\n')
  39. section_title = split_section[0]
  40. for line in split_section[1].split('\n'):
  41. resource = parse_line(line, section_title)
  42. resources.append(resource)
  43. return resources
  44. def parse_sites(readme):
  45. section = readme.split('## Open-source sites\n\n')[1].split('\n\n## Contribute')[0]
  46. sites = []
  47. for line in section.split('\n'):
  48. site = parse_line(line, '')
  49. sites.append(site)
  50. return sites
  51. def parse_readme(readme):
  52. return {
  53. 'apps': parse_apps(readme),
  54. 'tools': parse_tools(readme),
  55. 'resources': parse_resources(readme),
  56. 'sites': parse_sites(readme),
  57. }
  58. if __name__ == '__main__':
  59. readme = open('README.md', 'r').read()
  60. readme_payload = parse_readme(readme)
  61. with open('./dist/api/v1/readme.json', mode='w+', encoding='utf-8') as f:
  62. f.write(json.dumps(readme_payload, indent=True))