ArticleReader Script

From Superk

Jump to: navigation, search

My nifty news reader script:

#!/usr/bin/env python

import MySQLdb
import datetime

class sql :
    """Class to handle all SQL methods"""
    def dbConnect(self, host, user,passwd, db) :
        """Create a DB connection"""
        db = MySQLdb.connect(host=host, user=user, passwd=passwd, db=db)
        return db.cursor()
        
    def getHeadlines(self, cur) :
    """Get all the headlines"""
        cur.execute("SELECT tbl_news_doc.doc_id, tbl_news_doc.doc_headline, tbl_news_doc.doc_modifyDate, tbl_news_doc.doc_author, tbl_news_categories.cat_item, tbl_name.fname, tbl_name.lname \
             FROM tbl_news_doc \
             LEFT JOIN tbl_news_categories ON tbl_news_doc.doc_category=tbl_news_categories.cat_id \
             LEFT JOIN tbl_name ON tbl_news_doc.doc_author=tbl_name.name_id \
             ORDER BY tbl_news_doc.doc_modifyDate DESC")
        return cur.fetchall()
        
    def getArticle(self, item, cur) :
    """Get article based on ID"""
        cur.execute("SELECT tbl_news_doc.doc_headline, tbl_news_doc.doc_modifyDate, tbl_news_doc.doc_body, tbl_news_categories.cat_item, tbl_name.fname, tbl_name.lname \
            FROM tbl_news_doc \
            LEFT JOIN tbl_news_categories ON tbl_news_doc.doc_category=tbl_news_categories.cat_id \
            LEFT JOIN tbl_name ON tbl_news_doc.doc_author=tbl_name.name_id \
            WHERE tbl_news_doc.doc_id = %s" % item)
        return cur.fetchone()

class display :
    """Class to handle all visual display methods"""
    def showHeadlines(self,res) :
        """List articles in DB"""
        count = 1
        for i in res :
            d = i[2]
            print "Item #" + str(i[0]) + " ---->\n" + i[1] + "\nBy " + i[-2] + " " + i[-1] + "\n" + d.strftime("%b %d, %Y") + "\nCategory: " + i[-3] + "\n"
            count += 1
    
    def showArticle(self, art) :
        """Display requested article"""
        t = art[1]
        print "\n" + art[0] + "\nBy " + art[4] + " " + art[5] + ", " + t.strftime("%b %d, %Y") + "\n" + "Category: " + art[3]
        print "\n-----\n" + art[2] + "\n-----\n"

def main() :
    """Main program block"""
    sqldb = sql()
    show = display()
    
    cursor = sqldb.dbConnect("server.somewhere.com", "dbuser", "secret", "mydatabase")
    rows = sqldb.getHeadlines(cursor)
    
    show.showHeadlines(rows)
    
    s = raw_input("View which article (Enter \"q\" to quit): ")
    
    if s == "q" :
        quit
    elif s == "" :
        main()
    else :
        article = sqldb.getArticle(s, cursor)
        show.showArticle(article)
        
        n = raw_input("Enter \"l\" for article list or \"q\" to quit: ")
        
        if n == "q" :
            quit
        else :
            main()
        
if __name__ == "__main__" :
    main()    

It first outputs:

bkrein@bkreinlap:~/python$ python mysql_query.py 
Item #3 ---->
A Test Headline
By Benjamin Krein
May 02, 2005
Category: Wildlife

Item #2 ---->
Summer Saturday Evenings
By Benjamin Krein
Mar 29, 2005
Category: Schedules

Item #1 ---->
This is a headline
By Benjamin Krein
Mar 01, 2005
Category: Wildlife

View which article (Enter "q" to quit): 

If you pick #3, you get:

View which article (Enter "q" to quit): 2

Summer Saturday Evenings
By Benjamin Krein, Mar 29, 2005
Schedules

-----
Summer Saturday Evenings offer an exciting and different way to see both the Desert Museum and the Sonoran Desert in general.  The evening programs that are exclusive to the summer season offer rare glimpses of desert creatures in their nocturnal environment (where many can be far more active!).  In addition to a new perspective on desert wildlife, the Desert Museum takes on a unique personality under the dark skies.  Paths are lit up with subtle lights and the sky sparkles with Tucson's famously clear night skies.

Trained docents and Museum staff are scattered throughout the museum grounds ready and willing to provide visitors with vast amounts of exciting and interesting information focusing on how the desert operates at night.  Restaurants and gift shops are open later as well.  Enjoy a relaxing meal in the peacefulness of the Tucson dusk or have a refreshing ice cream to help cool down the hot summer days.  There is no lack of exciting opportunities for a Saturday evening at the Arizona-Sonora Desert Museum! 
-----


Enter "l" for article list or "q" to quit: 
Personal tools