<commit_before>#!/usr/bin/env python

# Copyright 2014 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

'''A set of utilities to interface with the Chrome Webstore API.'''

import SimpleHTTPServer
import SocketServer
import httplib
import json
import os
import re
import sys
import thread
import urllib
import webbrowser

PROJECT_ARGS = {
  'client_id': ('937534751394-gbj5334v9144c57qjqghl7d283plj5r4'
      '.apps.googleusercontent.com'),
  'grant_type': 'authorization_code',
  'redirect_uri': 'http://localhost:8000'
}

PORT = 8000

APP_ID = 'kgejglhpjiefppelpmljglcjbhoiplfn'
OAUTH_DOMAIN = 'accounts.google.com'
OAUTH_AUTH_COMMAND = '/o/oauth2/auth'
OAUTH_TOKEN_COMMAND = '/o/oauth2/token'
WEBSTORE_API_SCOPE = 'https://www.googleapis.com/auth/chromewebstore'

API_ENDPOINT_DOMAIN = 'www.googleapis.com'
COMMAND_GET_UPLOAD_STATUS = (
    '/chromewebstore/v1.1/items/%s?projection=draft' % APP_ID)
COMMAND_POST_PUBLISH = '/chromewebstore/v1.1/items/%s/publish' % APP_ID
COMMAND_POST_UPLOAD = '/upload/chromewebstore/v1.1/items/%s' % APP_ID

class CodeRequestHandler(SocketServer.StreamRequestHandler):
  def handle(self):
    content = self.rfile.readline()
    self.server.code = re.search('code=(.*) ', content).groups()[0]
    self.rfile.close()

def GetAuthCode():
  Handler = CodeRequestHandler
  httpd = SocketServer.TCPServer(("", PORT), Handler)
  query = '&'.join(['response_type=code',
                    'scope=%s' % WEBSTORE_API_SCOPE,
                    'client_id=%(client_id)s' % PROJECT_ARGS,
                    'redirect_uri=%(redirect_uri)s' % PROJECT_ARGS])
  auth_url = ' https://%s%s?%s' % (OAUTH_DOMAIN, OAUTH_AUTH_COMMAND, query)
  print 'Navigating to %s' % auth_url
  webbrowser.open(auth_url)
  httpd.handle_request()
  httpd.server_close()
  return httpd.code

def GetOauthToken(code, client_secret):
  PROJECT_ARGS['code'] = code
  PROJECT_ARGS['client_secret'] = client_secret
  body = urllib.urlencode(PROJECT_ARGS)
  conn = httplib.HTTPSConnection(OAUTH_DOMAIN)
  conn.putrequest('POST', OAUTH_TOKEN_COMMAND)
  conn.putheader('content-type', 'application/x-www-form-urlencoded')
  conn.putheader('content-length', len(body))
  conn.endheaders()
  conn.send(body)
  content = conn.getresponse().read()
  return json.loads(content)

def GetPopulatedHeader(client_secret):
  code = GetAuthCode()<commit_msg>Make ChromeVox web store upload script more robust<commit_after>#!/usr/bin/env python

# Copyright 2014 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

'''A set of utilities to interface with the Chrome Webstore API.'''

import SimpleHTTPServer
import SocketServer
import httplib
import json
import os
import re
import sys
import thread
import urllib
import webbrowser

PROJECT_ARGS = {
  'client_id': ('937534751394-gbj5334v9144c57qjqghl7d283plj5r4'
      '.apps.googleusercontent.com'),
  'grant_type': 'authorization_code',
  'redirect_uri': 'http://localhost:8000'
}

PORT = 8000

APP_ID = 'kgejglhpjiefppelpmljglcjbhoiplfn'
OAUTH_DOMAIN = 'accounts.google.com'
OAUTH_AUTH_COMMAND = '/o/oauth2/auth'
OAUTH_TOKEN_COMMAND = '/o/oauth2/token'
WEBSTORE_API_SCOPE = 'https://www.googleapis.com/auth/chromewebstore'

API_ENDPOINT_DOMAIN = 'www.googleapis.com'
COMMAND_GET_UPLOAD_STATUS = (
    '/chromewebstore/v1.1/items/%s?projection=draft' % APP_ID)
COMMAND_POST_PUBLISH = '/chromewebstore/v1.1/items/%s/publish' % APP_ID
COMMAND_POST_UPLOAD = '/upload/chromewebstore/v1.1/items/%s' % APP_ID

class CodeRequestHandler(SocketServer.StreamRequestHandler):
  def handle(self):
    content = self.rfile.readline()
    self.server.code = re.search('code=(.*) ', content).groups()[0]
    self.rfile.close()

def GetAuthCode():
  Handler = CodeRequestHandler
  httpd = SocketServer.TCPServer(("", PORT), Handler)
  query = '&'.join(['response_type=code',
                    'scope=%s' % WEBSTORE_API_SCOPE,
                    'client_id=%(client_id)s' % PROJECT_ARGS,
                    'redirect_uri=%(redirect_uri)s' % PROJECT_ARGS])
  auth_url = 'https://%s%s?%s' % (OAUTH_DOMAIN, OAUTH_AUTH_COMMAND, query)
  print 'Navigating to %s' % auth_url
  webbrowser.open(auth_url)
  httpd.handle_request()
  httpd.server_close()
  return httpd.code

def GetOauthToken(code, client_secret):
  PROJECT_ARGS['code'] = code
  PROJECT_ARGS['client_secret'] = client_secret
  body = urllib.urlencode(PROJECT_ARGS)
  conn = httplib.HTTPSConnection(OAUTH_DOMAIN)
  conn.putrequest('POST', OAUTH_TOKEN_COMMAND)
  conn.putheader('content-type', 'application/x-www-form-urlencoded')
  conn.putheader('content-length', len(body))
  conn.endheaders()
  conn.send(body)
  content = conn.getresponse().read()
  return json.loads(content)

def GetPopulatedHeader(client_secret):
  code = GetAuthCode()
{'fraction_non_alphanumeric': 0.12611879576891782, 'fraction_numerical': 0.019934906427990236, 'mean_word_length': 4.791519434628976, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 4, 'lorem ipsum': 0, 'www.': 6, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5014658', 'n_tokens_mistral': 1808, 'n_tokens_neox': 1743, 'n_words': 324}