forked from XPUB/XPPL
You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
51 lines
1.7 KiB
Python
51 lines
1.7 KiB
Python
#import click
|
|
#from flask import Flask
|
|
from app import app, db
|
|
from app.models import Book, Author, Stack
|
|
from csv import DictReader
|
|
import argparse
|
|
from app.cover import get_cover
|
|
import os
|
|
|
|
ap = argparse.ArgumentParser("import csv into flask")
|
|
ap.add_argument("csv", help = "csv file to import")
|
|
ap.add_argument("--limit", type=int, default = None, help = "limit to x number of x")
|
|
args = ap.parse_args()
|
|
with open(args.csv) as f:
|
|
for row in DictReader(f):
|
|
cover = ''
|
|
if row['Filename']:
|
|
fullpath = os.path.join(app.config['UPLOAD_FOLDER'], row['Filename'])
|
|
name, file_extension = os.path.splitext(row['Filename'])
|
|
print ('get_cover', fullpath, name)
|
|
cover = get_cover(fullpath, name)
|
|
|
|
book = Book(row['Title'], row['Filename'], cover, row['Format'], row['Category'], None)
|
|
|
|
db.session.add(book)
|
|
authors = row['Author'].split(',')
|
|
authors = [x.strip() for x in authors]
|
|
for author in authors:
|
|
if author:
|
|
a = db.session.query(Author).filter_by(author_name=author).first()
|
|
if a == None:
|
|
a = Author(author_name=author)
|
|
db.session.add(a)
|
|
book.authors.append(a)
|
|
stacks = row['Stack']
|
|
stacks = row['Stack'].split(',')
|
|
stacks = [x.strip() for x in stacks]
|
|
for stack in stacks:
|
|
if stack:
|
|
b = db.session.query(Stack).filter_by(stack_name=stack).first()
|
|
if b == None:
|
|
b = Stack(stack_name=stack, stack_description=stack_description)
|
|
|
|
db.session.add(b)
|
|
book.stacks.append(b)
|
|
db.session.commit()
|
|
|
|
|
|
|
|
#books = db.session.query(Book).all()
|