ade@google.com | c7da92b | 2010-10-25 20:03:48 -0400 | [diff] [blame] | 1 | # Copyright (C) 2010 Google Inc. |
| 2 | # |
| 3 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | # you may not use this file except in compliance with the License. |
| 5 | # You may obtain a copy of the License at |
| 6 | # |
| 7 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | # |
| 9 | # Unless required by applicable law or agreed to in writing, software |
| 10 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | # See the License for the specific language governing permissions and |
| 13 | # limitations under the License. |
| 14 | import optparse |
| 15 | import httplib2 |
| 16 | import logging |
| 17 | import oauth_wrap |
| 18 | import os |
| 19 | import sys |
| 20 | |
| 21 | def load_properties_file(path): |
| 22 | properties = {} |
| 23 | for line in open(path): |
| 24 | line = line.strip() |
| 25 | if line.startswith('#'): |
| 26 | continue |
| 27 | |
| 28 | key,value = line.split('=') |
| 29 | properties[key.strip()] = value.strip() |
| 30 | return properties |
| 31 | |
| 32 | def fetch(url): |
| 33 | logging.debug('Now fetching: %s' % url) |
| 34 | |
| 35 | path = os.path.expanduser('~/.oacurl.properties') |
| 36 | if not os.path.exists(path): |
| 37 | logging.debug('User is not logged in.') |
| 38 | |
| 39 | print 'You are not logged in' |
| 40 | sys.exit(1) |
| 41 | properties = load_properties_file(path) |
| 42 | oauth_parameters = { |
| 43 | 'consumer_key': properties['consumerKey'], |
| 44 | 'consumer_secret' : properties['consumerSecret'], |
| 45 | 'oauth_token' : properties['accessToken'], |
| 46 | 'oauth_token_secret':properties['accessTokenSecret']} |
| 47 | |
| 48 | http = oauth_wrap.get_authorised_http(oauth_parameters) |
| 49 | response, content = http.request(url) |
| 50 | logging.debug(response) |
| 51 | logging.debug(content) |
| 52 | |
| 53 | return response,content |
| 54 | |
| 55 | def get_command(args): |
| 56 | if args[0] == 'fetch': |
| 57 | return 'fetch' |
| 58 | return None |
| 59 | |
| 60 | def configure_logging(options): |
| 61 | if options.verbose: |
| 62 | logging.basicConfig(level=logging.DEBUG) |
| 63 | |
| 64 | def main(): |
| 65 | usage = '''Usage: %prog [options] fetch <url> |
| 66 | Example: %prog -v fetch "https://www.googleapis.com/buzz/v1/people/@me/@self?alt=json&pp=1" |
| 67 | ''' |
| 68 | parser = optparse.OptionParser(usage=usage) |
| 69 | parser.set_defaults(verbose=False) |
| 70 | parser.add_option('-v', '--verbose', action='store_true', dest='verbose') |
| 71 | parser.add_option('-q', '--quiet', action='store_false', dest='verbose') |
| 72 | |
| 73 | (options, args) = parser.parse_args() |
| 74 | |
| 75 | configure_logging(options) |
| 76 | logging.debug('Options: %s and Args: %s' % (str(options), str(args))) |
| 77 | |
| 78 | command = get_command(args) |
| 79 | |
| 80 | if not command: |
| 81 | parser.error('Invalid arguments') |
| 82 | return |
| 83 | |
| 84 | if command == 'fetch': |
| 85 | response, content = fetch(args[1]) |
| 86 | print response |
| 87 | print content |
| 88 | |
| 89 | if __name__ == '__main__': |
| 90 | main() |