async def get_keyword_graph_statistics(): if not hasattrs(request.args, ['categoryId', 'startDate', 'timeUnit', 'keyword']): return "categoryId, startDate, timeUnit, keyword is required. enddate is optional", 400 keyword = request.args['keyword'] category_id = request.args['categoryId'] time_unit = request.args['timeUnit'] if time_unit not in ['month', 'week', 'date']: return 'timeUnit should be within month, week, date', 400 try: start_date_str = request.args['startDate'] start_date = datetime.date.fromisoformat(start_date_str) if request.args['endDate']: end_date_str = request.args['endDate'] end_date = datetime.date.fromisoformat(end_date_str) else: end_date = datetime.date.today() if start_date > end_date: return "start date should be before end date", 400 except: return "invalid format", 400 statistics = await fetch_keyword_graph_statistics(keyword, category_id, time_unit, start_date, end_date) return jsonify(statistics)
async def get_publish_count(): '''발행량''' if not hasattrs(request.args, ['keyword', 'startDate']): return "keyword, startDate is required", 400 keyword = request.args['keyword'] try: start_date_str = request.args['startDate'] start_date = datetime.date.fromisoformat(start_date_str) if request.args['endDate']: end_date_str = request.args['endDate'] end_date = datetime.date.fromisoformat(end_date_str) else: end_date = datetime.date.today() if start_date > end_date: return "start date should be before end date", 400 except: return "invalid format", 400 blog, cafe = await asyncio.gather( fetch_blog_post_published_count(keyword, start_date, end_date), fetch_cafe_post_published_count(keyword, start_date, end_date), ) return json.dumps({ 'keyword': keyword, 'period': { 'startDate': start_date.isoformat(), 'endDate': end_date.isoformat(), }, "blog": blog, "cafe": cafe, })
async def get_product_rank_within_keywords_coupang(): body = request.json if not hasattrs(body, ['keywords', 'productUrl']): return 'no keywords / productUrl', 400 result = crawl_product_rank_within_keywords_coupang( body['keywords'], body['productUrl']) return jsonify(result)
async def get_blog_post_naver_main_search_rank(): if not hasattrs(request.args, ['postId', 'keyword']): return 'no postId/keyword', 400 post_id = request.args['postId'] keyword = request.args['keyword'] rank = await fetch_blog_post_naver_main_search_rank(post_id, keyword) return jsonify(rank)
async def get_relative_ratio(): '''연관키워드''' if not hasattrs(request.args, ['keywords', 'startDate']): return "keywords, startDate is required", 400 keywords = [x.strip() for x in request.args['keywords'].split(',')] try: start_date_str = request.args['startDate'] start_date = datetime.date.fromisoformat(start_date_str) if request.args['endDate']: end_date_str = request.args['endDate'] end_date = datetime.date.fromisoformat(end_date_str) else: end_date = datetime.date.today() if start_date > end_date: return "start date should be before end date", 400 except: return "invalid format", 400 ratio = await fetch_relative_ratio(keywords, start_date, end_date, TimeUnit.DATE) return jsonify({ 'keywords': keywords, 'period': { 'startDate': start_date.isoformat(), 'endDate': end_date.isoformat(), }, 'ratio': ratio })
async def get_naver_search_autocomplete(): '''연관키워드''' if not hasattrs(request.args, ['keyword']): return 'no keyword', 400 keyword = request.args['keyword'] related_keywords = await fetch_naver_search_autocomplete_keywords(keyword) return jsonify(related_keywords)
async def get_naver_shopping_product_count(): '''네이버 쇼핑 제품 개수''' if not hasattrs(request.args, ['keyword']): return 'no keyword', 400 keyword = request.args['keyword'] product_count = await fetch_naver_shopping_product_count(keyword) return jsonify(product_count)
async def get_product_rank_within_keywords_naver(): body = request.json if not hasattrs(body, ['keywords', 'productUrl']): return 'no keywords / productUrl', 400 url = body['productUrl'] keywords = body['keywords'] result = crawl_product_rank_within_keywords_naver(keywords, url) return jsonify(result)
async def get_blog_post_hashtags(): if not hasattrs(request.args, ['blogId', 'postId']): return 'no blogId / postId', 400 blog_id = request.args['blogId'] post_id = request.args['postId'] hashtags = await fetch_blog_post_hashtags(blog_id, post_id) return jsonify(hashtags)
async def get_search_section_order(): if not hasattrs(request.args, ['keyword']): return 'no keyword', 400 keyword = request.args['keyword'] pc_order = fetch_PC_search_section_order(keyword) mobile_order = fetch_mobile_search_section_order(keyword) return jsonify({ 'keyword': keyword, 'pc': pc_order, 'mobile': mobile_order, })
async def get_category_shopping_trending_keywords(): if not hasattrs(request.args, ['categoryId', 'startDate']): return "categoryId, startDate is required", 400 category_id = request.args['categoryId'] try: start_date_str = request.args['startDate'] start_date = datetime.date.fromisoformat(start_date_str) if request.args['endDate']: end_date_str = request.args['endDate'] end_date = datetime.date.fromisoformat(end_date_str) else: end_date = datetime.date.today() if start_date > end_date: return "start date should be before end date", 400 except: return "invalid format", 400 data = await fetch_category_shopping_trending_keywords( category_id, start_date, end_date) return jsonify(data)