diff --git a/oacurl.py b/oacurl.py index 68b4ca9..64493b5 100644 --- a/oacurl.py +++ b/oacurl.py @@ -11,13 +11,18 @@ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. -import optparse +from apiclient.discovery import build +from apiclient.oauth import FlowThreeLegged + +import datetime import httplib2 import logging import oauth_wrap +import optparse import os import sys + def load_properties_file(path): properties = {} for line in open(path): @@ -29,6 +34,20 @@ def load_properties_file(path): properties[key.strip()] = value.strip() return properties + +def save_properties(consumer_key, consumer_secret, token_key, token_secret, path): + file = open(path, 'w') + + # File format and order is based on oacurl.java's defaults + now = datetime.datetime.today() + now_string = now.strftime('%a %b %d %H:%m:%S %Z %Y') + file.write('#%s\n' % now_string) + file.write('consumerSecret=%s\n' % consumer_secret) + file.write('accessToken=%s\n' % token_key) + file.write('consumerKey=%s\n' % consumer_key) + file.write('accessTokenSecret=%s\n' % token_secret) + file.close() + def fetch(url): logging.debug('Now fetching: %s' % url) @@ -38,6 +57,7 @@ def fetch(url): print 'You are not logged in' sys.exit(1) + properties = load_properties_file(path) oauth_parameters = { 'consumer_key': properties['consumerKey'], @@ -52,7 +72,48 @@ def fetch(url): return response,content + +def buzz_login(): + buzz_discovery = build("buzz", "v1").auth_discovery() + + flow = FlowThreeLegged(buzz_discovery, + consumer_key='anonymous', + consumer_secret='anonymous', + user_agent='google-api-client-python-buzz-cmdline/1.0', + domain='anonymous', + scope='https://www.googleapis.com/auth/buzz', + xoauth_displayname='oacurl.py') + + authorize_url = flow.step1_get_authorize_url() + + print 'Go to the following link in your browser:' + print authorize_url + print + + accepted = 'n' + while accepted.lower() == 'n': + accepted = raw_input('Have you authorized me? (y/n) ') + verification = raw_input('What is the verification code? ').strip() + + credentials = flow.step2_exchange(verification) + path = os.path.expanduser('~/.oacurl.properties') + save_properties('anonymous', 'anonymous', credentials.token.key, credentials.token.secret,path) + + +def generic_login(): + #TODO(ade) Implement support for other services + print 'Support for services other than Buzz is not implemented yet. Sorry.' + +def login(options): + if options.buzz: + buzz_login() + else: + generic_login() + + def get_command(args): + if args[0] == 'login': + return 'login' if args[0] == 'fetch': return 'fetch' return None @@ -69,6 +130,7 @@ def main(): parser.set_defaults(verbose=False) parser.add_option('-v', '--verbose', action='store_true', dest='verbose') parser.add_option('-q', '--quiet', action='store_false', dest='verbose') + parser.add_option('--buzz', action='store_true', dest='buzz') (options, args) = parser.parse_args() @@ -85,6 +147,10 @@ def main(): response, content = fetch(args[1]) print response print content + return + + if command == 'login': + login(options) if __name__ == '__main__': main() \ No newline at end of file