-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgenerator.py
248 lines (211 loc) · 9.33 KB
/
generator.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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
import datetime
from orm import *
from sqlalchemy.sql import func, and_
from random import randint
from timeit import default_timer as timer
from queries import query_card_data, NoTextError, regionCodeCheck
import uuid
import ast
# from templates_parser import templates_parser
from text_templates import text_templates
deploy_mode = False
def generateMeta(args):
with session_scope() as sess:
# text_templates = templates_parser()
polls = args['type']
regions = args['region']
parties = args['party']
candidates = args['candidate']
# TODO
time = datetime.datetime.strptime(args['time'], '%Y%m%d%H%M%S')
# print(datetime.datetime.now())
# time = datetime.datetime.now()
serial_current = str(uuid.uuid4().hex)
arguments = args
# if 'time' in arguments:
# del arguments['time']
# 데이터가 업데이트 되는 시간
# 쿼리로 임의로 시간을 시뮬레이셔할때는
if time > datetime.datetime(2018, 6, 13, 18, 59, 59):
t = 18
else:
t = time.hour
time_update = []
# TODO:
try:
time_update.append(sess.query(VoteProgressLatest.datatime).order_by(VoteProgressLatest.datatime.desc()).first()[0])
except TypeError:
pass
try:
time_update.append(sess.query(OpenProgress2.datatime).order_by(OpenProgress2.datatime.desc()).first()[0])
except TypeError:
pass
try:
time_update.append(sess.query(OpenProgress3.datatime).order_by(OpenProgress3.datatime.desc()).first()[0])
except TypeError:
pass
try:
time_update.append(sess.query(OpenProgress4.datatime).order_by(OpenProgress4.datatime.desc()).first()[0])
except TypeError:
pass
try:
time_update.append(sess.query(OpenProgress11.datatime).order_by(OpenProgress11.datatime.desc()).first()[0])
except TypeError:
pass
serial_ontable = sess.query(QueryTime.serial).filter(QueryTime.args==str(arguments), QueryTime.times==str(time_update)).first() # 값이 나오면 같은게 있다는 것
if deploy_mode:
if serial_ontable != None:
serial_ontable = serial_ontable[0]
# print("serial: ", serial_ontable)
meta_previous = sess.query(MetaCards.meta).filter(MetaCards.serial==serial_ontable).scalar()
else:
serial_ontable = None
mete_previous = None
if (serial_ontable != None) and (meta_previous != None): # 전에 있으면
meta = ast.literal_eval(meta_previous)
meta['updated'] = False
else: # 전에 없으면
if deploy_mode:
row = QueryTime(serial=serial_current, args=str(arguments), times=str(time_update))
sess.add(row)
# sess.commit()
card_seqs, seqs_type, template = getCardSeqs(sess, polls, regions, parties, candidates, time)
# print(template, card_seqs)
meta = {}
meta['scenario'] = str(card_seqs)
meta['updated'] = True
meta['serial'] = serial_current
meta['card_count'] = len(card_seqs)
meta['design_variation'] = randint(1,4)
meta_cards = []
index = 0
for i, card_seq in enumerate(card_seqs):
if card_seqs[i-1] is card_seq:
index += 1
else:
index = 0
order = i+1
try:
meta_card = query_card_data(text_templates, sess, order, index, polls, regions, parties, candidates, time, card_seq, seqs_type, template)
except NoTextError:
print("pass: ", card_seq)
continue
meta_cards.append(meta_card)
# end for
# try:
meta_cards = list({v['data']['text']:v for v in meta_cards}.values())
meta['cards'] = meta_cards
if deploy_mode:
meta_row = MetaCards(serial=serial_current, meta=str(meta))
sess.add(meta_row)
# sess.commit()
return meta
def getCardSeqs(sess, polls, regions, parties, candidates, time):
# vote에서 중요
if time > datetime.datetime(2018, 6, 13, 18, 59, 59):
t = 18
else:
t = time.hour
if time > datetime.datetime(2018, 6, 13, 23, 59, 59):
t2 = 23
else:
t2 = time.hour
card_seqs = []
if t2 > 18:
if (len(candidates) > 0):
# print(1)
try:
candidate, candidate_region, candidate_poll_code = sess.query(CandidateInfo.name, CandidateInfo.sggName, CandidateInfo.sgTypecode).filter(CandidateInfo.huboid==candidates[0]).first()
# print(candidate_region)
except TypeError:
raise NoTextError
if candidate_poll_code == 2: # 국회의원
openrate = sess.query(func.max(OpenProgress.openPercent)).filter(OpenProgress.sgg==candidate_region, OpenProgress.datatime<=time, OpenProgress.sggCityCode!=None).scalar() # ,
elif candidate_poll_code == 3:
openrate = sess.query(func.max(OpenProgress.openPercent)).filter(OpenProgress.sido==candidate_region, OpenProgress.gusigun=='합계',OpenProgress.datatime<=time).scalar() # ,
elif candidate_poll_code == 4:
openrate = sess.query(func.max(OpenProgress.openPercent)).filter(OpenProgress.gusigun==candidate_region, OpenProgress.datatime<=time, OpenProgress.sggCityCode!=None).scalar() # ,
elif candidate_poll_code == 11:
openrate = sess.query(func.max(OpenProgress.openPercent)).filter(OpenProgress.sido==candidate_region, OpenProgress.gusigun=='합계',OpenProgress.datatime<=time).scalar() # ,
else:
openrate = None
# print(candidate_poll_code, openrate)
elif (len(candidates) == 0) and (len(regions) > 0):
# print(2)
region_num, _ = regionCodeCheck(regions[0])
try:
region1, region2 = sess.query(PrecinctCode.sido, PrecinctCode.gusigun).filter(PrecinctCode.sggCityCode==region_num).first()
except TypeError:
raise NoTextError
openrate = sess.query(func.max(OpenProgress.openPercent)).filter(OpenProgress.datatime<=time, OpenProgress.sido==region1, OpenProgress.gusigun=='합계').scalar()
# print(openrate)
# parites
elif (len(candidates) == 0) and (len(regions) == 0) and (len(polls) > 0):
# print(3)
if polls[0] == 2:
s = sess.query(OpenProgress.sgg, func.max(OpenProgress.n_total).label('n_total'), func.max(OpenProgress.invalid).label('invalid'), func.max(OpenProgress.tooTotal).label('tooTotal')).filter(OpenProgress.datatime<=time, OpenProgress.electionCode==2, OpenProgress.sggCityCode!=None).group_by(OpenProgress.sgg)
elif polls[0] == 3:
s = sess.query(OpenProgress.sido, func.max(OpenProgress.n_total).label('n_total'), func.max(OpenProgress.invalid).label('invalid'), func.max(OpenProgress.tooTotal).label('tooTotal')).filter(OpenProgress.datatime<=time, OpenProgress.electionCode==3, OpenProgress.gusigun=='합계').group_by(OpenProgress.sido)
elif polls[0] == 4:
s = sess.query(OpenProgress.gusigun, func.max(OpenProgress.n_total).label('n_total'), func.max(OpenProgress.invalid).label('invalid'), func.max(OpenProgress.tooTotal).label('tooTotal')).filter(OpenProgress.datatime<=time, OpenProgress.electionCode==4, OpenProgress.sggCityCode!=None).group_by(OpenProgress.gusigun)
elif polls[0] == 11:
s = sess.query(OpenProgress.sido, func.max(OpenProgress.n_total).label('n_total'), func.max(OpenProgress.invalid).label('invalid'), func.max(OpenProgress.tooTotal).label('tooTotal')).filter(OpenProgress.datatime<=time, OpenProgress.electionCode==11, OpenProgress.gusigun=='합계').group_by(OpenProgress.sido)
openrate = sess.query((func.sum(s.subquery().c.n_total) + func.sum(s.subquery().c.invalid)) / func.sum(s.subquery().c.tooTotal) * 100).scalar()
else:
# print(4)
s = sess.query(OpenProgress.sido, func.max(OpenProgress.n_total).label('n_total'), func.max(OpenProgress.invalid).label('invalid'), func.max(OpenProgress.tooTotal).label('tooTotal')).filter(OpenProgress.datatime<=time, OpenProgress.electionCode==3, OpenProgress.gusigun=='합계').group_by(OpenProgress.sido)
openrate = sess.query((func.sum(s.subquery().c.n_total) + func.sum(s.subquery().c.invalid)) / func.sum(s.subquery().c.tooTotal) * 100).scalar()
else:
openrate = 0
if openrate == None:
openrate = 0
# print("openrate: ", openrate)
if t2 <= 18: # 투표중
card_seqs.extend([1, 2, 3, 6, 23]) # 6 특이사항
card_seqs.extend([4] * len(regions))
card_seqs.extend([5] * len(candidates))
card_seqs.sort()
seqs_type = 0
template = 1
# 어떤 선거의 개표율 10 기준?
elif (t2 > 18) and (openrate < 10): # 투표마감이후
card_seqs.extend([1, 2, 3, 6, 22, 23]) # 6 특이사항
card_seqs.extend([4] * len(regions))
card_seqs.extend([5] * len(candidates))
card_seqs.sort()
seqs_type = 0
template = 2
elif (t2 > 18) and (openrate >= 10) and (openrate < 30): # 개표율 10% 이상
card_seqs.extend([1, 2, 3, 7, 8, 9, 20, 23]) # 6, 13, 20 특이사항
card_seqs.extend([4] * len(regions))
card_seqs.extend([5] * len(candidates))
card_seqs.extend([10] * len(regions))
card_seqs.extend([11] * len(polls))
card_seqs.extend([12] * len(candidates))
card_seqs.extend([16] * len(regions))
card_seqs.extend([17] * len(candidates))
card_seqs.extend([18] * len(parties))
card_seqs.sort()
seqs_type = 1
template = 3
elif (t2 > 18) and (openrate >= 30): # 개표율 30% 이상
card_seqs.extend([1, 2, 7, 15, 20, 23]) # 13, 20 특이사항
card_seqs.extend([10] * len(regions))
card_seqs.extend([11] * len(polls))
card_seqs.extend([12] * len(candidates))
card_seqs.extend([16] * len(regions))
card_seqs.extend([17] * len(candidates))
card_seqs.extend([18] * len(parties))
card_seqs.sort()
card_seqs.insert(1, 21)
seqs_type = 1
if openrate < 100:
template = 4
else:
template = 5
# 내가 선택한 선거에서 한명이라도 당선 확정이 나오는 경우 21번을 index 1에 insert
# else:
# seqs_type = 0
return card_seqs, seqs_type, template
if __name__ == '__main__':
print(VoteProgressLatest)