def func_dm(self, screen_name_or_short_id_or_page='', *content): if not content: try: page = int(screen_name_or_short_id_or_page) if screen_name_or_short_id_or_page else 1 except ValueError: return u'Unknown page number: %s.' % screen_name_or_short_id_or_page self._job["data"] = self._api.get_direct_messages(page=page) self._job["title"] = 'Direct Messages: Page %s' % page else: if screen_name_or_short_id_or_page and screen_name_or_short_id_or_page[0] == '#': long_id, long_id_type = self._util.restore_short_id(screen_name_or_short_id_or_page) if long_id_type == db.TYPE_STATUS: status = self._api.get_status(long_id) screen_name = status['user']['screen_name'] else: direct_message = self._api.get_direct_message(long_id) screen_name = direct_message['sender_screen_name'] else: screen_name = screen_name_or_short_id_or_page message = ' '.join(content) try: self._job["data"] = self._api.post_direct_message(screen_name.encode('UTF8'), message.encode('UTF8')) except twitter.ForbiddenError, e: message_length = twitter.actual_len(message) if message_length > twitter.CHARACTER_LIMIT: raise twitter.ForbiddenError('%s\nMaybe too long: %d' % (str(e), message_length)) else: raise e else:
def func_replyall(self, short_ids, *content): def add_mention_user(screen_name): if screen_name not in mention_users and screen_name != self._user['screen_name']: mention_users.append(screen_name) first_long_id = None mention_users = list() for short_id in short_ids.split(','): long_id, long_id_type = self._util.restore_short_id(short_id) try: if long_id_type == db.TYPE_STATUS: if first_long_id is None: first_long_id = long_id data = self._api.get_status(long_id) add_mention_user(data['user']['screen_name']) else: data = self._api.get_direct_message(long_id) add_mention_user(data['sender_screen_name']) for m in re.finditer('@(%s)' % _screen_name_regex, data['text']): add_mention_user(m.group(1)) except twitter.NotFoundError: pass if not mention_users: raise twitter.NotFoundError('Not found.') message = u'%s %s' % (' '.join('@' + x for x in mention_users), ' '.join(content)) try: self._job["data"] = self._api.post_update(message.encode('UTF8'), first_long_id) except twitter.ForbiddenError, e: message_length = twitter.actual_len(message) if message_length > twitter.CHARACTER_LIMIT: raise twitter.ForbiddenError('%s\nMaybe too long: %d' % (str(e), message_length)) else: raise e
def func_rt(self, short_id, *content): long_id, long_id_type = self._util.restore_short_id(short_id) if long_id_type == db.TYPE_DM: raise TypeError('Can not retweet a direct message.') status = self._api.get_status(long_id) if not content and not status['user']['protected']: self._job["data"] = self._api.create_retweet(long_id) self._job["no_duplicate"] = True else: user_msg = ' '.join(content) if user_msg and ord(user_msg[-1]) < 128: user_msg += ' ' if 'retweeted_status' in status: status = status['retweeted_status'] message = u'%sRT @%s' % (user_msg, status['user']['screen_name']) actual_length = twitter.actual_len(message) if actual_length > twitter.CHARACTER_LIMIT: raise ValueError('Content with length %d is too long to be RT.' % actual_length) message_strip_index = twitter.CHARACTER_LIMIT + len(message) - actual_length message = '%s: %s' % (message, status['text']) for m in re.finditer('@%s' % _screen_name_regex, status['text']): m_start = m.start() m_end = m.end() if m_start <= message_strip_index < m_end: message_strip_index = m_start break message_stripped = message[:message_strip_index] self._job["data"] = self._api.post_update(message_stripped.encode('UTF8')) self._job["no_duplicate"] = True
def parse_command(self, cmd): if cmd[0] in (u"-", '-', ' '): args = cmd[1:].split() if args[0] in SHORT_COMMANDS: args[0] = SHORT_COMMANDS[args[0]] if not self._user and args[0] != 'invite': return func_name = 'func_' + args[0] result = getattr(self, func_name)(*args[1:]) if result: self._job["title"] = result else: if not self._user: return try: self._job["data"] = self._api.post_update(cmd.encode('UTF8')) except twitter.ForbiddenError, e: message_length = twitter.actual_len(cmd) if message_length > twitter.CHARACTER_LIMIT: raise twitter.ForbiddenError('%s\nMaybe too long: %d' % (str(e), message_length)) else: raise e else: