from flask import Flask, request, url_for
app = Flask(__name__)
app.secret_key = 'This is really unique and secret'
ID = ''
instructions = '(see steps 1 to 3 <a href="https://www.gtplanet.net/forum/threads/how-to-share-your-own-tracks-via-url.335446/" target="_blank">here</a>)'
@app.route('/')
def enter_id():
return """
<p>Enter your PSN ID</p>
<form method="POST" action="%s"><input name="id" /><input type="submit" value="Next" /></form>
""" % (url_for('save_id'),)
@app.route('/save_id', methods=['POST'])
def save_id():
global ID
ID = request.form["id"]
return """
<p>You have entered PSN ID: %s</p>
<p>Please check that the spelling is correct before continuing.</p>
<p><a href="%s">Change PSN ID</a> or <a href="%s">Continue</a></p>
""" % (ID, url_for('enter_id'), url_for('enter_link'))
@app.route('/enter_link')
def enter_link():
global instructions
return """
<p>Enter the URL link to your track: %s</p>
<p>Example: https://www.gran-turismo.com/gb/gt6/user/#!/mypage/course/1234567/</p>
<form method="POST" action="%s"><input name="link" /><input type="submit" value="Go!" /></form>
""" % (instructions, url_for('output'))
@app.route('/output', methods=['POST'])
def output():
global ID
global instructions
s = "%s" % (request.form['link'],)
s = s.split('/')
if len(s) != 11 or s[4] != 'gt6' or s[8] != 'course':
return """
<p>The link you entered is not valid. Please read the instructions %s and enter a new link.</p>
<p><a href="%s">Enter new URL</a></p>
<p><a href="%s">Change PSN ID</a></p>
""" % (instructions, url_for('enter_link'), url_for('enter_id'))
if s[-1] == '':
del s[-1]
s[7] = "friend/" + "%s" % (ID)
s2 = ''
for item in s:
s2 = s2 + item + '/'
return """
<p>The link has been successfully converted:</p>
<p>%s</p>
<p><a href="%s">Enter new URL</a></p>
<p><a href="%s">Change PSN ID</a></p>
""" % (s2, url_for('enter_link'), url_for('enter_id'))