Fetches data from Google Search Console and stores it into SQLite
Project description
gsc-api-handler
gsc-api-handler is a lightweight Python package designed to authorize and fetch data from the Google Search Console API, storing it directly into a local SQLite database.
🚀 Features
- 🔐 OAuth 2.0 user authorization with automatic token refresh
- 📊 Data fetching with pagination support
- 🗃️ Stores GSC metrics (clicks, impressions, CTR, position) into SQLite
- 🧩 Easily integrable with Django or other Python-based projects
- ✅ Minimal dependencies and clean, modular structure
📦 Installation
Install the package in editable mode during development:
git clone https://github.com/your-username/gsc-api-handler.git
cd gsc-api-handler
pip install gsc-api-handler
You may want to use a virtual environment:
python -m venv .venv
source .venv/bin/activate # On Windows: .venv\Scripts\activate
pip install gsc-api-handler
⚙️ Configuration
This package uses python-decouple to manage environment variables.
- Create a
.envfile in the project root, outside thesrc/folder. - Add the following variables:
GSC_CLIENT_SECRET=gsc_credentials/client_secret.json
GSC_TOKEN_PATH=gsc_credentials/token.pickle
- Create a
gsc_credentials/folder and place yourclient_secret.jsonfile inside.
The token will be created automatically after successful authentication.
If
.envis missing, default valuesclient_secret.jsonandtoken.picklewill be used from the current working directory.
✅ Usage Example
Here is an example script (test_fetch.py) that uses the package to fetch GSC data and store it in SQLite:
from gsc_api_handler import fetch_and_store_gsc_data
from decouple import config # Dodati ovaj import ako ga već nema u primeru
from pathlib import Path
CLIENT_SECRETS_FILE = Path(config("GSC_CLIENT_SECRET", default="client_secret.json"))
TOKEN_FILE = Path(config("GSC_TOKEN_PATH", default="token.pickle"))
# BASE_DIR = Path(__file__).resolve().parent # U test_fetch.py bi ovo radilo
# CLIENT_SECRETS_FILE = BASE_DIR / "gsc_credentials" / "client_secret.json"
# TOKEN_FILE = BASE_DIR / "gsc_credentials" / "token.pickle"
rows = fetch_and_store_gsc_data(
site_url='sc-domain:yourdomain.com',
db_path='gsc_yourdomain.sqlite',
creds_path=CLIENT_SECRETS_FILE,
token_path=TOKEN_FILE,
dimensions=['query', 'page', 'date'],
start_date='2024-06-01',
end_date='2024-06-15'
)
print(f"{rows} rows fetched and saved.")
📁 Output Table Format
The gsc_data table contains:
| Column | Type | Description |
|---|---|---|
country |
TEXT | User country |
device |
TEXT | Device used |
query |
TEXT | Search query |
page |
TEXT | Landing page |
date |
TEXT | Date of the visit |
clicks |
INTEGER | Number of clicks |
impressions |
INTEGER | Number of impressions |
ctr |
REAL | Click-through rate |
position |
REAL | Average position in search |
Table name and dimensions are fully configurable.
📚 Requirements
This package depends on:
google-api-python-clientgoogle-authgoogle-auth-oauthlibpython-dateutilpython-decouple
All dependencies are listed in pyproject.toml.
📤 Publishing (optional)
If you plan to publish this package on PyPI:
- Update
pyproject.tomlwith your author name, email, and version. - Run:
python -m build
twine upload dist/*
📝 License
This project is licensed under the MIT License
✨ Author
Developed by Miki Zivkovic
© 2025-present
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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file gsc_api_handler-1.0.0.tar.gz.
File metadata
- Download URL: gsc_api_handler-1.0.0.tar.gz
- Upload date:
- Size: 19.2 MB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.10.10
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
8edee3534421d3380ba1344a43628d0b016dcf987d29a5c7758140c5d507751b
|
|
| MD5 |
ae52b9f1681d8141162dc67c0b1b2609
|
|
| BLAKE2b-256 |
5e7f3f281621a1cd710d578e79f3a99ae1a6edfeefd2328d1060b3d6a8a7c92a
|
File details
Details for the file gsc_api_handler-1.0.0-py3-none-any.whl.
File metadata
- Download URL: gsc_api_handler-1.0.0-py3-none-any.whl
- Upload date:
- Size: 7.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.10.10
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
765271dc458266c0a8070d6ef89e96933dcaabb1dc225c80f20f9d91a0373ecf
|
|
| MD5 |
67afbbadb54a4084f227f6a3d5e6a6ed
|
|
| BLAKE2b-256 |
88f3e5ffff8ec6887edd41ff8ff92b49dd57a7c13b5ee96aea727c489a5e092d
|