瀏覽代碼

initial commit

Benjamin Gleitzman 12 年之前
當前提交
abeeb4d91a
共有 2 個文件被更改,包括 67 次插入0 次删除
  1. 18 0
      README.md
  2. 49 0
      howdoi.py

+ 18 - 0
README.md

@@ -0,0 +1,18 @@
+howdoi - a unix code search tool
+========================
+
+Are you a hack programmer? Do you find yourself constantly Googling for how to do basic programing tasks?
+
+Suppose you want to know how to format a date in bash. Why open your browser and read through blogs when you can just..
+
+$ howdoi format string bash
+> date +"%m-%d-%y"
+
+Usage:
+
+`howdoi query`
+
+Extra notes:
+
+*   Requires [BeautifulSoup](http://www.crummy.com/software/BeautifulSoup/)
+*   Special thanks to Rich Jones (@miserlou) for the idea

+ 49 - 0
howdoi.py

@@ -0,0 +1,49 @@
+#!/usr/bin/python
+
+##################################################
+#
+# howdoi - a unix code search tool.
+# written by Benjamin Gleitzman (gleitz@mit.edu)
+# inspired by Rich Jones (rich@anomos.info)
+#
+##################################################
+
+import urllib
+import urllib2
+import sys
+import json
+
+from BeautifulSoup import BeautifulSoup as bs
+
+SEARCH_URL = "https://www.googleapis.com/customsearch/v1?key=AIzaSyCo6SQ6XNvvS3fdJLcDNR4mpdIGGmVcXAk&cx=015163316206774170098:pj94ujarmcg&q={0}&alt=json"
+
+def get_result(url):
+    print url
+    result = urllib2.urlopen(url)
+    return result.read()
+
+def get_instructions(query):
+    url = SEARCH_URL.format(urllib.quote(query))
+    result = get_result(url)
+    if not result:
+        return ''
+    else:
+        response = json.loads(result)
+        try:
+            link = response['items'][0]['link']
+            page = get_result(link)
+            soup = bs(page)
+            return soup.find("div", {"class": "answer"}).find("code").text
+        except:
+            return ''
+
+
+def howdoi(query):
+    instructions = get_instructions(query) or "Sorry, couldn't find any help with that topic"
+    print instructions
+
+if __name__ == "__main__":
+    if len(sys.argv) < 2:
+        print "USAGE: howdoi query (e.g. howdoi format date bash)"
+    else:
+        howdoi(" ".join(sys.argv[1:]))