Exemple #1
0
def build_http_connection(config, timeout=120, disable_ssl_validation=False):
    """
    :config: dict like, proxy and account information are in the following
             format {
                 "username": xx,
                 "password": yy,
                 "proxy_url": zz,
                 "proxy_port": aa,
                 "proxy_username": bb,
                 "proxy_password": cc,
                 "proxy_type": http,http_no_tunnel,sock4,sock5,
                 "proxy_rdns": 0 or 1,
             }
    :return: Http2.Http object
    """

    proxy_type_to_code = {
        "http": socks.PROXY_TYPE_HTTP,
        "http_no_tunnel": socks.PROXY_TYPE_HTTP_NO_TUNNEL,
        "socks4": socks.PROXY_TYPE_SOCKS4,
        "socks5": socks.PROXY_TYPE_SOCKS5,
    }
    if config.get("proxy_type") in proxy_type_to_code:
        proxy_type = proxy_type_to_code[config["proxy_type"]]
    else:
        proxy_type = socks.PROXY_TYPE_HTTP

    rdns = scu.is_true(config.get("proxy_rdns"))

    proxy_info = None
    if config.get("proxy_url") and config.get("proxy_port"):
        if config.get("proxy_username") and config.get("proxy_password"):
            proxy_info = ProxyInfo(proxy_type=proxy_type,
                                   proxy_host=config["proxy_url"],
                                   proxy_port=int(config["proxy_port"]),
                                   proxy_user=config["proxy_username"],
                                   proxy_pass=config["proxy_password"],
                                   proxy_rdns=rdns)
        else:
            proxy_info = ProxyInfo(proxy_type=proxy_type,
                                   proxy_host=config["proxy_url"],
                                   proxy_port=int(config["proxy_port"]),
                                   proxy_rdns=rdns)
    if proxy_info:
        # Creating Http object from httplib2shim library which is a wrapper over httplib2 library. This code is added since
        # the configuration of inputs failed when proxy is enabled on the instance.
        http = httplib2shim.Http(
            proxy_info=proxy_info,
            timeout=timeout,
            disable_ssl_certificate_validation=disable_ssl_validation)
    else:
        # Creating Http object from httplib2shim library which is a wrapper over httplib2 library. This code is added since
        # the configuration of inputs failed when proxy is enabled on the instance.
        http = httplib2shim.Http(
            timeout=timeout,
            disable_ssl_certificate_validation=disable_ssl_validation)

    if config.get("username") and config.get("password"):
        http.add_credentials(config["username"], config["password"])
    return http
Exemple #2
0
def main():

    credentials = get_credentials()
    http = credentials.authorize(httplib2shim.Http())
    service = discovery.build('drive', 'v3', http=http)
    mime = 'application/vnd.google-apps.document'

    for root, dirs, files in os.walk("Pdf"):
        for file in files:
            if file.endswith(".pdf"):
                file_name = os.path.splitext(file)[0]
                print("[OCR processing] " + file_name)
                res = service.files().create(body={
                    'name': file,
                    'mimeType': mime
                },
                                             media_body=MediaFileUpload(
                                                 "Pdf/" + file,
                                                 mimetype=mime,
                                                 resumable=True)).execute()

                downloader = MediaIoBaseDownload(
                    io.FileIO("Text/" + file_name + ".txt", 'wb'),
                    service.files().export_media(fileId=res['id'],
                                                 mimeType="text/plain"))
                done = False
                while done is False:
                    status, done = downloader.next_chunk()

                service.files().delete(fileId=res['id']).execute()
Exemple #3
0
def main():

    credentials = get_credentials()
    http = credentials.authorize(httplib2shim.Http())
    service = discovery.build('drive', 'v3', http=http)
    mime = 'application/vnd.google-apps.document'

    filename = '大同區B1015'
    imgfile = filename + '.pdf'
    txtfile = filename + '.txt'

    res = service.files().create(
        body={
            'name': imgfile,
            'mimeType': mime
        },
        media_body=MediaFileUpload(imgfile, mimetype=mime,
                                   resumable=True)).execute()

    downloader = MediaIoBaseDownload(
        io.FileIO(txtfile, 'wb'),
        service.files().export_media(fileId=res['id'], mimeType="text/plain"))
    done = False
    while done is False:
        status, done = downloader.next_chunk()

    service.files().delete(fileId=res['id']).execute()
Exemple #4
0
def main():
    # Seed the random number generator with OS randomness
    random.seed(os.urandom(32), version=2)

    opts = docopt.docopt(__doc__)
    with open(opts['--spec']) as fobj:
        spec = yaml.load(fobj)

    logging.basicConfig(
        level=logging.WARN if opts['--quiet'] else logging.INFO)

    http = httplib2shim.Http()

    credentials_path = spec.get('credentials_path', './credentials.json')
    LOG.info('Using %s for client secrets', credentials_path)
    creds = (service_account.ServiceAccountCredentials.from_json_keyfile_name(
        credentials_path, SCOPES))

    if not creds or creds.invalid:
        LOG.error('Credentials are invalid')

    service = build('drive', 'v3', http=creds.authorize(http))

    loop_sleep = int(opts['--loop-sleep'])
    while True:
        run_pipeline(service, spec)
        if not opts['--loop']:
            break
        LOG.info('Sleeping for %s seconds', loop_sleep)
        time.sleep(loop_sleep)
def google_calendar_raw_connection(oauth2_clinet_id, oauth2_secrete, sender_id,
                                   change, org_credentials):
    """
    This method used for connect with google calendar api.
    """
    if not org_credentials:
        error_message = 'This sender {sender_id} don\'t have oauth2 access-token, yet. Please send this link {register_link} to the sender. So he can register with his google account, again.'.format(
            sender_id=sender_id, register_link=settings.REGISTER_URL)
        logging.error(error_message)
        return None
    else:
        json_credentials = json.loads(org_credentials)
        credentials = client.GoogleCredentials(
            None, json_credentials['client_id'],
            json_credentials['client_secret'],
            json_credentials['refresh_token'],
            json_credentials['token_expiry'],
            "https://accounts.google.com/o/oauth2/token", None)

    # Create an httplib2.Http object to handle our HTTP requests and authorize it
    # with our good Credentials.
    http = httplib2shim.Http()
    http = credentials.authorize(http)
    service = discovery.build('calendar', 'v3', http=http)

    return {'http': http, 'service': service}
Exemple #6
0
 def _build_storage_service(self, credentials):
     http = httplib2shim.Http(proxy_info=self._get_proxy_info, timeout=30)
     http = AuthorizedHttp(credentials, http=http)
     return build_service_client('storage',
                                 'v1',
                                 http=http,
                                 cache_discovery=False)
Exemple #7
0
def build_cloud_resource(api_base_url,
                         api_key=None,
                         credentials=None,
                         timeout=None,
                         headers_supplier=None,
                         response_inspector=None,
                         http_transport=None,
                         raw=False):
    """Builds an Earth Engine Cloud API resource.

  Args:
    api_base_url: The base URL of the cloud endpoints.
    api_key: An API key that's enabled for use with the Earth Engine Cloud API.
    credentials: OAuth2 credentials to use when authenticating to the API.
    timeout: How long a timeout to set on requests, in seconds.
    headers_supplier: A callable that will return a set of headers to be applied
      to a request. Will be called once for each request.
    response_inspector: A callable that will be invoked with the raw
      httplib2.Response responses.
    http_transport: An optional custom http_transport to use.
    raw: Whether or not to return raw bytes when making method requests.

  Returns:
    A resource object to use to call the Cloud API.
  """
    discovery_service_url = (
        '{}/$discovery/rest?version={}&prettyPrint=false'.format(
            api_base_url, VERSION))
    if http_transport is None:
        http_transport = httplib2.Http(timeout=timeout)
    if credentials is not None:
        http_transport = AuthorizedHttp(credentials, http=http_transport)
    request_builder = _wrap_request(headers_supplier, response_inspector)
    # Discovery uses json by default.
    if raw:
        alt_model = model.RawModel()
    else:
        alt_model = None

    def build(**kwargs):
        return discovery.build('earthengine',
                               VERSION,
                               discoveryServiceUrl=discovery_service_url,
                               developerKey=api_key,
                               http=http_transport,
                               requestBuilder=request_builder,
                               model=alt_model,
                               cache_discovery=False,
                               **kwargs)  # pytype: disable=wrong-keyword-args

    try:
        # google-api-python-client made static_discovery the default in version 2,
        # but it's not backward-compatible. There's no reliable way to check the
        # package version, either.
        resource = build(static_discovery=False)
    except TypeError:
        resource = build()
    resource._baseUrl = api_base_url
    return resource
Exemple #8
0
def get_service():
    global _service
    if not _service:
        credentials = GoogleCredentials.get_application_default(
        ).create_scoped(['https://www.googleapis.com/auth/cloud-platform'])
        http = httplib2shim.Http()
        credentials.authorize(http)
        _service = discovery.build('language', 'v1beta1', http=http)
    return _service
Exemple #9
0
def get_resource(client_secrets_file, credentials_file, get_code_callback):
    """Authenticate and return a googleapiclient.discovery.Resource object."""
    get_flow = oauth2client.client.flow_from_clientsecrets
    flow = get_flow(client_secrets_file, scope=YOUTUBE_UPLOAD_SCOPE)
    storage = oauth2client.file.Storage(credentials_file)
    credentials = _get_credentials(flow, storage, get_code_callback)
    if credentials:
        http = credentials.authorize(httplib2shim.Http())
        return googleapiclient.discovery.build("youtube", "v3", http=http)
Exemple #10
0
def _create_session():
    credentials = gcloud.credentials.get_credentials()
    credentials = credentials.create_scoped(
        ['https://www.googleapis.com/auth/cloud-platform'])
    credentials.refresh(httplib2shim.Http(proxy_info=None))
    session = requests.Session()
    session.headers['Authorization'] = 'Bearer {}'.format(
        credentials.access_token)
    return session
Exemple #11
0
    def init_google_http():
        # For requests to Google Genomics, pick up the default credentials from
        # the environment (see https://developers.google.com/identity/protocols/application-default-credentials).

        credentials = GoogleCredentials.get_application_default()
        credentials = credentials.create_scoped(
            'https://www.googleapis.com/auth/genomics')

        http = httplib2.Http()
        http = credentials.authorize(http)

        return http
def build_cloud_resource(api_base_url,
                         api_key=None,
                         credentials=None,
                         timeout=None,
                         headers_supplier=None,
                         response_inspector=None,
                         http_transport=None,
                         raw=False):
  """Builds an Earth Engine Cloud API resource.

  Args:
    api_base_url: The base URL of the cloud endpoints.
    api_key: An API key that's enabled for use with the Earth Engine Cloud API.
    credentials: OAuth2 credentials to use when authenticating to the API.
    timeout: How long a timeout to set on requests, in seconds.
    headers_supplier: A callable that will return a set of headers to be applied
      to a request. Will be called once for each request.
    response_inspector: A callable that will be invoked with the raw
      httplib2.Response responses.
    http_transport: An optional custom http_transport to use.
    raw: Whether or not to return raw bytes when making method requests.

  Returns:
    A resource object to use to call the Cloud API.
  """
  discovery_service_url = (
      '{}/$discovery/rest?version={}&prettyPrint=false'
      .format(api_base_url, VERSION))
  if http_transport is None:
    http_transport = httplib2.Http(timeout=timeout)
  if credentials is not None:
    http_transport = AuthorizedHttp(credentials, http=http_transport)
  request_builder = _wrap_request(headers_supplier, response_inspector)
  # Discovery uses json by default.
  if raw:
    alt_model = model.RawModel()
  else:
    alt_model = None
  resource = discovery.build(
      'earthengine',
      VERSION,
      discoveryServiceUrl=discovery_service_url,
      developerKey=api_key,
      http=http_transport,
      requestBuilder=request_builder,
      model=alt_model)
  return resource
