Add quote import script

This commit is contained in:
Rien Maertens 2019-02-14 00:50:38 +01:00
parent 8f5a6a6357
commit 7ab27fd9ef
No known key found for this signature in database
GPG key ID: 10C6D8C91DE58E4A
3 changed files with 26 additions and 3 deletions

View file

@ -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()))

View file

@ -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

20
import_quotes.py Normal file
View file

@ -0,0 +1,20 @@
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()