Пример #1
0
 def build_conversation(self, account, previous):
     if not previous: return # nothing to do
     previous = [drug(**previous)]
     #print "try to build conversation",previous[0].pid,"(%s)"%account.service
     Post, Conversation = self.db.Post, self.db.Conversation
     while True:
         posts = Post.find().filter_by(pid = previous[-1].reply).all()
         if posts:
             previous.append(prepare_post(posts[0].__dict__))
         else:
             try:
                 status = api_call(account.service, 'statuses/user_timeline',
                     {'id': previous[-1].replied_user,
                      'max_id':previous[-1].reply,
                      'count': 1})
             except:
                 print "[ERROR] during conversion fetch"
                 print_exc()
                 break
             if not status: break # nothing fetched or empty list
             update = parse_post(account, "", status[0])
             update['source_id'] = update['user_id']
             #print account.service, "con", update['pid']
             update['by_conversation'] = True
             Post(**update).add()
             previous.append(drug(**update))
         if previous[-1].reply is None: break
     if len(previous) == 1: return # still no conversation
     ids = " ".join(list(map(lambda p: str(p.pid), previous[1:])))
     Conversation(pid = previous[0].pid, ids = ids).save()
     #print "conversation", previous[0].pid, "build."
     self.app.reader.update()
Пример #2
0
    def run(self):
        if not self.service or not self.user:
            self.app.logStatus.emit("Error: no user given! ("+self.service+")")
            self.app.killThread.emit(self.id)
            self.quit()
            return

        ok = False
        n, i = 1, 0
        _id = ''
        trys = 0
        page = 1
        step = 200

        new_statuses = None

        try:
            res = api_call(self.service, 'users/show', {'id': self.user})
            protected = res['protected']
            servicecount = res['statuses_count']
            ok = True
        except:
            print_exc()
            protected = True # by error :P
        if protected:
            self.app.updates.updates.user(self.service, self.user, 0, ok)
            self.app.killThread.emit(self.id)
            self.quit()
            return
        while servicecount > 0:
            fetch_count = min(n==1 and int(step/10) or step, servicecount)
            print "%i Fetching %i from page %i, %i updates remaining (%s)" % \
                (n, fetch_count, page, servicecount, self.service),"[%i]"%trys
            new_statuses,new_ok = get_page(self.service, self.user, fetch_count,
                page, page == 2 and _id and {'max_id':_id} or {})
            ok = ok and new_ok
            stop = False
            if n > 1:
                servicecount -= len(new_statuses)
                if len(new_statuses) == 0:
                    trys += 1
            for status in new_statuses:
                _id = str(status['id'])
                id = self.service + _id
                if status['id'] in self.knownids:
                    print id, "(found)"
                    stop = True
                else:
                    print id
                    i += 1
                    update = parse_post(self.service, status)
                    self.knownids.append(status['id'])
                    self.app.addMessage.emit(self.service, update)
            n += 1
            #self.yieldCurrentThread()
            if stop or trys > 3: break
            if fetch_count != int(step/10):
                page += 1

        if i:
            self.app.logStatus.emit("%i updates on %s from %s" % \
                (i, self.service, self.user))
        else:
            self.app.logStatus.emit("Error: no results for {0} on {1}!".\
                format(self.user, self.service))
        self.app.updates.updates.user(self.service, self.user, i, ok)
        print self.service + " done."
        self.app.killThread.emit(self.id)
        self.quit()