-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimport_awards.py
132 lines (98 loc) · 2.89 KB
/
import_awards.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# -*- coding: utf-8 -*-
from app.route_helpers import new_session
from app.orm_decl import (Work, Edition, Part, Person, Award,
AwardCategory, Awarded)
from app import app
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
from sqlalchemy.pool import NullPool
import re
import os
import csv
from typing import Dict, List, Tuple
from importbib import missing_from_db
db_url = app.config['SQLALCHEMY_DATABASE_URI']
categories = {}
awards = {}
def get_categories(s):
cats = s.query(AwardCategory).all()
for cat in cats:
categories[cat.name] = cat.id
def get_awards(s):
aws = s.query(Award).all()
for award in aws:
awards[award.name] = award.id
def save_personal_award(s, line):
award = line[1]
name = line[4]
if line[2]:
year = int(line[2])
else:
year = None
person = s.query(Person)\
.filter(Person.name == name).first()
if not person:
print(f'Awarded person not found: {name}')
return
if award not in awards:
print(f'Award not found: {award}')
return
awarded = Awarded(award_id=awards[award],
person_id=person.id,
year=year)
try:
s.add(awarded)
s.commit()
except Exception as e:
print(f'Could not add personal award: {e}.')
def save_novel_award(s, line):
award = line[1]
cat = line[3]
title = line[4]
if line[2]:
year = int(line[2])
else:
year = None
work = s.query(Work)\
.filter(Work.orig_title == title).first()
if not work:
print(f'Work not found: {title}')
return
if award not in awards:
print(f'Award not found: {award}')
return
if cat not in categories:
print(f'Category not found: {cat}')
return
awarded = Awarded(award_id=awards[award],
category_id=categories[cat],
year=year,
work_id=work.id)
try:
s.add(awarded)
s.commit()
except Exception as e:
print(f'Could not add novel award: {e}.')
def save_story_award(s, line):
pass
def import_awards():
s = new_session()
engine = create_engine(db_url, poolclass=NullPool)
session = sessionmaker()
session.configure(bind=engine)
s = session()
get_categories(s)
get_awards(s)
with open('bibfiles/awards.csv') as csvfile:
awardscsv = csv.reader(csvfile, delimiter=';', quotechar='"')
for award in awardscsv:
if int(award[0]) == 0:
save_personal_award(s, award)
elif int(award[0]) == 1:
save_novel_award(s, award)
elif int(award[0]) == 2:
save_story_award(s, award)
else:
print(f'Skipped {award}')
if __name__ == '__main__':
import_awards()