def search(search_engine): try: if request.method == 'GET': num = request.args.get('num') count = int(num) qformat = request.args.get('format') or 'json' if qformat not in ['json', 'xml']: abort(400, 'Not Found - undefined format') engine = search_engine if engine not in ['google', 'bing', 'duckduckgo', 'yahoo']: err = [404, 'Incorrect search engine', qformat] return bad_request(err) query = request.args.get('query') if not query: err = [400, 'Not Found - missing query', qformat] return bad_request(err) result = feedgen(query, engine[0], count) if not result: err = [404, 'No response', qformat] return bad_request(err) flag = 0 data = db['queries'].find({"query": query}).limit(1) for documents in data: flag = 1 """this is done because db['queries'].find({"query": query}).limit(1) returns the documents and not boolean hence for will count if for dosen't run then the element is unique""" if (flag == 0): # ie for dosen't run db['queries'].delete_many({"query": ""}) # this is done in order to delete document which has empty query db['queries'].insert({ "query": query, "engine": engine, "qformat": qformat }) for line in result: line['link'] = line['link'].encode('utf-8') line['title'] = line['title'].encode('utf-8') if (engine == 'b'): line['desc'] = line['desc'].encode('utf-8') if (qformat == 'json'): jsonfeed = json.dumps(result).encode('utf-8') return Response(jsonfeed, mimetype='application/json') elif (qformat == 'xml'): xmlfeed = parseString((dicttoxml( result, custom_root='channel', attr_type=False)).encode('utf-8')).toprettyxml() return Response(xmlfeed, mimetype='application/xml') except Exception as e: return (e)
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: err = [404, 'Incorrect search engine', engine] return bad_request(err) query = request.args.get('query') if not query: err = [400, 'Not Found - missing query', qformat] return bad_request(err) result = feedgen(query, engine, count) if not result: err = [404, 'No response', qformat] return bad_request(err) 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)
def search(search_engine): try: num = request.args.get('num') or 10 count = int(num) qformat = request.args.get('format') or 'json' if qformat not in ('json', 'xml'): abort(400, 'Not Found - undefined format') engine = search_engine if engine not in ('google', 'bing', 'duckduckgo', 'yahoo', 'ask', 'yandex', 'ubaidu', 'exalead', 'quora', 'tyoutube', 'parsijoo', 'mojeek'): err = [404, 'Incorrect search engine', qformat] return bad_request(err) query = request.args.get('query') if not query: err = [400, 'Not Found - missing query', qformat] return bad_request(err) result = feedgen(query, engine[0], count) if not result: err = [404, 'No response', qformat] return bad_request(err) if db['queries'].find({query: query}).limit(1) is False: db['queries'].insert({ "query": query, "engine": engine, "qformat": qformat }) for line in result: line['link'] = line['link'].encode('utf-8') line['title'] = line['title'].encode('utf-8') if engine in ['b', 'a']: line['desc'] = line['desc'].encode('utf-8') if qformat == 'json': jsonfeed = json.dumps(result).encode('utf-8') return Response(jsonfeed, mimetype='application/json') xmlfeed = parseString((dicttoxml(result, custom_root='channel', attr_type=False))).toprettyxml() return Response(xmlfeed, mimetype='application/xml') except Exception as e: print(e) return Response(json.dumps(errorObj).encode('utf-8'), mimetype='application/json')
def search(search_engine): try: if request.method == 'GET': num = request.args.get('num') count = int(num) qformat = request.args.get('format') or 'json' if qformat not in ['json', 'xml']: abort(400, 'Not Found - undefined format') engine = search_engine if engine not in ['google', 'bing', 'duckduckgo', 'yahoo']: err = [404, 'Incorrect search engine', qformat] return bad_request(err) query = request.args.get('query') if not query: err = [400, 'Not Found - missing query', qformat] return bad_request(err) result = feedgen(query, engine[0], count) if not result: err = [404, 'No response', qformat] return bad_request(err) if ((db['queries'].find({query: query}).limit(1)) == False): db['queries'].insert({ "query": query, "engine": engine, "qformat": qformat }) for line in result: line['link'] = line['link'].encode('utf-8') line['title'] = line['title'].encode('utf-8') if (engine == 'b'): line['desc'] = line['desc'].encode('utf-8') if (qformat == 'json'): jsonfeed = json.dumps(result).encode('utf-8') return Response(jsonfeed, mimetype='application/json') elif (qformat == 'xml'): xmlfeed = parseString((dicttoxml( result, custom_root='channel', attr_type=False)).encode('utf-8')).toprettyxml() return Response(xmlfeed, mimetype='application/xml') except Exception as e: return (e)