Browse Source

Cleanup after adding support for additional search engines

Add test
Benjamin Gleitzman 7 years ago
parent
commit
1117537485
4 changed files with 39 additions and 24 deletions
  1. 5 0
      CHANGES.txt
  2. 5 5
      README.rst
  3. 16 19
      howdoi/howdoi.py
  4. 13 0
      test_howdoi.py

+ 5 - 0
CHANGES.txt

@@ -1,4 +1,9 @@
+1.1.12
+------
+-  Add additional search engine support
+
 1.1.11
+------
 -  Fix issue with UTF-8 encoding
 
 1.1.10

+ 5 - 5
README.rst

@@ -97,8 +97,8 @@ Usage
                             number of answers to return
       -C, --clear-cache     clear the cache
       -v, --version         displays the current version of howdoi
-      
-      
+
+
 As a shortcut, if you commonly use the same paremeters each time and don't want to type them, add something similar to your .bash_profile (or otherwise). This example gives you 5 colored results each time.
 
 ::
@@ -110,7 +110,7 @@ And then to run it from the command line simply type:
 ::
 
     $h this is my query for howdoi
-    
+
 Author
 ------
 
@@ -120,13 +120,13 @@ Author
 Notes
 -----
 
--  You can set the HOWDOI_SEARCH_ENGINE environment variable to change the search engines to find StackOverflow links (default: google).  Other options for now include `bing`.
 -  Works with Python2 and Python3
 -  A standalone Windows executable with the howdoi application `is available here <https://dl.dropbox.com/u/101688/website/misc/howdoi.exe>`_.
 -  An Alfred Workflow for howdoi can be found at `http://blog.gleitzman.com/post/48539944559/howdoi-alfred-even-more-instant-answers <http://blog.gleitzman.com/post/48539944559/howdoi-alfred-even-more-instant-answers>`_.
 -  Slack integration available through `slack-howdoi <https://github.com/ellisonleao/slack-howdoi>`_.
 -  Howdoi uses a cache for faster access to previous questions. Caching functionality can be disabled by setting the HOWDOI_DISABLE_CACHE environment variable. The cache is stored in `~/.cache/howdoi`.
--  You can set the HOWDOI_URL environment variable to change the source url for answers (default: stackoverflow.com). Other options include `serverfault.com` or `pt.stackoverflow.com`. Here's the `full list <http://stackexchange.com/sites?view=list#traffic>`_.
+-  You can set the HOWDOI_URL environment variable to change the source url for answers (default: `stackoverflow.com`, also supported: `serverfault.com`, `pt.stackoverflow.com`, `full list <http://stackexchange.com/sites?view=list#traffic>`_).
+-  You can set the HOWDOI_SEARCH_ENGINE environment variable to change the underlying search engine for StackOverflow links (default: `google`, also supported: `bing`).
 -  Setting the HOWDOI_COLORIZE environment variable will colorize the output by default.
 -  Special thanks to Rich Jones (`@miserlou <https://github.com/miserlou>`_) for the idea.
 

+ 16 - 19
howdoi/howdoi.py

@@ -60,6 +60,10 @@ USER_AGENTS = ('Mozilla/5.0 (Macintosh; Intel Mac OS X 10.7; rv:11.0) Gecko/2010
                 'Chrome/19.0.1084.46 Safari/536.5'),
                ('Mozilla/5.0 (Windows; Windows NT 6.1) AppleWebKit/536.5 (KHTML, like Gecko) Chrome/19.0.1084.46'
                 'Safari/536.5'), )
+SEARCH_URLS = {
+    'bing': SCHEME + 'www.bing.com/search?q=site:{0}%20{1}',
+    'google': SCHEME + 'www.google.com/search?q=site:{0}%20{1}'
+}
 STAR_HEADER = u('\u2605')
 ANSWER_HEADER = u('{2}  Answer from {0} {2}\n{1}')
 NO_ANSWER_MSG = '< no answer given >'
@@ -92,41 +96,33 @@ def _get_result(url):
         raise e
 
 
-def _generate_links_of_bing(html):
+def _extract_links_from_bing(html):
     html.remove_namespaces()
     return [a.attrib['href'] for a in html('.b_algo')('h2')('a')]
 
 
-def _generate_links_of_google(html):
+def _extract_links_from_google(html):
     return [a.attrib['href'] for a in html('.l')] or \
         [a.attrib['href'] for a in html('.r')('a')]
 
 
-def _generate_links(html, search_engine):
+def _extract_links(html, search_engine):
     if search_engine == 'bing':
-        return _generate_links_of_bing(html)
-    elif search_engine == 'google':
-        return _generate_links_of_google(html)
+        return _extract_links_from_bing(html)
+    return _extract_links_from_google(html)
 
 
-def _dispatch_url(search_engine):
-    known_engines = {
-        'bing': SCHEME + 'www.bing.com/search?q=site:{0}%20{1}',
-        'google': SCHEME + 'www.google.com/search?q=site:{0}%20{1}'
-    }
-    return known_engines.get(search_engine, None)
+def _get_search_url(search_engine):
+    return SEARCH_URLS.get(search_engine, SEARCH_URLS['google'])
 
 
 def _get_links(query):
     search_engine = os.getenv('HOWDOI_SEARCH_ENGINE', 'google')
-    search_url = _dispatch_url(search_engine)
-
-    if search_url is None:
-        return None
+    search_url = _get_search_url(search_engine)
 
     result = _get_result(search_url.format(URL, url_quote(query)))
     html = pq(result)
-    return _generate_links(html, search_engine)
+    return _extract_links(html, search_engine)
 
 
 def get_link_at_pos(links, position):
@@ -213,6 +209,8 @@ def _get_instructions(args):
         return False
 
     question_links = _get_questions(links)
+    if not question_links:
+        return False
 
     only_hyperlinks = args.get('link')
     star_headers = (args['num_answers'] > 1 or args['all'])
@@ -236,8 +234,7 @@ def _get_instructions(args):
 def format_answer(link, answer, star_headers):
     if star_headers:
         return ANSWER_HEADER.format(link, answer, STAR_HEADER)
-    else:
-        return answer
+    return answer
 
 
 def _enable_cache():

+ 13 - 0
test_howdoi.py

@@ -53,6 +53,19 @@ class HowdoiTestCase(unittest.TestCase):
         for query in self.pt_queries:
             self.assertTrue(self.call_howdoi(query))
 
+    def test_answers_bing(self):
+        os.environ['HOWDOI_SEARCH_ENGINE'] = 'bing'
+        for query in self.queries:
+            self.assertTrue(self.call_howdoi(query))
+        for query in self.bad_queries:
+            self.assertTrue(self.call_howdoi(query))
+
+        os.environ['HOWDOI_URL'] = 'pt.stackoverflow.com'
+        for query in self.pt_queries:
+            self.assertTrue(self.call_howdoi(query))
+
+        os.environ['HOWDOI_SEARCH_ENGINE'] = ''
+
     def test_answer_links_using_l_option(self):
         for query in self.queries:
             response = self.call_howdoi(query + ' -l')