<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">&lt;<a href="mailto:kirby.urner@gmail.com">kirby.urner@gmail.com</a>&gt;</span><br>
Date: Wed, Dec 30, 2009 at 5:49 PM<br>Subject: student project (market reseach)<br><br><br><br>&quot;&quot;&quot;<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>&quot;&quot;&quot;<br><br>import urllib2<br>import urllib<br>import simplejson<br>import logging<br><br>APP_ID = &quot;your_app_id_here&quot;<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 = &#39;log.log&#39;<br>        self.end_point = &#39;<a href="http://api.search.live.net/json.aspx?Appid=%s&amp;%27%app_id" target="_blank">http://api.search.live.net/json.aspx?Appid=%s&amp;&#39;%app_id</a><br>

        logging.basicConfig(level=loglevel,<br>                          format=&#39;%(asctime)s %(name)-6s %(levelname)-8s %(message)s&#39;,<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>(&#39;Query:%s&#39;%query)<br>        <a href="http://logging.info" target="_blank">logging.info</a>(&#39;Sources:%s&#39;%sources)<br>

        <a href="http://logging.info" target="_blank">logging.info</a>(&#39;Other Args:%s&#39;%extra_args)<br>        <br>        payload={}<br>        payload[&#39;Appid&#39;] = self.app_id<br>        payload[&#39;query&#39;] = query<br>

        payload[&#39;sources&#39;] = 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>(&#39;final_url:%s&#39;%final_url)<br>

        response = urllib.urlopen(final_url)<br>        data = simplejson.load(response)<br>        if &#39;Errors&#39; in data[&#39;SearchResponse&#39;]:<br>            <a href="http://logging.info" target="_blank">logging.info</a>(&#39;Error&#39;)<br>

            <a href="http://logging.info" target="_blank">logging.info</a>(&#39;data:%s&#39;%data)<br>            data = data[&#39;SearchResponse&#39;]<br>            errors_list = [el[&#39;Message&#39;] for el in data[&#39;Errors&#39;]]<br>

            error_text = &#39;,&#39;.join(errors_list)<br>            raise BingException(error_text)<br>        <a href="http://logging.info" target="_blank">logging.info</a>(&#39;data:%s&#39;%data)<br>        return data<br>
    <br>    def do_web_search(self, query, extra_args={}):<br>
        return self.talk_to_bing(query, sources=&#39;web&#39;, extra_args=extra_args)<br>    <br>    def do_image_search(self, query, extra_args={}):<br>        return self.talk_to_bing(query, sources=&#39;image&#39;, extra_args=extra_args)<br>

    <br>    def do_news_search(self, query, extra_args={}):<br>        return self.talk_to_bing(query, sources=&#39;news&#39;, extra_args=extra_args)<br>    <br>    def do_spell_search(self, query, extra_args={}):<br>        return self.talk_to_bing(query, sources=&#39;spell&#39;, extra_args=extra_args)<br>

    <br>    def do_related_search(self, query, extra_args={}):<br>        return self.talk_to_bing(query, sources=&#39;relatedsearch&#39;, extra_args=extra_args)<br>    <br>    def do_phonebook_search(self, query, extra_args={}):<br>

        return self.talk_to_bing(query, sources=&#39;Phonebook&#39;, extra_args=extra_args)<br>    <br>    def do_answers_search(self, query, extra_args={}):<br>        return self.talk_to_bing(query, sources=&#39;InstantAnswer&#39;, 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 &lt; n:<br>        try:<br>            results = bing.talk_to_bing(&quot;Flextegrity&quot;, sources = &quot;web&quot;, extra_args={&#39;web.offset&#39;:hits+1})<br>

        except BingException:<br>            print BingException<br>            break<br>        <br>        hits += len(results[&#39;SearchResponse&#39;][&#39;Web&#39;][&#39;Results&#39;])<br><br>        for result in results[&#39;SearchResponse&#39;][&#39;Web&#39;][&#39;Results&#39;]:<br>

            theurl = result[&#39;Url&#39;]<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(&quot;Flextegrity&quot;, sources = &quot;web&quot;)<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__ == &quot;__main__&quot;:<br>   tests()<br><font color="#888888">   <br><br><br>-- <br>&gt;&gt;&gt; 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>&gt;&gt;&gt; from mars import math<br><a href="http://www.wikieducator.org/Digital_Math">http://www.wikieducator.org/Digital_Math</a><br><br>