From 3489b0abafd1b4622f2629468a4202fab9510d1a Mon Sep 17 00:00:00 2001 From: Mike Lang Date: Wed, 27 Nov 2019 22:32:46 -0800 Subject: [PATCH] api_ping: A simple util for hitting google apis to prevent inactivity --- api_ping/Dockerfile | 17 ++++++++++++ api_ping/api_ping/__init__.py | 0 api_ping/api_ping/__main__.py | 16 ++++++++++++ api_ping/api_ping/main.py | 49 +++++++++++++++++++++++++++++++++++ api_ping/setup.py | 12 +++++++++ 5 files changed, 94 insertions(+) create mode 100644 api_ping/Dockerfile create mode 100644 api_ping/api_ping/__init__.py create mode 100644 api_ping/api_ping/__main__.py create mode 100644 api_ping/api_ping/main.py create mode 100644 api_ping/setup.py diff --git a/api_ping/Dockerfile b/api_ping/Dockerfile new file mode 100644 index 0000000..1fb9aab --- /dev/null +++ b/api_ping/Dockerfile @@ -0,0 +1,17 @@ +FROM alpine:3.7 +# dependencies needed for compiling c extensions +# also busybox-extras for telnet for easier use of backdoor +RUN apk --update add py2-pip gcc python-dev musl-dev busybox-extras + +# Install gevent so that we don't need to re-install it when common changes +RUN pip install gevent + +# Install common lib first as it changes less +COPY common /tmp/common +RUN pip install /tmp/common && rm -r /tmp/common + +# Install actual application +COPY api_ping /tmp/api_ping +RUN pip install /tmp/api_ping && rm -r /tmp/api_ping + +ENTRYPOINT ["python2", "-m", "api_ping"] diff --git a/api_ping/api_ping/__init__.py b/api_ping/api_ping/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/api_ping/api_ping/__main__.py b/api_ping/api_ping/__main__.py new file mode 100644 index 0000000..54a65ec --- /dev/null +++ b/api_ping/api_ping/__main__.py @@ -0,0 +1,16 @@ + +import gevent.monkey +gevent.monkey.patch_all() + +import logging +import os + +import argh + +from api_ping.main import main + +LOG_FORMAT = "[%(asctime)s] %(levelname)8s %(name)s(%(module)s:%(lineno)d): %(message)s" + +level = os.environ.get('WUBLOADER_LOG_LEVEL', 'INFO').upper() +logging.basicConfig(level=level, format=LOG_FORMAT) +argh.dispatch_command(main) diff --git a/api_ping/api_ping/main.py b/api_ping/api_ping/main.py new file mode 100644 index 0000000..eb919fc --- /dev/null +++ b/api_ping/api_ping/main.py @@ -0,0 +1,49 @@ + +import json + +from common.googleapis import GoogleAPIClient + +# Youtube video to look up (look at this graph) +YOUTUBE_VIDEO_ID = 'sIlNIVXpIns' + +# Sheet id to look up (DB2019 public sheet) +SHEET_ID = '15t-UsfNlP5xUxLTLH-EUjrOYriOvXuNGAPxi_8EZJhU' + + +def youtube(client): + client.request('GET', 'https://www.googleapis.com/youtube/v3/videos', + params={'part': 'id', 'id': YOUTUBE_VIDEO_ID}, + ).raise_for_status() + + +def sheets(client): + client.request('GET', + 'https://sheets.googleapis.com/v4/spreadsheets/{}/values/A1'.format(SHEET_ID), + ).raise_for_status() + + +ACTIONS = { + 'youtube': youtube, + 'sheets': sheets, +} + + +def main( + *targets +): + """Does an action on the given google api targets, preventing issues due to api inactivity. + A target should consist of a comma-seperated list of apis to hit, then a colon, then a creds file. + eg. "sheets,youtube:my_creds.json". + """ + for target in targets: + if ':' not in target: + raise ValueError("Bad target: {!r}".format(target)) + apis, credfile = target.split(':', 1) + apis = apis.split(',') + with open(credfile) as f: + creds = json.load(f) + client = GoogleAPIClient(creds['client_id'], creds['client_secret'], creds['refresh_token']) + for api in apis: + if api not in ACTIONS: + raise ValueError("No such api {!r}".format(api)) + ACTIONS[api](client) diff --git a/api_ping/setup.py b/api_ping/setup.py new file mode 100644 index 0000000..027bca8 --- /dev/null +++ b/api_ping/setup.py @@ -0,0 +1,12 @@ +from setuptools import setup, find_packages + +setup( + name = "wubloader-api-ping", + version = "0.0.0", + packages = find_packages(), + install_requires = [ + "argh", + "requests", + "wubloader-common", + ], +)