Skip to content

Commit

Permalink
refactor: 메서드 위치 및 변수명 변경 (#68)
Browse files Browse the repository at this point in the history
* refactor: 메서드 위치 및 변수명 변경

* refactor: 불필요한 주석 제거
  • Loading branch information
GanziDaeyong authored Feb 26, 2024
1 parent 91e1c3a commit d311a43
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 60 deletions.
11 changes: 6 additions & 5 deletions crawlers/base_crawler.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,14 +178,15 @@ def normalize(self, meal, **kwargs):
meal = normalizer_cls().normalize(meal, **kwargs)
return meal

def is_meal_name(self, name):
name = text_normalizer(name, True)
if not name:
def is_meal_name_when_normalized(self, name):
normalized_name = text_normalizer(name, True)
if not normalized_name:
return False
return name and all(re.match(".*" + p + ".*", name) is None for p in self.not_meal)
is_meal_name = all(re.match(".*" + p + ".*", normalized_name) is None for p in self.not_meal)
return is_meal_name

def found_meal(self, meal):
if meal and self.is_meal_name(meal.name):
if meal and self.is_meal_name_when_normalized(meal.name):
self.meals.append(meal)

@abstractmethod
Expand Down
8 changes: 4 additions & 4 deletions crawlers/snuco_crawler.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ def is_next_line_keyword(self, meal):
return any((str == code) for str in self.next_line_str) or any((str in code) for str in self.next_line_keyword)

def filter_menu_names(self, meal_names: list):
return [name for name in meal_names if self.is_meal_name(name)]
return [name for name in meal_names if self.is_meal_name_when_normalized(name)]

def filter_and_split_menu_names(self, meal_name: list):
names = []
Expand Down Expand Up @@ -140,7 +140,7 @@ async def run(self, date=None, **kwargs):
await super().run(url, date=date, **kwargs)

def found_meal(self, meal):
if meal and self.is_meal_name(meal.name) and "교직" not in meal.name:
if meal and self.is_meal_name_when_normalized(meal.name) and "교직" not in meal.name:
self.meals.append(meal)

def crawl(self, soup, **kwargs):
Expand Down Expand Up @@ -179,8 +179,8 @@ def crawl(self, soup, **kwargs):
for name in filtered_names:
meal = Meal(restaurant, name, date, types[col_idx])
meal = self.normalize(meal)
# is_meal_name에서 normalizer도 호출한다.
if self.is_meal_name(meal.name):

if self.is_meal_name_when_normalized(meal.name):
# ISSUE#54 220동 이름 오류 수정
# ex) ㅁ 바비든든( ~ ): 덮밥류 -> 바비든든: 덮밥류
if meal.restaurant == "220동식당":
Expand Down
60 changes: 11 additions & 49 deletions handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,16 @@
from itertools import compress
import asyncio
import argparse
from slack import send_slack_message
from crawlers.base_crawler import text_normalizer
from crawlers.vet_crawler import VetRestaurantCrawler
from crawlers.snudorm_crawler import SnudormRestaurantCrawler
from crawlers.snuco_crawler import SnucoRestaurantCrawler
from slack import (
send_new_restaurants_message,
send_deleted_menus_message,
send_new_menus_message,
send_edited_menus_message,
)


def compare_restaurants(db_restaurants, crawled_meals):
Expand Down Expand Up @@ -71,15 +76,6 @@ def compare_menus(db_menus, crawled_meals, restaurants):
)


def send_new_restaurants_message(new_restaurants):
print(f"New restaurants: {repr(new_restaurants)}")
if new_restaurants:
slack_message = f"{len(new_restaurants)} new restaurants found: "
for restaurant in new_restaurants:
slack_message = slack_message + '"' + restaurant.get("name_kr") + '" '
send_slack_message(slack_message)


