def on_post(self, request, response): webm_list = request.context['doc'] resp_data = [] try: for webm in webm_list: md5 = webm["md5"] url = webm["url"] webm_response = None webm_redis_info = get_cache(md5) # clean_md5, delayed or None if isinstance(webm_redis_info, dict): webm_redis_info['md5'] = md5 webm_response = webm_redis_info elif webm_redis_info == "delayed": webm_response = {"md5": md5, "message": "Уже анализируется"} elif webm_redis_info is None: if is_valid_2ch_url(url): analyse_video.delay(md5, url) set_cache_delayed(md5) webm_response = {"md5": md5, "message": "Добавлено в очередь на анализ"} else: webm_response = {"md5": md5, "message": "Неправильный url"} else: webm_response = {"md5": md5, "message": "Some error"} resp_data.append(webm_response) request.context['result'] = resp_data except Exception as e: response.status = status_codes.HTTP_400 request.context['result'] = {"message": "Неправильный запрос"} print("error in post request:", e)
def on_get(self, request, response): md5 = request.get_param('md5') url = request.get_param('url') response.set_header("Access-Control-Allow-Origin", "*") webm_redis_info = get_cache(md5) # data, delayed or None # print("redis:", webm_redis_info) # If data in cache if isinstance(webm_redis_info, dict): webm_redis_info['md5'] = md5 response.status = status_codes.HTTP_200 request.context['result'] = webm_redis_info # If webm is analysed elif webm_redis_info == "delayed": response.status = status_codes.HTTP_202 request.context['result'] = {"md5": md5, "message": "Уже анализируется"} # If no cache data elif webm_redis_info is None: if is_valid_2ch_url(url): analyse_video.delay(md5, url) set_cache_delayed(md5) response.status = status_codes.HTTP_202 request.context['result'] = {"md5": md5, "message": "Добавлено в очередь на анализ"} else: # not valid url response.status = status_codes.HTTP_400 request.context['result'] = {"md5": md5, "message": "Неправильная ссылка в запросе"} else: response.status = status_codes.HTTP_502 request.context['result'] = {"md5": md5, "message": "Some error"}
def test_https_2ch_hk_b_valid_webm_link(self): assert is_valid_2ch_url( "https://2ch.hk/b/src/152120959/14936167914140.webm") is True
def test_https_2ch_hk_ts_invalid_board(self): assert is_valid_2ch_url( "https://2ch.hk/ts/src/152120959/14936167914140.webm") is False
def test_https_2ch_hk_b_invalid_extension(self): assert is_valid_2ch_url( "https://2ch.hk/b/src/15212092159/149361679141540.jpeg") is False
def test_https_2ch_ru_b_invalid_domain(self): assert is_valid_2ch_url( "https://2ch.ru/pr/src/152120959/14936167914140.webm") is False