-
Notifications
You must be signed in to change notification settings - Fork 0
/
import_images.py
67 lines (53 loc) · 1.92 KB
/
import_images.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import csv
from sqlalchemy import pool
from app.orm_decl import (EditionImage, Work, Edition, Part, Person,
Contributor)
from app import app
import sys
import os
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
from sqlalchemy.pool import NullPool
from typing import List, Any
db_url = app.config['SQLALCHEMY_DATABASE_URI']
just_check: bool = False
img_dir = '/static/images/books/'
def add_image(s: Any, image: List[str]) -> None:
image_src = img_dir + image[0]
author_str = image[1]
title_str = image[2]
pubyear_str = image[3]
editions = s.query(Edition)\
.filter(Edition.title == title_str)\
.filter(Edition.pubyear == pubyear_str)\
.join(Part)\
.filter(Part.edition_id == Edition.id)\
.join(Contributor)\
.filter(Contributor.part_id == Part.id, Contributor.role_id == 1)\
.join(Person, Person.id == Contributor.person_id)\
.filter(Person.id == Contributor.person_id)\
.filter(Person.name.like(author_str + '%'))\
.all()
if not editions:
print(f'Not found: {author_str}: {title_str}.')
return
else:
print(f'Found: {author_str}: {title_str}')
if just_check == False:
for edition in editions:
ei = EditionImage(edition_id=edition.id, image_src=image_src)
s.add(ei)
s.commit()
def import_images(params) -> None:
engine = create_engine(db_url, poolclass=NullPool)
session = sessionmaker()
session.configure(bind=engine)
s = session()
if len(params) > 0:
just_check = True
with open('bibfiles/covers.csv') as csvfile:
imagescsv = csv.reader(csvfile, delimiter=';', quotechar='"')
for image in imagescsv:
add_image(s, image)
if __name__ == '__main__':
import_images(sys.argv[1:])