<br>Greetings to edu-sig friends, HNY!<br>Kirby<br><br clear="all"><br><br><br><div class="gmail_quote">---------- Forwarded message ----------<br>From: <b class="gmail_sendername">kirby urner</b> <span dir="ltr"><<a href="mailto:kirby.urner@gmail.com">kirby.urner@gmail.com</a>></span><br>
Date: Wed, Dec 30, 2009 at 5:49 PM<br>Subject: student project (market reseach)<br><br><br><br>"""<br><br>Slinging code from this resource:<br><a href="http://uswaretech.com/blog/2009/06/bing-python-api/" target="_blank">http://uswaretech.com/blog/2009/06/bing-python-api/</a><br>
<br>More background in blog:<br><a href="http://mybizmo.blogspot.com/2009/12/back-stage.html" target="_blank">http://mybizmo.blogspot.com/2009/12/back-stage.html</a><br><br>Also edu-sig:<br>open source buzzbot project proposal<br>
<a href="http://mail.python.org/pipermail/edu-sig/2009-September/009520.html" target="_blank">http://mail.python.org/pipermail/edu-sig/2009-September/009520.html</a><br>
<br>"""<br><br>import urllib2<br>import urllib<br>import simplejson<br>import logging<br><br>APP_ID = "your_app_id_here"<br><br>class BingException(Exception):<br> pass<br><br>class Bing(object):<br>
def __init__(self, app_id, loglevel=logging.INFO):<br> self.app_id = app_id<br> self.log_filename = 'log.log'<br> self.end_point = '<a href="http://api.search.live.net/json.aspx?Appid=%s&%27%app_id" target="_blank">http://api.search.live.net/json.aspx?Appid=%s&'%app_id</a><br>
logging.basicConfig(level=loglevel,<br> format='%(asctime)s %(name)-6s %(levelname)-8s %(message)s',<br> filename=self.log_filename) <br> <br> <br>
def talk_to_bing(self, query, sources, extra_args={}):<br> <a href="http://logging.info" target="_blank">logging.info</a>('Query:%s'%query)<br> <a href="http://logging.info" target="_blank">logging.info</a>('Sources:%s'%sources)<br>
<a href="http://logging.info" target="_blank">logging.info</a>('Other Args:%s'%extra_args)<br> <br> payload={}<br> payload['Appid'] = self.app_id<br> payload['query'] = query<br>
payload['sources'] = sources<br> payload.update(extra_args)<br> query_string = urllib.urlencode(payload)<br> final_url = self.end_point + query_string<br> <a href="http://logging.info" target="_blank">logging.info</a>('final_url:%s'%final_url)<br>
response = urllib.urlopen(final_url)<br> data = simplejson.load(response)<br> if 'Errors' in data['SearchResponse']:<br> <a href="http://logging.info" target="_blank">logging.info</a>('Error')<br>
<a href="http://logging.info" target="_blank">logging.info</a>('data:%s'%data)<br> data = data['SearchResponse']<br> errors_list = [el['Message'] for el in data['Errors']]<br>
error_text = ','.join(errors_list)<br> raise BingException(error_text)<br> <a href="http://logging.info" target="_blank">logging.info</a>('data:%s'%data)<br> return data<br>
<br> def do_web_search(self, query, extra_args={}):<br>
return self.talk_to_bing(query, sources='web', extra_args=extra_args)<br> <br> def do_image_search(self, query, extra_args={}):<br> return self.talk_to_bing(query, sources='image', extra_args=extra_args)<br>
<br> def do_news_search(self, query, extra_args={}):<br> return self.talk_to_bing(query, sources='news', extra_args=extra_args)<br> <br> def do_spell_search(self, query, extra_args={}):<br> return self.talk_to_bing(query, sources='spell', extra_args=extra_args)<br>
<br> def do_related_search(self, query, extra_args={}):<br> return self.talk_to_bing(query, sources='relatedsearch', extra_args=extra_args)<br> <br> def do_phonebook_search(self, query, extra_args={}):<br>
return self.talk_to_bing(query, sources='Phonebook', extra_args=extra_args)<br> <br> def do_answers_search(self, query, extra_args={}):<br> return self.talk_to_bing(query, sources='InstantAnswer', extra_args=extra_args)<br>
<br>def testmulti( n = 100 ):<br> theurls = []<br> hits = -1<br> bing = Bing(APP_ID) # your APP_ID<br> while hits < n:<br> try:<br> results = bing.talk_to_bing("Flextegrity", sources = "web", extra_args={'web.offset':hits+1})<br>
except BingException:<br> print BingException<br> break<br> <br> hits += len(results['SearchResponse']['Web']['Results'])<br><br> for result in results['SearchResponse']['Web']['Results']:<br>
theurl = result['Url']<br> theurls.append(theurl)<br> <br> return theurls<br><br>def onepass():<br> return testmulti(n = 1)<br><br>def simplecheck():<br> bing = Bing(APP_ID) # your APP_ID <br>
try:<br> results = bing.talk_to_bing("Flextegrity", sources = "web")<br> except BingException:<br> print BingException<br> return results<br><br>def tests():<br> #print simplecheck()<br>
# print onepass()<br> print testmulti(50)<br> <br>if __name__ == "__main__":<br> tests()<br><font color="#888888"> <br><br><br>-- <br>>>> from mars import math<br><a href="http://www.wikieducator.org/Digital_Math" target="_blank">http://www.wikieducator.org/Digital_Math</a><br>
<br>
</font></div><br><br clear="all"><br>-- <br>>>> from mars import math<br><a href="http://www.wikieducator.org/Digital_Math">http://www.wikieducator.org/Digital_Math</a><br><br>