def test_is_username(self): valid = ['dialelo', 'mental_floss', '4n_4Wfu1_US3RN4M3'] for user in valid: self.failUnless(is_username(user)) invalid = ['-asd', 'adsd?'] for user in invalid: self.failIf(is_username(user))
def test_is_username(self): valid = ['dialelo', 'mental_floss', '4n_4Wfu1_US3RN4M3'] for user in valid: self.assertTrue(is_username(user)) invalid = ['-asd', 'adsd?'] for user in invalid: self.assertFalse(is_username(user))
def apply_attribute(string, hashtag='hashtag', attag='attag', url='url'): """ Apply an attribute to `string` dependending on wether it is a hashtag, a Twitter username or an URL. >>> apply_attribute('#Python') ('hashtag', u'#Python') >>> apply_attribute('@dialelo') ('attag', u'@dialelo') >>> apply_attribute('@dialelo', attag='username') ('username', u'@dialelo') >>> apply_attribute('http://www.dialelo.com') ('url', u'http://www.dialelo.com') >>> apply_attribute('turses') u'turses' """ string = unicode(string) if is_hashtag(string): return (hashtag, string) elif string.startswith('@') and is_username(string[1:]): return (attag, string) elif is_url(string): return (url, string) else: return string
def search_user_handler(self, username): """ Handles creating a timeline tracking the searched user's tweets. """ if username is None: self.info_message(_('Search cancelled')) return # TODO make sure that the user EXISTS and THEN fetch its tweets username = sanitize_username(username) if not is_username(username): self.info_message(_('Invalid username')) return else: self.info_message(_('Fetching latest tweets from @%s' % username)) success_message = _('@%s\'s timeline created' % username) timeline_created = partial(self.info_message, success_message) error_message = _('Unable to create @%s\'s timeline' % username) timeline_not_created = partial(self.error_message, error_message) self.append_timeline(name='@%s' % username, update_function=self.api.get_user_timeline, update_args=username, on_success=timeline_created, on_error=timeline_not_created)
def unfollow_user_handler(self, username): """ Handles unfollowing the user given in `username`. """ if username is None: self.info_message(_('Search cancelled')) return username = sanitize_username(username) if username == self.user.screen_name: self.error_message(_('That doesn\'t make any sense')) return # TODO make sure that the user EXISTS and THEN follow if not is_username(username): self.info_message(_('Invalid username')) return else: self.info_message(_('Unfollowing @%s' % username)) success_message = _('You are no longer following %s' % username) unfollow_done = partial(self.info_message, success_message) error_template = _('We can not ensure that you are not following %s') error_message = error_template % username unfollow_error = partial(self.error_message, error_message) self.api.destroy_friendship(screen_name=username, on_error=unfollow_error, on_success=unfollow_done)
def follow_user_handler(self, username): """ Handles following the user given in `username`. """ if username is None: self.info_message(_('Search cancelled')) return username = sanitize_username(username) if username == self.user.screen_name: self.error_message(_('You can\'t follow yourself')) return # TODO make sure that the user EXISTS and THEN follow if not is_username(username): self.info_message(_('Invalid username')) return else: self.info_message(_('Following @%s' % username)) success_message = _('You are now following @%s' % username) follow_done = partial(self.info_message, success_message) error_template = _('We can not ensure that you are following @%s') error_message = error_template % username follow_error = partial(self.error_message, error_message) self.api.create_friendship(screen_name=username, on_error=follow_error, on_success=follow_done)
def apply_attribute(string, hashtag='hashtag', attag='attag', url='url'): """ Apply an attribute to `string` dependending on wether it is a hashtag, a Twitter username or an URL. >>> apply_attribute('#Python') ('hashtag', u'#Python') >>> apply_attribute('@dialelo') ('attag', u'@dialelo') >>> apply_attribute('@dialelo', attag='username') ('username', u'@dialelo') >>> apply_attribute('http://www.dialelo.com') ('url', u'http://www.dialelo.com') >>> apply_attribute('turses') u'turses' """ string = str(string) if is_hashtag(string): return (hashtag, string) elif string.startswith('@') and is_username(string[1:]): return (attag, string) elif is_url(string): return (url, string) else: return string