#!/usr/bin/python2.6
# encoding: utf-8

import os, time, glob
from urllib import quote
try:
	import sqlite3
except ImportError:
	from pysqlite2 import dbapi2 as sqlite3

def infKwd(name):
	try:
		keywords = []
		names = []
		connect = sqlite3.connect('system/%s.dw' % name)
		connect.text_factory = str
		cursor = connect.cursor()
		cursor.execute('select keywords, name from doorway where status>-1') 
		for row in cursor: 
			keywords.append(row[0].decode('utf_8').replace('\r',''))
			names.append(row[1].decode('utf_8').replace('\r',''))
		return keywords, names
	except Exception,msg:
		print msg

def unFreeze(name,count):
	connect = sqlite3.connect('system/%s.dw' % name)
	cursor = connect.cursor()
	cursor.execute('select keywords from doorway where status=-1 limit %d' % count)
	keywords = []
	for row in cursor:
		keywords.append(row[0])
	for keyword in keywords:
		cursor.execute('update doorway set status=0 where keywords="%s"' % keyword)
	connect.commit()
	return 1

def activeKwd(name,keyword,status=1):
	try:
		connect = sqlite3.connect('system/%s.dw' % name)
		cursor = connect.cursor()
		cursor.execute('update doorway set status=%s where keywords="%s"' % (status,keyword))
		connect.commit()
		return 1
	except Exception,msg:
		print msg

def updKwd(name,keyword,upd=1):
	try:
		connect = sqlite3.connect('system/%s.dw' % name)
		cursor = connect.cursor()
		cursor.execute('update doorway set upd=%s where keywords="%s"' % (upd,keyword))
		connect.commit()
		return 1
	except Exception,msg:
		print msg

def isItValid(name,page):
	keywords, names = infKwd(name)
	if page in names:
		return True, keywords, names
	else:
		return False, [], []

def isItActive(name,keyword):
	try:
		connect = sqlite3.connect('system/%s.dw' % name)
		cursor = connect.cursor()
		cursor.execute('select status from doorway where keywords="%s"' % keyword) 
		for row in cursor: 
			status = row[0]
		return status
	except Exception,msg:
		print msg		

def isItUpd(name,keyword):
	try:
		connect = sqlite3.connect('system/%s.dw' % name)
		cursor = connect.cursor()
		cursor.execute('select upd from doorway where keywords="%s"' % keyword) 
		for row in cursor: 
			update = row[0]
		return update
	except Exception,msg:
		print msg

def getByPage(name,page):
	try:
		connect = sqlite3.connect('system/%s.dw' % name)
		cursor = connect.cursor()
		cursor.execute('select keywords from doorway where name="%s"' % page) 
		for row in cursor: 
			res = row[0]
		return res
	except Exception,msg:
		print msg
				
def getReq(name,keyword):
	try:
		connect = sqlite3.connect('system/%s.dw' % name)
		cursor = connect.cursor()
		cursor.execute('select requests from doorway where keywords="%s"' % keyword) 
		for row in cursor: 
			req = row[0]
		return req
	except Exception,msg:
		print msg

def getAllReq(name):
	try:
		req = 0
		connect = sqlite3.connect('system/%s.dw' % name)
		cursor = connect.cursor()
		cursor.execute('select requests from doorway') 
		for row in cursor: 
			req += row[0]
		return req
	except Exception,msg:
		print msg
	
def getLast(name,keyword):
	try:
		connect = sqlite3.connect('system/%s.dw' % name)
		cursor = connect.cursor()
		cursor.execute('select last from doorway where keywords="%s"' % keyword) 
		for row in cursor:
			last = row[0]
		return last
	except Exception,msg:
		print msg

def getAllLast(name):
	try:
		last = 0
		connect = sqlite3.connect('system/%s.dw' % name)
		cursor = connect.cursor()
		cursor.execute('select last from doorway') 
		for row in cursor:
			if row[0] > last:
				last = row[0]
		return last
	except Exception,msg:
		print msg

def	setLast(name,keyword):
	last = int(time.time())
	try:
		connect = sqlite3.connect('system/%s.dw' % name)
		cursor = connect.cursor()
		cursor.execute('update doorway set last=%s where keywords="%s"' % (last,keyword))
		connect.commit()
		return 1
	except Exception,msg:
		print msg
		
def incrReq(name,keyword):
	req = getReq(name,keyword) + 1
	try:
		connect = sqlite3.connect('system/%s.dw' % name)
		cursor = connect.cursor()
		cursor.execute('update doorway set requests=%s where keywords="%s"' % (req,keyword))
		connect.commit()
		return 1
	except Exception,msg:
		print msg



