Created
April 13, 2020 20:39
-
-
Save pmreferrals/b65a2a88ece2de4950b2e66d151b7da1 to your computer and use it in GitHub Desktop.
pmreferrals
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from flask import Flask, render_template, request, flash | |
import uuid, urllib.parse, requests, random | |
app = Flask(__name__) | |
app.config['SECRET_KEY'] '' #this is set in prod :) | |
def get_codes() -> dict: | |
codes = {} | |
with open('codes.csv', 'r') as cs: | |
cs.readline() | |
for row in cs.readlines(): | |
row_info = row.strip('\n').split(',') | |
codes[row_info[0]] = int(row_info[1]) | |
return codes | |
def use_code() -> str: | |
codes = get_codes() | |
used_code = random.choice(list(codes.keys())) | |
codes[used_code] += 1 | |
with open('codes.csv', 'w') as cs: | |
data = 'code,freq\n' | |
for key in codes: | |
data += str(key + ',' + str(codes[key]) + '\n') | |
cs.write(data) | |
return used_code | |
def add_code(code: str): | |
resp = requests.get(str('https://activate.publicmobile.ca/?raf=' + code), verify = False) | |
codes = get_codes() | |
if code.isalnum() and (code not in codes) and ('Sorry, this referral code is invalid' not in resp.text): | |
with open('codes.csv', 'a') as cs: | |
cs.write(str(code + ',0\n')) | |
@app.route('/') | |
def index(): | |
return render_template('index.html') | |
@app.route('/view', methods=['POST']) | |
def view(): | |
code = use_code() | |
return render_template('view.html', code=code, url='https://generator.barcodetools.com/barcode.png?xdim=12&gen=3&ecl=0&data=' + urllib.parse.quote_plus('https://activate.publicmobile.ca/?raf=' + code)) | |
@app.route('/add', methods=['GET', 'POST']) | |
def add(): | |
code = request.form.get('code') | |
totalCodes = len(get_codes()) | |
if code: | |
flash('Your code was added to the database!', 'success') | |
add_code(code) | |
return render_template('add.html', totalCodes=totalCodes) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment