X-Git-Url: https://harishankar.org/repos/?p=biaweb_qt.git;a=blobdiff_plain;f=biaweb_exporter.py;fp=biaweb_exporter.py;h=5bbe32b906b484f746f0eca72f072686a8f2a516;hp=0000000000000000000000000000000000000000;hb=f37ae5235dba470cf02e25a885b83a2bc9c78694;hpb=bfa2d7e7b048794a95590ee79ba41aa861835cfa diff --git a/biaweb_exporter.py b/biaweb_exporter.py new file mode 100644 index 0000000..5bbe32b --- /dev/null +++ b/biaweb_exporter.py @@ -0,0 +1,141 @@ +# BiaWeb Website content manager (c) 2010 V.Harishankar +# Site exporter/generator class + +import os +import os.path +import time +import sqlite3 +import string +import shutil +import biaweb_db + +# to format the best rated articles in a HTML link list +def html_format_best_rated (best_rated): + items = [ "\n") + str_items = "".join (items) + return str_items + +# to format categories in a HTML link list +def html_format_categories (cats): + items = [ "\n") + str_items = "".join (items) + return str_items + +# function to generate main index file and stylesheet +def generate_home_page (dbname, conf, templates, category_str, bestrated_str): + # main template + tpl_main = string.Template (templates[0][1]) + # index bit + tpl_indexbit = string.Template (templates[6][1]) + # news bits + tpl_newsbit = string.Template (templates[2][1]) + + # get the latest articles - conf[4] is num of rss entries to be used also + latest_arts = biaweb_db.site_latest_articles (dbname, conf[4]) + if latest_arts == False: + return False + + news_items = [] + + # Run through the latest articles + # for the num of latest news items on index + for art in latest_arts: + # url is Category/Article.html + url = art[13] + "/" + art[8] + ".html" + # art[5] is creation time + strdate = time.ctime (art[5]) + # now populate the template variables. art[1] is title, art[2] is summary + strnews = tpl_newsbit.safe_substitute (news_title = art[1], + news_link = url, + news_datetime = strdate, + news_description = art[2] + ) + news_items.append (strnews) + # now convert it into a string + newsbit_str = "".join (news_items) + + # now populate the index template + indexbit_str = tpl_indexbit.safe_substitute (site_name = conf[1], + news_updates = newsbit_str + ) + # now populate the main page template + main_str = tpl_main.safe_substitute (site_title = conf[1], + site_url = "http://" + conf[0], + meta_keywords = conf[2], + meta_description = conf[3], + page_title = conf[1], + page_desc = conf[3], + contents_bit = indexbit_str, + list_of_categories = category_str, + list_best_rated = bestrated_str, + copyright = conf[6]) + + # write the index.html file in the destination directory + try: + findex = open (os.path.join (conf[5], "index.html"), "w+") + findex.write (main_str) + findex.close () + except IOError, OSError: + return False + + # write the style.css file in the destination directory + try: + fstyle = open (os.path.join (conf[5], "style.css"), "w+") + fstyle.write (templates[5][1]) + fstyle.close () + print "error" + except IOError, OSError: + return False + + return True + +# superfunction to generate the site +def generate_site (dbname, files_to_copy, folders_to_copy, search_type_full=True): + # get the configuration + conf = biaweb_db.get_configuration (dbname) + # get the templates + tpls = biaweb_db.get_templates (dbname) + + # get the list of categories + cats = biaweb_db.get_categories (dbname) + # cannot get categories return false + if cats == False: + return False + + # format the categories as a html bulleted list + cats_str = html_format_categories (cats) + + # get the best rated articles + best_rated = biaweb_db.site_get_bestrated (dbname) + # if cannot retrieve + if best_rated == False: + return False + # format the best rated articles as a html bulleted list + best_rated_str = html_format_best_rated (best_rated) + + # remove the destination tree and recreate it + try: + if os.path.exists (conf[5]): + shutil.rmtree (conf[5]) + os.mkdir (conf[5]) + except OSError: + return False + + # generate the index page including style sheet + ret = generate_home_page (dbname, conf, tpls, cats_str, best_rated_str) + if ret == False: + return False + + # finally when all is successfully done return true + return True