routes.py 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. from flask import request, Blueprint
  2. from flask_restful import Resource, Api
  3. import logging
  4. from bs4 import BeautifulSoup
  5. from models.config import HOMEPAGE_DIR, ROOM_PLANNER_DIR
  6. from models.utils.parsers import get_section_parser, SectionParser
  7. from models.utils.validators import is_valid_section
  8. from models.utils import read_line_md, write_md
  9. manages_app = Blueprint('manages', __name__)
  10. api = Api(manages_app)
  11. logger = logging.getLogger(__name__)
  12. DIR = {'home': HOMEPAGE_DIR, 'room_planner': ROOM_PLANNER_DIR}
  13. class RequiredData():
  14. def __init__(self):
  15. self.is_amp_img, self.is_amp_youtube = False, False
  16. def load(self, section_data, text):
  17. if '<b' in text:
  18. soup = BeautifulSoup(text, "html.parser")
  19. section_data.setdefault('b', []).append(soup.b.string)
  20. elif '<a ' in text and '</a>' in text:
  21. soup = BeautifulSoup(text, "html.parser")
  22. section_data.setdefault('a', []).append(soup.a.string)
  23. elif '<p' in text:
  24. soup = BeautifulSoup(text, "html.parser")
  25. section_data.setdefault('p', []).append(soup.p.string)
  26. elif '<amp-img' in text:
  27. self.is_amp_img = True
  28. elif '<amp-youtube' in text:
  29. self.is_amp_youtube = True
  30. elif 'card-text' in text:
  31. soup = BeautifulSoup(text, "html.parser")
  32. section_data.setdefault('card_text', []).append(soup.div.string)
  33. elif 'title mb' in text:
  34. soup = BeautifulSoup(text, "html.parser")
  35. section_data.setdefault('title_mb_text', []).append(soup.div.string)
  36. if self.is_amp_img:
  37. if 'src' in text:
  38. src = text.split('src=')[-1].replace('"', '').replace('\n', '')
  39. section_data.setdefault('img', []).append({'src': src})
  40. if '</amp-img' in text:
  41. self.is_amp_img = False
  42. elif self.is_amp_youtube:
  43. if 'data-videoid' in text:
  44. videoid = text.split('data-videoid')[-1].replace('"', '').replace('\n', '')
  45. section_data.setdefault('yt_video', []).append({'videoid': videoid})
  46. if '</amp-youtube' in text:
  47. self.is_amp_youtube = False
  48. class ManageData(Resource):
  49. def get(self):
  50. result = []
  51. require_data = RequiredData()
  52. for line in read_line_md(DIR.get(request.args.get('page', type=str), [])):
  53. if '<section' in line:
  54. soup = BeautifulSoup(line, "html.parser")
  55. section_class = '_'.join(soup.find_all(class_=True)[0]['class'])
  56. result.append({'sectionClass': section_class})
  57. elif not result:
  58. # for skip the line before fist section
  59. continue
  60. else:
  61. require_data.load(result[-1], line)
  62. return result
  63. def post(self):
  64. is_target_class_section = False
  65. content = ''
  66. section_parser = SectionParser(request.json)
  67. for line in read_line_md(DIR.get(request.args.get('page', type=str), [])):
  68. if is_target_class_section:
  69. if '</section>' in line:
  70. is_target_class_section = False
  71. content = section_parser.update(content, line)
  72. else:
  73. content += line
  74. if is_valid_section(request.args.get('section', type=str), line):
  75. is_target_class_section = True
  76. section_parser = get_section_parser(
  77. request.args.get('section', type=str))(request.json)
  78. write_md(DIR.get(request.args.get('page', type=str)), content)
  79. return {'content': content}
  80. api.add_resource(ManageData, '/api/manages/data')