Skip to content
This repository
branch: master
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 80 lines (65 sloc) 2.355 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
# This file is part of NewsFinder.
# https://github.com/joshuaeckroth/AINews
#
# Copyright (c) 2011 by the Association for the Advancement of
# Artificial Intelligence. This program and parts of it may be used and
# distributed without charge for non-commercial purposes as long as this
# notice is included.

"""
The database wrapper for MySQL. It provides the fundamental functions to
access the MySQL database.
"""

import sys
import re
import MySQLdb

from AINewsConfig import db
           
class AINewsDB:
    def __init__(self):
        host = db['database.host']
        user = db['database.user']
        pwd = db['database.pwd']
        database = db['database.db']
        try:
            self.con = MySQLdb.connect(host = host,
                                        user = user,
                                        passwd = pwd,
                                        db = database,
                                        charset = 'utf8',
                                        use_unicode = True,
                                        connect_timeout = 120)
            self.con.autocommit(True)
        except MySQLdb.Error, e:
            print "Error %d: %s" % (e.args[0], e.args[1])
            sys.exit (1)
        self.cursor = self.con.cursor()
    
            
    def __del__(self):
        self.close()
        
    def close(self):
        self.cursor.close()
        self.con.close()
        
    def execute(self, query, args = None):
        """
Execute SQL query with no return value
"""
        if args == None:
            self.cursor.execute(query)
        else:
            self.cursor.execute(query, args)
        return self.cursor.lastrowid

    def selectone(self, query, args = None):
        """
Execute SQL query with only one return value
"""
        self.execute(query, args)
        return self.cursor.fetchone()
        
    def selectall(self, query, args = None):
        """
Execute SQL query with multiple return values
"""
        self.execute(query, args)
        return self.cursor.fetchall()
    
    def crawled(self, url):
        urlrow = self.selectone("select url from crawled where url=%s", (url,))
        return (urlrow != None)

    def set_crawled(self, url):
        self.execute("insert into crawled (url) values (%s)", (url,))


Something went wrong with that request. Please try again.