Skip to content

Commit

Permalink
fix compatibility for python 3 (< 3.4) #3
Browse files Browse the repository at this point in the history
  • Loading branch information
aviaryan committed Feb 6, 2018
1 parent 97996f6 commit d9962f4
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 3 deletions.
2 changes: 2 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
language: python
python:
- "2.7"
- "3.2"
- "3.6"
# 3.2 added because https://github.com/aviaryan/python-gsearch/issues/3
# command to run tests
script: python -m unittest tests.tests
12 changes: 10 additions & 2 deletions gsearch/googlesearch.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,16 @@
# Python 3
from urllib import request
from html.parser import HTMLParser # keep it to avoid warning
from html import unescape
from urllib.parse import quote, unquote
# local
try:
from gsearch.data import user_agents # works in tests
except ImportError:
from data import user_agents # works in a normal run
try:
from html import unescape # Python 3.4+
except ImportError:
pass
except ImportError:
# Python 2
import urllib2 as request
Expand Down Expand Up @@ -86,7 +89,12 @@ def convert_unicode(text):
h = HTMLParser()
s = h.unescape(text)
else:
s = unescape(text)
try:
s = unescape(text)
except Exception:
# Python 3.3 and below
# https://stackoverflow.com/a/2360639/2295672
s = HTMLParser().unescape(text)
return s


Expand Down
2 changes: 1 addition & 1 deletion tests/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def test_results_count(self):
self.assertTrue(len(res) > 10, 'Less than 11 results returned')

def test_results_zero(self):
res = search('dsjaksfajsdhkhawkehkajdwek')
res = search('dsjaksfajsdhkhawkehkajdwek' + (str(randint(10,100)) * 5))
self.assertTrue(len(res) == 0, 'There was a result. What has this world come to?')

def test_unicode(self):
Expand Down

0 comments on commit d9962f4

Please sign in to comment.