def restaurants_transaction(crawled_meals, cursor):
get_restaurants_query = """
SELECT code
Expand All @@ -97,40 +93,6 @@ def restaurants_transaction(crawled_meals, cursor):
print("Restaurants checked")


def send_deleted_menus_message(deleted_menus):
slack_message = f"{len(deleted_menus)} menus deleted: \n"
for i, menu in enumerate(deleted_menus):
slack_message = slack_message + '"' + menu.get("name_kr") + '", '
if i % 5 == 4:
slack_message = slack_message + "\n"

send_slack_message(slack_message)
print(f"Menus deleted: {repr(deleted_menus)})")


def send_new_menus_message(new_menus: list):
slack_message = f"{len(new_menus)} new menus found: \n"
for i, menu in enumerate(new_menus):
slack_message = slack_message + '"' + menu.get("name_kr") + '", '
if i % 5 == 4:
slack_message = slack_message + "\n"

send_slack_message(slack_message)
print(f"New menus found: {repr(new_menus)}")


def send_edited_menus_message(edited_menus: list):
slack_message = f"{len(edited_menus)} menus edited: \n"

for i, menu in enumerate(edited_menus):
slack_message = slack_message + '"' + menu.get("name_kr") + '", '
if i % 5 == 4:
slack_message = slack_message + "\n"

send_slack_message(slack_message)
print(f"Menus edited: {repr(edited_menus)}")


def menus_transaction(crawled_meals, cursor):
get_restaurants_query = """
SELECT id, code
Expand All @@ -149,29 +111,29 @@ def menus_transaction(crawled_meals, cursor):

new_menus, deleted_menus, edited_menus = compare_menus(db_menus, crawled_meals, restaurants)

send_deleted_menus_message(deleted_menus)
if deleted_menus:
deleted_menus_id = [str(menu.get("id")) for menu in deleted_menus]
delete_menus_query = f"""
DELETE FROM menu
WHERE id in ({','.join(deleted_menus_id)});
"""
cursor.execute(delete_menus_query)
send_deleted_menus_message(deleted_menus)

send_new_menus_message(new_menus)
insert_menus_query = """
INSERT INTO menu(restaurant_id, code, date, type, name_kr, price, etc)
VALUES (%(restaurant_id)s, %(code)s, %(date)s, %(type)s, %(name_kr)s, %(price)s, %(etc)s);
"""
cursor.executemany(insert_menus_query, new_menus)
send_new_menus_message(new_menus)

send_edited_menus_message(edited_menus)
edited_menus_query = """
UPDATE menu
SET price=%(price)s, etc=%(etc)s, name_kr=%(name_kr)s
WHERE id=%(id)s;
"""
cursor.executemany(edited_menus_query, edited_menus)
send_edited_menus_message(edited_menus)

print("Menus checked")

Expand Down Expand Up @@ -242,12 +204,12 @@ def crawl(event, context):
menus_transaction(crawled_meals, cursor)
siksha_db.commit()

send_slack_message("Crawling has been successfully done")
_send_slack_message("Crawling has been successfully done")
return "Crawling has been successfully done"
except Exception as e:
siksha_db.rollback()
print(e)
send_slack_message("Crawling has been failed")
_send_slack_message("Crawling has been failed")
return "Crawling has been failed"
finally:
cursor.close()
Expand Down
38 changes: 36 additions & 2 deletions slack.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,41 @@
import os


def send_slack_message(msg):
body = {"channel": os.environ["SLACK_CHANNEL"], "text": msg}
def _send_slack_message(message: str):
body = {"channel": os.environ["SLACK_CHANNEL"], "text": message}
headers = {"Authorization": f'Bearer {os.environ["SLACK_TOKEN"]}'}
requests.post("https://slack.com/api/chat.postMessage", headers=headers, data=body, timeout=100)


def send_deleted_menus_message(menus: list):
message = f"{len(menus)} menus deleted: \n" + build_body_message(menus)
_send_slack_message(message)
print(f"Menus deleted: {repr(menus)})")


def send_new_menus_message(menus: list):
message = f"{len(menus)} new menus found: \n" + build_body_message(menus)
_send_slack_message(message)
print(f"New menus found: {repr(menus)})")


def send_edited_menus_message(menus: list):
message = f"{len(menus)} menus edited: \n" + build_body_message(menus)
_send_slack_message(message)
print(f"Menus edited: {repr(menus)})")


def send_new_restaurants_message(restaurants: list):
slack_message = f"{len(restaurants)} new restaurants found: \n" + build_body_message(restaurants)
if restaurants:
_send_slack_message(slack_message)
print(f"New restaurants: {repr(restaurants)}")


def build_body_message(menus_or_restaurants: list):
body_message = ""
for i, menu_or_restaurant in enumerate(menus_or_restaurants):
body_message += f'"{menu_or_restaurant.get("name_kr")}", '
if i % 5 == 4:
body_message += "\n"
return body_message

0 comments on commit d311a43

Please sign in to comment.