From b1a2e820e5867b65482e9e1a7113a86741c84d5d Mon Sep 17 00:00:00 2001 From: Drew DeVault Date: Sat, 25 Aug 2018 09:19:46 -0400 Subject: [PATCH] Modernize Flask and add Patreon API key refresh --- app.py | 1 + config.ini.example | 9 +++++++++ cronjob.py | 30 +++++++++++++++++++++++++++++- fosspay/app.py | 2 +- fosspay/blueprints/html.py | 18 +++++++++++------- fosspay/common.py | 2 +- fosspay/config.py | 2 ++ 7 files changed, 54 insertions(+), 10 deletions(-) mode change 100644 => 100755 app.py diff --git a/app.py b/app.py old mode 100644 new mode 100755 index 298d4d8..5f937b3 --- a/app.py +++ b/app.py @@ -1,3 +1,4 @@ +#!/usr/bin/env python3 from fosspay.app import app from fosspay.config import _cfg, _cfgi diff --git a/config.ini.example b/config.ini.example index bcb4af2..6efdbb6 100644 --- a/config.ini.example +++ b/config.ini.example @@ -49,5 +49,14 @@ goal=500 # Register a client here: https://www.patreon.com/portal/registration/register-clients # And put in the "Creator's Access Token" here: patreon-access-token= +# And the "Creator's Refresh Token" here: +patreon-refresh-token= +# Client ID +patreon-client-id= +# Client secret +patreon-client-secret= # And the Patreon campaign you want to connect with: patreon-campaign= + +# Command to reload fosspay (send it a SIGHUP) +reload-command=kill -HUP $(pgrep -xf '/usr/bin/python3.*app.py' | tail -n 1) diff --git a/cronjob.py b/cronjob.py index 973ad5c..11e781b 100755 --- a/cronjob.py +++ b/cronjob.py @@ -6,7 +6,9 @@ from fosspay.email import send_thank_you, send_declined from datetime import datetime, timedelta +import requests import stripe +import subprocess stripe.api_key = _cfg("stripe-secret") @@ -45,4 +47,30 @@ for donation in donations: else: print("Skipping {}".format(donation)) -print("Done. {} records processed.".format(len(donations))) +print("{} records processed.".format(len(donations))) + +if _cfg("patreon-refresh-token"): + print("Updating Patreon API token") + + r = requests.post('https://www.patreon.com/api/oauth2/token', params={ + 'grant_type': 'refresh_token', + 'refresh_token': _cfg("patreon-refresh-token"), + 'client_id': _cfg("patreon-client-id"), + 'client_secret': _cfg("patreon-client-secret") + }) + if r.status_code != 200: + print("Failed to update Patreon API token") + sys.exit(1) + resp = r.json() + with open("config.ini") as f: + config = f.read() + config = config.replace(_cfg("patreon-access-token"), resp["access_token"]) + config = config.replace(_cfg("patreon-refresh-token"), resp["refresh_token"]) + with open("config.ini", "w") as f: + f.write(config) + print("Refreshed Patreon API token") + reload_cmd = _cfg("reload-command") + if not reload_cmd: + print("Cannot reload application, add reload-command to config.ini") + else: + subprocess.run(reload_cmd, shell=True, check=True) diff --git a/fosspay/app.py b/fosspay/app.py index 5753235..5badb60 100644 --- a/fosspay/app.py +++ b/fosspay/app.py @@ -1,5 +1,5 @@ from flask import Flask, render_template, request, g, Response, redirect, url_for -from flask.ext.login import LoginManager, current_user +from flask_login import LoginManager, current_user from jinja2 import FileSystemLoader, ChoiceLoader import sys diff --git a/fosspay/blueprints/html.py b/fosspay/blueprints/html.py index e8c0c11..6b3c672 100644 --- a/fosspay/blueprints/html.py +++ b/fosspay/blueprints/html.py @@ -1,5 +1,5 @@ from flask import Blueprint, render_template, abort, request, redirect, session, url_for, send_file, Response -from flask.ext.login import current_user, login_user, logout_user +from flask_login import current_user, login_user, logout_user from datetime import datetime, timedelta from fosspay.objects import * from fosspay.database import db @@ -40,12 +40,16 @@ def index(): access_token = _cfg("patreon-access-token") campaign = _cfg("patreon-campaign") if access_token and campaign: - import patreon - client = patreon.API(access_token) - campaign = client.fetch_campaign() - attrs = campaign.json_data["data"][0]["attributes"] - patreon_count = attrs["patron_count"] - patreon_sum = attrs["pledge_sum"] + try: + import patreon + client = patreon.API(access_token) + campaign = client.fetch_campaign() + attrs = campaign.json_data["data"][0]["attributes"] + patreon_count = attrs["patron_count"] + patreon_sum = attrs["pledge_sum"] + except: + patreon_count = 0 + patreon_sum = 0 else: patreon_count = 0 patreon_sum = 0 diff --git a/fosspay/common.py b/fosspay/common.py index ff0abfb..ec16fd6 100644 --- a/fosspay/common.py +++ b/fosspay/common.py @@ -1,5 +1,5 @@ from flask import session, jsonify, redirect, request, Response, abort -from flask.ext.login import current_user +from flask_login import current_user from werkzeug.utils import secure_filename from functools import wraps from fosspay.objects import User diff --git a/fosspay/config.py b/fosspay/config.py index 730b896..a903730 100644 --- a/fosspay/config.py +++ b/fosspay/config.py @@ -1,4 +1,5 @@ import logging +import signal try: from configparser import ConfigParser @@ -26,6 +27,7 @@ def load_config(): global config config = ConfigParser() config.readfp(open('config.ini')) + signal.signal(signal.SIGHUP, lambda *args: load_config()) load_config()