class PBComplete(Resource): method_decorators = [check_api_key('pb')] querybuilder = QueryBuilder() @ns_platsannons.doc(params=swagger_doc_params, responses={ 200: 'OK', 401: 'Invalid API-key', 500: 'Technical exception' }) @ns_platsannons.expect(annons_complete_query) def get(self): args = annons_complete_query.parse_args() # This could be prettier args[settings.LIMIT] = 0 # Always return 0 ads when calling typeahead args[settings.TYPEAHEAD_QUERY] = args.pop(settings.FREETEXT_QUERY) result = platsannonser.find_platsannonser(args, self.querybuilder) return self.marshal_results(result) def marshal_results(self, esresult): result = { "time_in_millis": esresult.get('took', 0), "typeahead": esresult.get('aggs', []), } return result
class PBComplete(Resource): method_decorators = [check_api_key('pb')] querybuilder = QueryBuilder() @ns_platsannons.doc(description='Typeahead / Suggest next searchword', params=swagger_doc_params) @ns_platsannons.response(401, 'Invalid API-key') @ns_platsannons.expect(annons_complete_query) @ns_platsannons.marshal_with(typeahead_results) def get(self): start_time = int(time.time() * 1000) args = annons_complete_query.parse_args() # This could be prettier args[settings.LIMIT] = 0 # Always return 0 ads when calling typeahead query_string = args.pop(settings.FREETEXT_QUERY) or '' args[settings.TYPEAHEAD_QUERY] = query_string args[settings.FREETEXT_QUERY] = ' '.join(query_string.split(' ')[0:-1]) result = platsannonser.find_platsannonser(args, self.querybuilder) log.debug("Query results after %d milliseconds." % (int(time.time() * 1000) - start_time)) return self.marshal_results(result, start_time) def marshal_results(self, esresult, start_time): result = { "time_in_millis": esresult.get('took', 0), "typeahead": esresult.get('aggs', []), } log.debug("Sending results after %d milliseconds." % (int(time.time() * 1000) - start_time)) return result
class BulkZip(Resource): method_decorators = [check_api_key('bulk', 300)] @ns_bulk.doc(params={ settings.DATE: "Date to zip ads for. Accepts date as YYYY-MM-DD or 'all'. " "(Note that 'all' can take a couple of minutes to compile.)" " Rate limit is one request every five minutes." }, responses={ 200: 'OK', 401: 'Invalid API-key', 429: 'Rate limit exceeded', 500: 'Technical error' }) @ns_bulk.expect(bulk_zip_query) def get(self): start_time = int(time.time() * 1000) args = bulk_zip_query.parse_args() bytes_result = repository.zip_ads(args.get(settings.DATE), start_time) filename = "ads_%s.zip" % args.get(settings.DATE) log.debug("Elapsed time for completion: %d" % int((time.time() * 1000) - start_time)) return send_file(bytes_result, attachment_filename=filename, cache_timeout=60, as_attachment=True)
class AuranestComplete(Resource): method_decorators = [check_api_key('all')] @ns_auranest.doc(description='Typeahead / Suggest the next search term') @ns_auranest.expect(auranest_typeahead) def get(self): args = auranest_typeahead.parse_args() return auranest.autocomplete(args.get(settings.FREETEXT_QUERY))
class MarketSearch(Resource): method_decorators = [check_api_key('all')] @ns_market.doc(description='Search with freetext query') @ns_market.expect(market_query) @ns_market.marshal_with(market_list) def get(self): args = market_query.parse_args() return repository.find_annonser(args)
class Proxy(Resource): method_decorators = [check_api_key('pb')] @ns_platsannons.doc( description='Load a job ad by ID', ) @ns_platsannons.response(401, 'Invalid API-key') @ns_platsannons.response(404, 'Job ad not found') @ns_platsannons.expect(load_ad_query) @ns_platsannons.marshal_with(job_ad) def get(self, id): return platsannonser.fetch_platsannons(str(id))
class MarketComplete(Resource): method_decorators = [check_api_key('all')] @ns_market.doc(description='Typeahead / Suggest the next search term') @ns_market.expect(market_typeahead) @ns_market.marshal_with(autocomplete_list) def get(self): args = market_typeahead.parse_args() return { 'typeahead': repository.autocomplete(args.get(settings.FREETEXT_QUERY)) }
class AuranestSearch(Resource): method_decorators = [check_api_key('all')] @ns_auranest.doc(description='Search with freetext query') @ns_auranest.expect(auranest_query) def get(self): args = auranest_query.parse_args() return self.marshal_default(auranest.find_annonser(args)) @ns_auranest.marshal_with(auranest_lista) def marshal_default(self, results): return results
class PBSearch(Resource): method_decorators = [check_api_key('pb')] querybuilder = QueryBuilder() @ns_platsannons.doc( description='Search using parameters and/or freetext', params={ **swagger_doc_params, **swagger_filter_doc_params }, ) @ns_platsannons.response(401, 'Invalid API key') @ns_platsannons.expect(pb_query) @ns_platsannons.marshal_with(open_results) def get(self): start_time = int(time.time() * 1000) args = pb_query.parse_args() log.debug("Query parsed after %d milliseconds." % (int(time.time() * 1000) - start_time)) result = platsannonser.find_platsannonser( args, self.querybuilder, start_time, request.headers.get('X-Fields')) log.debug("Query results after %d milliseconds." % (int(time.time() * 1000) - start_time)) max_score = result.get('max_score', 1.0) hits = [ dict( hit['_source'], **{ 'relevance': (hit['_score'] / max_score) if max_score > 0 else 0.0 }) for hit in result.get('hits', []) ] return self.marshal_results(result, hits, start_time) def marshal_results(self, esresult, hits, start_time): total_results = {'value': esresult.get('total', {}).get('value')} result = { "total": total_results, "positions": esresult.get('positions', 0), "query_time_in_millis": esresult.get('took', 0), "result_time_in_millis": int(time.time() * 1000) - start_time, "stats": esresult.get('stats', []), "freetext_concepts": esresult.get('concepts', {}), "hits": hits } log.debug("Sending results after %d milliseconds." % (int(time.time() * 1000) - start_time)) return result
class BulkLoad(Resource): method_decorators = [check_api_key('bulk')] @ns_bulk.doc( params={ settings.DATE: "Load ad changes since date" }, responses={ 200: 'OK', 401: 'Invalid API-key', 500: 'Technical error' } ) @ns_bulk.expect(bulk_query) def get(self): return []
class PBSearch(Resource): method_decorators = [check_api_key('pb')] querybuilder = QueryBuilder() @ns_platsannons.doc(params={ **swagger_doc_params, **swagger_filter_doc_params }, responses={ 200: 'OK', 401: 'Invalid API-key', 500: 'Technical exception' }) @ns_platsannons.expect(pb_query) def get(self): start_time = int(time.time() * 1000) args = pb_query.parse_args() log.debug("Query parsed after %d milliseconds." % (int(time.time() * 1000) - start_time)) result = platsannonser.find_platsannonser(args, self.querybuilder, start_time) if args.get(settings.FREETEXT_QUERY): result['concepts'] = ttc.text_to_concepts( args.get(settings.FREETEXT_QUERY)) log.debug("Query results after %d milliseconds." % (int(time.time() * 1000) - start_time)) hits = [hit['_source'] for hit in result.get('hits', [])] return self.marshal_results(result, hits, start_time) def marshal_results(self, esresult, hits, start_time): result = { "total": esresult.get('total', 0), "positions": esresult.get('positions', 0), "query_time_in_millis": esresult.get('took', 0), "result_time_in_millis": int(time.time() * 1000) - start_time, "stats": esresult.get('stats', {}), "freetext_concepts": esresult.get('concepts', {}), "hits": hits } log.debug("Sending results after %d milliseconds." % (int(time.time() * 1000) - start_time)) return result
class BulkLoad(Resource): method_decorators = [check_api_key('bulk', 60)] @ns_bulk.doc(params={ settings.DATE: "Stream ads updated since datetime. " "Accepts datetime as YYYY-MM-DDTHH:MM:SS, " "for example 2019-06-11T10:00:00. " "Rate limit is one request per minute." }, responses={ 200: 'OK', 401: 'Invalid API-key', 429: 'Rate limit exceeded', 500: 'Technical error' }) @ns_bulk.expect(bulk_stream_query) def get(self): args = bulk_stream_query.parse_args() return Response(repository.load_all(args.get(settings.DATE)), mimetype='application/json')
class OpenSearch(Resource): method_decorators = [check_api_key('open')] querybuilder = QueryBuilder() @ns_open.doc(params={ **swagger_doc_params, **swagger_filter_doc_params }, responses={ 200: 'OK', 401: 'Invalid API-key', 500: 'Technical exception' }) @ns_open.expect(pb_query) def get(self): args = pb_query.parse_args() result = platsannonser.find_platsannonser(args, self.querybuilder) return self.marshal_results(result) @ns_open.marshal_with(simple_lista) def marshal_results(self, result): return result
class BulkZip(Resource): method_decorators = [check_api_key('bulk')] @ns_bulk.doc( params={ "date": "Date to zip ads for. Accepts date as YYYY-MM-DD, 'all' " "or 'yesterday'. (Note that 'all' can take a couple of minutes to compile.)" }, responses={ 200: 'OK', 401: 'Invalid API-key', 500: 'Technical error' } ) @ns_bulk.expect(bulk_query) def get(self): start_time = int(time.time()*1000) args = bulk_query.parse_args() bytes_result = platsannonser.zip_ads(args.get(settings.ZIPDATE), start_time) filename = "ads_%s.zip" % args.get(settings.ZIPDATE) log.debug("Elapsed time for completion: %d" % int((time.time()*1000)-start_time)) return send_file(bytes_result, attachment_filename=filename, as_attachment=True)