Skip to content

Instantly share code, notes, and snippets.

@quandyfactory
Created December 22, 2009 21:24
Show Gist options
  • Save quandyfactory/262067 to your computer and use it in GitHub Desktop.
Save quandyfactory/262067 to your computer and use it in GitHub Desktop.
Code to run Wayne MacPhail's URL Lengthener.
def get_longurl(path):
"""
Code to run Wayne MacPhail's URL Lengthener.
TODO: re-populate form fields on failed post.
"""
output = []
addline = output.append
formfields = web.input(longurl='', url='')
longurl = tools.strip_html(formfields.longurl.strip().lower().replace('http://', '').replace('/','_'))
url = tools.strip_html(formfields.url.strip().lower())
if len(longurl) > 2000:
addline('<p class="red">Sorry, but your long alias is, er, too long. Please keep it under 2000 characters.</p>')
longurl = ''
if len(url) > 2000:
addline('<p class="red">Sorry, but your destination url is too long. Please keep it under 2000 characters.</p>')
url = ''
if url[:3] == 'www': url = 'http://%s' % url
if len(url) > 0 and url[:7] != 'http://':
addline('<p class="red">Your destination URL must include the protocol (e.g. http://).</p>')
url = ''
if longurl != '' and url != '':
query = sql.text("""
select * from longurl
where longurl = :longurl and longurl != ''
""", bind=sql.engine)
rs = query.execute(longurl=longurl).fetchall()
if len(rs) == 0:
query = sql.text("""
insert into longurl (longurl, url)
values (:longurl, :url)
""", bind=sql.engine)
rs = query.execute(longurl=longurl, url=url)
addline('<p class="green">Your Long URL has been saved as:</p>')
addline('<ul>')
addline('<li><a target="_blank" href="/longurl/%s/">http://quandyfactory.com/longurl/%s/</a></li>' % (longurl, longurl))
addline('</ul>')
else:
addline('<p class="red">Sorry, but that Long URL has already been used.</p>')
ff = quandy.Formfield()
fflist = []
fflist.append(ff.write(widget='input', id='url', title='Destination URL', value='http://'))
fflist.append(ff.write(widget='input', id='longurl', title='Long Alias'))
fflist.append(ff.write(widget='input', type='submit', id='create_long_url', value='Create Long URL'))
myform = quandy.Form()
addline(myform.write(formfields=fflist, method='post', id='create_long_url_form', title='Create a Long URL', action='/%s' % '/'.join(path)))
query = sql.text("""
select longurl, url from longurl
order by longurl_id desc
""", bind=sql.engine)
rs = query.execute().fetchall()
if len(rs) > 0:
addline('<h3>Saved Long URLs (Newest on Top)</h3>')
addline('<p><strong>Note:</strong> this is user-generated content. <em>Follow these links at your peril!</em> I\'ll try to keep an eye out for truly offensive links, but I cannot guarantee that I will be able to identify and delete them right away.</p>')
addline('<p>Hover over a long URL to see the destination URL as a tooltip.</p>')
addline('<ul>')
for row in rs:
addline('<li><p><a title="%s" target="_blank" href="/longurl/%s/">http://quandyfactory.com/longurl/%s/</a></p></li>' % (row.url, row.longurl, row.longurl))
addline('</ul>')
addline('<p><em>Wayne MacPhail\'s URL Lengthener is based on an <a href="http://twitter.com/wmacphail/status/6929150854">idea</a> by <a href="http://twitter.com/wmacphail/status/6935590394">Wayne</a> <a href="http://twitter.com/wmacphail/status/6936928561">MacPhail</a>. Concept used with permission.</em></p>')
return '\n'.join(output)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment