OLD | NEW |
---|---|
(Empty) | |
1 #!/usr/bin/env python | |
2 | |
3 # Copyright 2014 The Chromium Authors. All rights reserved. | |
4 # Use of this source code is governed by a BSD-style license that can be | |
5 # found in the LICENSE file. | |
6 | |
7 '''A set of utilities to interface with the Chrome Webstore API.''' | |
8 | |
9 import os | |
10 import SimpleHTTPServer | |
11 import SocketServer | |
12 import httplib | |
13 import json | |
14 import re | |
15 import thread | |
16 import urllib | |
17 import webbrowser | |
18 | |
19 PROJECT_ARGS = { | |
20 'client_id': | |
21 '937534751394-gbj5334v9144c57qjqghl7d283plj5r4.apps.googleusercontent.com', | |
dmazzoni
2014/07/28 05:29:07
nit: 4-space indent for line continuation?
David Tseng
2014/07/29 23:52:18
Done.
| |
22 'grant_type': 'authorization_code', | |
23 'redirect_uri': 'http://localhost:8000' | |
24 } | |
25 | |
26 PORT = 8000 | |
27 | |
28 APP_ID = 'kgejglhpjiefppelpmljglcjbhoiplfn' | |
29 OAUTH_DOMAIN = 'accounts.google.com' | |
30 OAUTH_AUTH_COMMAND = '/o/oauth2/auth' | |
31 OAUTH_TOKEN_COMMAND = '/o/oauth2/token' | |
32 WEBSTORE_API_SCOPE = 'https://www.googleapis.com/auth/chromewebstore' | |
33 | |
34 API_ENDPOINT_DOMAIN = 'www.googleapis.com' | |
35 COMMAND_GET_UPLOAD_STATUS = ( | |
36 '/chromewebstore/v1.1/items/%s?projection=draft' % APP_ID) | |
37 COMMAND_POST_PUBLISH = '/chromewebstore/v1.1/items/%s/publish' % APP_ID | |
38 COMMAND_POST_UPLOAD = '/upload/chromewebstore/v1.1/items/%s' % APP_ID | |
39 | |
40 class CodeRequestHandler(SocketServer.StreamRequestHandler): | |
41 def handle(self): | |
42 content = self.rfile.readline() | |
43 self.server.code = re.search('code=(.*) ', content).groups()[0] | |
44 self.rfile.close() | |
45 | |
46 def GetAuthCode(): | |
47 Handler = CodeRequestHandler | |
48 httpd = SocketServer.TCPServer(("", PORT), Handler) | |
49 auth_url = ''.join([' https://%s%s?' % (OAUTH_DOMAIN, OAUTH_AUTH_COMMAND), | |
50 'response_type=code&', | |
dmazzoni
2014/07/28 05:29:07
How about something like this to aid readability:
David Tseng
2014/07/29 23:52:18
Done.
| |
51 'scope=%s&' % WEBSTORE_API_SCOPE, | |
52 ('client_id=%(client_id)s&' | |
53 'redirect_uri=%(redirect_uri)s' % PROJECT_ARGS)]) | |
54 print 'Navigating to %s' % auth_url | |
55 webbrowser.open(auth_url) | |
56 httpd.handle_request() | |
57 return httpd.code | |
58 | |
59 def GetOauthToken(code, client_secret): | |
60 PROJECT_ARGS['code'] = code | |
61 PROJECT_ARGS['client_secret'] = client_secret | |
62 body = urllib.urlencode(PROJECT_ARGS) | |
63 conn = httplib.HTTPSConnection(OAUTH_DOMAIN) | |
64 conn.putrequest('POST', OAUTH_TOKEN_COMMAND) | |
65 conn.putheader('content-type', 'application/x-www-form-urlencoded') | |
66 conn.putheader('content-length', len(body)) | |
67 conn.endheaders() | |
68 conn.send(body) | |
69 content = conn.getresponse().read() | |
70 return json.loads(content) | |
71 | |
72 def GetPopulatedHeader(client_secret): | |
73 code = GetAuthCode() | |
74 access_token = GetOauthToken(code, client_secret) | |
75 url = 'www.googleapis.com' | |
76 | |
77 return {'Authorization': 'Bearer %(access_token)s' % access_token, | |
78 'x-goog-api-version': 2, | |
79 'Content-Length': 0 | |
80 } | |
81 | |
82 def SendGetCommand(command, client_secret): | |
83 headers = GetPopulatedHeader(client_secret) | |
84 conn = httplib.HTTPSConnection(API_ENDPOINT_DOMAIN) | |
85 conn.request('GET', command, '', headers) | |
86 return conn.getresponse() | |
87 | |
88 def SendPostCommand(command, body, client_secret): | |
89 headers = GetPopulatedHeader(client_secret) | |
90 conn = httplib.HTTPSConnection(API_ENDPOINT_DOMAIN) | |
91 conn.request('PUT', command, body, headers) | |
92 return conn.getresponse() | |
93 | |
94 def GetUploadStatus(client_secret): | |
95 '''Gets the status of a previous upload. | |
96 Args: | |
97 client_secret ChromeVox's client secret creds. | |
98 ''' | |
99 return SendGetCommand(COMMAND_GET_UPLOAD_STATUS, client_secret) | |
100 | |
101 # httplib fails to persist the connection during upload; use curl instead. | |
102 def PostUpload(file, client_secret): | |
103 '''Posts an uploaded version of ChromeVox. | |
104 Args: | |
105 file A string path to the ChromeVox extension zip. | |
106 client_secret ChromeVox's client secret creds. | |
107 ''' | |
108 header = GetPopulatedHeader(client_secret) | |
109 curl_command = ''.join(['curl ', | |
dmazzoni
2014/07/28 05:29:07
I think ' '.join with no spaces in the arguments w
David Tseng
2014/07/29 23:52:18
Done.
| |
110 '-H "Authorization: %(Authorization)s" ' % header, | |
111 '-H "x-goog-api-version: 2" ', | |
dmazzoni
2014/07/28 05:29:07
nit: line up with previous line, and below
David Tseng
2014/07/29 23:52:18
Done.
| |
112 '-X PUT ', | |
113 '-T %s ' % file, | |
114 '-v ', | |
115 'https://%s%s' % (API_ENDPOINT_DOMAIN, | |
116 COMMAND_POST_UPLOAD)]) | |
117 os.system(curl_command) | |
dmazzoni
2014/07/28 05:29:07
Return the result of this system call or throw if
David Tseng
2014/07/29 23:52:18
Done.
| |
118 | |
119 def PostPublish(client_secret): | |
120 '''Publishes an previously uploaded ChromeVox extension. | |
121 Args: | |
122 client_secret ChromeVox's client secret creds. | |
123 ''' | |
124 return SendPostCommand(COMMAND_POST_PUBLISH, client_secret) | |
OLD | NEW |