Comics #838, #839
[nose_ears_website.git] / csv_to_content.py
blob5f09d16be3c1169d4bd4253ed57661456bb6e155
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]
40 series = row[5]
42 csvfile_lang = csvfiles_lang[lang]
43 reader_lang = csv.reader(csvfile_lang, delimiter=',', quotechar='"')
44 for row_lang in reader_lang:
45 _id_lang = row_lang[0]
46 if _id_main == _id_lang:
47 if (len(row_lang) < 4):
48 print('Insufficient columns in line '+_id_main+' (lang='+lang+')')
49 exit()
51 title = row_lang[1]
52 altText = row_lang[2]
53 authorComment = row_lang[3]
54 table.append([_id_main, topics, creationDate, publishDate, version, title, altText, authorComment, series])
55 break
56 print (lang)
57 for row in table:
58 _id = row[0]
59 topics = row[1]
60 topics_t = topics.replace("|", "\",\"")
61 if topics_t != "":
62 topics_t = "\"" + topics_t + "\""
63 creationDate = row[2]
64 publishDate = row[3]
65 version = row[4]
66 title = row[5]
67 altText = row[6]
68 authorComment = row[7]
69 series = row[8]
71 if title != "___UNUSED___":
72 out = ""
73 out += "+++\n"
74 out += "weight= \"" + _id + "\"\n"
75 out += "title = \"" + title + "\"\n"
76 out += "topics = [" + topics_t + "]\n"
77 out += "series = \"" + series + "\"\n"
78 out += "creationDate = \"" + creationDate + "\"\n"
79 out += "date = \"" + publishDate + "\"\n"
80 out += "publishDate = \"" + publishDate + "\"\n"
81 out += "comic_version = \"" + version + "\"\n"
82 if altText != "" and altText != None:
83 out += "alt_text = \"" + altText + "\"\n"
84 if authorComment != "" and authorComment != None:
85 out += "author_comment = \"" + authorComment + "\"\n"
86 out += "+++"
88 if lang != 'en':
89 lang_tag = '.'+lang
90 else:
91 lang_tag = ''
92 with open("./content/comic/"+_id+lang_tag+".md", "w", encoding='utf-8') as file:
93 file.write(out)
95 csvfile_lang.close()
96 csvfile_main.close()