示例#1
0
    def addEvent(self, raw_event):
        # do not call the method saveDocument, instead, call this method
        # add an event to the db. raw_event can either be a json or an instance of Event 
        if not type(raw_event) is types.DictType:
            new_event = raw_event.toDict()
        else:
            new_event = raw_event

        event_type = tool.getEventType(new_event)

        if event_type == 'photos':
            new_event = PhotoEvent(new_event)
        else:
            new_event = TweetEvent(new_event)

        new_event.sortElements()
        new_event = new_event.toDict()
        # before adding, find if any event can be merged
        region = new_event['region']
        condition = ({'region.min_lat': region['min_lat'],
                      'region.min_lng': region['min_lng'],
                      'region.max_lat': region['max_lat'],
                      'region.max_lng': region['max_lng']})
        #       condition = {'region.' + k:v for k,v in region.items()}
        old_events = self.getAllDocuments(condition).sort('created_time', -1)

        for old_event in old_events:
            end_time1 = int(new_event[event_type][0]['created_time'])
            begin_time1 = int(new_event[event_type][-1]['created_time'])
            end_time2 = int(old_event[event_type][0]['created_time'])
            begin_time2 = int(old_event[event_type][-1]['created_time'])
            time_interval = InstagramConfig.merge_time_interval
            #           print 'new: ',end_time1,begin_time1
            #           print 'old: ',end_time2,begin_time2
            if end_time1 + time_interval >= begin_time2 and end_time2 + time_interval >= begin_time1:
                # if can merge
                if event_type == 'photos':
                    merged_event = PhotoEvent(old_event)
                else:
                    merged_event = TweetEvent(old_event)
                merged = merged_event.mergeWith(new_event)
                if merged >= 0:
                    print '%d out of %d %s are merged into an old event' % (merged,
                                                                            len(new_event[event_type]),
                                                                            event_type)
                #                   print old_event['_id'], new_event['_id']
                if merged > 0:
                    self.updateDocument(merged_event)
                return merged_event
            # cannot merge
        print 'create a new event'
        super(EventInterface, self).saveDocument(new_event)
        return new_event
示例#2
0
 def __init__(self, event, corpus=None, representor=None):
     self._representor = representor
     self._corpus = corpus
     super(BaseFeature, self).__init__(tool.getEventType(event), event)