Skip to content

Commit

Permalink
added linkpolis, supporting unicode (at least a little bit), and prob…
Browse files Browse the repository at this point in the history
…ably more.
  • Loading branch information
shwoop committed Oct 20, 2013
1 parent 5d8ee0c commit 1053e19
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 53 deletions.
110 changes: 71 additions & 39 deletions src/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,26 @@
import hashlib
from urllib import urlretrieve

def cur2lst(sqlite3cursor):
""" flip sqlite3 cursor's into lists before returning them """
try:
output = [x for x in sqlite3cursor]
except TypeError, e:
print "cur2lst: {0}".format(e.strerror)
output = []

return output


class Database:
"""
Database Class
Connects on init and has add_website and list_last_sites for adding
or reading a website from the database.
"""
def __init__(self, care_about_threads=False, database="../db/laamaj.db",
imgdir = '../db/img/'):
""" Create a database connection. """
def __init__(self, care_about_threads=False, database=u"../db/laamaj.db",
imgdir = u"../db/img/"):
## Create a database connection.
self._con = None
self._cur = None
self._db = database
Expand All @@ -26,16 +37,21 @@ def __init__(self, care_about_threads=False, database="../db/laamaj.db",

self._imgdir = imgdir

self._sql_insert_website = "INSERT INTO websites \
self._sql_insert_website = u"INSERT INTO websites \
(ws_date, ws_user, ws_chan, ws_url, ws_localfile) \
VALUES (date('now'), ?, ?, ?, ?);"
self._sql_retreive_website = "select ws_user||\' - \'||ws_chan\
||\' - \'||ws_url from websites where ws_id > (select max(ws_id) \
from websites) - ? order by ws_id desc;"
self._sql_retreive_website = u"SELECT ws_user||\' - \'||ws_chan\
||\' - \'||ws_url FROM websites WHERE ws_id > (SELECT max(ws_id) \
FROM websites) - ? ORDER BY ws_id DESC;"
#self._sql_duplicate_check = u"SELECT Count(*) FROM websites WHERE \
# ws_url = ?;"
self._sql_duplicate_check = u"SELECT ws_user FROM websites WHERE \
ws_url = ? ORDER BY ws_id ASC;"

def __del__(self):
self._close()


def _connect(self):
try:
self._con = sqlite3.connect(self._db,
Expand All @@ -45,6 +61,7 @@ def _connect(self):
except sqlite3.Error, e:
print("Error : "+e.args[0])


def _close(self):
if self._con:
self._con.close()
Expand All @@ -55,62 +72,77 @@ def _close(self):
print("no open connection")
self._cur = None


def close(self):
self._close()


def _commit(self):
if self._con:
self._con.commit()


def add_website(self, user, chan, website):
print("adding website")
exts = ['.jpg', '.jpeg', '.png', '.gif']
for ext in exts:
if (website.endswith(ext)):
origfile = website.split('/')
origfile = origfile.pop()
print('Original filename: ' + origfile)
print u"adding website"

localfilename = hashlib.md5(origfile).hexdigest() + ext
## Linkpolis
speedy = self._cur.execute(self._sql_duplicate_check,
(website,)).fetchone()
if speedy:
status = u"repost"
output = [speedy[0]]

else:
## image check
exts = [u".jpg", u".jpeg", u".png", u".gif"]

if [ext for ext in exts if website.endswith(ext)]:
origfile = website.split(u"/").pop()
print u"Original filename: {0}".format(origfile)
localfilename = hashlib.md5(origfile).hexdigest() + ext[0]
urlretrieve(website, self._imgdir + localfilename)
status = u"image"

output = self._cur.execute(
self._sql_insert_website,
(user, chan, website, localfilename)
)
self._con.commit()
return output
else:
localfilename = u""
status = u"site"

## update the db
output = self._cur.execute(
self._sql_insert_website,
(user, chan, website, localfilename))
self._con.commit()
output = cur2lst(output)

return status, output

output = self._cur.execute(
self._sql_insert_website,
(user, chan, website, '')
)
self._con.commit()
return output

def list_last_sites(self, numero=5):
print("output sites")
print(u"output sites")
try:
print((str(numero)))
print((unicode(numero)))
data = self._cur.execute(
self._sql_retreive_website,
(str(numero))
(unicode(numero))
)
data = cur2lst(data)
if data:
print (data)
print data
else:
data = "No Results"
return(data)
data = [u"No Results"]

except sqlite3.Error, e:
print("Error : ",e.args[0])
data = "Error : {error}".format(error=e.args[0])
return(data)
print "Error : ",e.args[0]
data = ["Error : {error}".format(error=e.args[0])]

return data


def exe(self, toexecute):
tmp = self._cur.execute(toexecute)
print (tmp)
return (tmp)
tmp = cur2lst(self._cur.execute(toexecute))
print tmp
return tmp



if __name__ == "__main__":
Expand Down
38 changes: 24 additions & 14 deletions src/laamaj.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,31 +22,36 @@


def connectHandler(connection, server):
print('Connected to ' + server)
print(u"Connected to {0}".format(server))
for channel in channels:
connection.join(channel)


def textHandler(connection, msgfrom, target, text):
print(target + ': <' + msgfrom + '> ' + text)

msgfrom = unicode(msgfrom, "UTF-8")
target = unicode(target, "UTF-8")
text = unicode(text, "UTF-8")

print(u"{0}: <{1}> {2}".format(target, msgfrom, text))

## Check for commands (!<command>)
if (text.startswith('!')):

command = text.split(' ',1)[0]

if command == "!sites":
if text.startswith("!"):
command = text.split(u" ",1)[0]
if command == u"!sites":
sites = db.list_last_sites()
for site in sites:
connection.send_msg(target, str(site[0]))
connection.send_msg(target, unicode(site[0]))
time.sleep(send_lag)

else:
## Parse for url's
for word in text.split():
if "http" in word:
db.add_website(msgfrom, target, word)

if u"http" in word:
res, out = db.add_website(msgfrom, target, word)
print (res, out)
if res == u"repost":
connection.send_msg(target, "reposts of reposts \
of reposts...")

def main():
"""
Expand All @@ -72,8 +77,13 @@ def main():
con.add_on_raw_handler(lambda con, msg: None)
con.add_on_connected_handler(connectHandler)
con.add_on_text_handler(textHandler)
con.add_on_join_handler(lambda con, who, chan: print("{0} has joined {1}".format(who, chan)))
con.add_on_part_handler(lambda con, who, chan: print("{0} has left {1}".format(who, chan)))
con.add_on_join_handler(
lambda con, who, chan:
print("{0} has joined {1}".format(who, chan))
)
con.add_on_part_handler(
lambda con, who, chan:
print("{0} has left {1}".format(who, chan)))

con.connect()
con.process()
Expand Down

0 comments on commit 1053e19

Please sign in to comment.