I am working on integrating social search through the Datasift API into my
project.
The API gives a json object, and I have printed part of it out to the
screen, so I know that the link is working and I am receiving the right
stuff.  I now want to save some of the fields from the json to a model.
models.py:
class SearchResult(models.Model):
    sm_User = models.CharField(max_length = 100)
    sm_Name = models.CharField(max_length = 100)
    sm_created_at = models.CharField(max_length = 100)
    sm_content = models.CharField(max_length = 160)
    sm_Source = models.CharField(max_length = 100)
    sm_Link = models.CharField(max_length = 150)
    sm_type = models.CharField(max_length = 50)

    def __unicode__(self):
        return self.sm_content

views.py
def runsearch(request, dSearch_id):
    search = dSearch.objects.get(pk = dSearch_id)
    search_name = str(search.dName)
    search_keywords = str(search.dKeywords)
    template = loader.get_template('myapp/runsearch.html')
    results = SearchResult.objects.all()
    if len(results)==0:
        getinteractions.GetPosts(search_keywords)
    results = SearchResult.objects.all()
    context = Context({
        'search_name': search_name,
        'search_keywords': search_keywords,
    })
    return HttpResponse(template.render(context))

in getinteractiions.py:
def GetPosts(keyword):
    user = datasift.User('nigellegg', '1d940b94903285ff73ba0dbe1b13a8a3')
    csdl = 'interaction.content contains "football" and language.tag ==
"en"'
    definition = user.create_definition(csdl)
    consumer = definition.get_consumer(EventHandler(), 'http')
    consumer.consume()
    consumer.run_forever()

class EventHandler(datasift.StreamConsumerEventHandler):
    def __init__(self):
        self.num = 10

 def on_interaction(self, consumer, interaction, hash):
        result = SearchResult()
        result.sm_type = interaction['interaction']['type']
        if interaction['interaction']['type']=='twitter':
            result.sm_User =
interaction['interaction']['author']['username']
            result.sm_Name = interaction['interaction']['author']['name']
        result.sm_created_at = interaction['interaction']['created_at']
        result.sm_content =
interaction['interaction']['content'].encode('ascii', 'replace')
        result.sm_Source = interaction['interaction']['source']
        result.sm_Link = interaction['interaction']['link']
        result.save()
        self.num -= 1
        if self.num == 0:
            #Stopping consumer...
            consumer.stop()

The page shows ok in the browser, but there are no results.  Looking at the
server log (development server) I get
result.sm_Link = interaction['interaction']['link']
KeyError: 'link'

Is this a reserved name problem, or have I made another error? I'm not sure
I can change the field name, as that is how it came to me from datasift.
Many thanks in advance.
Nigel Legg
http://www.trevanianlegg.co.uk
http://twitter.com/nigellegg
http://uk.linkedin.com/in/nigellegg

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/django-users.
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to