Skip to content

Instantly share code, notes, and snippets.

@Talon876
Last active December 7, 2016 01:49
Show Gist options
  • Save Talon876/a7a54d5c3434639e8ba5f3aa9e8110f6 to your computer and use it in GitHub Desktop.
Save Talon876/a7a54d5c3434639e8ba5f3aa9e8110f6 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
from flask import Flask, request, redirect, render_template, abort
import json
import random
RL_STATS = 'https://rocketleaguestats.com/profile/steam/{}'
RL_PROFILE = 'https://signature.rocketleaguestats.com/normal/steam/{}.png'
class Mapper:
def __init__(self):
self.mappings = {
'Dude': 76561197978137181,
'Bro': 76561197989320587,
'Guy': 76561198041121017,
'Man': 76561197996257333,
'Dawg': 76561198146597093,
}
def add_mapping(self, user):
name = self.generate_name()
self.mappings[name] = user
return name
def resolve(self, name):
return self.mappings.get(name, None)
def generate_name(self, dudebros = ['Dude', 'Bro', 'Guy', 'Man', 'Dawg']):
random.shuffle(dudebros)
return ''.join(dudebros)
mapper = Mapper()
app = Flask(__name__)
@app.route('/')
def index():
return render_template('index.html')
@app.route('/<name>')
def resolve(name):
profile = mapper.resolve(name)
if profile == None:
abort(404)
else:
return redirect(RL_STATS.format(profile), code=301)
@app.route('/sig/<name>.png')
def resolve_sig(name):
profile = mapper.resolve(name)
if profile == None:
abort(404)
else:
return redirect(RL_PROFILE.format(profile), code=301)
@app.route('/api/bro', methods=['POST'])
def save_profile():
payload = request.get_json(force=True)
name = mapper.add_mapping(payload['id'])
user = {
'name': name,
'id': payload['id']
}
return json.dumps(user)
if __name__=='__main__':
app.run(host='0.0.0.0')
<!-- templates/index.html -->
<html>
<head>
<title>dudebroguymandawg</title>
<head>
<center>
<img src="http://dudebroguymandawg.xyz/lego_avatar.gif"><br>
<font size="20" id='title'>dude bro guy man dawg</font>
<div style='padding-bottom:4px' id='bros'>
</div>
<input type='text', id='bro' placeholder='Enter steam id'></input>
<button id='addbro'>Add Bro</button>
<div id='flash'></div>
</center>
<script>
var getBroName = function() {
return document.getElementById('bro').value;
};
var flashIt = function(msg) {
document.getElementById('flash').innerHTML = msg;
};
var addBro = function(bro, cb) {
var request = new XMLHttpRequest();
request.open('POST', '/api/bro', true);
request.onload = function() {
cb(JSON.parse(request.responseText));
};
request.send(JSON.stringify({id: getBroName()}));
};
document.getElementById('addbro').onclick = function() {
var name = getBroName();
if (name !== '') {
addBro(name, function(newbro) {
console.log(newbro.name);
flashIt('Created <a href="/' + newbro.name + '">' + newbro.name + '</a>');
});
}
};
var renderBros = function(bros) {
var broHtml = bros.map(function(bro) {
return "<a href='/" + bro + "'><img src='/sig/" + bro + ".png'/></a>";
});
document.getElementById('bros').innerHTML = broHtml.join('\n');
};
var init = function() {
var bros = window.location.hash !== ""
? window.location.hash.substring(1, window.location.hash.length).split(',')
: ['Dude', 'Bro', 'Guy', 'Man', 'Dawg'];
document.getElementById('title').innerHTML = bros.map(function(bro) { return bro.toLowerCase(); }).join(' ');
renderBros(bros);
};
init();
</script>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment