Search code examples
pythonjsonbingbing-api

Processing a JSON response in python for the Bing API


I am writing the back-end of an answers aggregator site in python using the Bing API (python bindings here:http://uswaretech.com/blog/2009/06/bing-python-api/). The following is my code:

#!/usr/bin/python
from bingapi import bingapi
import re
import cgi
import cgitb
from HTMLParser import HTMLParser

class MLStripper(HTMLParser):
    def __init__(self):
            self.reset()
            self.fed = []
    def handle_data(self, d):
            self.fed.append(d)
    def get_data(self):
            return ''.join(self.fed)

def strip_tags(html):
    s = MLStripper()
    s.feed(html)
    return s.get_data()

def strip_tags2(data):
    p = re.compile(r'<[^<]*?>')
    q = re.compile(r'[&;!@#$%^*()]*')
    data = p.sub('', data)
    return q.sub('', data)

def getUrl(item):
    return item['Url']

def getContent(item):
    return item['Description']

def getInfo(siteStr, qry):
    query = "{0} {1}".format(qry, siteStr)
    bing = bingapi.Bing('APP_ID_HERE')
    j = bing.do_web_search(query)
    results = j['SearchResponse']['Web']['Results']
    return result

def updateRecent(qry):
    f = open("recent.txt", "r")
    lines = f.readlines()
    f.close()
    lines = lines[1:]

    if len(qry) > 50: #truncate if string too long
            qry = (qry[:50] + '...')
    qry = strip_tags2(qry) #strip out the html if injection try

    lines.append("\n%s" % qry)
    f = open("recent.txt", "w")
    f.writelines(lines)
    f.close()

if __name__ == '__main__':
    form = cgi.FieldStorage()
    qry = form["qry"].value
    qry = r'%s' % qry

    updateRecent(qry)

    siteStr = "site:answers.yahoo.com OR site:chacha.com OR site:blurtit.com OR site:answers.com OR site:question.com OR site:answerbag.com OR site:stackexchange.com"

    print "Content-type: text/html"
    print

    header = open("header.html", "r")
    contents = header.readlines()
    header.close()
    for item in contents:
            print item

    print """
    <div id="results">
    <center><h1>Results:</h1></center>
    """
    print getInfo(siteStr, qry)
    for item in getInfo(siteStr, qry):
            print "<h3>%s</h3>" % getUrl(item)
            print "<br />"
            print "<p style=\"color:gray\">%s</p>" % getContent(item)
            print "<br />"
    print "</div>"

    footer = open("footer.html", "r")
    contents = footer.readlines()
    footer.close()
    for thing in contents:
            print thing

For some reason when I run this in my browser (sending it a query using a text input) it doesn't print anything. Can someone explain why this is happening? Thx in advance!


Solution

  • Nevermind, just found a syntax error that apache didn't pick up I guess. In the "getInfo()" function it says "return result" when it should say "return results".