def _has_search(cache, obj): if 'searches' not in cache: cache['searches'] = list(Search.find()) for res in cache['searches']: if res['name'] == obj['name'] \ and res['category'] == obj['category'] \ and res.get('season') == obj.get('season') \ and res.get('episode') == obj.get('episode') \ and res.get('album') == obj.get('album'): return True return False
def list_media(type, skip, limit): cache = {} spec = {} category = request.args.get('category') if category: if type in ('search', 'similar'): spec['category'] = category else: spec['info.subtype'] = category query = request.args.get('query') if query: spec.update(_get_search_spec(query)) sort = request.args.get('sort', 'date') if sort == 'name': sort = [('name', ASCENDING)] elif sort == 'rating': sort = [('rating', DESCENDING)] else: sort = [('date', DESCENDING), ('created', DESCENDING)] params = {'sort': sort, 'skip': skip, 'limit': limit} items = [] if type == 'media': for res in Media.find(spec, **params): search = Media.get_search(res) items.append(_get_object(res, type=type, has_search=_has_search(cache, search), has_similar=_has_similar(cache, search))) elif type == 'release': for res in Release.find(spec, **params): search = Release.get_search(res) items.append(_get_object(res, type=type, has_search=_has_search(cache, search), has_similar=_has_similar(cache, search))) elif type == 'search': for res in Search.find(spec, **params): items.append(_get_object(res, type=type, has_search=True, has_similar=_has_similar(cache, res))) elif type == 'similar': for res in SimilarSearch.find(spec, **params): items.append(_get_object(res, type=type, has_similar=True)) return serialize({'result': items})
def process_searches(): count = 0 for search in MSearch.find( sort=[('session.last_search', ASCENDING)]): search = Search(search) if not search.validate(): continue target = '%s.workers.search.process_search' % settings.PACKAGE_NAME get_factory().add(target=target, args=(search._id,), timeout=TIMEOUT_SEARCH) count += 1 if count == WORKERS_LIMIT: break