source: main/trunk/model-sites-dev/eurovision-lod/collect/eurovision/prepare/voting-excel/xlsx-to-jsonmetadata.py@ 34863

Last change on this file since 34863 was 34863, checked in by davidb, 3 years ago

Voting metadata added in

  • Property svn:executable set to *
File size: 6.6 KB
Line 
1#!/usr/bin/env python
2
3from __future__ import print_function
4
5import os
6import re
7import sys
8import json
9
10import argparse
11import openpyxl
12
13import xlsxutil
14
15def eprint(*args, **kwargs):
16 print(*args, file=sys.stderr, **kwargs)
17
18def fileset_voting_for_esc_country_in_year(data_hashmap):
19
20 # Only generating a single fileset record here, however
21 # the Greenstone format allows for this to be an array
22 # of fileset entries => return [ fileset ]
23
24 fileset = {}
25
26 return [ fileset ]
27
28
29def filter_finalist_votes(entry):
30 return entry.get('(semi-) final') == "f" and entry.get('Duplicate') == None
31
32def sortkey_year_to_country(entry):
33 return str(entry.get('Year'))+entry.get('To country')
34
35
36def create_to_country_voting_groups(data_hashmap_array):
37
38 # Example values for header-names
39 # (semi-) final: f, sf
40 # Jury or Televoting: J, T
41 # Year: 1975
42 # To country: Belgium
43 # From country: Belgium
44 # Edition: 1975f, 1975sf
45
46
47 # Filter down to just the voting results concerning finals
48 data_hashmap_array_finals = list(filter(filter_finalist_votes, data_hashmap_array))
49
50 # Sort so array entries are grouped by the country receiving the votes in a given year
51 data_hashmap_array_finals.sort(key=sortkey_year_to_country)
52
53
54 # Debug output
55 #
56 # for data_hashmap in data_hashmap_array_finals:
57 # print data_hashmap.get('To country'), data_hashmap.get('Year'), data_hashmap.get('Points'), "(Points from " + data_hashmap.get('From country') + ")"
58
59
60 # Build array of country groups
61 # A country grouping includes all the votes that country receive that year
62 country_groups = []
63
64 prev_data_hashmap = data_hashmap_array_finals[0]
65 country_group = [ ]
66
67 i = 1
68 num_finals = len(data_hashmap_array_finals)
69
70 while (i < num_finals):
71 country_group.append(prev_data_hashmap)
72 data_hashmap = data_hashmap_array_finals[i]
73
74 if (data_hashmap.get('To country') != prev_data_hashmap.get('To country')):
75 # moving on to a new country group
76 country_groups.append(country_group)
77 country_group = [ ]
78
79 prev_data_hashmap = data_hashmap
80 i = i + 1
81
82 country_group.append(prev_data_hashmap)
83 country_groups.append(country_group)
84
85 return country_groups
86
87
88def fileset_voting_for_esc_country_in_year(to_country_year_votes):
89
90 # Looking to build data-structure (for output as JSON) in the form
91 # { "FileSet":
92 # [
93 # { "FileName": "France1991\.nul" },
94 # { "Description":
95 # {
96 # "Metadata":
97 # [
98 # { "name": "Germany-J", "content": 12 }, # J = Jury Vote
99 # { "name": "Germany-T", "content": 6 }, # T = Televote (if present)
100 # ...
101 # ]
102 # }
103 # }
104 # ]
105 # }
106
107 fileset_array = []
108
109 metadata_array = []
110
111 jury_metadata_vals = []
112 tele_metadata_vals = []
113
114 for to_country_year_vote in to_country_year_votes:
115 to_country = to_country_year_vote.get('To country')
116 year = to_country_year_vote.get('Year')
117 from_country = to_country_year_vote.get('From country')
118 vote_type = to_country_year_vote.get('Jury or Televoting')
119 points = to_country_year_vote.get('Points')
120
121 voting_rec = { "name": from_country+"-"+vote_type, "content": points }
122
123 metadata_array.append(voting_rec)
124
125 if (vote_type == "J"):
126 jury_metadata_vals.append(from_country+"-J")
127 elif (vote_type == "T"):
128 tele_metadata_vals.append(from_country+"-T")
129 else:
130 eprint("Warning: Unrecognized voting type: " + vote_type)
131
132 if (len(jury_metadata_vals)>0):
133 metadata_array.append({ "name": "JuryVotesJSON", "content": json.dumps(jury_metadata_vals) })
134 if (len(tele_metadata_vals)>0):
135 metadata_array.append({ "name": "TeleVotesJSON", "content": json.dumps(tele_metadata_vals) })
136
137 id_to_country = to_country_year_votes[0].get('To country')
138 id_to_country = re.sub(r'\s+', '', id_to_country)
139 id_year = to_country_year_votes[0].get('Year');
140 id = id_to_country + str(id_year);
141 filename_id = id + "\\.nul"
142
143 fileset = {
144 "FileSet" : [
145 { "FileName": filename_id, },
146 { "Description" : { "Metadata" : metadata_array } }
147 ]
148 }
149
150 return fileset
151
152
153if __name__ == "__main__":
154
155 parser = argparse.ArgumentParser()
156 parser.add_argument('input-file.xlsx')
157 parser.add_argument('output-file.json', nargs='?')
158 parser.add_argument('--sheetname')
159
160 args = parser.parse_args()
161
162 excel_input_filename = getattr(args,'input-file.xlsx');
163 json_output_filename = getattr(args,'output-file.json');
164 sheetname = getattr(args,'sheetname');
165
166 if (json_output_filename == None):
167 json_output_filename = os.path.splitext(excel_input_filename)[0]+'.json'
168
169 worksheet = xlsxutil.load_xslx_sheet(excel_input_filename,sheetname)
170
171 data_hashmap_array = xlsxutil.convert_worksheet_to_hashmaps(worksheet)
172
173 print("Number of data rows: " + str(len(data_hashmap_array)))
174
175
176 to_country_year_voting_groups = create_to_country_voting_groups(data_hashmap_array)
177
178 # Debug output
179 #
180 # print to_country_year_voting_groups
181
182 # Next step is to express the grouped by-country voting data
183 # in the Greenstone JSON metadata format:
184
185 # { "DirectoryMetadata":
186 # [
187 # { "FileSet":
188 # [
189 # { "FileName": "France1991\.nul" },
190 # { "Description":
191 # {
192 # "Metadata":
193 # [
194 # { "name": "Germany-J", "content": "12" }, # J = Jury Vote
195 # ...
196 # ]
197 # }
198 # }
199 # ]
200 # }
201 # ...
202 # ]
203 # }
204
205 directory_metadata = []
206
207 print("Creating Greenstone JSON voting metadata for:")
208 for to_country_year_votes in to_country_year_voting_groups:
209
210 fileset = fileset_voting_for_esc_country_in_year(to_country_year_votes)
211 directory_metadata.append(fileset)
212
213 filename_id = fileset.get('FileSet')[0].get('FileName')
214 num_countries_voting_data = len(fileset.get('FileSet')[1].get('Description').get('Metadata'))
215
216 print(" " + filename_id.ljust(28) + ": " + str(num_countries_voting_data) + " votes")
217
218 greenstone_metadata_json = { "DirectoryMetadata": directory_metadata }
219
220
221 with open(json_output_filename, 'w') as outfile:
222 json.dump(greenstone_metadata_json, outfile, indent=2)
Note: See TracBrowser for help on using the repository browser.