Modernize Flask and add Patreon API key refresh

static-cp-fix
Drew DeVault 6 years ago
parent deaaa734c2
commit b1a2e820e5
  1. 1
      app.py
  2. 9
      config.ini.example
  3. 30
      cronjob.py
  4. 2
      fosspay/app.py
  5. 18
      fosspay/blueprints/html.py
  6. 2
      fosspay/common.py
  7. 2
      fosspay/config.py

@ -1,3 +1,4 @@
#!/usr/bin/env python3
from fosspay.app import app
from fosspay.config import _cfg, _cfgi

@ -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)

@ -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)

@ -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

@ -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

@ -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

@ -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()

Loading…
Cancel
Save