Comic #800
[nose_ears_website.git] / csv_to_content.py
blobdc333fda7f4146efd8907be0ace09b42e4b41655
1 #!/usr/bin/env python3
2 # -*- coding: utf-8 -*-
4 # Run this script to generate the comic .md files from the CSV files
5 # and put them into content/comic.
7 import csv
8 csvfile_main = open('./comic_data.csv', newline='')
9 if csvfile_main == None:
10 print('Could not load main CSV file!')
11 exit()
13 langs = ['en', 'de']
14 csvfiles_lang = {}
15 for lang in langs:
16 csvfile = open('./comic_data_'+lang+'.csv', newline='')
17 if csvfile == None:
18 print('Could not load language CSV file!')
19 exit()
20 csvfiles_lang[lang] = csvfile
22 for lang in langs:
23 # Re-open main file to reset read/write pointer
24 # TODO: Properly use seek function...
25 csvfile_main = open('./comic_data.csv', newline='')
26 if csvfile_main == None:
27 print('Could not re-open main CSV file!')
28 exit()
30 table = []
31 reader_main = csv.reader(csvfile_main, delimiter=',', quotechar='"')
32 for row in reader_main:
33 _id_main = row[0]
34 topics = row[1]
35 creationDate = row[2]
36 publishDate = row[3]
37 # The version of the comic, in case a comic needs updating
38 # The first version is always 1 and is incremented later.
39 version = row[4]
41 csvfile_lang = csvfiles_lang[lang]
42 reader_lang = csv.reader(csvfile_lang, delimiter=',', quotechar='"')
43 for row_lang in reader_lang:
44 _id_lang = row_lang[0]
45 if _id_main == _id_lang:
46 if (len(row_lang) < 4):
47 print('Insufficient columns in line '+_id_main+' (lang='+lang+')')
48 exit()
50 title = row_lang[1]
51 altText = row_lang[2]
52 authorComment = row_lang[3]
53 table.append([_id_main, topics, creationDate, publishDate, version, title, altText, authorComment])
54 break
55 print (lang)
56 for row in table:
57 _id = row[0]
58 topics = row[1]
59 topics_t = topics.replace("|", "\",\"")
60 if topics_t != "":
61 topics_t = "\"" + topics_t + "\""
62 creationDate = row[2]
63 publishDate = row[3]
64 version = row[4]
65 title = row[5]
66 altText = row[6]
67 authorComment = row[7]
69 if title != "___UNUSED___":
70 out = ""
71 out += "+++\n"
72 out += "weight= \"" + _id + "\"\n"
73 out += "title = \"" + title + "\"\n"
74 out += "topics = [" + topics_t + "]\n"
75 out += "creationDate = \"" + creationDate + "\"\n"
76 out += "date = \"" + publishDate + "\"\n"
77 out += "publishDate = \"" + publishDate + "\"\n"
78 out += "comic_version = \"" + version + "\"\n"
79 if altText != "" and altText != None:
80 out += "alt_text = \"" + altText + "\"\n"
81 if authorComment != "" and authorComment != None:
82 out += "author_comment = \"" + authorComment + "\"\n"
83 out += "+++"
85 if lang != 'en':
86 lang_tag = '.'+lang
87 else:
88 lang_tag = ''
89 with open("./content/comic/"+_id+lang_tag+".md", "w", encoding='utf-8') as file:
90 file.write(out)
92 csvfile_lang.close()
93 csvfile_main.close()