Skip to content

Instantly share code, notes, and snippets.

@dch
Created August 13, 2012 08:23
Show Gist options
  • Save dch/3338191 to your computer and use it in GitHub Desktop.
Save dch/3338191 to your computer and use it in GitHub Desktop.
app.net command line app
#!/usr/bin/env python
"""appdotnet
Usage:
appdotnet authenticate <client_id>
appdotnet set-accesstoken <access_token>
appdotnet global
appdotnet
appdotnet -h | --help
appdotnet --version
Options:
-h --help Show this screen.
--version Show version.
"""
import re
import sys
import ConfigParser
import requests # pip install --user requests
import envoy # pip install --user envoy
import docopt # pip install --user docopt
class AppDotNet(object):
def __init__(self):
self.config_path = os.path.expanduser('~/.appdotnot.cfg')
self.config = ConfigParser.ConfigParser(allow_no_value = True)
self.config.add_section('Service')
self.config.set('Service', 'URL', 'https://alpha-api.app.net/stream/0')
self.config.add_section('Authentication')
self.config.set('Authentication', 'redirect_uri', 'x-appdotnethelper:///')
self.config.set('Authentication', 'URL', 'https://alpha.app.net/oauth/authenticate')
self.config.read(self.config_path)
self.synchronize()
############################################################################
@property
def clientID(self):
if self.config.has_option('Authentication', 'client_id'):
return self.config.get('Authentication', 'client_id')
else:
return None
@clientID.setter
def clientID(self, clientID):
self.config.set('Authentication', 'client_id', clientID)
@property
def accessToken(self):
if self.config.has_option('Authentication', 'access_token'):
return self.config.get('Authentication', 'access_token')
else:
return None
@accessToken.setter
def accessToken(self, accessToken):
self.config.set('Authentication', 'access_token', accessToken)
def synchronize(self):
self.config.write(file(self.config_path, 'wb'))
############################################################################
def authenticate(self):
theRedirectURI = self.config.get('Authentication', 'redirect_uri')
theScopes = 'stream email write_post follow messages export'
theURL = '%(URL)s?client_id=%(client_id)s&response_type=token&redirect_uri=%(redirect_uri)s&scope=%(scopes)s'
theURL = theURL % dict(
URL = self.config.get('Authentication', 'URL'),
client_id = self.client_id,
redirect_uri=theRedirectURI,
scopes=theScopes)
# theURL = re.sub(r'[\n\r][ \t]*', '', theURL)
theURL = re.sub(r' ', '%20', theURL)
theCommand = 'open \'%s\'' % theURL
r = envoy.run(theCommand)
def list_global(self):
theURL = '%(URL)s/posts/stream/global' % dict(URL = self.config.get('Service', 'URL'))
theHeaders = { 'Authorization': 'Bearer %s' % self.accessToken }
r = requests.get(theURL, headers = theHeaders)
for thePost in r.json:
print '[@%s]: %s' % (thePost['user']['username'], thePost['text'])
if __name__ == '__main__':
argv=sys.argv[1:]
argv = 'global'
arguments = docopt.docopt(__doc__, argv=argv, version='appdotnet')
theApp = AppDotNet()
if arguments['authenticate'] and arguments['<client_id>']:
theApp.clientID = arguments['<client_id>']
theApp.synchronize()
theApp.authenticate()
elif arguments['set-accesstoken'] and arguments['<access_token>']:
theApp.accessToken = arguments['<access_token>']
theApp.synchronize()
elif arguments['global']:
theApp.list_global()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment