Skip to main content

No project description provided

Project description

wj_google

Getting started

Poetry

To make more easy the instalation, is recomended use Poetry, follow the instructions, and then use the command:

poetry install

To activate the vierual enviroment use:

poetry shell

Virtual Enviroment

Or you can use your own virtual enviroment, and install the requirements:

python -m venv <env_name>
<env_name>/scripts/activate
pip install -r requirements.txt

Connection to GCP

Is important consult the documentation of GCP, is required obtain the json credentials with the permission to use the resource that is going to use. ***

Supported services

The currently supported services are:

  • Google Drive
  • Buckets
  • BigQuery (just sql consults)***

Google Drive

The GCP account must have Google Drive API enabled, and make sure that you have a OAuth 2.0 application created, in Google Cloud console, you can see the documentation.

The following methods are currently supported:

  • Upload files
  • Download files
  • Create directory You must The package must be instantiated as follows:
from wj_google.google_api.google_api import GoogleApi

googleApi = GoogleApi(
    client_secret_file=CLIENT_SECRET_FILE, -> Json file must be configured in GCP
    api_name=API_NAME, -> drive in this case
    api_version=API_VERSION, -> Current version, 3 in this case
    scopes=GOOGLE_DRIVE_SCOPES, -> Scope of drive see doc
)

There are diferent scopes, as you can see in the documentation.

Google Drive

The GCP account must have Google Drive API enabled, and make sure that you have a OAuth 2.0 application created, in Google Cloud console, you can see the documentation.

The following methods are currently supported:

  • Upload files
  • Download files
  • Create directory

To use the library, it is necessary to prepare the credentials, and then instantiate the service that will be used.

from wj_google.google_api.google_api import GoogleApi

googleApi = GoogleApi(
    client_secret_file=CLIENT_SECRET_FILE, -> Json file must be configured in GCP
    api_name=API_NAME, -> drive in this case
    api_version=API_VERSION, -> Current version, 3 in this case
    scopes=GOOGLE_DRIVE_SCOPES, -> Scope of drive see doc
)

There are diferent scopes, as you can see in the documentation.

Uploading a file:

googleApi.upload_files_to_drive(
    folder_id, -> Id from the drive of the directory 
    file_paths, -> Path of yur file
    mime_types -> Type of file
)

Downloading a file:

googleApi.download_files_drive(
    file_ids -> Google Drive id file, you can find it in the uri
    file_names -> Name given in your local
)

Creating a directory:

googleApi.create_directory_drive(
    directory_name
)

Google storage buckets

This resource can be used with the credentials of a service user, you see de documentation.

The following methods are currently supported:

  • Upload files
  • Download files
  • Create directory

As in the previous case, credentials are required. After which the resource is instantiated.

from wj_google.google_cloud.cloud_connector import GoogleStorage
googleStorageClient = GoogleStorage(
    service -> STORAGE in this case and for now***
    credentials
)

Uploading a file:

googleStorageClient.upload_file(
    bucket_name -> Destination bucket 
    source_file_name -> Path of the file to upload
    destination_file_name Name of the file in bucket
)

Downloading a file:

googleStorageClient.download_file(
    bucket_name -> bucket file
    file_name -> name and path in the bucket
    destination_file -> name and path in your local of the file
)

Creating a bucket:

googleStorageClient.create_bucket(
    bucket_name,
    storage_class, -> Optional, to define the type of bucket to use
    location, -> Optional, to define in which region the bucket is hosted 
)

Listing bucket's files:

googleStorageClient.list_files(bucket_name)

Google BigQuery

This resource can be used with the credentials of a service user, you see de documentation.

With the credentials are required. After which the resource is instantiated, in this case just execute the query.

from google_cloud.cloud_connector import GoogleBigQuery

bigquery = GoogleBigQuery(
    service=BIGQUERY,
    credentials=GOOGLE_BIGQUEY_CREDENTIALS,
    scopes=scopes -> To use services like Google Drive
)

Do a consult:

query = 'YOUR_SQL_QUERY'
sql_job = bigquery.sql_query(query=query)

You can iterate over the sql_job:

for item in sql_job:
    print(item.name)

Add label to bigquery table:

labels = {
    "type": "social_media",
    "category": "cloud",
}
bigquery = GoogleBigQuery(service=BIGQUERY, credentials=GOOGLE_BIGQUEY_CREDENTIALS)
bigquery.add_table_labels(
    labels=labels, dataset_name=dataset_name, table_name=table_name
)

Delete label to bigquery table:

delete_labels_in_table(dataset_name, table_name)

Export queries to excel(Depreciated!):

You can use multiple queries and add it in a excel document, every query will be converted in a worksheet.

query1 = """
SELECT * FROM `test.tw` LIMIT 10
"""
query2 = """
SELECT * FROM `reports.reports` LIMIT 10
"""
queries = [query1, query2]
export_queries_to_excel(queries=queries)

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

wj_google-0.1.1.tar.gz (8.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

wj_google-0.1.1-py3-none-any.whl (10.1 kB view details)

Uploaded Python 3

File details

Details for the file wj_google-0.1.1.tar.gz.

File metadata

  • Download URL: wj_google-0.1.1.tar.gz
  • Upload date:
  • Size: 8.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.2 CPython/3.11.9 Linux/5.4.109+

File hashes

Hashes for wj_google-0.1.1.tar.gz
Algorithm Hash digest
SHA256 e6cf5861854e06610225b7739d2f5cf6b3ff094016599ed4f11a63453905d8e2
MD5 baf59fd3f74a485c71322dc47e7f242d
BLAKE2b-256 ece767a4ed98b134deb5b077ac838316240501d656a142ef6e539611847b4a3a

See more details on using hashes here.

File details

Details for the file wj_google-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: wj_google-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 10.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.2 CPython/3.11.9 Linux/5.4.109+

File hashes

Hashes for wj_google-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 c2e9f47a883fcd0ec790e45e51a3dddfff958162fc7e29cdf1c103142796564b
MD5 90907eedaf835f0607bfb1d01f9d4b18
BLAKE2b-256 d934be7c5403f6bd2580b1ecbb891caa81a08b883ba52393c10ebe74b199335a

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page