diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..4dda9a4 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +venv/ +config.py diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 0000000..669e8d6 --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,17 @@ +{ + // Use IntelliSense to learn about possible attributes. + // Hover to view descriptions of existing attributes. + // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 + "version": "0.2.0", + "configurations": [ + { + "name": "Python Debugger: Current File", + "type": "debugpy", + "request": "launch", + "program": "${file}", + "console": "integratedTerminal", + "justMyCode": false, + "args": ["--host=0.0.0.0"] + } + ] +} \ No newline at end of file diff --git a/__pycache__/config.cpython-310.pyc b/__pycache__/config.cpython-310.pyc new file mode 100644 index 0000000..66f5ef2 Binary files /dev/null and b/__pycache__/config.cpython-310.pyc differ diff --git a/app.py b/app.py new file mode 100644 index 0000000..1118f0c --- /dev/null +++ b/app.py @@ -0,0 +1,73 @@ +# app.py +from flask import Flask, render_template, request, redirect, url_for, session, flash +import requests.auth +from lib.datetime import filter_accounts_current_month +from lib.reqs import (get_urls, get_user_accounts) +import requests +import base64 +from flask import Flask +from config import DevelopmentConfig # or ProductionConfig + +app = Flask(__name__) +app.config.from_object(DevelopmentConfig) # Use DevelopmentConfig or ProductionConfig as needed + +@app.route('/') +def home(): + # If the user is logged in, redirect to a protected page like /accounts + if session.get('logged_in'): + base_url = app.config['BASE_URL'] # Access base_url from the config + all_accounts = get_user_accounts(base_url, session['auth_credentials']) + current_month_accounts = filter_accounts_current_month(all_accounts) + return render_template('home.html', username=session['username'], accounts=get_user_accounts(base_url, session['auth_credentials']), current_month_accounts=current_month_accounts) + return render_template('index.html') + +@app.route('/login', methods=['POST']) +def login(): + username = request.form['username'] + password = request.form['password'] + + # Encode the username and password in Base64 + credentials = f"{username}:{password}" + encoded_credentials = base64.b64encode(credentials.encode()).decode() + + base_url = app.config['BASE_URL'] # Access base_url from the config + login_url = f"{base_url}/Login" # Construct the full URL + + # Send GET request to the external login API with Basic Auth + response = requests.get( + login_url, + auth=requests.auth.HTTPBasicAuth(username, password) + ) + + # Check if login was successful + if response.status_code == 200 and response.json().get("auth") == "Success": + # Set session variable to indicate the user is logged in + session['logged_in'] = True + session['username'] = username + session['auth_credentials'] = encoded_credentials + return redirect(url_for('home')) # Redirect to the Accounts page + else: + # Show error on the login page + error = "Invalid username or password. Please try again." + return render_template('index.html', error=error) + +@app.route('/urls', methods=['GET']) +def urls(): + # Check if the user is logged in + if not session.get('logged_in'): + return redirect(url_for('home')) + # Placeholder content for Accounts page + base_url = app.config['BASE_URL'] # Access base_url from the config + return render_template('urls.html', urls=get_urls(base_url, session['auth_credentials'])) + +@app.route('/accounts', methods=['GET']) +def user_accounts(): + # Check if the user is logged in + if not session.get('logged_in'): + return redirect(url_for('home')) + # Placeholder content for Accounts page + base_url = app.config['BASE_URL'] # Access base_url from the config + return render_template('user_accounts.html', username=session['username'], user_accounts=get_user_accounts(base_url, session['auth_credentials'])) + +if __name__ == '__main__': + app.run(debug=app.config['DEBUG'], host=app.config['HOST'], port=app.config['PORT']) diff --git a/config.py.sample b/config.py.sample new file mode 100644 index 0000000..fc4419e --- /dev/null +++ b/config.py.sample @@ -0,0 +1,11 @@ +# config.py + +class Config: + DEBUG = False + BASE_URL = '' # Set your base URL here + +class DevelopmentConfig(Config): + DEBUG = True + +class ProductionConfig(Config): + BASE_URL = '' # Production base URL diff --git a/lib/__pycache__/datetime.cpython-310.pyc b/lib/__pycache__/datetime.cpython-310.pyc new file mode 100644 index 0000000..32a83d9 Binary files /dev/null and b/lib/__pycache__/datetime.cpython-310.pyc differ diff --git a/lib/__pycache__/reqs.cpython-310.pyc b/lib/__pycache__/reqs.cpython-310.pyc index 2cc4f9b..d8d9a49 100644 Binary files a/lib/__pycache__/reqs.cpython-310.pyc and b/lib/__pycache__/reqs.cpython-310.pyc differ diff --git a/lib/datetime.py b/lib/datetime.py new file mode 100644 index 0000000..f2de689 --- /dev/null +++ b/lib/datetime.py @@ -0,0 +1,25 @@ +from datetime import datetime +from flask import render_template + +def filter_accounts_current_month(accounts): + # Get the start and end of the current month + now = datetime.now() + start_of_month = datetime(now.year, now.month, 1) + if now.month == 12: + # If current month is December, next month is January of the next year + start_of_next_month = datetime(now.year + 1, 1, 1) + else: + # Otherwise, next month is just the next month of the same year + start_of_next_month = datetime(now.year, now.month + 1, 1) + + # Convert start and end of the month to epoch timestamps + start_of_month_timestamp = int(start_of_month.timestamp()) + start_of_next_month_timestamp = int(start_of_next_month.timestamp()) + + # Filter accounts with expiryDate in the current month + accounts_in_current_month = [ + account for account in accounts + if start_of_month_timestamp <= account['expiaryDate'] < start_of_next_month_timestamp + ] + + return accounts_in_current_month diff --git a/lib/reqs.py b/lib/reqs.py new file mode 100644 index 0000000..093baf5 --- /dev/null +++ b/lib/reqs.py @@ -0,0 +1,26 @@ +import requests +import json +from datetime import datetime + + +def get_urls(base_url, auth: str) -> list: + url = f"{base_url}/getUserAccounts/streams" + + payload = {} + headers = {"Authorization": f"Basic {auth}"} + + response = requests.request("GET", url, headers=headers, data=payload) + return json.loads(response.text) + + +def get_user_accounts(base_url, auth: str) -> list: + url = f"{base_url}/getUserAccounts" + + payload = {} + headers = {"Authorization": f"Basic {auth}"} + + response = requests.request("GET", url, headers=headers, data=payload) + res_json = json.loads(response.text) + for account in res_json: + account['expiaryDate_rendered'] = datetime.utcfromtimestamp(account['expiaryDate']).strftime('%d/%m/%Y') + return res_json diff --git a/templates/home.html b/templates/home.html new file mode 100644 index 0000000..0bb2afc --- /dev/null +++ b/templates/home.html @@ -0,0 +1,66 @@ + + + +
+ + +Stream Name | +Username | +Expiry Date | +
---|---|---|
{{ account.stream }} | +{{ account.username }} | +{{ account.expiaryDate_rendered }} | +