Esempio n. 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()
Esempio n. 2
0
def get_group(user):
    try:
        return api_call('identica', 'statusnet/groups/list', {'id':user})
    except:
        print "Error while getting group (%s):" % user
        print_exc()
        return None
Esempio n. 3
0
def get_friends(service, user, page):
    options = {
            'cursor': page,
            'id': user,
            }
    try:
        return pages[service].get(api_call(service, 'statuses/friends', options))
    except:
        print_exc()
        return []
Esempio n. 4
0
File: thread.py Progetto: dodo/blain
def get_friends(typ, service, user, page):
    options = {
            'cursor': page,
            'id': user,
            }
    try:
        return pages[typ].get(api_call(service, 'statuses/friends', options))
    except Exception as e:
        print "[ERROR] while getting friends (%s, %s, %i):"%(service,user,page),e
        return []
Esempio n. 5
0
def get_friends(service, user, page):
    options = {
            'cursor': page,
            'id': user,
            }
    try:
        return pages[service].get(api_call(service, 'statuses/friends', options))
    except:
        print "Error while getting friends (%s, %s, %i):"%(service, user, page)
        print_exc()
        return []
Esempio n. 6
0
def get_page(service, user, count, page, opts={}):
    options = {
            'page': page,
            'count': count,
            'id': user,
            'include_rts': 'true', #get all 200 tweets from twitter
            }
    options.update(opts)
    try:
        return api_call(service, 'statuses/user_timeline', options), True
    except:
        print_exc()
        return [], False
Esempio n. 7
0
File: thread.py Progetto: dodo/blain
def get_page(method, service, user, count, page, opts={}):
    options = {
            'page': page,
            'count': count,
            'id': user,
            'include_rts': 'true', #get all 200 tweets from twitter
            }
    options.update(opts)
    try:
        return api_call(service, method, options), True
    except Exception as e:
        print "[ERROR] while getting user (%s, %s, %i):" % (service,user,page),e
        return [], False
Esempio n. 8
0
File: thread.py Progetto: dodo/blain
 def run(self):
     if not self.service or not self.user:
         self.quit()
         return
     trys = 0
     page = -1
     new_friends = None
     try:
         friendscount = api_call(self.service, 'users/show',
             {'id': self.user})['friends_count']
     except Exception as e:
         print "[ERROR]", e
         self.end()
         return
     old_friends = list(map(unicode,self.friends.allKeys()))
     while friendscount > 0:
         page = next_page(self.account.type, page, new_friends)
         #print "Fetching from friends page %i, %i updates remaining (%s)" % \
         #    (page, friendscount, self.service),"[%i]"%trys
         new_friends = get_friends(
             self.account.type, self.service, self.user, page)
         stop = False
         friendscount -= len(new_friends)
         if len(new_friends) == 0:
             trys += 1
         for friend in new_friends:
             id = unicode(friend['screen_name'])
             if self.friends.contains(id):
                 #print id, "(found)", self.service
                 stop = True
                 if id in old_friends:
                     old_friends.remove(id)
             else:
                 #print id, "(new)", self.service
                 self.app.updates.add_timer("user",self.service,self.user,id)
             dump = json.dumps(clean_urls(friend))
             self.friends.setValue(id, dump)
         if stop or trys > 3: break
         #self.yieldCurrentThread()
     for id in old_friends:
         #print id, "(lost)", self.service
         self.app.updates.remove_timer("user", self.service, self.user, id)
         self.friends.remove(id)
     #print "friends list up-to-date. (%s)" % self.service
     self.end()
Esempio n. 9
0
 def run(self):
     if not self.service or not self.user:
         self.quit()
         return
     trys = 0
     page = -1
     new_friends = None
     try:
         friendscount = api_call(self.service, 'users/show',
             {'id': self.user})['friends_count']
     except:
         print_exc()
         self.end()
         return
     while friendscount > 0:
         page = next_page(self.service, page, new_friends)
         print "Fetching from friends page %i, %i updates remaining (%s)" % \
             (page, friendscount, self.service),"[%i]"%trys
         new_friends = get_friends(self.service, self.user, page)
         stop = False
         friendscount -= len(new_friends)
         if len(new_friends) == 0:
             trys += 1
         for friend in new_friends:
             id = str(friend['screen_name'])
             if self.friends.contains(id):
                 print id, "(found)", self.service
                 stop = True
             else:
                 print id, "(new)", self.service
             dump = json.dumps(friend)
             self.friends.setValue(id, dump)
         if stop or trys > 3: break
         #self.yieldCurrentThread()
     print "friends list up-to-date. (%s)" % self.service
     self.end()
Esempio n. 10
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()
Esempio n. 11
0
File: thread.py Progetto: dodo/blain
def get_group(service, user):
    try:
        return api_call(service, 'statusnet/groups/list', {'id':user})
    except Exception as e:
        print "Error while getting group (%s):" % user, e
        return None