Last active
August 29, 2015 14:02
-
-
Save drgarcia1986/ced41a90a107d89dae92 to your computer and use it in GitHub Desktop.
Encolhedor de links ultra simples
This file contains hidden or 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
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
from flask import Flask, redirect, abort, jsonify, url_for, request | |
import string | |
import random | |
app = Flask("encolinks") | |
links = {} | |
def gerador_id(): | |
chars = string.uppercase + string.lowercase + string.digits | |
return ''.join(random.choice(chars) for _ in range(6)) | |
@app.route("/encolher") | |
def encolher(): | |
id = gerador_id() | |
link = request.args.get('url', '') | |
if link == '': | |
abort(500) | |
links[id] = {"url": link, "qtd": 0} | |
return redirect(url_for('info', link=id), 301) | |
@app.route("/<link>") | |
def redirecionar(link): | |
try: | |
destino = links[link]["url"] | |
links[link]["qtd"] += 1 | |
return redirect(destino, 301) | |
except KeyError: | |
return abort(404) | |
@app.route("/info/<link>") | |
def info(link): | |
try: | |
return jsonify(links[link]) | |
except KeyError: | |
return abort(404) | |
@app.route("/lista") | |
def lista(): | |
return jsonify(links) | |
app.run(debug=True, use_reloader=True) |
This file contains hidden or 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
mkdir encolinks | |
touch encolinks/__init__.py | |
touch encolinks/app.py | |
cd encolinks | |
--------OpenSuse-------- | |
sudo zypper install python-virtualenv | |
---------Ubuntu--------- | |
sudo apt-get install python-virtualenv | |
virtualenv encolinks_env | |
source encolinks_env/bin/activate | |
pip install flask | |
pip install ipython | |
python app.py |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment