Skip to content

Commit

Permalink
Add support for Python 3
Browse files Browse the repository at this point in the history
  • Loading branch information
cclauss committed Dec 10, 2017
1 parent 5ede95a commit 2f294ea
Show file tree
Hide file tree
Showing 18 changed files with 79 additions and 106 deletions.
2 changes: 0 additions & 2 deletions .flake8.ini
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
[flake8]
max-line-length = 150
ignore = W191
exclude =
.git,
__pycache__
44 changes: 15 additions & 29 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,29 +1,15 @@
matrix:
include:
- language: python
python: "2.7"
install:
- pip install flake8 pytest
- pip install -r requirements.txt
before_script:
- flake8 . --count --max-complexity=12 --max-line-length=85 --show-source --statistics
script:
- python app/server.py > /dev/null &
- pytest --capture=sys
- kill $(lsof -t -i:7001)
after_success:
- codecov

- language: python
python: "3.6"
install:
- pip install flake8 pytest
- pip install -r requirements.txt
before_script:
- flake8 . --count --max-complexity=12 --max-line-length=85 --show-source --statistics
script:
- pytest --capture=sys
after_success:
- codecov


language: python
python:
- "2.7"
- "3.6"
install:
- pip install codecov flake8 pytest
- pip install -r requirements.txt
before_script:
- flake8 . --count --max-complexity=15 --max-line-length=85 --show-source --statistics
script:
- python app/server.py > /dev/null &
- pytest --capture=sys
- kill $(lsof -t -i:7001)
after_success:
- codecov
26 changes: 13 additions & 13 deletions app/scrapers/__init__.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
from __future__ import print_function

from ask import Ask
from baidu import Baidu
from bing import Bing
from dailymotion import Dailymotion
from duckduckgo import Duckduckgo
from exalead import Exalead
from google import Google
from mojeek import Mojeek
from parsijoo import Parsijoo
from quora import Quora
from yahoo import Yahoo
from yandex import Yandex
from youtube import Youtube
from .ask import Ask
from .baidu import Baidu
from .bing import Bing
from .dailymotion import Dailymotion
from .duckduckgo import Duckduckgo
from .exalead import Exalead
from .google import Google
from .mojeek import Mojeek
from .parsijoo import Parsijoo
from .quora import Quora
from .yahoo import Yahoo
from .yandex import Yandex
from .youtube import Youtube

scrapers = {
'ask': Ask(),
Expand Down
3 changes: 1 addition & 2 deletions app/scrapers/ask.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@

from __future__ import print_function
from generalized import Scraper
from .generalized import Scraper


class Ask(Scraper):
Expand Down
2 changes: 1 addition & 1 deletion app/scrapers/baidu.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from __future__ import print_function
from generalized import Scraper
from .generalized import Scraper


class Baidu(Scraper):
Expand Down
2 changes: 1 addition & 1 deletion app/scrapers/bing.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from __future__ import print_function
from generalized import Scraper
from .generalized import Scraper


class Bing(Scraper):
Expand Down
2 changes: 1 addition & 1 deletion app/scrapers/dailymotion.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from __future__ import print_function
from generalized import Scraper
from .generalized import Scraper
import json


Expand Down
2 changes: 1 addition & 1 deletion app/scrapers/duckduckgo.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from __future__ import print_function
from generalized import Scraper
from .generalized import Scraper


class Duckduckgo(Scraper):
Expand Down
2 changes: 1 addition & 1 deletion app/scrapers/exalead.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from __future__ import print_function
from generalized import Scraper
from .generalized import Scraper


class Exalead(Scraper):
Expand Down
2 changes: 1 addition & 1 deletion app/scrapers/google.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from __future__ import print_function
from generalized import Scraper
from .generalized import Scraper


class Google(Scraper):
Expand Down
2 changes: 1 addition & 1 deletion app/scrapers/mojeek.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from __future__ import print_function
from generalized import Scraper
from .generalized import Scraper


class Mojeek(Scraper):
Expand Down
2 changes: 1 addition & 1 deletion app/scrapers/parsijoo.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from __future__ import print_function
from generalized import Scraper
from .generalized import Scraper


class Parsijoo(Scraper):
Expand Down
2 changes: 1 addition & 1 deletion app/scrapers/quora.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from __future__ import print_function
from generalized import Scraper
from .generalized import Scraper


class Quora(Scraper):
Expand Down
12 changes: 8 additions & 4 deletions app/scrapers/yahoo.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
from __future__ import print_function
from generalized import Scraper
import urllib
from .generalized import Scraper

try:
from urllib.parse import unquote # Python 3
except ImportError:
from urllib import unquote # Python 2


class Yahoo(Scraper):
Expand All @@ -15,7 +19,7 @@ def parseResponse(self, soup):
""" Parse response and returns the urls
Returns: urls (list)
[[Tile1,url1], [Title2, url2],..]
[[Tile1, url1], [Title2, url2], ...]
"""
urls = []
for h in soup.findAll('h3', attrs={'class': 'title'}):
Expand All @@ -24,7 +28,7 @@ def parseResponse(self, soup):
r = y.get('href')
f = r.split('RU=')
e = f[-1].split('/RK=1')
u = urllib.unquote(e[0])
u = unquote(e[0])
urls.append({
'title': y.getText(),
'link': u
Expand Down
2 changes: 1 addition & 1 deletion app/scrapers/yandex.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from __future__ import print_function
from generalized import Scraper
from .generalized import Scraper


class Yandex(Scraper):
Expand Down
2 changes: 1 addition & 1 deletion app/scrapers/youtube.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from __future__ import print_function
from generalized import Scraper
from .generalized import Scraper


class Youtube(Scraper):
Expand Down
54 changes: 26 additions & 28 deletions app/server.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
from flask import (Flask, render_template, request, abort, Response,
make_response)
from scrapers import feedgen, scrapers
from pymongo import MongoClient
from dicttoxml import dicttoxml
from xml.dom.minidom import parseString
import json
import os
from argparse import ArgumentParser
from xml.dom.minidom import parseString

from dicttoxml import dicttoxml
from flask import (Flask, Response, abort, jsonify, make_response,
render_template, request)
from pymongo import MongoClient

from scrapers import feedgen, scrapers

app = Flask(__name__)
err = ""
Expand All @@ -20,9 +22,8 @@
}

parser = ArgumentParser()
parser.add_argument("--dev",
help="Start the server in development mode with debug=True",
action="store_true")
help_msg = "Start the server in development mode with debug=True"
parser.add_argument("--dev", help=help_msg, action="store_true")
args = parser.parse_args()


Expand All @@ -40,15 +41,14 @@ def bad_request(err):
@app.route('/api/v1/search/<search_engine>', methods=['GET'])
def search(search_engine):
try:
num = request.args.get('num') or 10
count = int(num)
qformat = request.args.get('format') or 'json'
count = int(request.args.get('num', 10))
qformat = request.args.get('format', 'json').lower()
if qformat not in ('json', 'xml'):
abort(400, 'Not Found - undefined format')

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

query = request.args.get('query')
Expand All @@ -65,26 +65,24 @@ def search(search_engine):
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 'desc' in line:
line['desc'] = line['desc'].encode('utf-8')
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':
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 jsonify(result)
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 Response(json.dumps(errorObj).encode(
'utf-8'), mimetype='application/json')
return jsonify(errorObj)


@app.after_request
Expand Down
22 changes: 5 additions & 17 deletions app/test_server.py
Original file line number Diff line number Diff line change
@@ -1,30 +1,18 @@
"""
# Let's get pytest turned on before we start testing in earnest
from .scrapers import small_test
def test_small_test():
small_test()
"""

import os
import sys

import pytest
import requests


PYTHON3 = sys.version_info.major >= 3
REASON = 'Python 3 blocked until #297 goes live'
REASON = 'Do you have query-server running on http://127.0.0.1:7001 ?'
TRAVIS_CI = os.getenv('TRAVIS', False) # Running in Travis CI?


def test_true():
assert True, "We have a problem!"


@pytest.mark.xfail(PYTHON3 or not TRAVIS_CI, reason=REASON)
@pytest.mark.xfail(not TRAVIS_CI, reason=REASON)
def test_invalid_url_api_call():
response = requests.get('http://localhost:7001/api/v1/search/invalid_url')
assert response.json()['Status Code'] == 404
Expand All @@ -35,9 +23,9 @@ def make_engine_api_call(engine_name):
assert requests.get(url).json()['Status Code'] == 400, engine_name


@pytest.mark.xfail(PYTHON3 or not TRAVIS_CI, reason=REASON)
@pytest.mark.xfail(not TRAVIS_CI, reason=REASON)
def test_engine_api_calls(engine_names=None):
engines = ('ask', 'baidu', 'bing', 'dailymotion', 'duckduckgo', 'exalead',
'google', 'mojeek', 'parsijoo', 'quora', 'yahoo', 'yandex', 'youtube')
engines = """ask baidu bing dailymotion duckduckgo exalead google
mojeek parsijoo quora yahoo yandex youtube""".split()
for engine_name in (engine_names or engines):
make_engine_api_call(engine_name)

0 comments on commit 2f294ea

Please sign in to comment.