-
Notifications
You must be signed in to change notification settings - Fork 1
/
import_month.py
executable file
·210 lines (187 loc) · 7.31 KB
/
import_month.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
#!/usr/bin/python
# -*- coding: utf-8 -*-
import sys
import os
import cPickle as pickle
import yaml
import json
import datetime
from liturgy import calc_ref_year, build_dict_lit_year, print_lit_date, get_lit_date, SelectingMassException
from database import Session, Mass, Reading
from quote import canonicalise_quote, decode_quote
from scrape import scrape_file
from utils import real_itermonthdays, PrependStream
from abbreviations import ABBR_VATICAN
def import_from_scrape(year, month):
lit_years = {}
session = Session()
for day in real_itermonthdays(year, month):
date = datetime.date(year, month, day)
print >> sys.stderr, "Importing %s..." % (date)
lit_date = get_lit_date(date, lit_years, session)
# Check if we already have a mass here
try:
lit_date.get_masses(strict=False)
except SelectingMassException:
pass
else:
print >> sys.stderr, " * skipping because a valid mass already exists"
continue
winner = lit_date.get_winner(remove_ok=True)
if winner is None:
print >> sys.stderr, " * skipping because there are no masses today"
continue
event = winner[1]
with open(os.path.join('scrape', '%04d-%02d-%02d.html' % (year, month, day))) as fhtml:
quotes = scrape_file(fhtml)
if u'auto' not in event.status.split(u' '):
event.status += u' auto'
mass = Mass()
mass.order = 0
mass.event = event
mass.digit = lit_date.digit
mass.letter = lit_date.letter
mass.title = None
mass.status = u'auto'
session.add(mass)
order = 0
if len(quotes) == 4:
titles = [u'Prima lettura', u'Salmo responsoriale', u'Seconda lettura', u'Vangelo']
elif len(quotes) == 3:
titles = [u'Prima lettura', u'Salmo responsoriale', u'Vangelo']
# Domenica delle Palme
elif len(quotes) == 5:
titles = [u'Vangelo delle Palme', u'Prima lettura', u'Salmo responsoriale', u'Seconda lettura', u'Vangelo']
# Pasqua
elif len(quotes) == 17:
titles = [u'Prima lettura',
u'Salmo responsoriale',
u'Seconda lettura',
u'Salmo responsoriale',
u'Terza lettura',
u'Salmo responsoriale',
u'Quarta lettura',
u'Salmo responsoriale',
u'Quinta lettura',
u'Salmo responsoriale',
u'Sesta lettura',
u'Salmo responsoriale',
u'Settima lettura',
u'Salmo responsoriale',
u'Epistola',
u'Salmo responsoriale',
u'Vangelo']
else:
raise Exception('Strange number of readings (%d)' % (len(quotes)))
for (quote, text), title in zip(quotes, titles):
reading = Reading()
reading.order = order
order += 1
reading.alt_num = 0
reading.mass = mass
reading.title = title
reading.quote = canonicalise_quote(quote)
reading.text = text
try:
decode_quote(quote, allow_only_chap=True, valid_abbr=ABBR_VATICAN)
except:
reading.quote_status = u'auto invalid'
else:
reading.quote_status = u'auto'
if text is None:
reading.text_status = u'missing'
else:
reading.text_status = u'auto'
session.add(reading)
session.flush()
# Write some interesting things
#print '#'
#print_lit_date(lit_date, PrependStream(sys.stdout, '# '))
#print
#print json.dumps(event.as_dict(), encoding='utf-8', ensure_ascii=False, indent=2, sort_keys=True)
#print
session.commit()
session.close()
# def import_from_pickle():
# data = pickle.load(sys.stdin)
# lit_years = {}
# session = Session()
# for piece in data:
# date = piece['date']
# lit_date = get_lit_date(date, lit_years, session)
# event = lit_date.get_winner()[1]
# quotes = map(lambda x: [None, canonicalise_quote(x)], piece['quotes'] + [piece['quote_vangelo']])
# quotes[-1][0] = piece['text_vangelo']
# mass = Mass()
# mass.order = 0
# mass.event = event
# mass.digit = lit_date.digit
# mass.letter = lit_date.letter
# mass.title = None
# mass.status = u'auto'
# session.add(mass)
# order = 0
# if len(quotes) == 4:
# titles = [u'Prima lettura', u'Salmo responsoriale', u'Seconda lettura', u'Vangelo']
# elif len(quotes) == 3:
# titles = [u'Prima lettura', u'Salmo responsoriale', u'Vangelo']
# # Domenica delle Palme
# elif len(quotes) == 5:
# titles = [u'Vangelo delle Palme', u'Prima lettura', u'Salmo responsoriale', u'Seconda lettura', u'Vangelo']
# # Pasqua
# elif len(quotes) == 17:
# titles = [u'Prima lettura',
# u'Salmo responsoriale',
# u'Seconda lettura',
# u'Salmo responsoriale',
# u'Terza lettura',
# u'Salmo responsoriale',
# u'Quarta lettura',
# u'Salmo responsoriale',
# u'Quinta lettura',
# u'Salmo responsoriale',
# u'Sesta lettura',
# u'Salmo responsoriale',
# u'Settima lettura',
# u'Salmo responsoriale',
# u'Epistola',
# u'Salmo responsoriale',
# u'Vangelo']
# else:
# raise Exception('Strange number of readings (%d)' % (len(quotes)))
# for (text, quote), title in zip(quotes, titles):
# reading = Reading()
# reading.order = order
# order += 1
# reading.alt_num = 0
# reading.mass = mass
# reading.title = title
# reading.quote = quote
# reading.text = text
# try:
# decode_quote(quote, allow_only_chap=True, valid_abbr=ABBR_VATICAN)
# except:
# reading.quote_status = u'auto (invalid)'
# else:
# reading.quote_status = u'auto'
# if text is None:
# reading.text_status = u'missing'
# else:
# reading.text_status = u'auto'
# session.add(reading)
# session.flush()
# # Write some interesting things
# print "#%s:" % (date)
# print "# Indications: %s" % (piece['indications'])
# print "# Winner: %s" % (event.title)
# print
# print json.dumps(event.as_dict(), encoding='utf-8', ensure_ascii=False, indent=2, sort_keys=True)
# print
# #session.commit()
# session.close()
if __name__ == '__main__':
import locale
import codecs
sys.stdout = codecs.getwriter(locale.getpreferredencoding())(sys.stdout)
#import_from_pickle()
import_from_scrape(*map(int, sys.argv[1:]))