Exemple #13
0
    def setup(self):
        """Triggers the OAuth2 setup flow for Google API endpoints. Requires the ability to open
    a link within a web browser in order to work.
    """
        store = file.Storage(self.drive_config.credentials_json)
        creds = store.get()

        if not creds or creds.invalid:
            flow = client.flow_from_clientsecrets(
                self.drive_config.client_secret_json, self._scopes)
            creds = tools.run_flow(flow, store)

        self._service = apiclient.discovery.build('drive',
                                                  'v3',
                                                  http=creds.authorize(
                                                      httplib2shim.Http()))

        log.log(logging.INFO, 'Drive connection established.')
Exemple #14
0
class BaseSnpediaHandler(webapp2.RequestHandler):
    http = httplib2.Http(timeout=60)

    def getSnppediaPageContent(self, snp):
        uri = ('http://bots.snpedia.com/api.php?action=query&prop=revisions&'
               'format=json&rvprop=content&titles=%s' % snp)
        response, content = self.http.request(uri=uri)

        page_id, page = json.loads(content)['query']['pages'].popitem()
        return page['revisions'][0]['*']

    def getContentValue(self, content, key):
        try:
            matcher = '%s=(.*)\n' % key
            return re.search(matcher, content, re.I).group(1)
        except (KeyError, AttributeError):
            return ''

    def complement(self, base):
        return {'A': 'T', 'T': 'A', 'G': 'C', 'C': 'G'}[base]
Exemple #15
0
def createAPIService(credentials, discoveryUrl=None):
    '''
    Create API Service for acccessing spreadsheets
    '''
    http = credentials.authorize(httplib2shim.Http())
    if not http:
        return None

    if discoveryUrl:
        # distinguishes between Service and Client Secret service Creation
        service = apiclient.discovery.build(
            'sheets',
            'v4',
            http=http,
            discoveryServiceUrl=discoveryUrl)
    else:
        service = apiclient.discovery.build('sheets', 'v4', http=http)
    if not service:
        return None
    return service
Exemple #16
0
#!/usr/bin/env python
# -*- coding: utf-8 -*-

import pygsheets
import telebot
import httplib2shim
from telebot import types as tp
import config
import sqlite3
from sqlite3 import Error

http = httplib2shim.Http()
token = "tg bot token"
wks = pygsheets.authorize(http=http).open('your google table').sheet1


def create_connection(path):
    connection = None
    try:
        connection = sqlite3.connect(path, check_same_thread=False)
        print("Connection to SQLite DB successful")
    except Error as e:
        print(f"The error '{e}' occurred")
    return connection


connection = create_connection("db.sqlite")


def execute_query(connection, query):
    cursor = connection.cursor()
Exemple #17
0
 def _init_service(self):
     self.service = discovery.build('calendar',
                                    'v3',
                                    http=AuthorizedHttp(
                                        self.credentials,
                                        http=httplib2shim.Http()))
Exemple #18
0
    os.path.dirname(__file__)),
                                       autoescape=True,
                                       extensions=['jinja2.ext.autoescape'])

# Supported data types
SET_TYPE_CALLSET = 'CALLSET'
SET_TYPE_READSET = 'READSET'

# But that call is not in the GA4GH API yet
SUPPORTED_BACKENDS = {}

if INCLUDE_BACKEND_ENSEMBL:
    SUPPORTED_BACKENDS['Ensembl'] = {
        'name': 'Ensembl',
        'ga4gh_api_version': '0.6.0',
        'http': httplib2.Http(timeout=60),
        'url': 'http://rest.ensembl.org/ga4gh/%s?%s',
        'datasets': {
            '1000 Genomes phase3': '6e340c4d1e333c7a676b1710d2e3953c'
        },
        'set_types': [SET_TYPE_CALLSET],
    }

if INCLUDE_BACKEND_GOOGLE:

    def init_google_http():
        # For requests to Google Genomics, pick up the default credentials from
        # the environment (see https://developers.google.com/identity/protocols/application-default-credentials).

        credentials = GoogleCredentials.get_application_default()
        credentials = credentials.create_scoped(