Skip to content

Instantly share code, notes, and snippets.

@iiogmgo
Created January 12, 2016 10:08
Show Gist options
  • Save iiogmgo/baad933801d82bc9a827 to your computer and use it in GitHub Desktop.
Save iiogmgo/baad933801d82bc9a827 to your computer and use it in GitHub Desktop.
from celery.utils.log import get_task_logger
task_logger = get_task_logger(__name__)
def send_pushes(wishlist, old_price, new_price):
"""
하나의 제품의 여러 개의 wishlist 에 대하여
각 wish 의 방해금지 시간을 체크하여 user 에게 푸쉬를 보내거나 딜레이시킵니다
"""
for wish in wishlist:
product_id = wish.product.id
email = wish.user.email
send_push(wish, old_price, new_price)
message = "{0},{1},{2},{3},{4}"\
.format("푸시", email, product_id, old_price, new_price)
push_logger.info(message)
PushHistory.commit()
@celery.task()
def post_info(product, selected_price, type_="crawler"):
"""
제품 크롤링 결과를 저장 및 반환합니다
"""
from app.controllers.crawler.crawler import get_crawl
keyword = ' '.join([product.brand, product.refined_title]) \
if product.brand and product.brand not in product.refined_title \
else product.refined_title
result = get_crawl(keyword=keyword, min_price=selected_price)
if result.get('code') != 400 and result.get('sellers_len') != 0:
product.reset_info()
infos_create(result['sellers'], product.id, type_=type_)
return result
@celery.task()
def check_price(product_id, func_name, filename=None):
"""
각 product 의 최저가를 갱신하여 이전 가격과 달라졌으면 push를 보냅니다
"""
from app.controllers.search.search import post_info
product = Product.get(product_id)
if product is None:
task_logger.info("{0},{1},{2},{3}".format(
func_name, "검색실패", product_id, "not found product")
)
return "fail"
if product.get_info_in_hour().count() > 0:
return
result = post_info(product, product.selected_price, func_name)
if result.get('code') == 400:
task_logger.info("{0},{1},{2},{3},{4},{5},{6}".format(
func_name, "검색실패", product.id, product.refined_title,
result['code'], result['error'], result['msg']))
return "fail"
if result.get('sellers_len') == 0:
task_logger.info("{0},{1},{2},{3},{4}".format(
func_name, "결과없음", product.id, product.refined_title,
result['sellers_len']))
return "nosearch"
old_price = 0 if product.latest_info is None \
else product.latest_info.price
task_logger.info("{0},{1},{2},{3},{4}".format(
func_name, "검색완료", product.id, product.refined_title,
result['sellers_len']))
low_info = result['sellers'][0]
new_price = low_info['price']
mall_name = low_info.get('mall_name', "")
# PriceHistory 갱신
price_history = PriceHistory.get_or_create(
product=product, date_=date.today())
price_condition = (
(isinstance(price_history.price, int) and
price_history.price > new_price) or
price_history.price is None
)
if price_condition:
price_history.price = new_price
price_history.mall_name = mall_name
PriceHistory.commit()
if new_price != old_price and len(mall_name) > 0:
# wish.product의 가격이 바뀐 시간을 저장하기 위해
for wish in product.wishlist:
wish.price_checked_time = datetime.now()
wish.is_new = True
Wish.commit()
query = product.get_filtered_wishlist(old_price, new_price)
send_pushes(query, old_price, new_price)
return "Done"
@celery.task()
def update_products():
now_time = datetime.now()
alert_to_slack("[위시리스트 상품] %s 크롤링 시작"
% now_time.strftime('%Y-%m-%d %H:%M:%S'))
products = Product.that_has_wish()
for product in products:
check_price.delay(product.id, "update_products")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment