def refreshContacts(self): """Refresh contacts of LineClient """ contact_ids = self._getAllContactIds() contacts = self._getContacts(contact_ids) self.contacts = [LineContact(self, contact) for contact in contacts] self.contacts.sort()
def getProfile(self): """Get `profile` of LINE account""" if self._check_auth(): self.profile = LineContact(self, self._getProfile()) return self.profile return None
def _findContactByUserid(self, userid): """Find a `contact` by userid :param userid: user id """ try: contact = self._findContactByUserid(userid) except TalkException as e: self.raise_error(e.reason) return LineContact(self, contact)
def refreshContacts(self): """Refresh contacts of LineClient """ if self._check_auth(): contact_ids = self._getAllContactIds() contacts = self._getContacts(contact_ids) self.contacts = [] for contact in contacts: self.contacts.append(LineContact(self, contact)) self.contacts.sort()
def findAndAddContactByUserid(self, userid): """Find and add a `contact` by userid :param userid: user id """ try: contact = self._findAndAddContactsByUserid(userid) except TalkException as e: self.raise_error(e.reason) contact = contact.values()[0] for c in self.contacts: if c.id == contact.mid: self.raise_error("%s already exists" % contact.displayName) return c = LineContact(self, contact) self.contacts.append(c) self.contacts.sort() return c
def _findAndAddContactByEmail(self, email): """Find and add a `contact` by email :param email: email """ try: contact = self._findAndAddContactsByEmail(email) except TalkException as e: self.raise_error(e.reason) contact = contact.values()[0] for c in self.contacts: if c.id == contact.mid: self.raise_error("%s already exists" % contact.displayName) return c = LineContact(self, contact.values()[0]) self.contacts.append(c) self.contacts.sort() return c
def _findAndAddContactByPhone(self, phone): """Find and add a `contact` by phone number :param phone: phone number (unknown format) """ try: contact = self._findAndAddContactsByPhone(phone) except TalkException as e: self.raise_error(e.reason) contact = contact.values()[0] for c in self.contacts: if c.id == contact.mid: self.raise_error("%s already exists" % contact.displayName) return c = LineContact(self, contact.values()[0]) self.contacts.append(c) self.contacts.sort() return c
def longPoll(self, count=50, debug=False): """Receive a list of operations that have to be processed by original Line cleint. :param count: number of operations to get from :returns: a generator which returns operations >>> for op in client.longPoll(): sender = op[0] receiver = op[1] message = op[2] print "%s->%s : %s" % (sender, receiver, message) """ """Check is there any operations from LINE server""" OT = OperationType try: operations = self._fetchOperations(self.revision, count) except EOFError: return except TalkException as e: if e.code == 9: self.raise_error("user logged in to another machine") else: return for operation in operations: if debug: print operation if operation.type == OT.END_OF_OPERATION: pass elif operation.type == OT.SEND_MESSAGE: pass elif operation.type == OT.RECEIVE_MESSAGE: message = LineMessage(self, operation.message) raw_sender = operation.message._from raw_receiver = operation.message.to sender = self.getContactOrRoomOrGroupById(raw_sender) receiver = self.getContactOrRoomOrGroupById(raw_receiver) # If sender is not found, check member list of group chat sent to if sender is None: try: for m in receiver.members: if m.id == raw_sender: sender = m break except (AttributeError, TypeError): pass # If sender is not found, check member list of room chat sent to if sender is None: try: for m in receiver.contacts: if m.id == raw_sender: sender = m break except (AttributeError, TypeError): pass if sender is None or receiver is None: self.refreshGroups() self.refreshContacts() self.refreshActiveRooms() sender = self.getContactOrRoomOrGroupById(raw_sender) receiver = self.getContactOrRoomOrGroupById(raw_receiver) if sender is None or receiver is None: contacts = self._getContacts([raw_sender, raw_receiver]) if contacts: if len(contacts) == 2: sender = LineContact(self, contacts[0]) receiver = LineContact(self, contacts[1]) yield (sender, receiver, message) elif operation.type in [60, 61]: pass else: print "[*] %s" % OT._VALUES_TO_NAMES[operation.type] print operation self.revision = max(operation.revision, self.revision)
def getProfile(self): """Get `profile` of LINE account""" self.profile = LineContact(self, self._getProfile()) return self.profile