Commit 7ab27fd9 authored by Rien's avatar Rien

Add quote import script

parent 8f5a6a63
......@@ -150,4 +150,4 @@ def add_quote():
@app.route('/', methods=['GET'])
def list_quotes():
return render_template('quotes.html', quotes = models.Quote.query.all())
return render_template('quotes.html', quotes = reversed(models.Quote.query.all()))
......@@ -33,12 +33,15 @@ class Quote(db.Model):
def __repr__(self):
return "<Quote {} \"{}\">".format(self.quoter, self.quote)
def __init__(self, quoter, quote, channel):
def __init__(self, quoter, quote, channel, created_at=None):
super()
self.quoter = quoter
self.quote = quote
self.channel = channel
self.created_at = datetime.utcnow()
if created_at is None:
self.created_at = datetime.utcnow()
else:
self.created_at = created_at
# Experimentally try to find quoted user
quotee_match = Quote.QUOTEE_REGEX.search(quote)
self.quotee = quotee_match.group(1) if quotee_match is not None else None
......
from flask import Flask, request, Response, abort, render_template
from flask_sqlalchemy import SQLAlchemy
from flask_migrate import Migrate
from app import models
from datetime import datetime
import config
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = config.DATABASE_URL
db = SQLAlchemy(app)
for line in open('quotes.tsv'):
split = line.split("\t")
assert len(split) == 4, "Too much tabs at line \"{}\", {}".format(line, len(split))
quoter, channel, quote_text, created_at = split
quote = models.Quote(quoter, quote_text, channel, created_at = datetime.strptime(created_at.strip(), "%Y-%m-%d %H:%M:%S"))
db.session.add(quote)
db.session.commit()
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment