Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
tree: 268063ba8d
Fetching contributors…

Cannot retrieve contributors at this time

executable file 109 lines (83 sloc) 3.326 kB
#!/usr/bin/env python
# TODO: Add support for gnome-keyring here too so the user doen't have to save
# their password in plaintext in a .conkrc or whatever.
import sys, re, optparse
import redditmonitor
USAGE = '''%prog [options] username password [format]
The format string needs to contain at least one of the following tokens:
%c comment karma
%k karma score
%m new messages
%u user name (optional)'''
FORMAT = 'You have %m new messages on reddit.'
class InvalidFormatStringException(Exception):
pass
def validate_format_string(format_string):
tokens = re.search('(%[kcm])', format_string)
if tokens:
return format_string
else:
raise InvalidFormatStringException
if __name__ == '__main__':
parser = optparse.OptionParser()
parser.add_option('-k', '--keyring', action='store_true', dest='keyring', default=False,
help='get the username & password from gnome-keyring (username & password are optional if this is true)')
parser.usage = USAGE
(options, args) = parser.parse_args()
if options.keyring:
try:
import gnomekeyring as keyring
import glib
glib.set_application_name('reddit_status')
except ImportError:
parser.print_usage()
print 'reddit_status: error: install gnome-keyring or supply the username & password'
sys.exit(1)
try:
creds = keyring.find_items_sync(keyring.ITEM_GENERIC_SECRET,
{'app_ident' : 'Reddit Monitor'})
# TODO: We're selecting the newest one out of laziness. Is there a
# better solution? Yes.
username = creds[0].attributes['username']
password = creds[0].secret
except keyring.NoMatchError:
parser.print_usage()
print 'reddit_status: error: no password found in gnome-keyring'
sys.exit(1)
try:
format = args[0]
except IndexError:
format = FORMAT
else:
try:
username = args[0]
password = args[1]
except IndexError:
parser.print_usage()
sys.exit(2)
try:
format = args[2]
except IndexError:
format = FORMAT
try:
format = validate_format_string(format)
except InvalidFormatStringException:
parser.print_usage()
print 'reddit_status: error: format string contains errors'
sys.exit(2)
session = redditmonitor.Reddit()
try:
session.login(username, password)
except redditmonitor.RedditInvalidUsernamePasswordException:
parser.print_usage()
print 'reddit_status: error: the username or password is incorrect'
sys.exit(1)
karma, comment_karma = session.get_karma()
messages = len(session.get_new_mail())
format = format.replace('\\n', '\n')
format = format.replace('%k', str(karma))
format = format.replace('%c', str(comment_karma))
format = format.replace('%m', str(messages))
format = format.replace('%u', sys.argv[1])
print format
sys.exit(0)
Jump to Line
Something went wrong with that request. Please try again.