Implemented generating the individual articles
[biaweb_qt.git] / biaweb_exporter.py
1 # BiaWeb Website content manager (c) 2010 V.Harishankar
2 # Site exporter/generator class
3
4 import os
5 import os.path
6 import time
7 import sqlite3
8 import string
9 import shutil
10 import biaweb_db
11
12 # to format the best rated articles in a HTML link list
13 def html_format_best_rated (best_rated):
14 items = [ "<ul>\n", ]
15 for art in best_rated:
16 # art[13] is category stub, art[8] is article stub
17 # thus forming the relative url as Category/Article.html
18 str_art = '<li><a href="' + art[13] + '/' + art[8] + '.html">' + art[1] + '</a></li>\n'
19 items.append (str_art)
20 items.append ("</ul>\n")
21 str_items = "".join (items)
22 return str_items
23
24 # to format categories in a HTML link list
25 def html_format_categories (cats):
26 items = [ "<ul>\n", ]
27 for cat in cats:
28 # cat[3] is category stub and cat[1] is category name
29 str_cat = '<li><a href="' + cat[3] + '/">' + cat[1] + '</a></li>\n'
30 items.append (str_cat)
31 items.append ("</ul>\n")
32 str_items = "".join (items)
33 return str_items
34
35 # to convert a rating number into rating images out of 10 stars
36 def html_format_rating (rating):
37 items = []
38 # if -1 then return unrated as the text
39 if rating == -1:
40 return "unrated"
41 # fill up the number of stars for the rating
42 for i in range (rating):
43 items.append ('<img src="star.gif" alt="*" />')
44 # fill up remaining slots (of 10) with grey stars
45 for i in range (10 - rating):
46 items.append ('<img src="star-grey.gif" alt="-" />')
47
48 rating_str = "".join (items)
49 return rating_str
50
51 # function to generate article pages
52 def generate_article_pages (dbname, conf, templates, category_str, bestrated_str):
53 # main template
54 tpl_main = string.Template (templates[0][1])
55 # article template
56 tpl_articlebit = string.Template (templates[1][1])
57
58 # get all articles from the database
59 articles = biaweb_db.site_articles (dbname)
60 if articles == False:
61 return
62
63 # walk through each article and generate the file in the appropriate category
64 # folder
65 for art in articles:
66 art_cdate = time.ctime (art[5])
67 art_mdate = time.ctime (art[6])
68 rating_str = html_format_rating (art[9])
69 # now build the article from the article bit template
70 article_str = tpl_articlebit.safe_substitute (article_title = art[1],
71 article_cdate = art_cdate,
72 article_mdate = art_mdate,
73 rating = rating_str,
74 article_contents = art[4])
75
76 # now build the article page
77 articlepage_str = tpl_main.safe_substitute (site_title = conf[1],
78 site_url = "http://" + conf[0],
79 meta_keywords = art[3],
80 meta_description = art[2],
81 page_title = conf[1],
82 page_desc = conf[3],
83 contents_bit = article_str,
84 list_of_categories = category_str,
85 list_best_rated = bestrated_str,
86 copyright = conf[6])
87 # write to the article file
88 try:
89 farticle = open (os.path.join (conf[5], art[13], art[8] + ".html"), "w+")
90 farticle.write (articlepage_str)
91 except OSError, IOError:
92 return False
93
94 # finally return true
95 return True
96
97 # function to generate category directories and indices
98 def generate_category_indices (dbname, conf, templates, category_str, bestrated_str, category_list):
99 # main template
100 tpl_main = string.Template (templates[0][1])
101 # table bit
102 tpl_tablebit = string.Template (templates[3][1])
103 # table row bit
104 tpl_trowbit = string.Template (templates[4][1])
105
106 # run through each category and generate category index page
107 for cat in category_list:
108 try:
109 # create the category directory
110 os.mkdir (os.path.join (conf[5], cat[3]))
111 except IOError, OSError:
112 return False
113
114 # now get the list of articles for the specified category
115 articles_list = biaweb_db.site_articles (dbname, cat[0])
116 if articles_list == False:
117 return False
118
119 tableitems = []
120 # run through the list of articles in category
121 for art in articles_list:
122 url = art[13] + "/" + art[8] + ".html"
123 creattime = time.ctime (art[5])
124 rating_str = html_format_rating (art[9])
125 # now build the table rows for each article
126 tableitem_str = tpl_trowbit.safe_substitute (article_url = url,
127 title = art[1],
128 created = creattime,
129 rating = rating_str)
130 tableitems.append (tableitem_str)
131 # generate the rows as a string
132 tablerows_str = "".join (tableitems)
133
134 # now create the page template
135 table_str = tpl_tablebit.safe_substitute (category_title = cat[1],
136 category_desc = cat[2],
137 table_rows = tablerows_str)
138
139 # now create the index page
140 categoryindex_str = tpl_main.safe_substitute (site_title = conf[1],
141 site_url = "http://" + conf[0],
142 meta_keywords = conf[2],
143 meta_description = cat[2],
144 page_title = conf[1],
145 page_desc = conf[3],
146 contents_bit = table_str,
147 list_of_categories = category_str,
148 list_best_rated = bestrated_str,
149 copyright = conf[6])
150
151 # now write to Category/index.html
152 try:
153 fcatindex = open (os.path.join (conf[5], cat[3], "index.html"), "w+")
154 fcatindex.write (categoryindex_str)
155 fcatindex.close ()
156 except OSError, IOError:
157 return False
158
159 # finally return true
160 return True
161
162 # function to generate main index file and stylesheet
163 def generate_home_page (dbname, conf, templates, category_str, bestrated_str):
164 # main template
165 tpl_main = string.Template (templates[0][1])
166 # index bit
167 tpl_indexbit = string.Template (templates[6][1])
168 # news bits
169 tpl_newsbit = string.Template (templates[2][1])
170
171 # get the latest articles - conf[4] is num of rss entries to be used also
172 latest_arts = biaweb_db.site_latest_articles (dbname, conf[4])
173 if latest_arts == False:
174 return False
175
176 news_items = []
177
178 # Run through the latest articles
179 # for the num of latest news items on index
180 for art in latest_arts:
181 # url is Category/Article.html
182 url = art[13] + "/" + art[8] + ".html"
183 # art[5] is creation time
184 strdate = time.ctime (art[5])
185 # now populate the template variables. art[1] is title, art[2] is summary
186 strnews = tpl_newsbit.safe_substitute (news_title = art[1],
187 news_link = url,
188 news_datetime = strdate,
189 news_description = art[2]
190 )
191 news_items.append (strnews)
192 # now convert it into a string
193 newsbit_str = "".join (news_items)
194
195 # now populate the index template
196 indexbit_str = tpl_indexbit.safe_substitute (site_name = conf[1],
197 news_updates = newsbit_str
198 )
199 # now populate the main page template
200 main_str = tpl_main.safe_substitute (site_title = conf[1],
201 site_url = "http://" + conf[0],
202 meta_keywords = conf[2],
203 meta_description = conf[3],
204 page_title = conf[1],
205 page_desc = conf[3],
206 contents_bit = indexbit_str,
207 list_of_categories = category_str,
208 list_best_rated = bestrated_str,
209 copyright = conf[6])
210
211 # write the index.html file in the destination directory
212 try:
213 findex = open (os.path.join (conf[5], "index.html"), "w+")
214 findex.write (main_str)
215 findex.close ()
216 except IOError, OSError:
217 return False
218
219 # write the style.css file in the destination directory
220 try:
221 fstyle = open (os.path.join (conf[5], "style.css"), "w+")
222 fstyle.write (templates[5][1])
223 fstyle.close ()
224 except IOError, OSError:
225 return False
226
227 return True
228
229 # superfunction to generate the site
230 def generate_site (dbname, files_to_copy, folders_to_copy, search_type_full=True):
231 # get the configuration
232 conf = biaweb_db.get_configuration (dbname)
233 # get the templates
234 tpls = biaweb_db.get_templates (dbname)
235
236 # get the list of categories
237 cats = biaweb_db.get_categories (dbname)
238 # cannot get categories return false
239 if cats == False:
240 return False
241
242 # format the categories as a html bulleted list
243 cats_str = html_format_categories (cats)
244
245 # get the best rated articles
246 best_rated = biaweb_db.site_get_bestrated (dbname)
247 # if cannot retrieve
248 if best_rated == False:
249 return False
250 # format the best rated articles as a html bulleted list
251 best_rated_str = html_format_best_rated (best_rated)
252
253 # remove the destination tree and recreate it
254 try:
255 if os.path.exists (conf[5]):
256 shutil.rmtree (conf[5])
257 os.mkdir (conf[5])
258 except OSError:
259 return False
260
261 # generate the index page including style sheet
262 ret = generate_home_page (dbname, conf, tpls, cats_str, best_rated_str)
263 if ret == False:
264 return False
265
266 # generate the category directories and indices
267 ret = generate_category_indices (dbname, conf, tpls, cats_str, best_rated_str, cats)
268 if ret == False:
269 return False
270
271 # generate the article pages
272 ret = generate_article_pages (dbname, conf, tpls, cats_str, best_rated_str)
273 if ret == False:
274 return False
275
276 # finally when all is successfully done return true
277 return True