routes.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. from flask import request, Blueprint
  2. from flask_restful import Resource, Api
  3. from os import path, makedirs
  4. import logging
  5. from bs4 import BeautifulSoup
  6. from models.config import CONTENT_DIR
  7. from models.utils import read_line_md, gen_md_file_dirs, translate, get_now_time
  8. from models.store_locations.templates import store_location_template, amp_img_template
  9. from models.store_locations import STORE_CONTENT_DIR
  10. from models.statics.routes import get_static_imgs_src
  11. store_locations_app = Blueprint('store_locations', __name__)
  12. logger = logging.getLogger(__name__)
  13. api = Api(store_locations_app)
  14. class StoreLocations(Resource):
  15. def __init__(self):
  16. self.exist_img_file_src = []
  17. def get_file_data(self, f_dir):
  18. result = {}
  19. is_amp_img = False
  20. for line in read_line_md(f_dir):
  21. if 'title: ' in line:
  22. result['title'] = line.split('title: ')[-1].replace('"', '').replace('\n', '')
  23. elif 'type: ' in line:
  24. result['type'] = line.split('type: ')[-1].replace('"', '').replace('\n', '')
  25. elif '<amp-img' in line:
  26. is_amp_img = True
  27. elif 'h2 class="mb-4"' in line:
  28. result['store'] = BeautifulSoup(line, 'html.parser').h2.string
  29. elif '營業時間 | ' in line:
  30. result['hour'] = line.split('營業時間 | ')[-1].replace('\n', '')
  31. elif '門市電話 | ' in line:
  32. result['phone'] = BeautifulSoup(line, 'html.parser').a.string
  33. elif '門市地點 | ' in line:
  34. result['location'] = BeautifulSoup(line, 'html.parser').a.string
  35. elif '停車資訊 | ' in line:
  36. result['parking'] = line.split('停車資訊 | ')[-1].replace('\n', '')
  37. if is_amp_img:
  38. if 'src=' in line:
  39. img_src = line.split('src=')[-1].replace('"', '').replace('\n', '')
  40. if img_src in self.exist_img_file_src:
  41. result.setdefault('imgs', []).append(img_src)
  42. if '</amp-img':
  43. is_amp_img = False
  44. return result
  45. def _get_district_name(self, title):
  46. return translate(title.replace('門市', '')).lower().replace(' ', '_')
  47. def _get_amp_img_md(self, imgs, title):
  48. result = ''
  49. for img_src in imgs:
  50. amp_img_md = amp_img_template.format(src=img_src, title=title)
  51. result += amp_img_md + '\n'
  52. return result
  53. def get(self):
  54. result = {}
  55. for zone, dir_ in STORE_CONTENT_DIR.items():
  56. self.exist_img_file_src = get_static_imgs_src('store_{}'.format(zone))
  57. for f_dir in gen_md_file_dirs(dir_):
  58. result.setdefault(zone, []).append(self.get_file_data(f_dir))
  59. return result
  60. def post(self):
  61. def get_store_dir():
  62. DISTRICT_STORES = {'store_north': '北部門市',
  63. 'store_central': '中部門市',
  64. 'store_south': '南部門市',
  65. 'store_east': '東部門市'}
  66. return path.join(CONTENT_DIR,
  67. store_data.get('type'),
  68. DISTRICT_STORES.get(store_data.get('type'), '中部門市'),
  69. store_data.get('title').replace('門市', ''))
  70. update_md = []
  71. for store_data in request.json:
  72. store_location_md = store_location_template.format(
  73. title=store_data.get('title'),
  74. date=get_now_time(),
  75. type=store_data.get('type'),
  76. district=self._get_district_name(store_data.get('title')),
  77. amp_imgs=self._get_amp_img_md(store_data.get('imgs'), store_data.get('title')),
  78. store=store_data.get('store'),
  79. hour=store_data.get('hour'),
  80. phone_without_dash=store_data.get('phone').replace('-', ''),
  81. phone=store_data.get('phone'),
  82. location=store_data.get('location'),
  83. parking=store_data.get('parking'))
  84. store_dir = get_store_dir()
  85. makedirs(store_dir, exist_ok=True)
  86. with open(path.join(store_dir, 'index.md'), 'w') as md:
  87. md.write(store_location_md)
  88. update_md.append(store_location_md)
  89. return update_md
  90. api.add_resource(StoreLocations, '/api/store_locations')