https://github.com/fivehealth/smooch-logs
Download Smooch logs using a headless browser.
https://github.com/fivehealth/smooch-logs
python selenium smooch
Last synced: 3 months ago
JSON representation
Download Smooch logs using a headless browser.
- Host: GitHub
- URL: https://github.com/fivehealth/smooch-logs
- Owner: fivehealth
- License: mit
- Created: 2020-07-30T13:55:36.000Z (almost 6 years ago)
- Default Branch: master
- Last Pushed: 2022-12-08T11:19:18.000Z (over 3 years ago)
- Last Synced: 2026-02-20T20:52:17.475Z (4 months ago)
- Topics: python, selenium, smooch
- Language: Python
- Homepage:
- Size: 85.9 KB
- Stars: 2
- Watchers: 0
- Forks: 0
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Smooch Logs Downloader
[](https://pypi.python.org/pypi/smooch_logs/)
[](https://pypi.python.org/pypi/smooch_logs/)
[](https://pypi.python.org/pypi/smooch_logs/)
[](https://pypi.python.org/pypi/smooch_logs/)
[](https://pypi.python.org/pypi/smooch_logs/)
This is a simple module for downloading Smooch logs from their website.
We obtain login credentials for Smooch using a Selenium with a Chromium headless browser.
## Installation
```bash
pipenv install smooch_logs
```
## Usage
### CLI
You use the `smooch_logs.downloader` script to directly download Smooch logs from thr CLI.
```bash
$ python -m smooch_logs.downloader --help
usage: downloader.py [-h] [-A app_id [app_id ...]] [--start date] [--end date] -o uri
Download Smooch logs for given application ID.
optional arguments:
-h, --help show this help message and exit
-A app_id [app_id ...], --apps app_id [app_id ...]
Smooch App IDs to download logs for. Defaults to all apps.
--start date Dump logs after this date/time (ISO date time format; default = all logs available which is ~30 days)
--end date Dump logs before this date/time (ISO date time format; default = now).
-o uri, --output uri Dump logs to this URI.
```
For example, to download logs for all apps in the last 3 days,
```bash
python -m smooch_logs.downloader --start `date --utc --iso-8601 --date="3 days ago"` -o last_3_days.json
```
### Module
```python
import logging
from smooch_logs import SmoochWebSession
from smooch_logs import SmoochLogsDownloader
from smooch_logs import SMOOCH_BASE_URL
logger = logging.getLogger(__name__)
with SmoochWebSession() as session:
r = session.get(f'{SMOOCH_BASE_URL}/webapi/apps?limit=999')
r.raise_for_status()
app_ids = [d['_id'] for d in r.json()['apps']]
logger.info(f'Found {len(app_ids)} Smooch applications.')
downloader = SmoochLogsDownloader(session)
for app_id in app_ids:
r = session.get(f'{SMOOCH_BASE_URL}/webapi/apps/{app_id}')
r.raise_for_status()
logger.info(f'Downloading logs for "{r.json()["name"]}" <{app_id}> from Smooch.')
for event in downloader.download(app_id, start=A.start, end=A.end):
print(json.dumps(event))
#end for
#end for
#end with
```
The [`SmoochWebSession`](smooch_logs/session.py) sets up the Selenium webdriver and required web-based operation to obtain `sessionId` from Smooch.
During development, it can be convenient to specify an existing session ID for the Smooch session by
```python
SmoochWebSession(session_id='xxx', logout=False)
```
The [`SmoochWebSession`](smooch_logs/session.py) object automatically checks for session validity and re-logins if necessary.
The [`SmoochLogsDownloader`](smooch_logs/downloader.py) is a convenience class for downloading Smooch logs for a particular application.
## Docker
The [Dockerfile](Dockerfile) included in this repository contains the necessary to run the Python download script.
It currently comes with a [shell script to download the last 3 days of logs](download_last_3_days.sh) for all Smooch applications and save them to the URI specified in environment variable `OUTPUT_URI`.
This is currently used by the authors in a daily job to download logs.
Feel free to modify as necessary for your use case.