2
# -*- coding: UTF-8 -*-
5
A small script for discovering Twitter users you follow who never
12
__author__ = 'Chris Lasher'
13
__email__ = 'chris DOT lasher <AT> gmail DOT com'
19
# Requires python-twitter
20
# see http://code.google.com/p/python-twitter/
24
# Configuration file name
25
RC_FILE = '.emptytwitsrc'
26
# By default, this program will look under the user's home directory for
28
RC_PATH = os.path.expanduser('~') + os.sep + RC_FILE
30
# The configuration file should be a file containing one section with
31
# two variables: username and password. For example, it should look
32
# something like below:
41
class UserInfoError(Exception):
43
Error raised when there is not enough information from the user
44
to obtain a handle for the API.
51
def make_cli_parser():
54
"python %prog [OPTIONS]",
57
By default, checks for a configuration file containing your user name
58
and password, for security purposes. You can alternatively specify
59
your user name and password directly on the command line.
61
python %prog -u USERNAME -p PASSWORD
63
See help for more details.
68
cli_parser = optparse.OptionParser(usage)
69
cli_parser.add_option('-c', '--config',
71
help="Specify a different path to a configuration file"
72
" [default: %default]"
74
cli_parser.add_option('-u', '--username',
75
help="Specify a user name directly"
77
cli_parser.add_option('-p', '--password',
78
help="Specify a password directly"
84
def find_non_tweeters(account, users):
86
Identifies Twitter users who have never tweeted.
89
-`account`: A Twitter API instance for the user's account
90
-`users`: Users of interest
93
-`non_tweeters`: a list of user instances for users who have never
98
non_tweeters = [user for user in users if not user.status]
102
def get_config_username_and_password(config_file_path):
104
if not os.path.isfile(config_file_path):
105
raise IOError("%s doesn't exist or isn't readable" % (
107
config = ConfigParser.ConfigParser()
108
config.read(config_file_path)
109
if config.has_option(USER_SECTION, 'username'):
110
username = config.get(USER_SECTION, 'username')
114
if config.has_option(USER_SECTION, 'password'):
115
password = config.get(USER_SECTION, 'password')
119
return username, password
122
def get_username_and_password(cli_opts):
123
username = cli_opts.username
124
password = cli_opts.password
125
if not (username and password):
126
config_file_path = cli_opts.config
127
cfg_username, cfg_password = \
128
get_config_username_and_password(config_file_path)
131
username = cfg_username
133
password = cfg_password
135
if not (username and password):
136
raise UserInfoError("Not enough information obtained.")
138
return username, password
142
cli_parser = make_cli_parser()
143
opts, args = cli_parser.parse_args(argv)
145
username, password = get_username_and_password(opts)
147
MSG = "Configuration %s does not exist or is not readable." % (
149
cli_parser.error(MSG)
150
except UserInfoError:
151
MSG = "Not enough user information specified."
152
cli_parser.error(MSG)
153
account = twitter.Api(username, password)
154
friends = account.GetFriends()
155
non_tweeters = find_non_tweeters(account, friends)
156
for friend in non_tweeters:
157
print friend.screen_name
160
if __name__ == '__main__':