Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Side by Side Diff: sitescripts/oauth2dl/bin/oauth2dl.py

Issue 29833582: Issue 4954 - Implement a downloader script supporting OAuth2 authentication/authorization (Closed)
Patch Set: removed .Python from .gitignore and added .DS_Store to .hgignore Created Aug. 2, 2018, 9:24 a.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « sitescripts/oauth2dl/bin/constants.py ('k') | sitescripts/oauth2dl/test/__init__.py » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 # This file is part of the Adblock Plus web scripts,
2 # Copyright (C) 2006-present eyeo GmbH
3 #
4 # Adblock Plus is free software: you can redistribute it and/or modify
5 # it under the terms of the GNU General Public License version 3 as
6 # published by the Free Software Foundation.
7 #
8 # Adblock Plus is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # GNU General Public License for more details.
12 #
13 # You should have received a copy of the GNU General Public License
14 # along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>.
15
16
17 import argparse
18 import io
19 import os
20 import sys
21 import json
22
23 from httplib2 import Http
24 from oauth2client.service_account import ServiceAccountCredentials
25
26 from sitescripts.oauth2dl.bin import constants as cnts
27
28
29 def download_file(url, key_file, scope):
30 """Download a file using Oauth2.
31
32 Parameters
33 ----------
34 url: str
35 The url of the file we want to download
36 key_file: str
37 Path/ url to key file used in Oauth2
38 scope: str
39 The scope used in Oauth2
40
41 Returns
42 -------
43 dict
44 Headers resulted from the HTTP request.
45 str
46 Content of the file we want to download/ error message if unsuccessful.
47
48 """
49 credentials = ServiceAccountCredentials.from_json_keyfile_name(
50 key_file,
51 scopes=[scope],
52 )
53
54 http_auth = credentials.authorize(Http())
55
56 headers, content = http_auth.request(url)
57 try:
58 content = content.decode('utf-8')
59 finally:
60 return headers, content
61
62
63 def parse_args():
64 """Set up the required arguments and returns them."""
65 parser = argparse.ArgumentParser(description='Download using Oauth2')
66
67 parser.add_argument('url', help='URL to download from')
68 parser.add_argument('-k', '--key', help='Oauth2 key file path',
69 default=os.environ.get('OAUTH2DL_KEY'))
70 parser.add_argument('-s', '--scope', help='Oauth2 scope',
71 default=os.environ.get('OAUTH2DL_SCOPE'))
72 parser.add_argument('-o', help='Path where to save the file.')
73
74 return parser.parse_args()
75
76
77 def main():
78 args = parse_args()
79
80 if args.key is None:
81 sys.exit(cnts.KEYFILE_NOT_FOUND_ERROR)
82
83 if args.scope is None:
84 sys.exit(cnts.SCOPE_NOT_FOUND_ERROR)
85
86 try:
87 headers, content = download_file(args.url, args.key, args.scope)
88 except KeyError as err:
89 sys.exit(cnts.INVALID_KEY_FILE.format(str(err), str(args.key)))
90 except Exception as err:
91 sys.exit(err)
92
93 if headers['status'] != '200':
94 try:
95 error_json = json.loads(content, encoding='utf-8')
96 sys.exit(cnts.GOOGLE_OAUTH_ERROR.format(
97 str(error_json['error']['code']),
98 str(error_json['error']['message']),
99 ))
100 except ValueError:
101 sys.exit(content)
102
103 if args.o is None:
104 sys.stdout.write(content)
105 else:
106 with io.open(args.o, encoding='utf-8', mode='wb') as f:
107 f.write(content)
108
109
110 if __name__ == '__main__':
111 main()
OLDNEW
« no previous file with comments | « sitescripts/oauth2dl/bin/constants.py ('k') | sitescripts/oauth2dl/test/__init__.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld