116 lines
2.5 KiB
Python
Executable File
116 lines
2.5 KiB
Python
Executable File
#!/usr/bin/env python3
|
|
# ____ ___ _ _
|
|
# | __ )_ _| | / |
|
|
# | _ \| |_ | | |
|
|
# | |_) | | |_| | |
|
|
# |____/___\___/|_|
|
|
#
|
|
# Bij1 Wings Sitemap Generator
|
|
|
|
from os.path import isfile
|
|
import json
|
|
import sys
|
|
import xlsxwriter
|
|
|
|
if len(sys.argv) < 3:
|
|
print('Please supply a json-file and an output xlsx filename as arguments.')
|
|
quit()
|
|
|
|
json_filename = sys.argv[1]
|
|
output_filename = sys.argv[2]
|
|
|
|
if not isfile(json_filename):
|
|
print('Json file could not be found')
|
|
quit()
|
|
|
|
content = {}
|
|
|
|
with open(json_filename, "r") as content_raw:
|
|
content = json.loads(content_raw.read())
|
|
|
|
# ----------------------------
|
|
# Retrieve data from json
|
|
# ----------------------------
|
|
|
|
ordered_data = {
|
|
'articles': [],
|
|
'pages': [],
|
|
'fundraisers': [],
|
|
'signups': [],
|
|
'petitions': [],
|
|
}
|
|
|
|
for cat in ordered_data:
|
|
mobiledoc_key = 'content'
|
|
if cat not in ['pages', 'articles']:
|
|
mobiledoc_key = 'description'
|
|
|
|
for item in content['data'][cat]['edges']:
|
|
node = item['node']
|
|
ordered_data[cat].append({
|
|
'slug': node['slug'],
|
|
'title': node['title'],
|
|
'status': node['status'],
|
|
'mobiledoc': node[mobiledoc_key],
|
|
'language': node['locale']['name'],
|
|
})
|
|
|
|
# ----------------------------
|
|
# Generate xlsx file
|
|
# ----------------------------
|
|
|
|
workbook = xlsxwriter.Workbook(output_filename)
|
|
worksheet = workbook.add_worksheet()
|
|
bold = workbook.add_format({'bold': True})
|
|
|
|
header_format = workbook.add_format({
|
|
'bold': True,
|
|
'align': 'center',
|
|
'valign': 'vcenter',
|
|
'fg_color': '#D7E4BC',
|
|
'border': 1
|
|
})
|
|
|
|
worksheet.set_row(0, None, header_format)
|
|
|
|
col_widths = []
|
|
|
|
# Set up header column
|
|
column_names = [
|
|
'url', 'type', 'title', 'slug', 'language', 'status'
|
|
]
|
|
|
|
column_widths = {
|
|
'url': 50,
|
|
'type': 10,
|
|
'title': 40,
|
|
'slug': 20,
|
|
'language': 20,
|
|
'status': 20,
|
|
}
|
|
|
|
col = 0
|
|
for column_name in column_names:
|
|
worksheet.write(0, col, column_name)
|
|
width = (len(column_name) + 5)* 1.5
|
|
worksheet.set_column(col, col, column_widths[column_name])
|
|
col += 1
|
|
|
|
row = 0
|
|
|
|
for cat in ordered_data:
|
|
for node in ordered_data[cat]:
|
|
row += 1
|
|
url = 'https://www.bij1.org/'
|
|
if cat != 'pages':
|
|
url += cat + '/'
|
|
url += node['slug']
|
|
|
|
worksheet.write(row, 0, url)
|
|
worksheet.write(row, 1, cat)
|
|
worksheet.write(row, 2, node['title'])
|
|
worksheet.write(row, 3, node['slug'])
|
|
worksheet.write(row, 4, node['language'])
|
|
worksheet.write(row, 5, node['status'])
|
|
workbook.close()
|