2017-06-08 11:15:59 +02:00
|
|
|
import pymysql.cursors
|
2017-08-06 11:42:02 +02:00
|
|
|
from raven import Client
|
2017-06-08 11:15:59 +02:00
|
|
|
|
2017-06-09 07:56:55 +02:00
|
|
|
import config
|
2017-06-08 11:15:59 +02:00
|
|
|
import parser
|
|
|
|
|
2017-08-06 11:42:02 +02:00
|
|
|
if not config.DEBUG:
|
|
|
|
client = Client(
|
|
|
|
dsn=config.sentry_url,
|
|
|
|
processors=(
|
|
|
|
'raven.processors.SanitizePasswordsProcessor',
|
|
|
|
)
|
|
|
|
)
|
2017-06-08 11:15:59 +02:00
|
|
|
# Connect to the database
|
2017-06-10 21:53:30 +02:00
|
|
|
connection = pymysql.connect(host=config.db["host"],
|
|
|
|
user=config.db["user"],
|
|
|
|
password=config.db["password"],
|
|
|
|
db=config.db["db"],
|
2017-06-08 11:15:59 +02:00
|
|
|
charset='utf8mb4',
|
2017-06-09 08:26:03 +02:00
|
|
|
cursorclass=pymysql.cursors.DictCursor,
|
|
|
|
unix_socket='/var/run/mysqld/mysqld.sock')
|
2017-06-08 11:15:59 +02:00
|
|
|
|
|
|
|
try:
|
|
|
|
with connection.cursor() as cursor:
|
|
|
|
venue_sql = "SELECT id FROM venues WHERE name = %s"
|
|
|
|
check_meal_sql = "SELECT id FROM meals WHERE name=%s"
|
|
|
|
insert_meal_sql = "INSERT INTO meals (name) VALUE (%s)"
|
|
|
|
insert_sql = 'REPLACE INTO menus (venue, date, meal) VALUES (%s, %s, %s)'
|
2017-11-18 19:15:20 +01:00
|
|
|
for p in [parser.zuppa, parser.aai]:
|
2017-06-08 11:15:59 +02:00
|
|
|
cursor.execute(venue_sql, p.name)
|
|
|
|
venue_id = cursor.fetchone()["id"]
|
|
|
|
for i in p.get_menus():
|
|
|
|
cursor.execute(check_meal_sql, (i["name"]))
|
|
|
|
check = cursor.fetchone()
|
|
|
|
if check:
|
|
|
|
meal_id = check["id"]
|
|
|
|
else:
|
|
|
|
cursor.execute(insert_meal_sql, (i["name"]))
|
|
|
|
meal_id = cursor.lastrowid
|
|
|
|
cursor.execute(insert_sql, (venue_id, i["date"], meal_id))
|
|
|
|
connection.commit() # save changes
|
2017-06-01 20:50:11 +02:00
|
|
|
|
2017-06-08 11:15:59 +02:00
|
|
|
finally:
|
|
|
|
connection.close()
|