예제 #1
0
def search(search_engine):
    try:
        count = int(request.args.get('num', 10))
        qformat = request.args.get('format', 'json').lower()
        qtype = request.args.get('type', '')
        if qformat not in ('json', 'xml', 'csv'):
            abort(400, 'Not Found - undefined format')

        engine = search_engine
        if engine not in scrapers:
            error = [404, 'Incorrect search engine', engine]
            return bad_request(error)

        query = request.args.get('query')
        if not query:
            error = [400, 'Not Found - missing query', qformat]
            return bad_request(error)

        # first see if we can get the results for the cache
        engine_and_query = engine + ':' + query
        result = lookup(engine_and_query)
        if result:
            print("cache hit: {}".format(engine_and_query))
        else:
            result = feed_gen(query, engine, count, qtype)
            if result:
                # store the result in the cache to speed up future searches
                store(engine_and_query, result)
            else:
                error = [404, 'No response', engine_and_query]
                return bad_request(error)

        try:
            unicode  # unicode is undefined in Python 3 so NameError is raised
            for line in result:
                line['link'] = line['link'].encode('utf-8')
                if 'title' in line:
                    line['title'] = line['title'].encode('utf-8')
                if 'desc' in line:
                    line['desc'] = line['desc'].encode('utf-8')
        except NameError:
            pass  # Python 3 strings are already Unicode
        if qformat == 'json':
            return jsonify(result)
        elif qformat == 'csv':
            csvfeed = '"'
            csvfeed += '","'.join(result[0].keys())
            for line in result:
                csvfeed += '"\n"'
                csvfeed += '","'.join(line.values())
            csvfeed += '"'
            return Response(csvfeed)

        xmlfeed = dicttoxml(result, custom_root='channel', attr_type=False)
        xmlfeed = parseString(xmlfeed).toprettyxml()
        return Response(xmlfeed, mimetype='application/xml')
    except Exception as e:
        print(e)
        return jsonify(errorObj)
예제 #2
0
def search(search_engine):
    try:
        count = int(request.args.get('num', 10))
        qformat = request.args.get('format', 'json').lower()
        if qformat not in ('json', 'xml', 'csv'):
            abort(400, 'Not Found - undefined format')

        engine = search_engine
        if engine not in scrapers:
            error = [404, 'Incorrect search engine', engine]
            return bad_request(error)

        query = request.args.get('query')
        if not query:
            error = [400, 'Not Found - missing query', qformat]
            return bad_request(error)

        result = feed_gen(query, engine, count)
        if not result:
            error = [404, 'No response', qformat]
            return bad_request(error)

        if db['queries'].find({query: query}).limit(1) is False:
            db['queries'].insert({
                "query": query,
                "engine": engine,
                "qformat": qformat
            })

        try:
            unicode  # unicode is undefined in Python 3 so NameError is raised
            for line in result:
                line['link'] = line['link'].encode('utf-8')
                line['title'] = line['title'].encode('utf-8')
                if 'desc' in line:
                    line['desc'] = line['desc'].encode('utf-8')
        except NameError:
            pass  # Python 3 strings are already Unicode
        if qformat == 'json':
            return jsonify(result)
        elif qformat == 'csv':
            csvfeed = '"'
            csvfeed += '","'.join(result[0].keys())
            for line in result:
                csvfeed += '"\n"'
                csvfeed += '","'.join(line.values())
            csvfeed += '"'
            return Response(csvfeed)

        xmlfeed = dicttoxml(result, custom_root='channel', attr_type=False)
        xmlfeed = parseString(xmlfeed).toprettyxml()
        return Response(xmlfeed, mimetype='application/xml')
    except Exception as e:
        print(e)
        return jsonify(errorObj)