flask-wallapop-watcher/app.py

83 lines
2.3 KiB
Python
Raw Normal View History

2017-08-07 19:54:39 +02:00
from flask import Flask, render_template, request, redirect, url_for, flash
2017-08-06 00:38:21 +02:00
from forms import SearchForm
# Get data Wallapop
import json
from urllib3 import PoolManager
import urllib.parse
2017-08-07 19:54:39 +02:00
# Database
from models import db, Programado
2017-08-06 00:38:21 +02:00
# Flask
2017-08-01 09:22:15 +02:00
app = Flask(__name__)
app.config['DEBUG'] = True
2017-08-06 00:38:21 +02:00
app.config['SECRET_KEY'] = 'mi secreto'
2017-08-07 19:54:39 +02:00
@app.route('/', methods=('GET', 'POST'))
2017-08-06 00:38:21 +02:00
def buscador():
form = SearchForm()
results = None
if form.validate_on_submit():
name = form.name.data
price_max = form.price_max.data or ''
# Search in Wallapop
2017-08-06 12:51:28 +02:00
results = get_resultados(name, price_max)
2017-08-06 00:38:21 +02:00
return render_template('items/buscador.html', form=form, results=results)
2017-08-06 12:51:28 +02:00
2017-08-06 00:38:21 +02:00
@app.route('/programadas')
def programadas():
2017-08-07 19:54:39 +02:00
programado_all = Programado.query.all()
return render_template('items/programadas.html', programado_all=programado_all)
2017-08-01 09:22:15 +02:00
2017-08-06 12:51:28 +02:00
def get_resultados(name='', price_max=''):
http = PoolManager()
url_api = 'http://es.wallapop.com/rest/items?minPrice=&maxPrice={price_max}&dist=&order=creationDate-des&lat=41.398077&lng=2.170432&kws={kws}'.format(
kws=urllib.parse.quote(name, safe=''),
price_max=price_max
)
results = http.request('GET', url_api)
results = json.loads(
results.data.decode('utf-8')
)
return results['items']
2017-08-07 19:54:39 +02:00
@app.route('/programadas/nuevo', methods=('POST',))
def programadas_nuevo():
itemId = int(request.form['itemId'])
title = request.form['title']
pictureURL = request.form['pictureURL']
price = request.form['price']
# We saved in the database
my_program = Programado(
item_id=itemId,
title=title,
picture_URL=pictureURL,
price=price
)
db.session.add(my_program)
try:
db.session.commit()
flash('Añadida con éxito.')
except:
db.session.rollback()
return redirect(url_for('programadas'))
@app.route('/programadas/borrar', methods=('POST',))
def programadas_borrar():
my_program = Programado.query.get(request.form['id'])
db.session.delete(my_program)
try:
db.session.commit()
flash('Borrada "{title}".'.format(title=my_program.title))
except:
db.session.rollback()
return redirect(url_for('programadas'))
2017-08-01 09:22:15 +02:00
if __name__ == '__main__':
app.run()