from oauth2client.service_account import ServiceAccountCredentials from httplib2 import Http from apiclient import discovery from gaf_api.services import utils from datetime import datetime, timedelta, timezone creds = ServiceAccountCredentials.from_json_keyfile_dict( utils.load_config("google_keys.json"), scopes=['https://www.googleapis.com/auth/calendar']) service = discovery.build("calendar", "v3", http=creds.authorize(Http()), cache_discovery=False) calendar_id = utils.load_config("calendar_id.json").get("id") def get_days_events(): """ Returns the next 24 hours of events """ start_time = datetime.now(tz=timezone.utc) end_time = start_time + timedelta(days=1) res = service.events().list(calendarId=calendar_id, timeMin=start_time.isoformat(), timeMax=end_time.isoformat())\ .execute() events = [] for e in res.get("items"): event = {
import urllib.parse as urlparse import requests from pyramid.view import view_config from pyramid.request import Request from pyramid.response import Response from gaf_api.services import utils from gaf_api.auth.oauth import JwtHelper import gaf_api.database as db oauth = utils.load_config("oauth.json") jwt_config = utils.load_config("jwt_config.json") jwt_interface = JwtHelper(key=jwt_config["secret"]) @view_config(route_name="oauth:authenticate", request_method="GET") def oauth_start(request: Request): """ Redirects to the Discord OAuth2 login screen """ redirect_url = oauth["redirect_url"] query = urlparse.urlencode( query=utils.combine(redirect_uri=redirect_url, **oauth["params"])) url = urlparse.urljoin(oauth["auth_url"], "?" + query) return Response(status=307, headers={"Location": url}) @view_config(route_name="oauth:authorize", request_method="GET")
import psycopg2 import pprint from gaf_api.services import utils db_config = utils.load_config("db.json") conn_string = f"" \ f"host='{db_config['host']}' " \ f"dbname='{db_config['db']}' " \ f"user='******'db_user']}' " \ f"password='******'db_pass']}'" conn = psycopg2.connect(conn_string) cursor = conn.cursor() def get_user(user_id: int): cursor.execute("SELECT * FROM public.users WHERE id=(%s)", (user_id, )) user = cursor.fetchone() pprint.pprint(user) return user def add_user(user_id: id, access_token: str, refresh_token: str): user = get_user(user_id) if user is not None: print("User already exists, not creating a new one") return cursor.execute("INSERT INTO public.users VALUES (%s, %s, %s)", (user_id, access_token, refresh_token))
from datetime import datetime, timedelta, timezone import json from pyramid.view import view_config from pyramid.request import Request from jwt.exceptions import InvalidTokenError, DecodeError from gaf_api.services import calendar from gaf_api.resources import Root from gaf_api.auth.oauth import JwtHelper from gaf_api.auth.bot_interface import BotInterface from gaf_api.services.utils import load_config import gaf_api.database as db jwt_config = load_config("jwt_config.json") jwt_interface = JwtHelper(key=jwt_config["secret"]) bot_config = load_config("bot_config.json") bot_interface = BotInterface(token=bot_config["token"]) @view_config(route_name="v1:live", request_method="GET", context=Root) def live_check(request: Request): """ Checks if things are working fine """ return {"API Live": True} @view_config(route_name="v1:calendar/events",