From de54d2eb3b283030bd98f7a1f6d056ce2e56b4d7 Mon Sep 17 00:00:00 2001 From: Harishankar Date: Tue, 30 Nov 2010 18:41:08 +0530 Subject: [PATCH] Implemented the category index pages exporting Now completed implementation of exporting the category index pages. --- biaweb_db.py | 18 ++++ biaweb_exporter.py | 86 +++++++++++++++- biaweb_strings.py | 245 ++++++++++++++++++++++++++++++++++++++++++++- 3 files changed, 347 insertions(+), 2 deletions(-) diff --git a/biaweb_db.py b/biaweb_db.py index 9b7cb10..c1d5e60 100644 --- a/biaweb_db.py +++ b/biaweb_db.py @@ -207,6 +207,24 @@ def get_configuration (dbname): except sqlite3.Error: return False +# Function to get articles from a category or all articles (inner join with categories to get category stub) +def site_articles (dbname, catid = None): + try: + conn = sqlite3.connect (dbname) + c = conn.cursor () + if catid is None: + c.execute ("SELECT * FROM articles INNER JOIN categories ON articles.cid=categories.cid \ + ORDER BY cdate DESC;") + else: + c.execute ("SELECT * FROM articles INNER JOIN categories ON articles.cid=categories.cid \ + WHERE articles.cid=? ORDER BY cdate DESC;", (catid,)) + conn.commit () + rows = c.fetchall () + conn.close () + return rows + except sqlite3.Error: + return False + # Function to get the latest articles (inner join with categories to get category stub) def site_latest_articles (dbname, num_arts): try: diff --git a/biaweb_exporter.py b/biaweb_exporter.py index 5bbe32b..7c887c6 100644 --- a/biaweb_exporter.py +++ b/biaweb_exporter.py @@ -32,6 +32,87 @@ def html_format_categories (cats): str_items = "".join (items) return str_items +# to convert a rating number into rating images out of 10 stars +def html_format_rating (rating): + items = [] + # if -1 then return unrated as the text + if rating == -1: + return "unrated" + # fill up the number of stars for the rating + for i in range (rating): + items.append ('*') + # fill up remaining slots (of 10) with grey stars + for i in range (10 - rating): + items.append ('-') + + rating_str = "".join (items) + return rating_str + +# function to generate category directories and indices +def generate_category_indices (dbname, conf, templates, category_str, bestrated_str, category_list): + # main template + tpl_main = string.Template (templates[0][1]) + # table bit + tpl_tablebit = string.Template (templates[3][1]) + # table row bit + tpl_trowbit = string.Template (templates[4][1]) + + # run through each category and generate category index page + for cat in category_list: + try: + # create the category directory + os.mkdir (os.path.join (conf[5], cat[3])) + except IOError, OSError: + return False + + # now get the list of articles for the specified category + articles_list = biaweb_db.site_articles (dbname, cat[0]) + if articles_list == False: + return False + + tableitems = [] + # run through the list of articles in category + for art in articles_list: + url = art[13] + "/" + art[8] + ".html" + creattime = time.ctime (art[5]) + rating_str = html_format_rating (art[9]) + # now build the table rows for each article + tableitem_str = tpl_trowbit.safe_substitute (article_url = url, + title = art[1], + created = creattime, + rating = rating_str) + tableitems.append (tableitem_str) + # generate the rows as a string + tablerows_str = "".join (tableitems) + + # now create the page template + table_str = tpl_tablebit.safe_substitute (category_title = cat[1], + category_desc = cat[2], + table_rows = tablerows_str) + + # now create the index page + categoryindex_str = tpl_main.safe_substitute (site_title = conf[1], + site_url = "http://" + conf[0], + meta_keywords = conf[2], + meta_description = cat[2], + page_title = conf[1], + page_desc = conf[3], + contents_bit = table_str, + list_of_categories = category_str, + list_best_rated = bestrated_str, + copyright = conf[6]) + + # now write to Category/index.html + try: + fcatindex = open (os.path.join (conf[5], cat[3], "index.html"), "w+") + fcatindex.write (categoryindex_str) + fcatindex.close () + except OSError, IOError: + return False + + # finally return true + return True + # function to generate main index file and stylesheet def generate_home_page (dbname, conf, templates, category_str, bestrated_str): # main template @@ -94,7 +175,6 @@ def generate_home_page (dbname, conf, templates, category_str, bestrated_str): fstyle = open (os.path.join (conf[5], "style.css"), "w+") fstyle.write (templates[5][1]) fstyle.close () - print "error" except IOError, OSError: return False @@ -137,5 +217,9 @@ def generate_site (dbname, files_to_copy, folders_to_copy, search_type_full=True if ret == False: return False + ret = generate_category_indices (dbname, conf, tpls, cats_str, best_rated_str, cats) + if ret == False: + return False + # finally when all is successfully done return true return True diff --git a/biaweb_strings.py b/biaweb_strings.py index da2a227..30b9f2c 100644 --- a/biaweb_strings.py +++ b/biaweb_strings.py @@ -207,4 +207,247 @@ template_rss_item = """ ${description} ${item_link} -""" \ No newline at end of file +""" + +stopwords = """$ +0 +1 +2 +3 +4 +5 +6 +7 +8 +9 +a +able +about +after +again +all +almost +already +also +although +am +an +and +another +any +are +are +around +as +at +b +based +be +because +been +before +being +between +both +bring +but +by +c +came +can +com +come +comes +could +d +did +do +does +doing +done +e +each +eight +else +etc +even +every +f +five +for +four +from +g +get +gets +getting +go +going +got +h +had +has +have +he +he +her +here +him +himself +his +how +however +href +http +i +if +in +including +into +is +it +it +its +j +just +k +kb +know +l +like +looks +m +mailto +make +making +many +may +mb +me +means +might +more +more +most +move +mr +much +must +my +n +need +needs +never +nice +nine +no +not +now +o +of +often +oh +ok +on +on +one +only +or +org +other +our +out +over +own +p +piece +q +r +rather +re +really +s +said +same +say +says +see +seven +several +she +should +since +single +six +so +so +some +something +still +stuff +such +t +take +ten +than +that +the +their +them +them +then +there +there +these +they +they +thing +things +this +those +three +through +to +too +took +two +u +under +up +us +use +used +using +usual +v +ve +very +via +w +want +was +way +we +we +well +were +what +when +where +whether +which +while +whilst +who +why +will +with +within +would +x +y +yes +yet +you +your +z""".split ("\n") \ No newline at end of file -- 2.20.1