[Edu-sig] Fwd: student project (market reseach)

kirby urner kirby.urner at gmail.com
Thu Dec 31 02:50:20 CET 2009


Greetings to edu-sig friends, HNY!
Kirby




---------- Forwarded message ----------
From: kirby urner <kirby.urner at gmail.com>
Date: Wed, Dec 30, 2009 at 5:49 PM
Subject: student project (market reseach)



"""

Slinging code from this resource:
http://uswaretech.com/blog/2009/06/bing-python-api/

More background in blog:
http://mybizmo.blogspot.com/2009/12/back-stage.html

Also edu-sig:
open source buzzbot project proposal
http://mail.python.org/pipermail/edu-sig/2009-September/009520.html

"""

import urllib2
import urllib
import simplejson
import logging

APP_ID = "your_app_id_here"

class BingException(Exception):
    pass

class Bing(object):
    def __init__(self, app_id, loglevel=logging.INFO):
        self.app_id = app_id
        self.log_filename = 'log.log'
        self.end_point = '
http://api.search.live.net/json.aspx?Appid=%s&'%app_id<http://api.search.live.net/json.aspx?Appid=%s&%27%app_id>
        logging.basicConfig(level=loglevel,
                          format='%(asctime)s %(name)-6s %(levelname)-8s
%(message)s',
                          filename=self.log_filename)


    def talk_to_bing(self, query, sources, extra_args={}):
        logging.info('Query:%s'%query)
        logging.info('Sources:%s'%sources)
        logging.info('Other Args:%s'%extra_args)

        payload={}
        payload['Appid'] = self.app_id
        payload['query'] = query
        payload['sources'] = sources
        payload.update(extra_args)
        query_string = urllib.urlencode(payload)
        final_url = self.end_point + query_string
        logging.info('final_url:%s'%final_url)
        response = urllib.urlopen(final_url)
        data = simplejson.load(response)
        if 'Errors' in data['SearchResponse']:
            logging.info('Error')
            logging.info('data:%s'%data)
            data = data['SearchResponse']
            errors_list = [el['Message'] for el in data['Errors']]
            error_text = ','.join(errors_list)
            raise BingException(error_text)
        logging.info('data:%s'%data)
        return data

    def do_web_search(self, query, extra_args={}):
        return self.talk_to_bing(query, sources='web',
extra_args=extra_args)

    def do_image_search(self, query, extra_args={}):
        return self.talk_to_bing(query, sources='image',
extra_args=extra_args)

    def do_news_search(self, query, extra_args={}):
        return self.talk_to_bing(query, sources='news',
extra_args=extra_args)

    def do_spell_search(self, query, extra_args={}):
        return self.talk_to_bing(query, sources='spell',
extra_args=extra_args)

    def do_related_search(self, query, extra_args={}):
        return self.talk_to_bing(query, sources='relatedsearch',
extra_args=extra_args)

    def do_phonebook_search(self, query, extra_args={}):
        return self.talk_to_bing(query, sources='Phonebook',
extra_args=extra_args)

    def do_answers_search(self, query, extra_args={}):
        return self.talk_to_bing(query, sources='InstantAnswer',
extra_args=extra_args)

def testmulti( n = 100 ):
    theurls = []
    hits = -1
    bing = Bing(APP_ID)  # your APP_ID
    while hits < n:
        try:
            results = bing.talk_to_bing("Flextegrity", sources = "web",
extra_args={'web.offset':hits+1})
        except BingException:
            print BingException
            break

        hits += len(results['SearchResponse']['Web']['Results'])

        for result in results['SearchResponse']['Web']['Results']:
            theurl = result['Url']
            theurls.append(theurl)

    return theurls

def onepass():
    return testmulti(n = 1)

def simplecheck():
    bing = Bing(APP_ID)  # your APP_ID
    try:
        results = bing.talk_to_bing("Flextegrity", sources = "web")
    except BingException:
        print BingException
    return results

def tests():
    #print simplecheck()
    # print onepass()
    print testmulti(50)

if __name__ == "__main__":
   tests()



-- 
>>> from mars import math
http://www.wikieducator.org/Digital_Math




-- 
>>> from mars import math
http://www.wikieducator.org/Digital_Math
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/edu-sig/attachments/20091230/7c02b2ba/attachment.htm>


More information about the Edu-sig mailing list